2024-02-07 08:53 AM
Hello all,
I have been trying to get I2C communication working on my NUCLEO-U5A5ZJ-Q board based on STM32U5A5ZJJT6U. First I have used STM32-CUBE-IDE and CUBE-MX to generate code but I was getting error when I try to transmit so I write a custom code which is also attached attached and after doing a bit of stepping through the code, it looks as though a HAL_TIMEOUT occurs when the I2C driver executes the I2C_WaitOnTXISFlagUntilTimeout() function and the exact error I am getting is as below
if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == RESET))
{
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 IT RETURNS ERROR IN MY CASE ///////
}
I have looked through the datasheet for this particular micro to figure out why this flag would not be set, but it is not immediately clear and it is controlled by HW. Is there something wrong with my setup?
Also I have changed main as follow:
int main(void)
{
HAL_Init(); // Initialize the HAL library
SystemClock_Config(); // Configure the system clock
__HAL_RCC_GPIOB_CLK_ENABLE(); // Enable GPIOB clock
__HAL_RCC_I2C1_CLK_ENABLE(); // Enable I2C1 clock
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; // Configure GPIO pins for I2C1 SCL and SDA
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; // Alternate function open-drain
GPIO_InitStruct.Pull = GPIO_PULLUP; // Enable internal pull-up resistors
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; // High-speed output
GPIO_InitStruct.Alternate = GPIO_AF4_I2C2; // I2C1 alternate function
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); // Initialize GPIO pins
hi2c1.Instance = I2C2; // Select I2C1 peripheral
//hi2c1.Init.Timing = 0xE14;
hi2c1.Init.Timing = 0x30420F13;
//hi2c1.Init.ClockSpeed = 100000; // I2C clock speed (100 kHz)
//hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; // I2C duty cycle
hi2c1.Init.OwnAddress1 = 0; // Not used in master mode
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; // 7-bit addressing mode
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; // No dual addressing
hi2c1.Init.OwnAddress2 = 0; // Not used in master mode
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; // No general call
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; // Enable clock stretching
if (HAL_I2C_Init(&hi2c1) != HAL_OK) // Initialize I2C peripheral
{
Error_Handler(); // Handle initialization error
}
uint8_t sendData[] = {0x01}; // Data to be sent over I2C
while (1)
{
if (HAL_I2C_Master_Transmit(&hi2c1, 0x20, sendData, sizeof(sendData), 10000) != HAL_OK)
{
Error_Handler(); // Handle transmission error ////////////////////// HERE ITS RETURNING ERROR AFTER TIME OUT EXPIRE ///////////////////////////
}
}
}
Any help would be greatly appreciated.
Thanks!
2024-02-10 07:32 AM
The above code is generated by CubeMX. I have write code to toggle PF0 and PF1 and it was working as expected but not getting proper i2C output.
2024-02-10 07:34 AM
The above code is generated by CubeMX. I have write code to toggle PF0 and PF1 and it was working as expected but not getting proper i2C output.