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: Loopback with alternate settings

This sample defines a USB function of its own, a vendor-specific interface with three alternate settings: setting 0 has no endpoints, setting 1 loops data back on a pair of bulk endpoints, and setting 2 on the same endpoint addresses as interrupt ones. Each setting names itself with a string of the function, which the stack numbers for the device. When the host selects a setting, the stack switches the endpoints and calls the function’s set_alt(), which starts reading; what arrives is sent back.

Connect the board’s USB device port to a computer, and select a setting and exchange data with a USB library, as pyusb:

import usb.core

dev = usb.core.find(idVendor=0x1209, idProduct=0x0001)
dev.set_interface_altsetting(interface=0, alternate_setting=1)
dev.write(0x01, b"hello")
print(bytes(dev.read(0x82, 64)))
Run:

pbl samples samples/subsys/usb/loopback

Boards:

every board

Sources

src/main.c
#include <errno.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>

#include <pbl/drivers/udc.h>
#include <pbl/kernel/msgq.h>
#include <pbl/kernel/time.h>
#include <pbl/usb/usbd.h>
#include <pbl/util/size.h>

#define RUN_S 60

#define IFACE 0U

#define EP_OUT 0x01U
#define EP_IN  (PBL_UDC_EP_IN | 2U)

#define ALT_OFF  0U
#define ALT_BULK 1U
#define ALT_INT  2U

#define INT_MPS 16U

#define VENDOR_CLASS 0xffU

#define STRING_OFF  1U
#define STRING_BULK 2U
#define STRING_INT  3U

static const uint8_t s_desc[] = {
  /* Setting 0, without endpoints */
  9,
  PBL_USB_DESC_INTERFACE,
  IFACE,
  ALT_OFF,
  0,
  VENDOR_CLASS,
  0,
  0,
  STRING_OFF,
  /* Setting 1, looping back on bulk endpoints */
  9,
  PBL_USB_DESC_INTERFACE,
  IFACE,
  ALT_BULK,
  2,
  VENDOR_CLASS,
  0,
  0,
  STRING_BULK,
  7,
  PBL_USB_DESC_ENDPOINT,
  EP_OUT,
  PBL_UDC_EP_BULK,
  64,
  0,
  0,
  7,
  PBL_USB_DESC_ENDPOINT,
  EP_IN,
  PBL_UDC_EP_BULK,
  64,
  0,
  0,
  /* Setting 2, on the same endpoints as interrupt ones */
  9,
  PBL_USB_DESC_INTERFACE,
  IFACE,
  ALT_INT,
  2,
  VENDOR_CLASS,
  0,
  0,
  STRING_INT,
  7,
  PBL_USB_DESC_ENDPOINT,
  EP_OUT,
  PBL_UDC_EP_INTERRUPT,
  INT_MPS,
  0,
  1,
  7,
  PBL_USB_DESC_ENDPOINT,
  EP_IN,
  PBL_UDC_EP_INTERRUPT,
  INT_MPS,
  0,
  1,
};

static const char *const s_strings[] = {
  "Loopback off",
  "Loopback bulk",
  "Loopback interrupt",
};

/* The stack's callbacks run in the controller's interrupt: they queue, the main thread prints. */
static PBL_MSGQ_DEFINE(s_alts, sizeof(uint8_t), 8);

static uint8_t s_alt;
static uint8_t s_buf[512];
static atomic_uint s_looped;

static uint16_t prv_mps(void) {
  return s_alt == ALT_BULK ? pbl_usbd_bulk_mps() : INT_MPS;
}

static void prv_rx_start(void) {
  (void)pbl_udc_ep_queue(UDC, EP_OUT, s_buf, prv_mps());
}

static void prv_set_alt(uint8_t iface, uint8_t alt) {
  s_alt = alt;
  if (alt != ALT_OFF) {
    prv_rx_start();
  }
  (void)pbl_msgq_put(&s_alts, &alt, PBL_NO_WAIT);
}

static void prv_configured(bool on) {
  s_alt = ALT_OFF;
}

/* What arrives goes back, then the next packet is read. */
static void prv_xfer_done(uint8_t ep, int ret) {
  if (ret < 0) {
    return;
  }

  if (ep == EP_OUT) {
    atomic_fetch_add(&s_looped, (unsigned int)ret);
    (void)pbl_udc_ep_queue(UDC, EP_IN, s_buf, (size_t)ret);
  } else {
    prv_rx_start();
  }
}

PBL_USBD_FUNCTION_DEFINE(loopback) = {
  .desc = s_desc,
  .desc_len = sizeof(s_desc),
  .strings = s_strings,
  .num_strings = PBL_ARRAY_SIZE(s_strings),
  .first_iface = IFACE,
  .num_ifaces = 1U,
  .configured = prv_configured,
  .set_alt = prv_set_alt,
  .xfer_done = prv_xfer_done,
};

int main(void) {
  uint64_t end = pbl_uptime_ms() + RUN_S * 1000U;
  uint8_t alt;
  int ret;

  ret = pbl_usbd_enable();
  if (ret != 0) {
    fprintf(stderr, "usb: device failed (%d)\n", ret);
    return ret;
  }

  printf("usb: loopback up for %d s\n", RUN_S);

  while (pbl_uptime_ms() < end) {
    if (pbl_msgq_get(&s_alts, &alt, PBL_MSEC(100)) == 0) {
      printf("usb: alternate setting %u, %s\n", alt, s_strings[alt]);
    }
  }

  printf("usb: looped %u bytes\n", atomic_load(&s_looped));
  printf("Sample done\n");

  return 0;
}

Output

usb: loopback up

Generated from PebbleOS 12b6c531f. This page is maintained in the pebbleos repository: docs/samples/subsys/usb/loopback.md.

Overview

  • Sources
  • Output