CarletonURocketry / fetcher

A QNX process for fetching data from sensors over I2C.
https://carletonurocketry.github.io/fetcher/
MIT License
1 stars 0 forks source link

Unnecessary (and wrong) bitmasking for read/write addressing #25

Closed linguini1 closed 4 months ago

linguini1 commented 6 months ago

The I2C official specification describes that in a 7-bit addressing mode, the 8th bit is used for specifying a read (1) operation or a write (0) operation.

Silly me thought that I had to manually do this by masking the address appropriately (addr & 0xFE) vs (addr | 0x01). This is in fact totally wrong. Behind the scenes, the I2C library will shift the address up 1 bit and then mark the 8th bit appropriately for read/write operations.

Example:

// What I was doing
(0x42 | 0x01) // This becomes 0x43
(0x43 << 1) | 1 // This is what the library does, and it results in 0x87

// What would be correct
0x42 // Leave the address alone
(0x42 << 1) | 1 // The address is dealt with by the library and becomes 0x85

Please remove all the read_addr() and write_addr() macros in all the libraries within fetcher. Let the library handle addresses!

linguini1 commented 4 months ago

Fixed as a side product of #27.