STM32L452RE error with HAL_I2C

Post here first, or if you can't find a relevant section!
Post Reply
Bambo
Posts: 75
Joined: Wed Jan 15, 2020 8:36 pm

STM32L452RE error with HAL_I2C

Post by Bambo »

Hi, my program is struggling with the HAL_I2C library. When the program is running it calculates the current voltage from a battery, if the battery voltage is too low it should stop. However, after around a minute or so, an assertion fails in the STM32l4XX_HAL_I2C.c at line 3210: https://github.com/stm32duino/Arduino_C ... 2c.c#L3210

I'm using the https://github.com/adafruit/Adafruit_INA260 library to interface with an INA260AIPW.

I have done some reading around and have found that some people have had problems with the HAL I2C library and the I2C BUSY state in particular, it looks as though it is present in some STM32 erattas too. I found these results by searching "hal i2c busy eratta" into google.

https://electronics.stackexchange.com/q ... -behaviour
https://electronics.stackexchange.com/q ... ialization

Thanks
ozcar
Posts: 143
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: STM32L452RE error with HAL_I2C

Post by ozcar »

Bambo wrote: Wed Feb 17, 2021 10:18 am I have done some reading around and have found that some people have had problems with the HAL I2C library and the I2C BUSY state in particular, it looks as though it is present in some STM32 erattas too. I found these results by searching "hal i2c busy eratta" into google.

https://electronics.stackexchange.com/q ... -behaviour
https://electronics.stackexchange.com/q ... ialization

Thanks
I had an issue with I2C getting stuck in "busy" state a few years ago. However, that was on STM32F103, and not even using STM32duino.

In my case the busy state could be cleared via SWRST in CR1. At first that was what I was doing if the situation arose, but then I found that the cause did seem to be related to something mentioned on the second page you linked to there, of setting up the gpios before enabling the I2C peripheral clock. I did not touch the HAL_I2C_MspInit code, instead I added an extra call to __HAL_RCC_I2C1_CLK_ENABLE() before the I2C init call and the problem went away.
Bambo
Posts: 75
Joined: Wed Jan 15, 2020 8:36 pm

Re: STM32L452RE error with HAL_I2C

Post by Bambo »

Here's an attached stack trace. It's almost like some sort of memory leak, it only occurs if the program has been running for a while?
Attachments
devenv_qxQuf5IA8z.png
devenv_qxQuf5IA8z.png (49.52 KiB) Viewed 3516 times
ozcar
Posts: 143
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: STM32L452RE error with HAL_I2C

Post by ozcar »

You're way ahead of me. What makes you think this has anything to do with I2C getting stuck in busy state? When I had that problem, the busy state caused a HAL error code to be generated (after some timeout). I don't remember the details now, and I also don't know how that would surface when using STM32duino.

I can't match up the line number you originally mentioned, nor what is in your last post, but it has to be where it checks the transfer options?

Code: Select all

assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions)); 
Does the ina260_test example run OK?
Bambo
Posts: 75
Joined: Wed Jan 15, 2020 8:36 pm

Re: STM32L452RE error with HAL_I2C

Post by Bambo »

Hi, thanks for your help, The hal assert callback prints these locations.
stm32l4xx_hal_i2c.c line 3165
assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XFerOptions));
and
stm32l4xx_hal_i2c.c line 6428
assert_param(IS_TRANSFER_MODE(Mode));
The test sketch ina260_test works fine, here's the source code:

Code: Select all

#include <Adafruit_INA260.h>
#include <Wire.h>

#include "stm32l4xx.h"
#include "stm32l4xx_hal.h"
#include "stm32l4xx_hal_conf.h"

#include "Sysclock_Config.h"

void assert_failed(uint8_t* inFileName, uint32_t line) {
	char* fileNameAsString = (char*)inFileName;
	Serial2.println(F("*** [HAL ASSERT FAILED] ***"));
	Serial2.printf("%s\r\n", (char*)inFileName);
	Serial2.printf("%i\r\n", line);
}

void _Error_Handler(const char* file, int line) {
	Serial2.println(F("*** [ERROR HANDLED] ***"));
	Serial2.printf("%s\r\n", file);
	Serial2.printf("%i\r\n", line);
}

Adafruit_INA260 ina260 = Adafruit_INA260();

void setup() {
	Serial2.begin(115200); 

	// Wait until serial port is opened
	while (!Serial2) { delay(10); }


	Wire.setSDA(PA10);
	Wire.setSCL(PA9);
	Wire.begin();

	Serial2.println("Adafruit INA260 Test");

	if (!ina260.begin(0b1000100, &Wire)) {
		Serial2.println("Couldn't find INA260 chip");
		while (1);
	}
	Serial2.println("Found INA260 chip");
}

void loop() {
	Serial2.print("Current: ");
	Serial2.print(ina260.readCurrent());
	Serial2.println(" mA");

	Serial2.print("Bus Voltage: ");
	Serial2.print(ina260.readBusVoltage());
	Serial2.println(" mV");

	Serial2.print("Power: ");
	Serial2.print(ina260.readPower());
	Serial2.println(" mW");

	Serial2.println();
	delay(1000);
}
Bambo
Posts: 75
Joined: Wed Jan 15, 2020 8:36 pm

Re: STM32L452RE error with HAL_I2C

Post by Bambo »

So the HAL I2C error doesn't occur if i run the readVoltage() function in the ina260 library is called at a slower rate, i don't know if this is a real fix or just delaying it.
ozcar
Posts: 143
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: STM32L452RE error with HAL_I2C

Post by ozcar »

I'm not familiar with the internals of this. Maybe there is some clue in the actual values in XFerOptions or Mode when it fails, or perhaps there is some subtle difference between what you do in your code compared to the example.

If it is somehow related to the rate, or just the number of times, perhaps the example will fail if you rip out the delay (and maybe the print/println calls).
Post Reply

Return to “General discussion”