I2C Slave Wrong Read Count (NUCLEO-U5A5ZJ-Q)
Hey there :)
I am having trouble running the nucleo (NUCLEO-U5A5ZJ-Q) as an I2C slave where read/write sizes are unknown (only known by master)
For the configuration I am using CubeMX and most of the time the slave is working quite fine. However sometimes (1 in 100), the slave transmit count (how many bytes have been read by the master) is not correct (one off - although the master reads the correct amount of data (see analyzer picture))
My code structure:
// First run the cubeMX init code and enable listening
MX_I2C2_Init();
HAL_I2C_EnableListen_IT(hi2c);
// When the slave address maches, the write or read HAL function is called
void HAL_I2C_AddrCallback(I2C_HandleTypeDef* hi2c, uint8_t TransferDirection, uint16_t /*AddrMatchCode*/) {
if (TransferDirection == I2C_DIRECTION_TRANSMIT) {
slave_match_master_write = true;
HAL_I2C_Slave_Seq_Receive_IT(hi2c, buffer_, sizeof(buffer_), I2C_FIRST_FRAME);
} else {
slave_match_master_write = false;
HAL_I2C_Slave_Seq_Transmit_IT(hi2c, buffer_, sizeof(buffer), I2C_LAST_FRAME);
}
}
void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef* hi2c) {
if (slave_match_master_write == true) {
int32_t slave_rx_cnt = sizeof(buffer_) - hi2c->XferSize; // Value always correct
} else {
int32_t slave_tx_cnt = sizeof(buffer_) - hi2c->XferSize - 1; // Why -1?
}
HAL_I2C_EnableListen_IT(hi2c);
}
// This function gets called once every 100 frames (strange)
void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef* /*hi2c*/) {
// empty
}
// This function gets never called (why? - not that i need it)
void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef* /*hi2c*/) {
// empty
}
// The error callback gets called each time as expechted (master NAK)
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef* hi2c) {
uint32_t error = HAL_I2C_GetError(hi2c);
}
In line 20 I added the -1 because otherwise the slave_tx_cnt would be one to much (except for the one in 100 time when its one too small). So it seems to me that there is somehing off.
Does aynone has an idea what I am doing wrong?
Thanks for any hint.
Regards,
Alexander
Side note: Before that i tried it with DMA (same issue with the read size), but lets now focus on Interrupt Mode.
I have not tried it jet in blocking mode...
