2021-09-30 03:39 AM
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
Solved! Go to Solution.
2021-10-04 05:06 AM
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.
2021-09-30 03:49 AM
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
2021-09-30 03:54 AM
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
2021-09-30 04:10 AM
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
2021-09-30 11:55 PM
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.
2021-10-01 05:04 AM
Are you using DMA ?
In this case, it maybe you have the issue with DMA Initialization order.
Imen
2021-10-02 01:54 AM
I'm using polling mode, not DMA nor interrupt.
2021-10-04 05:06 AM
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.
2021-10-04 05:57 AM
Thank you for the update you provided.
Really glad to know you overcame this problem :) .
Imen
2021-12-16 08:11 AM
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.