Page 1 of 1
spi won't send last bytearray() value if its 0x00
Posted: Wed May 27, 2020 6:08 pm
by Delebel
I'm attempting to connect an LCD12864 to my pyboard using SPI and I must (tested in C with the same hardware works) send 3 bytes,a cmd then 2 bytes of data. Now my python code is almost working except that when the last byte is zero it is not sent. I use a buffer buf=bytearray(3) and load it with the cmd followed by 2 data bytes. However when the last byte is 0x00 it is not send if I believe the print statement I call prior to doing the Spi.write(buf). I falsely assumed the complete bytearray was going to be sent. Is there a method to force the SPI driver code taken from "from pyb import SPI" to send all 3 bytes regardless of their content?
Thanks ....Denis
P.S. a snippet of the print output
dat: bytearray(b'\xf8\x00\x10')
dat: bytearray(b'\xf8\x00`')
dat: bytearray(b'\xf8\x00\xc0')
dat: bytearray(b'\xf80\x00')
dat: bytearray(b'\xf8\x00\x10')
Re: spi won't send last bytearray() value if its 0x00
Posted: Wed May 27, 2020 7:19 pm
by Christian Walther
I am almost certain that your conclusion that only 2 bytes are being sent is wrong and you are making a common mistake.
Note that all your examples contain 3 bytes. They are (in hexadecimal)
F8 00 10
F8 00 60
F8 00 C0
F8 30 00
F8 00 10
0x60 is the ASCII code of '`', 0x30 is the ASCII code of '0', and printing out a bytearray (or bytes) displays these and other printable characters as ASCII characters, not as hexadecimal escape sequences.
Re: spi won't send last bytearray() value if its 0x00
Posted: Fri May 29, 2020 11:28 am
by pythoncoder
Python's way of printing bytes and bytearrays is confusing. An option is to use a function along these lines
Code: Select all
def bprint(a):
for x in a:
print('\\x{:02x}'.format(x), end='')
print()
to print everything in hex format:
Code: Select all
>>> bprint(b'\x01abc`\xff')
\x01\x61\x62\x63\x60\xff
Re: spi won't send last bytearray() value if its 0x00
Posted: Sat May 30, 2020 8:14 pm
by Delebel
Yes your are correct. After connecting a logic analyser on the spi line I confirmed that 24 bits are being transmitted on every transaction. I'm still at a lost why it doesn't work the 3.3 to 5v converter might be the culprit but I think I would have noticed from the analyser output. I do know that on my C controller the baud rate is 100000 kbs and here the lowest I can get if I can trust the print of the spi channel created is 164062 with a prescaler=256. And setting baudrate instead of prescaling yeilds the same #. So now I trying to figure out how I can slow down the SPI stream but I believe that jobs probably requires to mess with inline assembly or some C compiled bytecode which as a beginner in this environment is way out of my league.
Thanks for your reply...the saga to move from C to python continues...
Denis
Re: spi won't send last bytearray() value if its 0x00
Posted: Sat May 30, 2020 8:39 pm
by Roberthh
The simple one transistor/channel level converters require pull-up resistors at the LCD side.. For I2C, that's anyhow required. But a SPI device may not have them. The are typically driven by a push-pull output. Level converters based on a txb0104 for instance work better in your case. You may as well also simply use a 74hx125 gate as driver, if you only need the direction from the MCU to the LCD12864.
Re: spi won't send last bytearray() value if its 0x00
Posted: Sat May 30, 2020 9:18 pm
by Delebel
Bingo the LCD init setup operation required a 5 msecs delay between each commands bytes which I was doing in C but the code I acquired on git hub as my starting point did not have any such delay. One line added in the spi.write loop pyb.delay(5) and voila! My weekend has just got a bit brighter for it. Thank for your help. One day when I acquired more experience with MicroPhyton I will strive to return the favor to other newbies like myself today..
Thanks again...Denis