Try Embedded

Pico: a USB serial device, from enumeration to bulk packets

The Pico's Serial is a USB CDC device. Watch it enumerate, read its own device descriptor and the host's line coding, and time a burst of bulk packets from a…

Communication · Intermediate

In a real product: USB debug and configuration port (Consumer electronics): Most devices with a USB port show up as a virtual COM port for configuration, logging and firmware updates.

A Pico has no USB-to-serial chip. Its Serial is a USB device of its own, the RP2040's USB controller running TinyUSB as a CDC-ACM (virtual serial port) device. The computer sees a COM port or /dev/ttyACM0, but on the wire it is all USB: descriptors, endpoints and 64-byte packets.

This sketch waits until the host has enumerated it, then prints what the host learned and what the host told it. Then it runs a small console on the Serial Monitor. Type help.

What you'll learn

  • Enumeration: the host reads the device descriptor, gives the device an address and selects a configuration
  • What is in a device descriptor: the vendor and product ids, endpoint 0's packet size and the device class
  • The CDC line coding and the DTR line: what a terminal sends when it "opens the port"
  • Why USB serial ignores the baud rate, and what limits how fast it really is

How it works

When the Pico connects, the host resets it and reads the first bytes of its device descriptor at address 0, to learn endpoint 0's packet size. Then it sends SET_ADDRESS, reads the full descriptors and sends SET_CONFIGURATION. tud_mounted() becomes true at that point: the device is configured and its data endpoints are live.

A CDC device has three more endpoints besides endpoint 0. One interrupt IN endpoint tells the host about line state changes, and one bulk OUT and one bulk IN endpoint carry the data each way. When a terminal opens the port, it sends SET_LINE_CODING (baud rate, data bits, parity, stop bits) and SET_CONTROL_LINE_STATE with DTR set. while (!Serial) in a Pico sketch waits for exactly that DTR.

The baud rate is just a number the host stores in the device. Nothing is clocked at it. The data goes in bulk packets of up to 64 bytes, as fast as the host polls for them (full speed USB, 12 Mbit/s). The burst command times sending many bytes, and desc shows the device descriptor the host read.

The simulator plays the host: it enumerates the Pico, opens the port with DTR set and moves the packets between the Serial Monitor and the bulk endpoints.

Try this

  • Type burst 64, then burst 6400. How long does each byte take, and why is it nothing like 115 200 baud?
  • Remove while (!Serial) and print something at once in setup(). On a real Pico that text is often lost.
  • Change Serial.begin(115200) to Serial.begin(9600). Does burst get slower?
  • Look up the vendor id 2E8A: who does it belong to?

sketch.ino

// Raspberry Pi Pico: Serial is a USB CDC device (TinyUSB). Waits for the host to enumerate it and open the port, prints
// what was exchanged, then runs a small console: help, desc, line, burst <n>.
#include <tusb.h>

uint32_t mountedAt, openedAt;

void printLine() {
  cdc_line_coding_t lc;
  tud_cdc_get_line_coding(&lc);
  Serial.printf("line coding from the host: %lu baud, %u data bits, parity %u, stop %u; DTR %d, RTS %d\n",
                (unsigned long)lc.bit_rate, lc.data_bits, lc.parity, lc.stop_bits, Serial.dtr(), Serial.rts());
}

void printDescriptor() {
  const tusb_desc_device_t *d = (const tusb_desc_device_t *)tud_descriptor_device_cb();
  Serial.printf("device descriptor: USB %x.%02x, class %02X, VID %04X, PID %04X, endpoint 0 packet %u bytes, %u configuration(s)\n",
                d->bcdUSB >> 8, d->bcdUSB & 0xFF, d->bDeviceClass, d->idVendor, d->idProduct, d->bMaxPacketSize0,
                d->bNumConfigurations);
}

void burst(int n) {
  uint32_t start = micros();
  for (int i = 0; i < n; i++) Serial.write('a' + i % 26);
  Serial.flush();                                   // until the last packet has gone to the host
  uint32_t us = micros() - start;
  Serial.printf("\n%d bytes in %lu us: %lu bytes/s, about %d packets of 64 bytes\n", n, (unsigned long)us,
                us ? (unsigned long)(n * 1000000ULL / us) : 0, (n + 63) / 64);
}

void setup() {
  Serial.begin(115200);
  while (!tud_mounted()) delay(1);                  // enumerated and configured by the host
  mountedAt = millis();
  while (!Serial) delay(1);                         // a terminal opened the port: DTR set
  openedAt = millis();
  Serial.printf("USB: configured after %lu ms, port opened after %lu ms\n", (unsigned long)mountedAt, (unsigned long)openedAt);
  printDescriptor();
  printLine();
  Serial.println("type help");
}

void loop() {
  static String line;
  while (Serial.available()) {
    char c = Serial.read();
    if (c == '\r') continue;
    if (c != '\n') { line += c; continue; }
    line.trim();
    if (line == "help") Serial.println("commands: desc, line, burst <bytes>");
    else if (line == "desc") printDescriptor();
    else if (line == "line") printLine();
    else if (line.startsWith("burst ")) burst(constrain(line.substring(6).toInt(), 1, 20000));
    else if (line.length()) Serial.printf("unknown command: %s\n", line.c_str());
    line = "";
  }
}

Lessons behind it