---
title: "USB: Serial port echo"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/subsys/usb/cdc_acm/
last_updated: 2026-11-24
sdk_version: 4.44
---
# USB: Serial port echo

Source: https://developer.repebble.com/pebbleos/docs/samples/subsys/usb/cdc_acm/

This sample is a serial port over USB that sends back what the host sends, for a minute. The receive callback of the port runs in the controller’s interrupt, so it only queues the bytes, and the main thread echoes them.

Connect the board’s USB device port to a computer, which then has a new serial port, as `/dev/cu.usbmodem*` on macOS or `/dev/ttyACM*` on Linux, and type into it. The port has no flow control: bursts larger than the sample’s queue lose bytes.

Run:

`pbl samples samples/subsys/usb/cdc_acm`

Boards:

every board

## Sources

src/main.c

```
#include \<errno.h\>#include \<stdint.h\>#include \<stdio.h\>#include \<pbl/kernel/msgq.h\>#include \<pbl/kernel/time.h\>#include \<pbl/usb/cdc\_acm.h\>#define ECHO\_S 60#define QUEUE\_LEN 2048/\* The receive callback runs in the controller's interrupt: it queues, the main thread echoes. \*/static PBL\_MSGQ\_DEFINE(s\_rx, sizeof(uint8\_t), QUEUE\_LEN);static void prv\_rx(const struct pbl\_uart \*uart, uint8\_t c, void \*arg) {(void)pbl\_msgq\_put(&s\_rx, &c, PBL\_NO\_WAIT);}int main(void) {uint64\_t end = pbl\_uptime\_ms() + ECHO\_S \* 1000U;unsigned int count = 0U;uint8\_t c;int ret;/\* The device connects to the bus as the port comes up, with the other devices. \*/ret = pbl\_uart\_rx\_enable(USB\_CDC\_ACM\_UART, prv\_rx, nullptr);if (ret != 0) {fprintf(stderr, "usb: serial port failed (%d)\n", ret);return ret;}printf("usb: serial port up, echoing for %d s\n", ECHO\_S);while (pbl\_uptime\_ms() \< end) {if (pbl\_msgq\_get(&s\_rx, &c, PBL\_MSEC(100)) != 0) {continue;}pbl\_uart\_poll\_out(USB\_CDC\_ACM\_UART, c);count++;}printf("usb: echoed %u bytes\n", count);printf("Sample done\n");return 0;}
```

## Output

```
usb: serial port up
```

Generated from PebbleOS [12b6c531f](https://github.com/coredevices/pebbleos/commit/12b6c531f9e0c2f39fe4e9bb2886314697128f08). This page is maintained in the pebbleos repository: [docs/samples/subsys/usb/cdc\_acm.md](https://github.com/coredevices/pebbleos/blob/12b6c531f9e0c2f39fe4e9bb2886314697128f08/docs/samples/subsys/usb/cdc_acm.md).
