PyBoard SPI strange behavior

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
User avatar
kamikaze
Posts: 154
Joined: Tue Aug 16, 2016 10:10 am
Location: Latvia
Contact:

PyBoard SPI strange behavior

Post by kamikaze » Wed Nov 16, 2016 2:55 am

While writing an SSD1322 driver and testing it, I've found a strange behaviour of PyBoard. If I'm sending framebuffer bytes one by one - it works slower, but at least works. If I'm trying to call SPI.write(bytearray) - it fills approx. half of the screen and then crashes with timeout error. Also if I press Ctrl+D in REPL - pyboard restarts and SSD1322 fills the rest (but still not until the end). Next Ctrl+D fills the rest completely. And while pressing Ctrl+D I see "PYB: can't mount SD card" error. And I'm unable to import disp anymore. Hard reset helps.
MicroPython v1.8.6-18-g72a1a34 on 2016-11-14; PYBv1.1 with STM32F405RG
Type "help()" for more information.
>>>
PYB: sync filesystems
PYB: soft reboot
MicroPython v1.8.6-18-g72a1a34 on 2016-11-14; PYBv1.1 with STM32F405RG
Type "help()" for more information.
>>> import disp
>>> d = disp.create_display()
>>> d.fill_buffer(0b00000101)
>>> d.send_buffer_one_by_one()
>>> d.fill_buffer(0b00000111)
>>> d.send_buffer_one_by_one()
>>> d.fill_buffer(0b00001111)
>>> d.send_buffer()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "display/ssd1322.py", line 81, in send_buffer
File "display/__init__.py", line 47, in write
OSError: [Errno 110] ETIMEDOUT
>>>
PYB: sync filesystems
PYB: soft reboot
PYB: can't mount SD card
MicroPython v1.8.6-18-g72a1a34 on 2016-11-14; PYBv1.1 with STM32F405RG
Type "help()" for more information.
>>>
PYB: sync filesystems
PYB: soft reboot
PYB: can't mount SD card
MicroPython v1.8.6-18-g72a1a34 on 2016-11-14; PYBv1.1 with STM32F405RG
Type "help()" for more information.
>>>
PYB: sync filesystems
PYB: soft reboot
PYB: can't mount SD card
MicroPython v1.8.6-18-g72a1a34 on 2016-11-14; PYBv1.1 with STM32F405RG
Type "help()" for more information.
>>> import disp
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: no module named 'disp'
d.send_buffer_one_by_one()

d.send_buffer()

Here is the code

Also is it possible to specify a timeout for SPI.write()?
Last edited by kamikaze on Wed Dec 07, 2016 5:20 pm, edited 1 time in total.

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

Re: PyBoard strange behavior on SPI.write(bytes)

Post by pythoncoder » Wed Nov 16, 2016 9:20 am

Have you looked at the official driver for the SSD1306 display? Look under drivers/display in the source tree. This reliably writes a framebuffer to a device via SPI. SPI is widely used and I doubt there's a problem on the Pyboard side.

I assume you've checked the device datasheet carefully? It's essential to get the SPI settings right. A while ago I had reliability problems with the NRF24L01 driver - it usually worked - until I discovered that the phase setting was incorrect. Once fixed it was rock solid.
Peter Hinch
Index to my micropython libraries.

User avatar
kamikaze
Posts: 154
Joined: Tue Aug 16, 2016 10:10 am
Location: Latvia
Contact:

Re: PyBoard strange behavior on SPI.write(bytes)

Post by kamikaze » Wed Nov 16, 2016 2:36 pm

if it's an SSD1322-only problem then why there is an error about SD card on Ctrl+D in REPL? :shock:

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

Re: PyBoard strange behavior on SPI.write(bytes)

Post by pythoncoder » Thu Nov 17, 2016 9:34 am

I don't know why it's causing trouble with the SD card except that (clutching at straws) the SD card uses an SPI interface.

My approach would be to identify the initial cause of the problem, which is the ETIMEDOUT message, and attempt to establish the cause of that. If you can fix that, there's a good chance that other, presumed consequent, problems will go away. Based on experience with SPI I'd be looking for hardware causes. Ensure leads are short and check signal quality, polarity, phase and baud rate.
Peter Hinch
Index to my micropython libraries.

User avatar
kamikaze
Posts: 154
Joined: Tue Aug 16, 2016 10:10 am
Location: Latvia
Contact:

Re: PyBoard strange behavior on SPI.write(bytes)

Post by kamikaze » Mon Dec 05, 2016 2:37 am

found some references to SSD1322 C drivers:

1) Example in C from (microchip site)

2) another C example on github

both of them have the same initialization params. I've adjusted mine to be the same. Still getting the same problem for PyBoard SPI when writing full buffer at once. :x
Last edited by kamikaze on Tue Dec 06, 2016 1:30 pm, edited 1 time in total.

User avatar
kamikaze
Posts: 154
Joined: Tue Aug 16, 2016 10:10 am
Location: Latvia
Contact:

Re: PyBoard strange behavior on SPI.write(bytes)

Post by kamikaze » Tue Dec 06, 2016 1:46 am

pythoncoder wrote:I don't know why it's causing trouble with the SD card except that (clutching at straws) the SD card uses an SPI interface.

My approach would be to identify the initial cause of the problem, which is the ETIMEDOUT message, and attempt to establish the cause of that. If you can fix that, there's a good chance that other, presumed consequent, problems will go away. Based on experience with SPI I'd be looking for hardware causes. Ensure leads are short and check signal quality, polarity, phase and baud rate.
I've found init settings that do not fail. And the winner iiiiiis: baudrate! :idea:

With baudrate=3000000 it works fine. with 1000000 or 2000000 it still fails. Am I wrong or PyBoard`s SPI fails if it is unable to send whole buffer in some time limit and baudrate could be the reason o_O
Last edited by kamikaze on Tue Dec 06, 2016 1:30 pm, edited 1 time in total.

User avatar
kamikaze
Posts: 154
Joined: Tue Aug 16, 2016 10:10 am
Location: Latvia
Contact:

Re: PyBoard strange behavior on SPI.write(bytes)

Post by kamikaze » Tue Dec 06, 2016 2:00 am

ok, even after no error on send_buffer() call - Ctrl+D still fails for SD card after restart

User avatar
kamikaze
Posts: 154
Joined: Tue Aug 16, 2016 10:10 am
Location: Latvia
Contact:

Re: PyBoard strange behavior on SPI.write(bytes)

Post by kamikaze » Wed Dec 07, 2016 1:31 am

localizing the problem with SD card error even more - it happens when pressing Ctrl+D right after:

Code: Select all

import disp

d = disp.create_display()

User avatar
kamikaze
Posts: 154
Joined: Tue Aug 16, 2016 10:10 am
Location: Latvia
Contact:

Re: PyBoard strange behavior on SPI.write(bytes)

Post by kamikaze » Wed Dec 07, 2016 1:37 am

Finally I've found the reason - SD card error occurs when CS pin is being initialized as OUT and set to 1 HERE

Code: Select all

self.cs.init(self.cs.OUT, value=1)
This drives me crazy ) if I disable it - then no SD card error occurs but screen doesn't work :/

Can anybody confirm that he is able to init his SSD1306 or any similar screen in REPL and then he is not getting an SD card error when hitting Ctrl+D? :?: :roll:

I am running my code on SD card btw.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: PyBoard strange behavior on SPI.write(bytes)

Post by dhylands » Wed Dec 07, 2016 8:14 am

Which pin are you using for CS?

Post Reply