circuitpython/tests/pyb/i2c_error.py
Damien George c4e58eaa98 stmhal/i2c: Add option to I2C to enable/disable use of DMA transfers.
New keyword option in constructor and init() method is "dma=<bool>".
DMA is now disabled by default for I2C transfers because it currently does
not handle I2C bus errors very well (eg if slave device doesn't ACK or
NACK correctly during a transfer).
2016-11-11 17:36:19 +11:00

51 lines
1.1 KiB
Python

# test I2C errors, with polling (disabled irqs) and DMA
import pyb
from pyb import I2C
# init accelerometer
pyb.Accel()
# get I2C bus
i2c = I2C(1, I2C.MASTER, dma=True)
# test polling mem_read
pyb.disable_irq()
i2c.mem_read(1, 76, 0x0a) # should succeed
pyb.enable_irq()
try:
pyb.disable_irq()
i2c.mem_read(1, 77, 0x0a) # should fail
except OSError as e:
pyb.enable_irq()
print(repr(e))
i2c.mem_read(1, 76, 0x0a) # should succeed
# test polling mem_write
pyb.disable_irq()
i2c.mem_write(1, 76, 0x0a) # should succeed
pyb.enable_irq()
try:
pyb.disable_irq()
i2c.mem_write(1, 77, 0x0a) # should fail
except OSError as e:
pyb.enable_irq()
print(repr(e))
i2c.mem_write(1, 76, 0x0a) # should succeed
# test DMA mem_read
i2c.mem_read(1, 76, 0x0a) # should succeed
try:
i2c.mem_read(1, 77, 0x0a) # should fail
except OSError as e:
print(repr(e))
i2c.mem_read(1, 76, 0x0a) # should succeed
# test DMA mem_write
i2c.mem_write(1, 76, 0x0a) # should succeed
try:
i2c.mem_write(1, 77, 0x0a) # should fail
except OSError as e:
print(repr(e))
i2c.mem_write(1, 76, 0x0a) # should succeed