STM32G4 I2C Slave AF Error on Normal Transaction Completion
Hardware: STM32G473QETX
HAL Version: STM32G4 HAL
I2C Mode: Slave mode using HAL_I2C_Slave_Seq_Receive_IT()
Application: DS1307 RTC emulation
Issue Description
When implementing an I2C slave device (DS1307 RTC emulation), we observe that HAL_I2C_ErrorCallback() is consistently called with error code HAL_I2C_ERROR_AF (0x00000004 - Acknowledge Failure) at the end of successful write transactions from the I2C master
Transaction Flow
Master sends START + Address (0x68) + WRITE
Slave ACKs (address match detected)
Master sends data bytes (8 bytes total)
Slave receives all bytes correctly via HAL_I2C_SlaveRxCpltCallback()
Master sends NACK (to signal end of write)
Master sends STOP
HAL_I2C_ErrorCallback() is called with HAL_I2C_ERROR_AF
HAL_I2C_ListenCpltCallback() is also called
Expected Behavior
According to I2C protocol, the master sending NACK at the end of a write transaction is normal behavior to signal "I don't want to send more data." This should not be reported as an error.
Actual Behavior
The STM32 HAL reports this as HAL_I2C_ERROR_AF and calls HAL_I2C_ErrorCallback(), even though:
All data was received correctly
The transaction completed successfully
The master's behavior is per I2C specification
Questions
Is this the expected behavior of the STM32 HAL I2C slave implementation?
Is there a way to configure the HAL to not report AF errors for normal NACK conditions?
Should we rely on HAL_I2C_ListenCpltCallback() for transaction completion instead of treating AF errors as failures?
Additional Information
Both HAL_I2C_ErrorCallback() and HAL_I2C_ListenCpltCallback() are called for each transaction
The error callback is called first, then the listen complete callback
Data processing works correctly when done in HAL_I2C_ListenCpltCallback()
This behavior is consistent across all I2C write transactions
