Skip to main content
DMart.16
Associate II
Sep 30, 2021 10:39 AM UTC
Solved

STM32L4R5ZI gets stuck after initializing I2C

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.
DMart.16
DMart.16Best answer
Associate II
Oct 4, 2021 12:06 PM UTC

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
Sep 30, 2021 10:49 AM UTC

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
Sep 30, 2021 10:54 AM UTC

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
Sep 30, 2021 11:10 AM UTC

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
Dec 16, 2021 4:11 PM UTC

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
Oct 1, 2021 6:55 AM UTC

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
Oct 1, 2021 12:04 PM UTC

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
Oct 2, 2021 8:54 AM UTC

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

DMart.16
DMart.16AuthorBest answer
Associate II
Oct 4, 2021 12:06 PM UTC

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
Oct 4, 2021 12:57 PM UTC

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