---
title: "USB: Loopback with alternate settings"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/subsys/usb/loopback/
last_updated: 2026-11-24
sdk_version: 4.44
---
# USB: Loopback with alternate settings

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

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.coredev=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 3Ustatic 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](https://github.com/coredevices/pebbleos/commit/12b6c531f9e0c2f39fe4e9bb2886314697128f08). This page is maintained in the pebbleos repository: [docs/samples/subsys/usb/loopback.md](https://github.com/coredevices/pebbleos/blob/12b6c531f9e0c2f39fe4e9bb2886314697128f08/docs/samples/subsys/usb/loopback.md).
