2022-03-08 11:43 AM
After spending hours trying to figure out why my I2C lines weren't working and trying my code on a STM32G031 development board, I've noticed that Cube IDE does not generate the following line in the file: stm32g0xx_hal_msp.c.
GPIO_InitStruct.Alternate = GPIO_AF6_I2C1;
The generated code for the file: stm32g0xx_hal_msp.c for the STM32G031 chip is as follows:
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_GPIOA_CLK_ENABLE();
/**I2C1 GPIO Configuration
PA9 ------> I2C1_SCL
PA10 ------> I2C1_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF6_I2C1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* Peripheral clock enable */
__HAL_RCC_I2C1_CLK_ENABLE();
/* USER CODE BEGIN I2C1_MspInit 1 */
/* USER CODE END I2C1_MspInit 1 */
}
}
Whereas this isn't the same when configuring the STM32G061 chip with the exact same settings as seen with the STM32G031 chip. With this line missing, the I2C bus always timeouts.
STM32G061's I2C init Function:
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_GPIOA_CLK_ENABLE();
/**I2C1 GPIO Configuration
PA9 ------> I2C1_SCL
PA10 ------> I2C1_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* Peripheral clock enable */
__HAL_RCC_I2C1_CLK_ENABLE();
/* USER CODE BEGIN I2C1_MspInit 1 */
/* USER CODE END I2C1_MspInit 1 */
}
}
Is this a generation bug or am I not doing something correctly?
2022-03-09 05:54 AM
Welcome, @JHirn.1, to the community!
I can confirm this misbehaviour with STM32CubeIDE 1.9.0 and STM32CubeMX 6.5.0.
@Sara BEN HADJ YAHYA Please inform the CubeMX team.
Regards
/Peter
2022-03-09 06:34 AM
Hello @JHirn.1 (Community Member) ,
First le me welcome you to STM32 Community and thank you for having reported :smiling_face_with_smiling_eyes:
To directly answer your question, I confirm that this is a problem related to the STM32CubeMX code generation. It is an known problem that have been already reported internally to be fixed as soon as possible. I'll surely keep you posted with the updates.
In fact, the issue is reproduced with STM32G051/STM32G061 devices: when signals are mapped on PA10 and PA9 (whatever the signal is, in this case I2C1_SCL is mapped on PA9 and I2C1_SDA is mapped on PA10), GPIO_InitStruct.Alternate = GPIO_AF6_I2C1 will not be generated.
As workaround, you can remap those signals by assigning for example:
By doing this remapping, the code will be correctly generated by CubeMX:
**I2C1 GPIO Configuration
PB7 ------> I2C1_SDA
PB8 ------> I2C1_SCL
*/
GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF6_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
Sorry for any inconvenience that this may cause and thanks for your contribution.
Khouloud.