UART not working with today's version of micropython

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Tinus
Posts: 40
Joined: Sun Feb 14, 2021 4:53 pm

Re: UART not working with today's version of micropython

Post by Tinus » Thu Apr 15, 2021 3:16 pm

I do not know what a logic analyzer is but I do have a usb serial converter so I'll give that a try :)

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

Re: UART not working with today's version of micropython

Post by Roberthh » Thu Apr 15, 2021 3:17 pm

Do you use threading in your code?

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

Re: UART not working with today's version of micropython

Post by Roberthh » Thu Apr 15, 2021 3:23 pm

Simple logic analyzer: https://www.amazon.de/AZDelivery-%E2%AD ... 180&sr=8-5
Good for digital signals up to ~5 MHz. Very helpful.

Tinus
Posts: 40
Joined: Sun Feb 14, 2021 4:53 pm

Re: UART not working with today's version of micropython

Post by Tinus » Thu Apr 15, 2021 3:39 pm

Roberthh wrote:
Thu Apr 15, 2021 3:17 pm
Do you use threading in your code?
Yes I do.

I have one core to handle the sensor interrupts and running a loop to do calculations and one core to handle IO to the Nextion and the ESP32.

Okay. Hooked it up to the usb-serial

this is what I get over the serial port:

Code: Select all

page 0���msg.txt="heartrate=--"���msg.txt=""���msg.txt="init"���msg.txt="Starting wifi"���msg.txt="Connecting to network..."���m
sg.txt="10.0.0.138"���msg.txt="Getting data"���msg.txt="Start TEST in 5"���msg.txt="Start TEST in 4"���msg.txt="Start TEST in 3"
���msg.txt="Start TEST in 2"���msg.txt="Start TEST in 1"���page 2���page1.gTime.val=4���page1.gHeartRate.val=0���page1.gSpeed.va
l=57���page1.gStroke.val=51���page1.gDiissttaannccee..vvaall==77����page1.gSegProoggrreessss..vvaall==00�����page1.gSegTimee..vv
aall==22440000������ppage1.gSegDist.val=0���page1.gSSeeggNNrr..vvaall==11������page1.gSegTotal..vvaall==22������ppaaggee11..ggSS
eeggNNaammee..ttxxtt=="OHR"���page1.gTargetMin.txt="130"���page1.gTargetMax.txt="140"���page1.gTarget.txxtt==""""������ppaaggee1
1..gTargetSlide.vaall==00������ppaaggee11..ggTTaarrggeettCCoolloorr..vvaall==6644552200������page1.gTime.val=5���page1.gHeartRat
e.val=0���page1.gSpeed.val=57���page1.gStroke.val=51���page1.gDistannccee..vvaall==88������ppaage1.gSegProgressss..vvaall==00���
���ppaagge1.gSegTime.vaall==22440000������ppaaggee1.gSegDist.val=0���page1.gSeeggNNrr..vvaall==11������ppage1.gSegTotal..vvaall=
=22������ppaaggee11.gSegName.txt==""OOHHRR""������ppaaggee11.gTargetMin.txxtt==""113300""������ppaagge1.gTargetMax..ttxxtt==""11
4400""������ppage1.gTarget.txxtt==""""������ppaaggee11..gTargetSlide.vaall==00������ppaaggee11..ggTTaarrggeettCCoolloorr..vvaall
==6644552200������page1.gTime.val=5���page1.gHeartRate.val=0���page1.gSpeed.val=57���page1.gStroke.val=50���page1.gDistannccee..
vvaall==88������ppaage1.gSegProgressss..vvaall==00������ppaagge1.gSegTime.vaall==22440000������ppaaggee1.gSegDist.val=0���page1.
gSegNNrr..vvaall==11������ppaagge1.gSegTotal.vvaall==22������ppaaggee11..gSegName.txt=""OOHHRR""������ppaaggee11..gTargetMin.txt
="130"���page1.gTargetMax.txt="140"���page1.gTarget.txt==""""������ppaaggee11..ggTTargetSlide.val==00������ppaaggee11..ggTTaarrg
geettCCoolloorr..vvaall==6644552200������page1.gTime.val=5���page1.gHeartRate.val=0���page1.gSpeed.val=57���page1.gStroke.val=50
���page1.gDistaannccee..vvaall==88������ppage1.gSegProgresss..vvaall==00������ppaaggee1.gSegTime.val=2400���page1.gSeggDDiisstt.
.vvaall==00������ppaaggee11..ggSSeeggNNrr..vvaall==11������page1.gSegTotal.val=2���page1.gSegName.txt="OHR""������ppaaggee11..gg
TTaarrgetMin.txt="13300""������ppaaggee11..ggTTargetMax.txt="140"���page1.ggTTaarrggeett..ttxxtt==""""���page1.gTargeettSSlliidd
ee..vvaall==00������ppaaggee11..ggTTaarrggeettCCoolloorr..vvaall==6644552200������page1.gTime.val=5���page1.gHeartRate.val=0���p
age1.gSpeed.val=57���page1.gStroke.val=49���page1.gDissttaannccee..vvaall==88�����page1.gSegProoggrreessss..vvaall==00�����page1
.gSegTimee..vvaall==22440000������ppage1.gSegDist.val=1���page1..ggSSeeggNNrr..vvaall==11����page1.gSegToottaall..vvaall==22����
��ppage1.gSegName..ttxxtt==""OOHHRR""������ppage1.gTargetMiinn..ttxxtt==""113300""�����page1.gTargetMMaaxx..ttxxtt==""114400""��
��page1.gTargett..ttxxtt==""""������ppaagge1.gTargetSlidee..vvaall==00������ppaaggee11..ggTTaarrggeettCCoolloorr..vvaall==664455
2200������page1.gTime.val=5���page1.gHeartRate.val=0���page1.gSpeed.val=57���page1.gStroke.val=49���page1.gDistaannccee..vvaall=
=99������ppage1.gSegProgreessss..vvaall==00������ppaage1.gSegTime.vaall==22440000������ppaaggee1.gSegDist.val=1���page1.gSeggNNr
r..vvaall==11������ppaage1.gSegTotal.vvaall==22������ppaaggee11..gSegName.txt=""OOHHRR""������ppaaggee11..gTargetMin.txtt==""113
300""������ppaaggee1.gTargetMax.ttxxtt==""114400""������ppaage1.gTarget.txtt==""""������ppaaggee11..ggTargetSlide.val=00������pp
aaggee11..ggTTaarrggeettCCoolloorr..vvaall==6644552200������page1.gTime.val=5���page1.gHeartRate.val=0���page1.gSpeed.val=57���p
age1.gStroke.val=49���page1.gDistance.val=9���page1.gSegPrrooggrreessss..vvaall==00������ppaaggee11..ggSSeeggTTiimmee..vvaall==2
2440000������page1.gSegDist.val=1���page1.gSegNr..vvaall==11������ppaaggee11.gSegTotal.vall==22������ppaaggee11..ggSSegName.txt=
"OHHRR""
So it starts out fine when the commands are sent slowly then when I start sending multiple commands per second it goes back and forth between correct and doubled.
It is basically amazing that it even works some of the time.

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

