2016-01-29 19:06:04 -05:00
|
|
|
# test I2C errors, with polling (disabled irqs) and DMA
|
|
|
|
|
|
|
|
import pyb
|
|
|
|
from pyb import I2C
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
if not hasattr(pyb, "Accel"):
|
|
|
|
print("SKIP")
|
2019-12-13 00:43:17 -05:00
|
|
|
raise SystemExit
|
|
|
|
|
2016-01-29 19:06:04 -05:00
|
|
|
# init accelerometer
|
|
|
|
pyb.Accel()
|
|
|
|
|
|
|
|
# get I2C bus
|
2021-06-12 00:53:44 -04:00
|
|
|
i2c = I2C(1, I2C.CONTROLLER, dma=True)
|
2016-01-29 19:06:04 -05:00
|
|
|
|
|
|
|
# test polling mem_read
|
|
|
|
pyb.disable_irq()
|
2020-03-22 22:26:08 -04:00
|
|
|
i2c.mem_read(1, 76, 0x0A) # should succeed
|
2016-01-29 19:06:04 -05:00
|
|
|
pyb.enable_irq()
|
|
|
|
try:
|
|
|
|
pyb.disable_irq()
|
2020-03-22 22:26:08 -04:00
|
|
|
i2c.mem_read(1, 77, 0x0A) # should fail
|
2016-01-29 19:06:04 -05:00
|
|
|
except OSError as e:
|
|
|
|
pyb.enable_irq()
|
|
|
|
print(repr(e))
|
2020-03-22 22:26:08 -04:00
|
|
|
i2c.mem_read(1, 76, 0x0A) # should succeed
|
2016-01-29 19:06:04 -05:00
|
|
|
|
|
|
|
# test polling mem_write
|
|
|
|
pyb.disable_irq()
|
2020-03-22 22:26:08 -04:00
|
|
|
i2c.mem_write(1, 76, 0x0A) # should succeed
|
2016-01-29 19:06:04 -05:00
|
|
|
pyb.enable_irq()
|
|
|
|
try:
|
|
|
|
pyb.disable_irq()
|
2020-03-22 22:26:08 -04:00
|
|
|
i2c.mem_write(1, 77, 0x0A) # should fail
|
2016-01-29 19:06:04 -05:00
|
|
|
except OSError as e:
|
|
|
|
pyb.enable_irq()
|
|
|
|
print(repr(e))
|
2020-03-22 22:26:08 -04:00
|
|
|
i2c.mem_write(1, 76, 0x0A) # should succeed
|
2016-01-29 19:06:04 -05:00
|
|
|
|
|
|
|
# test DMA mem_read
|
2020-03-22 22:26:08 -04:00
|
|
|
i2c.mem_read(1, 76, 0x0A) # should succeed
|
2016-01-29 19:06:04 -05:00
|
|
|
try:
|
2020-03-22 22:26:08 -04:00
|
|
|
i2c.mem_read(1, 77, 0x0A) # should fail
|
2016-01-29 19:06:04 -05:00
|
|
|
except OSError as e:
|
|
|
|
print(repr(e))
|
2020-03-22 22:26:08 -04:00
|
|
|
i2c.mem_read(1, 76, 0x0A) # should succeed
|
2016-01-29 19:06:04 -05:00
|
|
|
|
|
|
|
# test DMA mem_write
|
2020-03-22 22:26:08 -04:00
|
|
|
i2c.mem_write(1, 76, 0x0A) # should succeed
|
2016-01-29 19:06:04 -05:00
|
|
|
try:
|
2020-03-22 22:26:08 -04:00
|
|
|
i2c.mem_write(1, 77, 0x0A) # should fail
|
2016-01-29 19:06:04 -05:00
|
|
|
except OSError as e:
|
|
|
|
print(repr(e))
|
2020-03-22 22:26:08 -04:00
|
|
|
i2c.mem_write(1, 76, 0x0A) # should succeed
|