pebble
  • Tutorials
  • Get the SDK
  • Guides
  • Documentation
  • PebbleOS
  • Mobile App
  • Examples
  • Index 01
  • Community
  • Blog
  • More
Privacy
Cookies
Publish

PebbleOS

  • Overview
  • Start here
    • Getting started guide
  • Pebble Firmware
    • Overview
    • Playground
    • Application SDK
  • Development
    • The pbl CLI
    • Build system
    • Samples
      • Kernel
      • Subsystems
      • Drivers
      • Third-party
      • Rust
      • Benchmarks
    • Emulator and simulator
    • Testing
    • Debugging
    • Conventions
  • System
    • Architecture overview
    • Kernel
    • Drivers
    • Subsystems
    • Libraries
    • Third-party
  • Hardware
    • Boards
    • Board Add-ons
    • Programmers
  • Community
    • Contribution Guidelines
  • Reference
    • Reference
    • Kconfig options

USB: Serial port echo

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. This page is maintained in the pebbleos repository: docs/samples/subsys/usb/cdc_acm.md.

Overview

  • Sources
  • Output