cancel
Showing results for 
Search instead for 
Did you mean: 

STM3G0B1 as I2C SLAVE with NoStretchMode. How to handle HAL_I2C_Slave_Seq_Transmit_IT?

ShoH
Visitor

I'm using STM3G0B1 as I2C SLAVE device with NVIC+NoStretchMode since I2C Master device doesn't support the clock stretching.

When HAL_I2C_AddrCallback is called (Read request from Master device), HAL_I2C_Slave_Seq_Transmit_IT seems not working because 0xFF is transmitted, not the expected value held by I2C_REGISTERS.
I guess 0xFF is the default value with NoStretchMode to respond immediately to the request.
How to resolve this issue? Is there a way to transmit the correct data with NoStretchMode for Read request?

 

 

hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_ENABLE;

...

void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode) {
  if (TransferDirection == I2C_DIRECTION_TRANSMIT) { 
    if (HAL_I2C_Slave_Seq_Receive_IT(hi2c, &regAddr, 1, I2C_FIRST_FRAME) != HAL_OK) {
      DBG_LOG("%s(%d): Error Receive Data", __func__, __LINE__);
      return;
    }
  } else {
    if (HAL_I2C_Slave_Seq_Transmit_IT(hi2c, I2C_REGISTERS + regAddr, 1, I2C_FIRST_FRAME) != HAL_OK) {
      DBG_LOG("%s(%d): Error Transmit Data", __func__, __LINE__);
      return;
    }
  }
}

 

 

 

1 REPLY 1
Saket_Om
ST Employee

Hello @ShoH 

You are setting a receive process when "TransferDirection == I2C_DIRECTION_TRANSMIT".

Please try with the snippet code below:

hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_ENABLE;

...

void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode) {
  if (TransferDirection != I2C_DIRECTION_TRANSMIT) { 
    if (HAL_I2C_Slave_Seq_Receive_IT(hi2c, &regAddr, 1, I2C_FIRST_FRAME) != HAL_OK) {
      DBG_LOG("%s(%d): Error Receive Data", __func__, __LINE__);
      return;
    }
  } else {
    if (HAL_I2C_Slave_Seq_Transmit_IT(hi2c, I2C_REGISTERS + regAddr, 1, I2C_FIRST_FRAME) != HAL_OK) {
      DBG_LOG("%s(%d): Error Transmit Data", __func__, __LINE__);
      return;
    }
  }
}

 

 

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar