To brush up on my skills, I am using I2C it with these devices on the same I2C bus:
- master: ESP2866, micropython built-int libraries
- slave: MSP430G2231 slave, C routines using USI, developed from MSP430Ware examples
- slave: STM32F100RB, Arduino routines (platform.io toolchain)
- slave: oled display, type SSD1306
Note: I have to rewrite most of the MSP430 code, because the examples are terrible. Example: when MSP430 receives an address other than its own... it replies with a NACK. This is incorrect, it should not respond.
Scenario:
- When the master sends a 4 bytes command to the STM32, the STM32 replies with 4 ACKs.
- When the ESP sends a 4 bytes command to the MSP430, the MSP430 replies with 3 ACKs.
This is clear looking at the MSP430Ware examples, here polished and commented:
case I2C_state_read_rx :
flag = I2C_rx(USISRL);
USICTL0 |= USIOE; // SDA = output
if (flag == _I2C_rx_has_space)
{
_send_ACK();
I2C_State = I2C_state_start_rx;
}
else // last byte
{
_send_NACK();
I2C_State = I2C_state_idle;
}
break;
The Master communicates smoothly with the three slaves. Taking into account the two cases, this is an excerpt from my ESP code:
// writeto retuns the number of ACKs from the slave
i = I2C.writeto(... slave address ..., cmd)
if i != len(cmd) - 1 and i != len(cmd) :
raise RuntimeError("I2C: writeto error: ack = ", i)
Searching the web, I found the electrical specs of I2C, but no clarification on this issue.
I don't understand if the MSP430 behaviour is wrong or there is a bug in the arduino STM32 library. It could be, because most of the time STM32 is used as master, not slave: maybe I am hitting a corner case.
Questions:
- When a slave receives an N bytes command, should it reply to the master with N-1 or N ACKs ?
- Are there any official specs on proper slaves behaviour?
P.S.: sorry for the use of Master / Slave, I don't think it is offensive in this context.
ADDENDUM
I found this Texas Instruments application note: SLVA704 - Understanding the I2C bus
Reading paragraphs 3.3: the Master wants to read a register from the Slave:
Looking at the figure 9 I understand this:
- The master sends the I2c address (the slave ACK it) and the register address (the slave ACK it)
- To read the reply, the master restart the communication: it sends the I2C address (slave ACK it) and the slave send one byte without ACK/NACK
Once the master has received the number of bytes it is expecting, it will send a NACK, signaling to the slave to halt communications and release the bus.
This is the case when reading a one-byte register.
Is it possible for the slave to respond with many bytes? Should it send ACK / NACK ? after each byte?
when MSP430 receives an address other than its own... it replies with a NACK. This is incorrect, it should not respond.
I haven't looked at the library code, so I might be missing the point. But on the physical layer, not responding and 'sending a NACK' is essentially the same. A NACK bit is a recessive state (HiZ) and an ACK is dominant (active LOW). \$\endgroup\$SDA
bus drive to hi-Z and waiting for at least a bit period. Nothing is sent. \$\endgroup\$