Re: UART not working with today's version of micropython

Post by Roberthh » Thu Apr 15, 2021 4:41 pm

OK. Sending frequent was the trigger. If I do that, I see errors too. Not doubling, but gaps. These should not occur either.

Can you try this firmware version: https://github.com/robert-hh/Shared-Stu ... rmware.uf2

Tinus
Posts: 40
Joined: Sun Feb 14, 2021 4:53 pm

Re: UART not working with today's version of micropython

Post by Tinus » Thu Apr 15, 2021 5:06 pm

running MicroPython v1.14-170-ga9bbf7083-dirty on 2021-04-15; Raspberry Pi Pico with RP2040

No improvement:

{"sessionid":"0_1_1618506200","startT":25227,"endT":35421,"staarrttDD""::2233111144,,""eeddDD""::3399666644,,""aavvggRR""::00}}

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

Re: UART not working with today's version of micropython

Post by Roberthh » Thu Apr 15, 2021 6:12 pm

Not good. The erorr I had seen disappeared with that change. But it also disappeared with setting timeout.
About your set-up and threads:
- in which thread is the UART communication placed?
- can you try to run the UART test with only thread0 active?


Edit: I could replicate the bug when the UART send process is in thread 1. That's a starting point.
Edit 2: But it's very rare. not at frequent as in your printout.

Tinus
Posts: 40
Joined: Sun Feb 14, 2021 4:53 pm

Re: UART not working with today's version of micropython

Post by Tinus » Thu Apr 15, 2021 6:22 pm

Yes, my uarts are both running in thread_1

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

Re: UART not working with today's version of micropython

Post by Roberthh » Thu Apr 15, 2021 8:18 pm

When enabling threading I see all kinds of errors, very rarely character doubling, more often just lock-up of the sending thread, or sending completely wrong data. So there seems to be a general issue or challenge with threads and interrupt. Maybe a stack overrun.
It does not matter in which thread the sending or maybe more specific the interrupt using task is running.

Tinus
Posts: 40
Joined: Sun Feb 14, 2021 4:53 pm

Re: UART not working with today's version of micropython

Post by Tinus » Thu Apr 15, 2021 8:29 pm

I was trying out the nightly build because I was having a problem with the sensor interrupt handler dying after about 40 minutes on the build I was using. So no luck there :)

Very odd that you do get errors but hardly ever the one I am seeing.
Thank you very much for taking the time to look at this.

Post Reply