2023-03-19 06:26 PM
Hi,
I am using STM32F303RETb (NUCLEO-F303) to try I2C for another device. But always return HAL_ERROR into HAL_I2C_Slave_Receive(). By the way of I2C_WaitOnFlagUntilTimeout.
What are the possible problems with this error?
this part is setting code in the MX_I2C1_Init().
/* USER CODE END I2C1_Init 1 */
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x00901D23;
hi2c1.Init.OwnAddress1 = 0x40;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_ENABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
/** Configure Analogue filter
this is main function.
/* USER CODE BEGIN 2 */
uint8_t commandBuffer[4] = {0};
uint8_t whoami[1] = {0x00};
HAL_StatusTypeDef s;
HAL_StatusTypeDef t;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
// I2C�?��?�期的�?�1�?イト�?��?��?�る
s = HAL_I2C_Slave_Receive(&hi2c1, (uint8_t *)commandBuffer, 1, 1000);
if (s == HAL_OK) {
t = HAL_I2C_Slave_Transmit(&hi2c1, (uint8_t *)whoami, 1, 1000);
}
HERE is where the error in question occurs.
static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status,
uint32_t Timeout, uint32_t Tickstart)
{
while (__HAL_I2C_GET_FLAG(hi2c, Flag) == Status)
{
/* Check for the Timeout */
if (Timeout != HAL_MAX_DELAY)
{
if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
{
hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
hi2c->State = HAL_I2C_STATE_READY;
hi2c->Mode = HAL_I2C_MODE_NONE;
/* Process Unlocked */
__HAL_UNLOCK(hi2c);
return HAL_ERROR;//HERE
}
}
}
return HAL_OK;
}
Is there anything I am missing in this code?
P.S.
Thank you for some answers.
Now that I know the cause, will report it.
In order to communicate with the device I was using, I made a mistake in setting I2C Speed Mode.
(I2C Speed Mode : Standard Mode → Fast Mode)
I'm sorry for confusing you.
Solved! Go to Solution.
2023-11-24 08:16 PM
I change the i2c config pins and i2c module was working as I expected
and i also try with i2c bit-banging also work
my i2c module problem solved
thanks you for replaying