2022-04-08 03:16 AM
Hi community,
I am trying to use I2C on the STM32L010RBTX,
during configuration in MX, i realized that when PB8 and PB9 is configured as I2C, the GPIO mode field shows n/a.
And the I2C won't work as well.
However, when i configure to PA9/PA10,
GPIO mode shows alternate function,
which, I2C works perfectly fine under the same code.
I am just using HAL_I2C_IsDeviceReady to check whether it is sending out anything.
Any idea how to make it work on PB8 and PB9?
Much thanks!
Solved! Go to Solution.
2022-11-28 06:27 AM
Hello @AWong.11 ,
This issue is fixed in STM32CubeMX latest release.
V6.7.0 is now available under this Link.
Thanks for your contribution :smiling_face_with_smiling_eyes:
Sara.
2022-04-08 06:59 AM
Look at the generated code to see if AF is specified. If not, you'll need to add that in manually in the I2C pin initialization code.
CubeMX has a bunch of bugs like this where it initializes the pins but with an uninitialized/zero AF field.
2022-04-08 07:01 AM
Hello @AWong.11 and welcome to the Community :)
Do you have pull up resistors?
Try changing the mode to push pull mode.
Maybe this FAQ helped you as describes few tips related to I2C : STM32 I2C does not work
Imen
2022-04-08 07:02 AM
Pin initialization code is in HAL_I2C_MspInit in the stm32l0xx_hal_msp.c file.
2022-04-08 07:53 AM
Thank you so much boss.
Added the AF init in MSP file and it worked.
Some how you've must had encountered these bugs many times:grinning_face_with_sweat:
2022-04-08 07:54 AM
Thank you for the reply Imen.
Problem has been solve and it's a bug of the code gen.
Please take note as well hope this can be mitigated in future updates.
Thanks for the reply anyways.
2022-04-13 10:37 AM
I have passed this issue along to our CubeMx team for review and fix.
Hi @Sara BEN HADJ YAHYA , please take this request into consideration.
Thanks
Imen
2022-04-14 01:09 AM
Hello @AWong.11 ,
Thanks for your feedback,
You are right, it is indeed a CubeMX bug.
It seems that the GPIO configuration for I2C is missing from HAL_I2C_MspInit() when Port B is used.
It should be as follow:
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(hi2c->Instance==I2C1)
{
/* USER CODE BEGIN I2C1_MspInit 0 */
/* USER CODE END I2C1_MspInit 0 */
__HAL_RCC_GPIOB_CLK_ENABLE();
/**I2C1 GPIO Configuration
PB8 ------> I2C1_SCL
PB9 ------> I2C1_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF6_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* Peripheral clock enable */
__HAL_RCC_I2C1_CLK_ENABLE();
/* USER CODE BEGIN I2C1_MspInit 1 */
/* USER CODE END I2C1_MspInit 1 */
}
}
The missing part should be added in the user section, this will prevent loosing it when the code is regenerated by CubeMX.
/* USER CODE BEGIN I2C1_MspInit 0 */
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF6_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE END I2C1_MspInit 0 */
With this being said, the issue is now reported to the dev team to be fixed. I will keep you posted.
Internal ticket number: 126421 (This is an internal tracking number and is not accessible or usable by customers).
Thanks for your contribution,
Sara.
2022-11-28 06:27 AM
Hello @AWong.11 ,
This issue is fixed in STM32CubeMX latest release.
V6.7.0 is now available under this Link.
Thanks for your contribution :smiling_face_with_smiling_eyes:
Sara.