SPI initialization not working

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.
Post Reply
Gordon_Hardman
Posts: 68
Joined: Sat May 03, 2014 11:31 pm

SPI initialization not working

Post by Gordon_Hardman » Wed Aug 29, 2018 4:24 pm

I am using a PyBoard 1.1, with the latest 1.9.4 firmware:
MicroPython v1.9.4 on 2018-05-11; PYBv1.1 with STM32F405RG

I try to create an SPI object, but it does not seem to take my initialization.

Here is from REPL:
>>> spi=SPI(1, SPI.MASTER, baudrate=600000, polarity=1, phase=0, bits=16, firstbit=SPI.MSB)
>>> spi
SPI(1, SPI.MASTER, baudrate=328125, prescaler=256, polarity=1, phase=0, bits=16)
>>>

The baudrate is wrong, and there appears to be no firstbit attribute.
Checking on a scope, the baudrate is indeed around 330kHz.
What am I doing wrong?

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: SPI initialization not working

Post by jickster » Wed Aug 29, 2018 10:31 pm

Gordon_Hardman wrote:
Wed Aug 29, 2018 4:24 pm
I am using a PyBoard 1.1, with the latest 1.9.4 firmware:
MicroPython v1.9.4 on 2018-05-11; PYBv1.1 with STM32F405RG

I try to create an SPI object, but it does not seem to take my initialization.

Here is from REPL:
>>> spi=SPI(1, SPI.MASTER, baudrate=600000, polarity=1, phase=0, bits=16, firstbit=SPI.MSB)
>>> spi
SPI(1, SPI.MASTER, baudrate=328125, prescaler=256, polarity=1, phase=0, bits=16)
>>>

The baudrate is wrong, and there appears to be no firstbit attribute.
Checking on a scope, the baudrate is indeed around 330kHz.
What am I doing wrong?
You imported the wrong SPI module.
You have to do

Code: Select all

from pyb import SPI
https://docs.micropython.org/en/latest/ ... l-protocol

Import SPI uses "machine.SPI".

chuckbook
Posts: 135
Joined: Fri Oct 30, 2015 11:55 pm

Re: SPI initialization not working

Post by chuckbook » Thu Aug 30, 2018 9:49 am

Note that STM32 SPI cannot provide all baudrates. In fact if a specific baudrate is requested, MPY selects the available baudrate <= requested baudrate. Also note that available baudrates vary with SYSCLK frequency.
All of the above is only true for HW SPI, SW SPI might give finer granularity at lower frequencies. But keep in mind that SW SPI usually consumes more power.
In the given example one could use a baudrate of 2*328125, which is closer to 600000 but obviously greater than 600000.

Post Reply