Thanks to visit codestin.com
Credit goes to github.com

Skip to content

rp2/machine_uart.c: Change wait in uart.flush() and uart.read(). #13379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 15, 2024

Conversation

robert-hh
Copy link
Contributor

Do not wait in the worst case up to the next ms tick.

Fixes Issue #13377

@dpgeorge
Copy link
Member

dpgeorge commented Jan 8, 2024

Thanks for the patch.

Now I think about it, maybe also the mp_machine_uart_read function needs to be changed in the same way?

UART interrupts are enabled for both RX and TX, but they are FIFO interrupts and so an IRQ won't necessarily fire, eg, if you're waiting for exactly 1 incoming byte.

For writing in mp_machine_uart_write, it might be OK to wait for an IRQ there, because the loop only waits if the FIFO is full, and that will definitely trigger an IRQ once the FIFO is drained.

@dpgeorge
Copy link
Member

dpgeorge commented Jan 8, 2024

Would be good if we can write a test for this failure, eg write some bytes out on the UART and time how long the flush takes.

@robert-hh
Copy link
Contributor Author

Thanks for the comments. I will do both, changing uart.read and write a test case.

@robert-hh robert-hh force-pushed the rp2_flush branch 2 times, most recently from c16ec84 to 609d760 Compare January 8, 2024 09:32
@robert-hh
Copy link
Contributor Author

Updated with a test and change for uart.read().

Copy link

codecov bot commented Jan 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (bd21820) 98.36% compared to head (609d760) 98.36%.

❗ Current head 609d760 differs from pull request most recent head 53d23f1. Consider uploading reports for the commit 53d23f1 to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #13379   +/-   ##
=======================================
  Coverage   98.36%   98.36%           
=======================================
  Files         159      159           
  Lines       21088    21088           
=======================================
  Hits        20743    20743           
  Misses        345      345           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@robert-hh robert-hh changed the title rp2/machine_uart.c: Fix uart.flush(). rp2/machine_uart.c: Change wait in uart.flush() and uart.read(). Jan 10, 2024
@projectgus
Copy link
Contributor

Good catch, thanks @robert-hh. Forgot that you don't always get an interrupt in these cases due to the FIFOs.

Do not wait in the worst case up to the timeout.

Fixes issue micropython#13377.

Signed-off-by: robert-hh <[email protected]>
@dpgeorge
Copy link
Member

I've updated the test so it tests multiple baud rates, and so it's possible to run on other ports/boards.

Currently only runs on rp2 but could be extended to run on other targets.

Signed-off-by: robert-hh <[email protected]>
@dpgeorge dpgeorge merged commit c41b421 into micropython:master Jan 15, 2024
@robert-hh robert-hh deleted the rp2_flush branch January 20, 2024 08:47
@sandyscott
Copy link

sandyscott commented Feb 13, 2024

In case someone else encounters this weird bug - this commit appears to have fixed an issue I had with the Pi Pico UART always reading an extra byte when invert = UART.INVERT_RX was set.

It wasn't waiting for any stop bits - so if the line rose and stayed high, this would be incorrectly interpreted as a null byte (a real null byte should have a stop bit - a negative-going pulse at the appropriate time). This was an issue for me because an RS485 transciever would set the line high when it stopped recieving, so every byte recieved would be followed by an extra null byte.

Anyway moving from v1.21.0 to v1.22.1 has resolved this issue for me, and this PR is the most likely candidate as to why, so thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants