Skip to content

arthurfprecht/RP2040-Zero-USB-UART-Bridge

 
 

Repository files navigation

1 Multiple USB UART Bridge

This is multiple USB UART Bridge with Raspberry Pi Pico Zero, based on zbn10's implementation. Original credits to him.

I know there are some similar implementations, but this has useful additional functions for dev environment.

  • 6 USB-UART Bridges
  • Pico USB Console other than the Bridges is also available for some functions
    • Capturing USB-UART Bridged data
    • Counts Tx/Rx Bytes of each USB-UART Bridge
    • Configurable baud rates for each individual UART

Before

After

2.How To Use

2.1 Write UF2 to Pico Zero and connect to your PC

You can use complied UF2 file in convenient.
If you will build UF2 yourself see #4.Build Requirement

2.2 Find COM port numbers if you use windows

This is troublesome work but needed. You can find 7 new USB COM ports on Device Manager.

  • Find COM ports witch 'Device Instance Path' of property is
    USB¥VID_2E8A&PID_000A&MI_00¥...

  • Note the linked COM port numbers
    USB¥VID_2E8A&PID_000A&MI_00¥...
    MI_00 = Pico Control console
    MI_02 = USB-UART Bridge 0
    MI_04 = USB-UART Bridge 1
    MI_06 = USB-UART Bridge 2
    MI_08 = USB-UART Bridge 3
    MI_0a = USB-UART Bridge 4
    MI_0c = USB-UART Bridge 5

I changed COM port FriendlyName in the Registry at my own risk to escape from pain.

2.3 Connect devices to Bridge UARTs

Bridge GPIO Pin (Tx/Rx)
USB-UART Bridge 0 0/1
USB-UART Bridge 1 4/5
USB-UART Bridge 2 7/8
USB-UART Bridge 3 14/15
USB-UART Bridge 4 26/27
USB-UART Bridge 5 28/29

3 Example

The environment

graph LR;
    1(PC)<-.->|COM9/Bridge0 USB|2(USB-UART-Bridge/Pico);
    1<-.->|COM7/Pico Control Console|2;
    2<-.->|Bridge0 UART|3(DeviceA);
Loading

3.1 Get help about the control console

Type 'help' at Pico control terminal, in my case it was COM7. In this and in the further examples, the line endings should be line feed/new line or line feed/carriage return + new line. This is a Sloeber Serial Monitor example.

>>Send to COM7: "help"<<
Usage: 
  show             	: show current parameters
  cap num          	: enable capture for bridge<n>
  chgbaud num newbaud	: change baud rate for bridge<n>
  uncap num        	: disable capture for bridge<num>
  capmode num mode 	: set capture mode for bridge<num> to <mode>
                   	: mode: 1=TXT, 2=HEX, 3=TXT&HEX
  capdelim num chr 	: set TXT capture delimiter for bridge<num>
                   	: chr: decimal number from 0 to 255 or
                   	:      \0,\b,\t,\n(default),\v,\f,\r, or
                   	:      signle character
  clear {num | all}	: clear bytes count
  help             	: print this help

3.2 Show Serial port status (baud rates, data transferred)

Type 'show' at Pico Control Console.

>>Send to COM7: "show"<<
Show:
  bridge        |  baudrt | capture | capmode | capdelim  |  usb->uart |  uart->usb
  B0 (GP0/1)    |  115200 | off     |     HEX | \n  ( 10) |         70 |          0
  B1 (GP4/5)    |  115200 | off     |     HEX | \n  ( 10) |          0 |         17
  B2 (GP8/9)    |  115200 | off     |     HEX | \n  ( 10) |          0 |          0
  B3 (GP12/13)  |  115200 | off     |     HEX | \n  ( 10) |          0 |          0
  B4 (GP16/17)  |  115200 | off     |     HEX | \n  ( 10) |          0 |          0
  B5 (GP20/21)  |  115200 | off     |     HEX | \n  ( 10) |          0 |          0

3.3 Change Serial port baud rate

Type 'chgbaud bridge_number new_baud_rate' at Pico Control Console. The 'show' before and after is simply to illustrate the baud rate change.

>>Send to COM7: "show"<<
Show:
  bridge        |  baudrt | capture | capmode | capdelim  |  usb->uart |  uart->usb
  B0 (GP0/1)    |  115200 | off     |     HEX | \n  ( 10) |          0 |          0
  B1 (GP4/5)    |  115200 | off     |     HEX | \n  ( 10) |          0 |          0
  B2 (GP7/8)    |  115200 | off     |     HEX | \n  ( 10) |          0 |          0
  B3 (GP14/15)  |  115200 | off     |     HEX | \n  ( 10) |          0 |          0
  B4 (GP26/27)  |  115200 | off     |     HEX | \n  ( 10) |          0 |          0
  B5 (GP28/29)  |  115200 | off     |     HEX | \n  ( 10) |          0 |          0

