SPI not initialising baudrate correctly

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Beau
Posts: 13
Joined: Sun Oct 26, 2014 1:04 pm

SPI not initialising baudrate correctly

Post by Beau » Fri Nov 07, 2014 10:29 pm

Hi,

I'm trying to set the SPI baudrate but it doesn't actually use the value I pass:

Code: Select all

>>> pyb.SPI(1,pyb.SPI.MASTER,baudrate=600000)
SPI(1, SPI.MASTER, baudrate=328125, polarity=1, phase=0, bits=8)
Any idea what could cause this?

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: SPI not initialising baudrate correctly

Post by Damien » Fri Nov 07, 2014 10:39 pm

Only a few baudrates are supported by the hardware. The logic of the SPI init function is that it picks the closest baudrate less than or equal to the one you ask for.

The baudrate is the CPU frequency divided by a prescaler. I'd you really need a specific rate try changing the CPU frequency.

Beau
Posts: 13
Joined: Sun Oct 26, 2014 1:04 pm

Re: SPI not initialising baudrate correctly

Post by Beau » Fri Nov 07, 2014 10:49 pm

I'm trying to set it to a slow rate.

Code: Select all

>>> pyb.SPI(1,pyb.SPI.MASTER,baudrate=12800)
SPI(1, SPI.MASTER, baudrate=328125, polarity=1, phase=0, bits=8)
Would 328125 be the lowest?

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: SPI not initialising baudrate correctly

Post by Damien » Fri Nov 07, 2014 11:38 pm

SPI(1) is clocked from CPU/2; SPI(2) and SPI(3) are clocked from CPU/4.

The SPI's can then have a prescaler (clock divider) between 2 and 256 (all powers of 2: 2 4 8 16 32 64 128 256).

If you want 12800Hz, then you'll need a prescaler of 256, and a CPU freq of 12800*256*4=13MHz (and use SPI(2) or SPI(3)).

The available CPU frequencies are: 8MHz, 16MHz, ... (listed at http://docs.micropython.org/en/latest/l ... l#pyb.freq).

Note that with a CPU freq below 36MHz you cannot use the USB.

An alternative is to do the SPI in software. So, write a function to clock the line and read/write data. If you want a baudrate of 12.8kHz then this should be doable in Python (since it can clock a line at >100kHz in pure Python without doing anything fancy).

Post Reply