tl;dr:
- same behaviour in SPI and SoftSPI
- problem is SPI.write() with a string argument
- SPI.write() works fine with bytearray argument
- I thinks its a bug
long version:
With a bytearray() the hardware and SoftSPI work fine.
like
Code: Select all
s = SPI(1, baudrate=100000, polarity=0, phase=0, sck=Pin(10), mosi=Pin(11), miso=Pin(12))
wert = 129
spidata = bytearray()
spidata.append(wert & 0xff)
s.write(spidata)
I get one byte on the SPI.
I checked with a for wert in range(0,256) of course.
But when using strings as argument to SPI.write() or SoftSPI.write(), i get this weird behaviour:
- characters with byte values below 128 (i.e. highbit not set) are output fine
- characters with the high bit set are converted to some 16-bit value and sent out as two bytes on the SPI
- strings with more than one character show the behaviour for each character, i.e. values <=127 are shifted out as one, >=128 as two bytes
- I did not analyse the 16-bit-values my characters were converted to; looked nonrandom/consistent though.
The behaviour is
exactly the same for SPI and SoftSPI, so it happens in the code shared between the two: the conversion from string to bytearray or suchlike.
My gut tells me that
using SPI.write() with a string as argument should raise TypeError to enforce use of bytearray.
Iirc. I got the string argument to SPI.write() from some "example" code out there on the Big Evil Internet.
And that will break always, it seems.
I am thinking about people out there without Osci to debug this, the TypeError would help them.