Skip to content
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

Simple serial terminal example to run on Pico #69

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions uart/terminal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Pico serial terminal

A simple example to allow a Pico to act as a serial terminal into another Raspberry Pi computer.
Useful for setting up a Pi when network setup fails for some reason.
Should work for Windows, Linux, or Mac host machines.

1. Add `enable_uart=1` to the Raspberry Pi computer `config.txt` and boot it.
2. Connect pins `pico:{1,2,3}` to `pi:{10,8,6}`
3. Connect to Pico using [Thonny][thonny]
4. From Thonny, run `terminal.py` on Pico, then power up the Pi

[thonny]: https://thonny.org/

You should now have a serial terminal to your Raspberry Pi computer through your Pico.

### Errata / Bugs

* Input is only taken a whole line at a time after the EOL character.
* Line input is ALWAYS echoed back to the terminal as a new line, even passwords
* Control characters are dropped so no curses tools (like `raspi-config`) work

For a full-fledged serial interface, look at [picoprobe][probe] referenced in Appendix A of the [Pico Getting Started Guide][get-start].

[get-start]: https://datasheets.raspberrypi.com/pico/getting-started-with-pico.pdf#page=61
[probe]: https://github.com/raspberrypi/picoprobe/
33 changes: 33 additions & 0 deletions uart/terminal/terminal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env micropython
from _thread import start_new_thread
from machine import UART, Pin

UART0 = 0 # uart0 is the FIRST uart
TX=0 # Default Pin number for TX on Pico uart0
RX=1 # Default Pin number for RX on Pico uart0
VS=2 # comment reminder: DONT FORGET to connect the common Ground (VSS)

uart = UART(UART0, 115200, parity=None, bits=8, stop=1, tx=Pin(TX, Pin.OUT), rx=Pin(RX, Pin.IN))

# Type a line (plus enter) in REPL to transmit down UART
def TX():
while True:
line = input() + "\n"
uart.write(line.encode())

# Busy thread to relay EVERY character arriving from uart
def RX():
while True:
recv = uart.read()
if(recv):
try:
print(recv.decode(), end='')
except UnicodeError:
# Caught a control char in buffer, eject it and move along
fix = [x for x in recv if x <= 127]
print(bytes(fix).decode(), end='')

# Run busy thread on second processor
start_new_thread(RX, tuple([]))
# Run input wait on this (BSP) processor
TX()