spi won't send last bytearray() value if its 0x00

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
Delebel
Posts: 32
Joined: Thu May 25, 2017 2:21 pm
Contact:

spi won't send last bytearray() value if its 0x00

Post by Delebel » Wed May 27, 2020 6:08 pm

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')

Christian Walther
Posts: 113
Joined: Fri Aug 19, 2016 11:55 am

Re: spi won't send last bytearray() value if its 0x00

Post by Christian Walther » Wed May 27, 2020 7:19 pm

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.

User avatar
pythoncoder
Posts: 4242
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: spi won't send last bytearray() value if its 0x00

Post by pythoncoder » Fri May 29, 2020 11:28 am

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
Peter Hinch

Delebel
Posts: 32
Joined: Thu May 25, 2017 2:21 pm
Contact:

Re: spi won't send last bytearray() value if its 0x00

Post by Delebel » Sat May 30, 2020 8:14 pm

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

User avatar
Roberthh
Posts: 2014
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: spi won't send last bytearray() value if its 0x00

Post by Roberthh » Sat May 30, 2020 8:39 pm

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.

Delebel
Posts: 32
Joined: Thu May 25, 2017 2:21 pm
Contact:

Re: spi won't send last bytearray() value if its 0x00

Post by Delebel » Sat May 30, 2020 9:18 pm

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

Post Reply