Skip to content

Commit afce84e

Browse files
author
blue-2357
committed
Merge pull request #754 from stickbreaker/master
Correct 10bit Device address handling.
2 parents 9624265 + 35f84ca commit afce84e

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

cores/esp32/esp32-hal-i2c.c

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

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

288291
//CMD WRITE ADDRESS
289-
i2c->dev->fifo_data.val = address & 0xFF;
290-
if(addr_10bit) {
291-
i2c->dev->fifo_data.val = (address >> 8) & 0xFF;
292+
if (addr_10bit) { // address is left-shifted with Read/Write bit set
293+
i2c->dev->fifo_data.data = (((address >> 8) & 0x6) | 0xF1); // send a9:a8 plus 1111 0xxR mask
294+
i2c->dev->fifo_data.data = ((address >> 1) & 0xFF); // send a7:a0, remove R bit (7bit address style)
295+
}
296+
else { // 7bit address
297+
i2c->dev->fifo_data.data = address & 0xFF;
292298
}
293299
i2cSetCmd(i2c, cmdIdx++, I2C_CMD_WRITE, addrLen, false, false, true);
294300
nextCmdCount = cmdIdx;

0 commit comments

Comments
 (0)