Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extmod/machine_i2c: Use writes not reads in i2c.scan #2494

Closed
wants to merge 1 commit into from
Closed

extmod/machine_i2c: Use writes not reads in i2c.scan #2494

wants to merge 1 commit into from

Conversation

deshipu
Copy link
Contributor

@deshipu deshipu commented Oct 8, 2016

As per discussion in #2449, using write requests instead of read requests for I2C.scan() seems to support a larger number of devices (especially ones that are write-only) and to be more compatible with what the hardware i2c on PyBoard does.

I have tested this change with a number of I²C devices I have at hand:

  • SSD1306 OLED display (address 60)
  • HT16K33 LED Matrix (address 112)
  • Atmega328p running in slave mode using Arduino's <Wire.h> library (address 9)
  • WeMos D1 Mini Motor shield (address 48)

Especially the last device is problematic, as it pulls down the data line as soon as it encounters any errors in the transmission. Thus, with all four devices connected to the same bus, the "read" scan results in:

>>> i2c.scan()
[9, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119]

Using writes seems to be more robust, as even a read-only I²C device has to implement writes in order to be able to receive the address of the register to read. Using writes, with the same four devices on the bus, the result is:

>>> i2c.scan()
[9, 48, 60, 112]

@dpgeorge
Copy link
Member

Merged in eaef6b5, thank you.

@dpgeorge dpgeorge closed this Oct 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants