Acknowledge from master to slave should be picked up in ACKSTAT bit in I2CxSTAT register.
In code I use, adopted from MCC and modified, there are some macro definitions used:
#define I2C_TRANSMIT_REG I2C2TRN // Defines the transmit register used to send data.
#define I2C_RECEIVE_REG I2C2RCV // Defines the receive register used to receive data.
#define I2C_SCLREL I2C2CONbits.SCLREL = 1 // Release I2C clock stretch.
#define I2C_ACKNOWLEDGE_STATUS_BIT I2C2STATbits.ACKSTAT // I2C ACK status bit.
And in code:
// this is the state where an ACK or NACK is expected
// to occur after the slave has placed data to the
// transmit register.
// if the transaction was ACK'ed, more data needs to be sent
// if the transaction was NACK'ed then we don't need to send
// more data
if (I2C_ACKNOWLEDGE_STATUS_BIT == 0)
// prepare next data
i2c_data = I2C_RECEIVE_REG;
I2C_TRANSMIT_REG = I2C2_SlaveCallback(I2C_SLAVE_TRANSMIT_REQUEST, i2c_data);
I2C_SCLREL; /* Release the clock */
else //if (I2C_ACKNOWLEDGE_STATUS_BIT == 1)
// no more data to be sent so we go to idle state
I2C_Slave_State = S_SLAVE_IDLE;
Debugging I2C Slave code when communicating with a Master running in another processor, may be tricky.
If you stop in debugger to examine something, then master run away, making it difficult to proceed.
I prefer to debug I2C slave code, with master code running in the same microcontroller, using another I2C peripheral,
until I have code that work. Before trying testing with master running in another device.
post edited by Mysil - 2019/04/22 12:06:50