Page 1 of 1

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

Posted: Fri Jun 28, 2019 10:38 pm
by happyday
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.

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

Posted: Mon Jul 01, 2019 6:40 am
by pythoncoder
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.

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

Posted: Mon Jul 01, 2019 10:21 am
by happyday
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.

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

Posted: Mon Jul 01, 2019 3:47 pm
by pythoncoder
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.

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

Posted: Mon Jul 01, 2019 7:15 pm
by happyday
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.

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

Posted: Tue Jul 02, 2019 6:05 am
by pythoncoder
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.