Page 1 of 1
SPI not initialising baudrate correctly
Posted: Fri Nov 07, 2014 10:29 pm
by Beau
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?
Re: SPI not initialising baudrate correctly
Posted: Fri Nov 07, 2014 10:39 pm
by Damien
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.
Re: SPI not initialising baudrate correctly
Posted: Fri Nov 07, 2014 10:49 pm
by Beau
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?
Re: SPI not initialising baudrate correctly
Posted: Fri Nov 07, 2014 11:38 pm
by Damien
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).