From a695187de8226c751b54a855af102bfe1cb8b6a9 Mon Sep 17 00:00:00 2001 From: chuck todd Date: Sat, 21 Oct 2017 13:14:14 -0600 Subject: [PATCH] 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~ --- cores/esp32/esp32-hal-i2c.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/cores/esp32/esp32-hal-i2c.c b/cores/esp32/esp32-hal-i2c.c index a6b538d8e..92d3e0b25 100644 --- a/cores/esp32/esp32-hal-i2c.c +++ b/cores/esp32/esp32-hal-i2c.c @@ -176,10 +176,13 @@ i2c_err_t i2cWrite(i2c_t * i2c, uint16_t address, bool addr_10bit, uint8_t * dat //CMD WRITE(ADDRESS + DATA) if(!index) { - i2c->dev->fifo_data.data = address & 0xFF; - dataSend--; - if(addr_10bit) { - i2c->dev->fifo_data.data = (address >> 8) & 0xFF; + if(addr_10bit){// address is leftshifted with Read/Write bit set + i2c->dev->fifo_data.data = (((address >> 8) & 0x6) | 0xF0); // send a9:a8 plus 1111 0xxW mask + i2c->dev->fifo_data.data = ((address >> 1) & 0xFF); // send a7:a0, remove W bit (7bit address style) + dataSend -= 2; + } + else { // 7bit address + i2c->dev->fifo_data.data = address & 0xFF; dataSend--; } } @@ -272,10 +275,14 @@ i2c_err_t i2cRead(i2c_t * i2c, uint16_t address, bool addr_10bit, uint8_t * data i2cSetCmd(i2c, 0, I2C_CMD_RSTART, 0, false, false, false); //CMD WRITE ADDRESS - i2c->dev->fifo_data.val = address & 0xFF; - if(addr_10bit) { - i2c->dev->fifo_data.val = (address >> 8) & 0xFF; + if(addr_10bit){// address is leftshifted with Read/Write bit set + i2c->dev->fifo_data.data = (((address >> 8) & 0x6) | 0xF1); // send a9:a8 plus 1111 0xxR mask + i2c->dev->fifo_data.data = ((address >> 1) & 0xFF); // send a7:a0, remove R bit (7bit address style) + } + else { // 7bit address + i2c->dev->fifo_data.data = address & 0xFF; } + i2cSetCmd(i2c, 1, I2C_CMD_WRITE, addrLen, false, false, true); while(len) { -- GitLab