Skip to content

Commit a695187

Browse files
authoredOct 21, 2017
Correct 10bit Device address handling.
The existing code did not follow protocol with 10bit addressed devices. Per _Philps/NXP Semiconductors UM10204 I2C-bus specification and user manual Rev. 6 4April2014_ pg.15 3.1.11 10-bit addressing: ~The first seven bits of the first byte are the combination of 1111 0xx of which the last two bits (xx) are the two Most-Significant Bits (MSB) of the 10-bit address; the eighth bit of the first byte is the R/!W! bit the determines the direction of the message~
1 parent 0c038b4 commit a695187

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed
 

Diff for: ‎cores/esp32/esp32-hal-i2c.c

+14-7
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,13 @@ i2c_err_t i2cWrite(i2c_t * i2c, uint16_t address, bool addr_10bit, uint8_t * dat
176176

177177
//CMD WRITE(ADDRESS + DATA)
178178
if(!index) {
179-
i2c->dev->fifo_data.data = address & 0xFF;
180-
dataSend--;
181-
if(addr_10bit) {
182-
i2c->dev->fifo_data.data = (address >> 8) & 0xFF;
179+
if(addr_10bit){// address is leftshifted with Read/Write bit set
180+
i2c->dev->fifo_data.data = (((address >> 8) & 0x6) | 0xF0); // send a9:a8 plus 1111 0xxW mask
181+
i2c->dev->fifo_data.data = ((address >> 1) & 0xFF); // send a7:a0, remove W bit (7bit address style)
182+
dataSend -= 2;
183+
}
184+
else { // 7bit address
185+
i2c->dev->fifo_data.data = address & 0xFF;
183186
dataSend--;
184187
}
185188
}
@@ -272,10 +275,14 @@ i2c_err_t i2cRead(i2c_t * i2c, uint16_t address, bool addr_10bit, uint8_t * data
272275
i2cSetCmd(i2c, 0, I2C_CMD_RSTART, 0, false, false, false);
273276

274277
//CMD WRITE ADDRESS
275-
i2c->dev->fifo_data.val = address & 0xFF;
276-
if(addr_10bit) {
277-
i2c->dev->fifo_data.val = (address >> 8) & 0xFF;
278+
if(addr_10bit){// address is leftshifted with Read/Write bit set
279+
i2c->dev->fifo_data.data = (((address >> 8) & 0x6) | 0xF1); // send a9:a8 plus 1111 0xxR mask
280+
i2c->dev->fifo_data.data = ((address >> 1) & 0xFF); // send a7:a0, remove R bit (7bit address style)
281+
}
282+
else { // 7bit address
283+
i2c->dev->fifo_data.data = address & 0xFF;
278284
}
285+
279286
i2cSetCmd(i2c, 1, I2C_CMD_WRITE, addrLen, false, false, true);
280287

281288
while(len) {

0 commit comments

Comments
 (0)
Please sign in to comment.