Skip to main content
DMart.16
Associate II
September 30, 2021
Solved

STM32L4R5ZI gets stuck after initializing I2C

  • September 30, 2021
  • 9 replies
  • 2606 views

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

This topic has been closed for replies.
Best answer by DMart.16

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.

9 replies

waclawek.jan
Super User
September 30, 2021

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
DMart.16Author
Associate II
September 30, 2021

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

ST Technical Moderator
September 30, 2021

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

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Thanks
Associate
December 16, 2021

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.​

DMart.16
DMart.16Author
Associate II
October 1, 2021

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

ST Technical Moderator
October 1, 2021

Are you using DMA ?

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

Imen

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Thanks
DMart.16
DMart.16Author
Associate II
October 2, 2021

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

DMart.16
DMart.16AuthorBest answer
Associate II
October 4, 2021

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.

ST Technical Moderator
October 4, 2021

Thank you for the update you provided.

Really glad to know you overcame this problem :) .

Imen

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Thanks