Skip to main content
Associate II
February 5, 2024
Question

I2C1 stops working over 8MHz STM32F042F6P6

  • February 5, 2024
  • 6 replies
  • 4179 views

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.

6 replies

Andrew Neil
Super User
February 5, 2024

When you increase the HCLK, do you adjust the I2C to match?

And what, exactly, do you mean by "stops working"?

Have you viewed the I2C signals on a scope? How do they look?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Davaol19Author
Associate II
February 5, 2024

Thank for your fast reply Andrew,

Davaol19_0-1707125270628.png

This is the way i configured the project trying to run the microcontroller at 16MHz (this node is working as slave), but with this configuration i can't receive anything on the master. But if i change de HCLK frequency to 8MHz, all works perfectly. 

With "it's not working" i mean that with 8MHz in HCLK the comunication works as i expect but if i put any higher frequency te comunication stops.

I'm not seen the signals on the oscilloscope yet, i'm waiting for a mate to let me use his.

Andrew Neil
Super User
February 5, 2024

Also show your configuration for 8MHz ...

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Davaol19Author
Associate II
February 5, 2024

I am using STM32F042F6P6

Davaol19Author
Associate II
February 5, 2024

Here it is:

 

Davaol19_0-1707127473999.png

 

TDK
Super User
February 5, 2024

it stops working

> te comunication stops

What exactly is happening that leads you to believe that communication stops? Does something in your code return an error message? Are the read values not as expected?

Are you debugging and stepping through the code or are you just running it and expecting an LED to blink or something, and inferring communcation stops from lack of the LED blinking?

"If you feel a post has answered your question, please click ""Accept as Solution""."
Davaol19Author
Associate II
February 5, 2024

Yes, i'm sorry but i'm a beginner and i don't have a lot of knowledge debugging, with " not working" i mean that i'm not receiving the data on the master. 

The code works in an specific frequency only, but in other projects i tried and no matters de frequency if the project is set with STM32CubeIDE or MX, the I2C still working.

TDK
Super User
February 5, 2024

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?

"If you feel a post has answered your question, please click ""Accept as Solution""."
Tesla DeLorean
Guru
February 5, 2024

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.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Davaol19Author
Associate II
February 5, 2024

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 */

}



}

/**************************************************************************************/

 

 

 

Andrew Neil
Super User
February 5, 2024

Use this button to properly post source code:

AndrewNeil_0-1707150229023.png

 

To get that extra row of icons, press this button:

AndrewNeil_1-1707150229027.png

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Visitor II
October 20, 2025

I also have the same issue with I2C communication. My project includes AHT20 as the temperature sensor to read the environment status.

In the beginning, I successfully implemented the AHT20 communication under HAL_I2C_Receive/Transmit, under 72MHz HCLK. As the project has heavy workload in data conversion, I tried to use the HAL_I2C_Receive_IT/Transmit_IT to reduce the time lag in peripherals' communication.

But the only way to let the AHT20 work is by locking the HCLK to 8MHz. Any frequencies of HLCK are higher than 8MHz, whether it is the crystal or the internal generator, the system can't generate the interruption to be captured by the callback function. No way to resolve this issue till now.