cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4R5ZI gets stuck after initializing I2C

DMart.16
Associate II

Hello and Thank you

I'm starting with I2C on STM32L4R5ZI and I can't get it to work.

I'm generating a very simple project for the Nucleo board in CubeIDE.

I have only initialize the parameters in CubeIDE, and when I try to run it the red LED ligths up and the programm doesn't execute.

I've been messing with it and it doesn't execute code after the initialization of the I2C peripheral ("MX_I2C1_Init();").

I have tried changing the clock that the peripheral is using but that doesn't seem to work.

Looks like the CubeIDE initialization is not working, but I can't find why.

Thank you so much

Daniel

1 ACCEPTED SOLUTION

Accepted Solutions
DMart.16
Associate II

I've finally made it!

Looks like there was a problem with the sysTick clock (used as Timebase Source) so I've just changed it to another timer (TIM6 in this case) and it all works now.

View solution in original post

9 REPLIES 9

Which pins are you using for I2C? What is connected to those pins? Do you have pullups connected?

> it doesn't execute code after the initialization of the I2C peripheral

What does this mean? Where is the program counter?

JW

DMart.16
Associate II

The pins are PB8 and PB9, I haven't connected anything to them yet.

I've tried setting an LED (LED2) after the initialization of the I2C and it won't turn on.

Any code after "MX_I2C1_Init();" wont execute, even if I reset the board with the button. And the red LED (LED3) keeps turning on.

Daniel

Imen.D
ST Employee

Hello @DMart.20n​ ,

Which CubeMX version are you using ?

Try to change clock enable __HAL_RCC_I2C1_CLK_ENABLE() before the GPIO initializationHAL_GPIO_Init().

Please let me know if this resolve your issue.

If your issue is solved, please select the answer as “Best�? as this will help other users to find answers faster.

Imen

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

Thank you for your answer, but enabling the clock before the GPIO didn't work.

I'm using CubeIDE v1.7.0 (CubeMX inside it).

I've noticed that LD8 also lights by itself (same as LD3).

I'll give you a bit more info:

The code:

__HAL_RCC_I2C1_CLK_ENABLE();
  MX_GPIO_Init();
  MX_I2C1_Init();
 
  HAL_GPIO_WritePin(GPIOB, LD2_Pin, GPIO_PIN_SET);
  HAL_I2C_Master_Transmit(&hi2c1, 0x00 << 1, (uint8_t *)"123", 1, 1000);
  HAL_GPIO_WritePin(GPIOB, LD3_Pin, GPIO_PIN_SET);

The thing is that when I load the code the board will get stuck and light up LD3 and LD8, same if I reset.

If I comment the I2C code (inizialitation and transmit) the code will load up without a problem.

Daniel

Update:

I've unpluged the nucleo board and plugged it back and it appears that the code runs.

After loading the code it gets stuck, but if you reset the power (unpluging and pluging back) it works, as seen in the image.

I don't know why it gets stuck after loading the code though.

0693W00000FBAzVQAX.png

Are you using DMA ?

In this case, it maybe you have the issue with DMA Initialization order.

Imen

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

I'm using polling mode, not DMA nor interrupt.

DMart.16
Associate II

I've finally made it!

Looks like there was a problem with the sysTick clock (used as Timebase Source) so I've just changed it to another timer (TIM6 in this case) and it all works now.

Thank you for the update you provided.

Really glad to know you overcame this problem :) .

Imen

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

Exactly this solves at least my Errors. In all other peripheral I use (SPI, UART) it is generated this way. First enable Peripheral clock, then GPIO clock. If first doing GPIO stuff first and then Enable Peripheral it looks in case of I2C like playing lottery if the busy flag is reset correctly. If the order is changed (enabling both clocks before manipulate GPIO) the device starts always correctly. May also depend potentially on compiler settings , too. Most time I use non-optimze setting's.​