How to fix the I2C slave issue with STM32G070? HAL_I2C_ListenCpltCallback, HAL_I2C_AddrCallback, HAL_I2C_SlaveRxCpltCallback, HAL_I2C_SlaveTxCpltCallback
Hi,
I'm trying to setup a stm32g070kb (32pin) MCU as I2C slave by using CubeIDE.
I would like to create a code which will work same as I2C Eeprom, but when I ask the stm32 (with another MCU) to return me a register or registers value, the returned registers are shifted by 1 register.
On this picture I recorded the communication between I2C Eprom and STM32G070 slave.
The gap in the STM32 communication makes the request shift by 1 byte for the master MCU, the eeprom doesn't have the gap.
here is the code:
// emulated I2C RAM
static uint8_t my_i2c_ram[128];
static uint8_t my_i2c_offset; // index of current RAM cell
static uint8_t first=1; // first byte --> new my_i2c_offset
void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
{
first = 1;
HAL_I2C_EnableListen_IT(hi2c); // slave is ready again
}
void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
{
if( TransferDirection==I2C_DIRECTION_TRANSMIT )
{
if( first )
{
HAL_I2C_Slave_Seq_Receive_IT(hi2c, &my_i2c_offset, 1, I2C_NEXT_FRAME);
}
else
{
HAL_I2C_Slave_Seq_Receive_IT(hi2c, &my_i2c_ram[my_i2c_offset], 1, I2C_NEXT_FRAME);
}
}
else
{
HAL_I2C_Slave_Seq_Transmit_IT(hi2c, &my_i2c_ram[my_i2c_offset], 1, I2C_NEXT_FRAME);
}
}
void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
if(first)
{
first = 0;
}
else
{
my_i2c_offset++;
}
HAL_I2C_Slave_Seq_Receive_IT(hi2c, &my_i2c_ram[my_i2c_offset], 1, I2C_NEXT_FRAME);
}
void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
{
my_i2c_offset++;
HAL_I2C_Slave_Seq_Transmit_IT(hi2c, &my_i2c_ram[my_i2c_offset], 1, I2C_NEXT_FRAME);
}
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
{
if( HAL_I2C_GetError(hi2c)==HAL_I2C_ERROR_AF )
{
// transaction terminated by master
my_i2c_offset--;
}
else
{
myerror = HAL_I2C_GetError(hi2c) ;
}
}and the CubeIDE settings:

