unexpected/don't understand: SPI - cprrect register readings only when print statements included

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
happyday
Posts: 14
Joined: Sat Jan 21, 2017 12:11 pm

unexpected/don't understand: SPI - cprrect register readings only when print statements included

Post by happyday » Fri Jun 28, 2019 10:38 pm

I am using an ESP32 DevKit C to talk with an atm90e32 energy monitor over HSPI. MOSI/MISO traffic goes back and forth. However, the values for voltage sent back are correct when I pepper the script with print statements. When I do not, the readings are off. I thought I fixed SPI timing by judiciously inserting time.sleep_us(<usually 4>).

I don't understand this behavior and am hoping someone might be able to help me understand (i.e.: is it a timing thing I just can't get right? Something else?). Thank you.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: unexpected/don't understand: SPI - cprrect register readings only when print statements included

Post by pythoncoder » Mon Jul 01, 2019 6:40 am

Are you setting up the SPI with polarity=1, phase=1? From the chip datasheet these look like the correct values. The other SPI issue is to keep leads fairly short.
Peter Hinch
Index to my micropython libraries.

happyday
Posts: 14
Joined: Sat Jan 21, 2017 12:11 pm

Re: unexpected/don't understand: SPI - cprrect register readings only when print statements included

Post by happyday » Mon Jul 01, 2019 10:21 am

Thank you. I am. I can get it to work. The surprise I have is now I wait 3ms between read/writes. Then I get the right values for voltage. With CircuitPython, I need not wait this long.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: unexpected/don't understand: SPI - cprrect register readings only when print statements included

Post by pythoncoder » Mon Jul 01, 2019 3:47 pm

It's possible that CircuitPython creates delays of this order by default. Bus accesses are mediated by Adafruit classes which set a lock to protect against conflicts. Obviously this must take some time. CircuitPython is designed for ease of use by beginners whereas MicroPython is designed for performance (and assumes more knowledgeable users).

I would read the chip datasheet carefully for any latency requirements.
Peter Hinch
Index to my micropython libraries.

happyday
Posts: 14
Joined: Sat Jan 21, 2017 12:11 pm

Re: unexpected/don't understand: SPI - cprrect register readings only when print statements included

Post by happyday » Mon Jul 01, 2019 7:15 pm

Thank you. I have read the data set. And have looked at similar micropython code for a similar chip (the atm90e36). It is odd. I'm looking at the timing of CircuitPython / SPI and the same traffic with Micropython/SPI using a logic analyzer, checking the timing. For a reason I don't understand, I have to add 3ms to micropython/SPI, but not to CircuitPython. There is clearly something I don't understand (and am missing). I appreciate your thoughts on this.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: unexpected/don't understand: SPI - cprrect register readings only when print statements included

Post by pythoncoder » Tue Jul 02, 2019 6:05 am

In which case I'm baffled. You evidently know what you're doing and you're interfacing to a high quality chip.

If the data as seen by a logic analyser is the same except this 3ms delay, it's hard to see how the chip "knows" that the source is MicroPython rather than CircuitPyhon. There must be something we're missing here.
Peter Hinch
Index to my micropython libraries.

Post Reply