2024-02-05 01:23 AM - last edited on 2024-02-05 07:36 AM by SofLit
Hello all,
I'm having problems with I2C1, i have a code that works perfectly while i configure the HCLK to 8MHz but it stops working when i set any higher frequency. Has anybody faced similar problem?
I am using STM32CubeIDE for setting the project, this microcontroller has a separate clock from HCLK, and i can't understand why I'm having this problem.
Thanks in advance.
2024-02-05 07:30 AM
Those signals look fine, in both plots.
> SCL drops to 0V and SDA is pulled up.
Not from what I can see. SCL sends out a clock and SDA toggles between 0 and 1 depending on what needs to be sent.
2024-02-05 07:34 AM
Monitor return values from HAL_I2C functions.
Again, "not receiving data" is ambiguous. Be more specific. How are you expecting to receive the data? What should STM32 do with data in order for you to receive it? What makes you think it's not received?
2024-02-05 07:37 AM
Yes, the picture is the comunication when i set the HCLK to 8Mhz, but if a set any higher frequency SCL drops to 0V and SDA remains pulled up.
The clock configuration is in the pictures in previous replys
2024-02-05 07:38 AM
So in this context the STM32F042F6P6 is the SLAVE device on the bus?
What device is the MASTER?
Show configuration of I2C at both ends of the transaction
And the ERROR status either end is reporting, ie the Master is not seeing a NACK from the Slave when it expects it.
2024-02-05 07:51 AM
I expect to see the data in the scope exactly the same with HCLK set to 8MHz and 16MHz, because is the unique change that i'm doing in the code that works, from wich i took the pictures.
I change de frequency using STMCubeIDE that automatically modifies all the parameters to maintain de 100KHz for the I2C.
What i'm seeing when i change the frequency is that the bus crashes, because in the scope I can't see the request from the Master, but i'im not modifying anything in te Master, only in the Salve. And the modification in the slave makes the bus crash. But the modification is only the HCLK and done by using STMCubeIDE, and it's something i've done before in other projects and worked well.
Sorry but for now i can't give more information because i need to learn how to debug the pcb with STMCubeIde to see if the code is throwing any exception or where it gets blocked.
Thanks for the help,
2024-02-05 08:03 AM
@Davaol19 wrote:What i'm seeing when i change the frequency is that the bus crashes, because in the scope I can't see the request from the Master,
But how do you know that the problem is due specifically to the I2C1 peripheral in your slave?
Or, in other words, how do you know that's it's not something else going wrong at over 8 MHz ?
2024-02-05 08:10 AM - edited 2024-02-05 08:37 AM
Configuration in the Slave (STM32F042F6P6):
/**************************************************************************************/
static void MX_I2C1_Init(void)
{
/* USER CODE BEGIN I2C1_Init 0 */
/* USER CODE END I2C1_Init 0 */
/* USER CODE BEGIN I2C1_Init 1 */
/* USER CODE END I2C1_Init 1 */
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x00303D5B;
hi2c1.Init.OwnAddress1 = 194;
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_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
/** Configure Analogue filter
*/
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
{
Error_Handler();
}
/** Configure Digital filter
*/
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN I2C1_Init 2 */
/* USER CODE END I2C1_Init 2 */
}
/**************************************************************************************/
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_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* Peripheral clock enable */
__HAL_RCC_I2C1_CLK_ENABLE();
/* I2C1 interrupt Init */
HAL_NVIC_SetPriority(I2C1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(I2C1_IRQn);
/* USER CODE BEGIN I2C1_MspInit 1 */
/* USER CODE END I2C1_MspInit 1 */
}
}
/**************************************************************************************/
Configuration in the Master (STM32F042G4U6) working at 48MHz:
This node only sends a request every seconf to the slave, but i tried and with the HCLK in the slave set
to 8MHz, i can set any frequency in The Master and the comunication still works. But if
i modify the Slave frequency over 8MHz stop working as i commented before.
/**************************************************************************************/
static void MX_I2C1_Init(void) {
/* USER CODE BEGIN I2C1_Init 0 */
/* USER CODE END I2C1_Init 0 */
/* USER CODE BEGIN I2C1_Init 1 */
/* USER CODE END I2C1_Init 1 */
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x2000090E;
hi2c1.Init.OwnAddress1 = 0;
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_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK) {
Error_Handler();
}
/** Configure Analogue filter
*/
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE)
!= HAL_OK) {
Error_Handler();
}
/** Configure Digital filter
*/
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK) {
Error_Handler();
}
/* USER CODE BEGIN I2C1_Init 2 */
/* USER CODE END I2C1_Init 2 */
}
/**************************************************************************************/
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
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF1_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* Peripheral clock enable */
__HAL_RCC_I2C1_CLK_ENABLE();
/* I2C1 interrupt Init */
HAL_NVIC_SetPriority(I2C1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(I2C1_IRQn);
/* USER CODE BEGIN I2C1_MspInit 1 */
/* USER CODE END I2C1_MspInit 1 */
}
}
/**************************************************************************************/
2024-02-05 08:23 AM
Use this button to properly post source code:
To get that extra row of icons, press this button:
2024-02-05 08:25 AM
Okay thanks!!
2024-02-06 12:05 AM
Thats true, i don't know if the problem is in the peripheral.