From 3e5179e529415530f7f3b9e73819846cd29613af Mon Sep 17 00:00:00 2001 From: brianddk Date: Sun, 8 Oct 2023 23:22:56 -0500 Subject: [PATCH 1/2] Simple serial terminal example to run on Pico --- uart/terminal/README.md | 20 ++++++++++++++++++++ uart/terminal/terminal.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 uart/terminal/README.md create mode 100644 uart/terminal/terminal.py diff --git a/uart/terminal/README.md b/uart/terminal/README.md new file mode 100644 index 0000000..7f6a18a --- /dev/null +++ b/uart/terminal/README.md @@ -0,0 +1,20 @@ +# Pico serial terminal + +A simple example to allow a Pico to act as a serial terminal into another Raspberry Pi computer. +Usefull 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. + +### Erratta / 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 diff --git a/uart/terminal/terminal.py b/uart/terminal/terminal.py new file mode 100644 index 0000000..ea5fb8d --- /dev/null +++ b/uart/terminal/terminal.py @@ -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() From 08562a2b3e3bf17543a7455d00fc1888636c3c1d Mon Sep 17 00:00:00 2001 From: brianddk Date: Mon, 9 Oct 2023 08:59:22 -0500 Subject: [PATCH 2/2] Update README.md Add reference to picoprobe. --- uart/terminal/README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/uart/terminal/README.md b/uart/terminal/README.md index 7f6a18a..4a48625 100644 --- a/uart/terminal/README.md +++ b/uart/terminal/README.md @@ -1,7 +1,7 @@ # Pico serial terminal A simple example to allow a Pico to act as a serial terminal into another Raspberry Pi computer. -Usefull for setting up a Pi when network setup fails for some reason. +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. @@ -13,8 +13,13 @@ Should work for Windows, Linux, or Mac host machines. You should now have a serial terminal to your Raspberry Pi computer through your Pico. -### Erratta / Bugs +### 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/