cancel
Showing results for 
Search instead for 
Did you mean: 

Not able to use Port B alternate function (I2C)

AWong.11
Associate II

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.

0693W00000LxM6wQAF.pngHowever, when i configure to PA9/PA10,

GPIO mode shows alternate function,

which, I2C works perfectly fine under the same code.

0693W00000LxM8xQAF.pngI 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!

1 ACCEPTED SOLUTION

Accepted Solutions
Sara BEN HADJ YAHYA
ST Employee

Hello @AWong.11​ ,

This issue is fixed in STM32CubeMX latest release.

V6.7.0 is now available under this Link.

Thanks for your contribution ��

Sara.

View solution in original post

8 REPLIES 8
TDK
Guru

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.

If you feel a post has answered your question, please click "Accept as Solution".
Imen.D
ST Employee

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

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

Pin initialization code is in HAL_I2C_MspInit in the stm32l0xx_hal_msp.c file.

If you feel a post has answered your question, please click "Accept as Solution".

Thank you so much boss.

Added the AF init in MSP file and it worked.

0693W00000LxOXfQAN.pngSome how you've must had encountered these bugs many times��

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.

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

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
Sara BEN HADJ YAHYA
ST Employee

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.

Sara BEN HADJ YAHYA
ST Employee

Hello @AWong.11​ ,

This issue is fixed in STM32CubeMX latest release.

V6.7.0 is now available under this Link.

Thanks for your contribution ��

Sara.