I2C freezes the STM

Post here first, or if you can't find a relevant section!
Post Reply
DrBanana
Posts: 59
Joined: Fri Apr 22, 2016 11:15 am

I2C freezes the STM

Post by DrBanana » Tue Oct 31, 2017 10:39 am

The I2C freezing the uC was problem of Arduino, but seems to happen in STM library too.

The problem is, after some time I2C causes whole uC to freeze and it happens on random. To replicate the issue, one can short SDA and SCL pins for little time while I2C transmission is being done. After that uC will freeze.

To find which method is causing the problem, I put multiple Serial.println and found the causing method i2c_master_xfer() inside i2c.c file at \STM32F1\cores\maple\libmaple.

But I cant debug further as Serial seems not be working inside i2c.c file.

Here is the test code, reading I2C EEPROM.

Code: Select all

#include <HardWire.h>

#define EE_DeviceAddress 0x50
HardWire HW(1, I2C_FAST_MODE);

void setup() {
	Serial1.begin(9600);
	HW.begin();
}

void loop() {
	Serial1.println(String(micros()) + ", " + String(ReadByte(0)));
	delay(200);
}

byte ReadByte(uint16_t address)
{
	byte devAddr = EE_DeviceAddress | ((address >> 8) & 0x07); // extracts block, bitwise OR onto device addr.
	Serial1.println("Beginning");
	HW.beginTransmission(devAddr); // begin I2C transfer
	Serial1.println("Begin done");
	HW.write(address); // specify which memory address you're starting on
	Serial1.println("Write done");
	HW.endTransmission();	
	Serial1.println("End done");
	HW.requestFrom(devAddr, 1);
	Serial1.println("Request done");
	return HW.read();
}

racemaniac
Posts: 617
Joined: Sat Nov 07, 2015 9:09 am

Re: I2C freezes the STM

Post by racemaniac » Tue Oct 31, 2017 1:16 pm

i don't think there is anything that actually freezes the stm, but there are "freezes" that can be:
- the code ending up in an error handler that just implements an infinite loop (but not sure if there are such handlers in the arduino core)
- the peripheral ending in an error state, that isn't resolved by its interrupt handler (and thus continuously retriggering that interrupt) (at least i think it's something like that. when starting out with the stm32 i had regular issues that probably were caused by something like that. once i made sure the error interrupts properly handled said error condition, the stm would no longer "freeze")

DrBanana
Posts: 59
Joined: Fri Apr 22, 2016 11:15 am

Re: I2C freezes the STM

Post by DrBanana » Tue Oct 31, 2017 3:22 pm

racemaniac wrote:
Tue Oct 31, 2017 1:16 pm
i don't think there is anything that actually freezes the stm, but there are "freezes" that can be:
- the code ending up in an error handler that just implements an infinite loop (but not sure if there are such handlers in the arduino core)
- the peripheral ending in an error state, that isn't resolved by its interrupt handler (and thus continuously retriggering that interrupt) (at least i think it's something like that. when starting out with the stm32 i had regular issues that probably were caused by something like that. once i made sure the error interrupts properly handled said error condition, the stm would no longer "freeze")
How exactly did you solve the freeze issue ?

racemaniac
Posts: 617
Joined: Sat Nov 07, 2015 9:09 am

Re: I2C freezes the STM

Post by racemaniac » Tue Oct 31, 2017 3:38 pm

DrBanana wrote:
Tue Oct 31, 2017 3:22 pm
racemaniac wrote:
Tue Oct 31, 2017 1:16 pm
i don't think there is anything that actually freezes the stm, but there are "freezes" that can be:
- the code ending up in an error handler that just implements an infinite loop (but not sure if there are such handlers in the arduino core)
- the peripheral ending in an error state, that isn't resolved by its interrupt handler (and thus continuously retriggering that interrupt) (at least i think it's something like that. when starting out with the stm32 i had regular issues that probably were caused by something like that. once i made sure the error interrupts properly handled said error condition, the stm would no longer "freeze")
How exactly did you solve the freeze issue ?
lots of frustration & debugging ^^'
reading the manual, looking at what could be happening, etc ^^'
there is no easy answer

DrBanana
Posts: 59
Joined: Fri Apr 22, 2016 11:15 am

Re: I2C freezes the STM

Post by DrBanana » Tue Oct 31, 2017 4:10 pm

i2c_master_xfer has timeout parameter that wasn't used. I set it to 3 seconds and Init() the I2C if its times out. Seems to work fine.

dannyf
Posts: 167
Joined: Wed May 11, 2016 4:29 pm

Re: I2C freezes the STM

Post by dannyf » Sun Nov 12, 2017 2:27 pm

there is no easy answer
if your code uses SPL, it's not datasheet compliant - yes, ST's own code doesn't follow the datasheet.

The datasheet is correct, however, and the issue is with the clearing of EV6.

arpruss
Posts: 83
Joined: Sat Sep 30, 2017 3:34 am

Re: I2C freezes the STM

Post by arpruss » Thu Nov 16, 2017 1:58 pm

dannyf wrote:
Sun Nov 12, 2017 2:27 pm
if your code uses SPL, it's not datasheet compliant - yes, ST's own code doesn't follow the datasheet
What is SPL?

victor_pv
Posts: 1734
Joined: Mon Apr 27, 2015 12:12 pm

Re: I2C freezes the STM

Post by victor_pv » Thu Nov 16, 2017 6:12 pm

The libmaple hardwire implementation can freeze on certain conditions, I have seen it too.
I was using a memory without the pull up resistors (didn't notice they were missing), and it would work fine some times, but freeze others.
As Racemaniac pointed out, the problem is in the handling of error conditions. I looked with the debugger and could see the code hanging on an infinite loop because it had tried to do a STOP and an error flag had been set. Instead of managing the error flag, it just seats in a loop waiting for the STOP event to actually complete, which will not because it already has errored out.
I didn't care to look at changing that code since I found the pullup resistors were missing and that was causing the STOP to fail, but the code should either manage the errors, or at least ignore them, otherwise those freezes may happen.
The problem is software though, the hardware seemed to be working fine and flagging an error because the lines levels were not right.

evildave_666
Posts: 34
Joined: Mon Apr 27, 2015 11:20 pm

Re: I2C freezes the STM

Post by evildave_666 » Sat Nov 18, 2017 1:10 am

arpruss wrote:
Thu Nov 16, 2017 1:58 pm
dannyf wrote:
Sun Nov 12, 2017 2:27 pm
if your code uses SPL, it's not datasheet compliant - yes, ST's own code doesn't follow the datasheet
What is SPL?
The Standard Peripheral Library, an outdated API provided by ST but still somewhat widely used.

Post Reply