platformio / platform-atmelsam

Atmel SAM: development platform for PlatformIO
https://registry.platformio.org/platforms/platformio/atmelsam
Apache License 2.0
82 stars 108 forks source link

Deadlock in SERCOM::startTransmissionWIRE() #65

Open sampscl opened 5 years ago

sampscl commented 5 years ago

In SERCOM.cpp the startTransmissionWIRE() function does some setup, transmits the slave address, and waits on completion. In the current GitHub version (cf28026aef2f65e2ddc5c631d2b01a144ff0a068), this is on line 529. There are circumstances in which the MB flag will never be set, in particular when the bus is in an "unknown" state. I've run across the case where startTransmissionWIRE() deadlocks on the MB flag when addressing an SMBUS device on a SAMD21G18 MCU.

A temporary fix for me was to add a check for sercom->I2CM.INTFLAG.bit.ERROR and return false if that is set while waiting on MB. I didn't want to mod the code and do a pull request since I don't have a good way of verifying that my changes work in all circumstances and on all supported variants.

ivankravets commented 5 years ago

See https://github.com/platformio/platformio-pkg-framework-arduinosam/issues/8

Is this issue fixed in the latest 1.2.9 core by Adafruit? We plan to update it soon.

sampscl commented 5 years ago

Bottom line up front: I'm going to work with the Arduino project for this. I attached a logic analyzer capture of the "offending" bus activity. At the end of the attached capture, the bus state in the SAMD21 SERCOM is "busy" and it never leaves that state. Looking at the signals, there is never a "stop" condition on the bus -- so the SERCOM is correct. The problem is that there is no way for a calling program to detect this condition -- and that is where I'll start with the Arduino folks.

i2c-smbus-fail

ivankravets commented 5 years ago

Please re-test with upstream version https://docs.platformio.org/en/latest/platforms/atmelsam.html#upstream

sampscl commented 5 years ago

Please re-test with upstream version https://docs.platformio.org/en/latest/platforms/atmelsam.html#upstream

I still have a problem with the upstream version. It showed up in a different location, but the end result is the same. DDD screen capture attached for reference.

ddd-i2c-busyloop
sampscl commented 5 years ago

I didn't mean to close the issue!

ivankravets commented 5 years ago

Does it work with Arduino IDE?

sampscl commented 5 years ago

Does it work with Arduino IDE?

If I get time, I will try it. My project is pretty tightly coupled with platformio/cmake/gitlab. I've honestly never used the Arduino IDE.

In the meantime, I've got a fork and branch of ArduinoCore-samd at github that worked nicely overnight last night.

ivankravets commented 5 years ago

Could you reproduce this issue with the latest release of dev/platform?