Serial echo hangs? I must be crazy...

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
User avatar
dbc
Posts: 89
Joined: Fri Aug 28, 2015 11:02 pm
Location: Sunnyvale, CA

Serial echo hangs? I must be crazy...

Post by dbc » Sun Jul 16, 2017 5:55 pm

I'm experience strange hangs with Pyboard v1.1 running

Code: Select all

>>> sys.implementation
(name='micropython', version=(1, 9, 1))
I'm working on a robot with a RasPi3 for ROS/Linux and a PyBoard for the hard realtime. I'm using serial to connect the two. Now first off the RasPi3 serial port is well known to be sub-standard, and I've stolen the Bluetooth UART from the RasPi to reroute it to the I/O header pins as that is supposed to be a better UART, at least it has a deeper buffer. In any case, that is one of the UART's that I'm using as the PyBoard's peer. I've also tried with an FTDI serial USB cable.

I've written a little loopback code shown below, just to see if I can get data from the RasPi to the PyBoard and back again without corruption. Strangely, the PyBoard seems to occasionally go out to lunch, drop off the USB console connection and block storage mount point, and require a hard reset. The symptoms are exactly that of some kind of lock race in the PyBoards serial driver allowing a buffer overflow to scribble everywhere in SRAM, at least that is where my suspicion would turn first. On the other hand, the PyBoard serial driver is very old, very well-exercised functionality, so I would expect it to be very robust by now. Anyway, my code is below. Am I missing something obvious? The code should just be a software implementation of a piece of wire....

Code: Select all

from pyb import UART

def run(s=115200):
    uart = UART(1, s)
    while True:
        ch = uart.readchar()
        if ch < 0:
            continue
        uart.writechar(ch)

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

Re: Serial echo hangs? I must be crazy...

Post by pythoncoder » Mon Jul 17, 2017 7:59 am

Well ch < 0 occurs on timeout. What is the default timeout? I'd try

Code: Select all

def run(s=115200):
    uart = UART(1, s)
    while True:
        if uart.any():
            ch = uart.readchar()
            uart.writechar(ch)
Which is not to excuse a crash...
Peter Hinch
Index to my micropython libraries.

User avatar
dbc
Posts: 89
Joined: Fri Aug 28, 2015 11:02 pm
Location: Sunnyvale, CA

Re: Serial echo hangs? I must be crazy...

Post by dbc » Mon Jul 17, 2017 4:19 pm

Yes, that's a cleaner way to code it. I'll make the excuse that I was in a hurry to get quick and dirty diagnostics going. The code on the other side when I was seeing crashes is somewhat suspect, and I've redone that now but only have it working with an FTDI cable with hard-wired loopback. Next step is to replace the jumper wire jammed into the FTDI cable with the PyBoard software loopback.

The PyBoard crash still smells suspicious, though. If I can replicate the crash with my new host-side diagnostic then I'll have more faith in the symptom. Then it would be an interesting test to try both the time-out version and the uart.any() version of the software loopback to see if it makes a difference. Maybe the time-out has a lock race. But until I test with my new host-side driver I'm going to hold off on blaming the PyBoard too much.

User avatar
dbc
Posts: 89
Joined: Fri Aug 28, 2015 11:02 pm
Location: Sunnyvale, CA

Re: Serial echo hangs? I must be crazy...

Post by dbc » Mon Jul 24, 2017 12:19 am

I still get crashes using the uart.any() call instead of waiting for the timeout. Also, I have different device on another port that gave me a crash bad enough to wipe the file system. While debugging my driver the serial port was crap-flooded, and at some point I got a complete crash.

So... my suspicion is that when the read buffer is full there is some kind of race that causes a buffer overflow. I'll have to try to make a test case that does not require an external driver, or maybe just an external FTDI cable or something.

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

Re: Serial echo hangs? I must be crazy...

Post by pythoncoder » Mon Jul 24, 2017 5:38 am

Use two Pyboards, one to flood the other?
Peter Hinch
Index to my micropython libraries.

User avatar
dbc
Posts: 89
Joined: Fri Aug 28, 2015 11:02 pm
Location: Sunnyvale, CA

Re: Serial echo hangs? I must be crazy...

Post by dbc » Mon Jul 24, 2017 3:08 pm

Use two Pyboards, one to flood the other?
Yes, I have a pair I can try that with. Also, I've been able to cause the behaviour with an FTDI serial adapter cable. A pair of PyBoards would probably be a better test from the viewpoint of Micropython maintainers because that guarantees access to a known test rig.

Post Reply