>>Send to COM7: "chgbaud 0 9600"<<
Changed B0 to 9600 baud rate

>>Send to COM7: "show"<<
Show:
  bridge        |  baudrt | capture | capmode | capdelim  |  usb->uart |  uart->usb
  B0 (GP0/1)    |    9600 | off     |     HEX | \n  ( 10) |          0 |          0
  B1 (GP4/5)    |  115200 | off     |     HEX | \n  ( 10) |          0 |          1
  B2 (GP7/8)    |  115200 | off     |     HEX | \n  ( 10) |          0 |          0
  B3 (GP14/15)  |  115200 | off     |     HEX | \n  ( 10) |          0 |          0
  B4 (GP26/27)  |  115200 | off     |     HEX | \n  ( 10) |          0 |          0
  B5 (GP28/29)  |  115200 | off     |     HEX | \n  ( 10) |          0 |          0

3.4 Setup to capture data stream

Type 'cap 0', 'capmode 0 1' and 'show'.

---- Sent message: "cap 0\n" ----
Capture:
  enable for B0 (GP0/1)
---- Sent message: "capmode 0 1\n" ----
Cpature mode:
  B0 (GP0/1) capture mode to 1
---- Sent message: "show\n" ----
Show:
  bridge        |  baudrt | capture | capmode | capdelim  |  usb->uart |  uart->usb
  B0 (GP0/1)    |  115200 | off     | TXT     | \n  ( 10) |        474 |      85463
  B1 (GP4/5)    |  115200 | off     |     HEX | \n  ( 10) |          0 |       8261
  B2 (GP8/9)    |  115200 | off     |     HEX | \n  ( 10) |          0 |          0
  B3 (GP12/13)  |  115200 | off     |     HEX | \n  ( 10) |          0 |          0
  B4 (GP16/17)  |  115200 | off     |     HEX | \n  ( 10) |          0 |          0
  B5 (GP20/21)  |  115200 | off     |     HEX | \n  ( 10) |          0 |          0

3.5 Capture 'Hello World !'

Run sample MicroPython Program at the DeviceA::

import time
from machine import UART,Pin

u = UART(0, 115200)
i = 0
while True:
    u.write(str(i))
    i += 1
    u.write(' Hello World !\n')

    time.sleep(1)

Then the Bridge captures them at COM7:

B0 (GP0/1) (uart->usb txt):0 Hello World !\n
B0 (GP0/1) (uart->usb txt):1 Hello World !\n
B0 (GP0/1) (uart->usb txt):2 Hello World !\n
...

COM9 terminal shows:

0 hello World !
1 hello World !
2 hello World !
...

4.Build Requirement

  • Required software

    • current UF2 is built with Sloeber 5.0.0 and RP2040 package 4.6.0. But it should work with Arduino IDE as well.
  • Board Configuration

    • Board is Waveshare RP2040 Zero
    • Use Adafruit TinyUSB as USB stack in the board configuration menu
  • You have to change some package source code

    • '#define CFG_TUD_CDC 1' to '#define CFG_TUD_CDC 7'
      in the tusb_config_rp2040.h
    • 'uint8_t _desc_cfg_buffer[256];' to 'uint8_t _desc_cfg_buffer[1024];'
      in the class Adafruit_USBD_Device in the Adafruit_USBD_Device.h

5.Important Notes

  • Do not forget to use LF (line feed, also called new line) or CRLF (carriage return + line feed/new line) to send commands to Pico Control Console.
  • Capture function is for light debugging purpose. Capturing multiple bridges at the same time can overflows the buffer.

6 Command Details

The commands listed below are a little more complicated and thus explained in detail. The other commands are more self-explicative, and more support can be found on the 'help' on the Pico control console.

6.1 cap / uncap

  • Enable / Disable capture on each bridge.
  • bi-directional data streams are captured simultaneously.

6.2 capmode

  • Captured data is displayed in TEXT, HEX or TEXT&HEX.
  • If you choose TEXT mode and data stream contains bytes which numbers is greater than 127, the text could be unreadable. Control characters less than 128 are translated readable strings.
  • In HEX mode, the data column is divided into 16 byte units and displayed. data less than 16 bytes will be displayed 1 second after being received.
  • In TEXT mode and TEXT&HEX mode, the data is divided by delimiter. If a text column exceeds buffer size, the text column is divided into buffer size units.

6.3 capdelim

  • You can specify the delimiter which is a character byte.
  • There are three ways to specify.
    1. A number from 0 to 255.
    2. A special character \0 \b \t \n \v \f or \r.
    3. A character such as /.
  • If you wanna specify SPACE character, use number 32.

7 ToDo

  • triggered capturing
  • rewriting data stream
  • UART-UART Bridging and capturing by USB
  • RGB LED indication of operation
  • and so on..

About

Multiple USB-UART Bridge with Raspberry Pi Pico Zero

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 100.0%