---
title: "USB: Mass storage"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/subsys/usb/msc/
last_updated: 2026-12-03
sdk_version: 4.44
---
# USB: Mass storage

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

This sample is a USB disk: a RAM disk (`CONFIG_DISK_RAM`) that the sample formats at boot with a FAT file system, labelled `PEBBLEOS`, holding a `README.TXT`, then gives the host as the logical unit of the mass storage function. The host mounts it as any USB drive; what it writes is lost when the board resets. With `CONFIG_SAMPLE_READ_ONLY`, the host may only read it.

Connect the board’s USB device port to a computer; on macOS:

```
diskutil list external
cat /Volumes/PEBBLEOS/README.TXT
dd if=/dev/urandom of=/Volumes/PEBBLEOS/test.bin bs=1k count=128 && sync
diskutil eject PEBBLEOS
```

The integration tests in `tests/integration/usb/test_msc.py` run on this sample (see [Integration tests](https://developer.repebble.com/pebbleos/docs/development/tests/integration.md)).

Run:

`pbl samples samples/subsys/usb/msc`

Boards:

every board

## Sources

src/main.c

```
#include \<stdio.h\>#include \<string.h\>#include \<pbl/drivers/disk/ram.h\>#include \<pbl/fs/fat.h\>#include \<pbl/fs/fs.h\>#include \<pbl/usb/msc.h\>#include \<pbl/usb/usbd.h\>#define README "Hello from PebbleOS!\n"#ifdef CONFIG\_SAMPLE\_READ\_ONLY#define READ\_ONLY true#else#define READ\_ONLY false#endifPBL\_USB\_MSC\_LUN\_DEFINE(ram, &pbl\_disk\_ram, READ\_ONLY);static struct pbl\_fs\_mount s\_mount = {.mnt\_point = "/disk",.ops = &pbl\_fs\_fat\_ops,.fs\_data = (void \*)&pbl\_disk\_ram,};static int prv\_write\_readme(void) {struct pbl\_fs\_file file;ssize\_t written;int ret;ret = pbl\_fs\_open(&file, "/disk/README.TXT", PBL\_FS\_O\_WRITE | PBL\_FS\_O\_CREATE);if (ret \< 0) {return ret;}written = pbl\_fs\_write(&file, README, sizeof(README) - 1U);ret = pbl\_fs\_close(&file);return (written \< 0) ? (int)written : ret;}/\* The host owns the disk once it is given over USB, so the device unmounts it first. \*/static int prv\_format(void) {int ret;ret = pbl\_fs\_fat\_mkfs(&pbl\_disk\_ram, "PEBBLEOS");if (ret != 0) {return ret;}ret = pbl\_fs\_mount(&s\_mount);if (ret != 0) {return ret;}ret = prv\_write\_readme();if (ret != 0) {(void)pbl\_fs\_unmount(&s\_mount);return ret;}return pbl\_fs\_unmount(&s\_mount);}int main(void) {struct pbl\_disk\_info info;int ret;ret = pbl\_disk\_get\_info(&pbl\_disk\_ram, &info);if (ret == 0) {ret = prv\_format();}if (ret != 0) {fprintf(stderr, "msc: formatting the RAM disk failed (%d)\n", ret);return ret;}printf("msc: RAM disk of %u KiB, FAT with README.TXT%s\n",(unsigned int)(info.sector\_count \* info.sector\_size / 1024U),READ\_ONLY ? ", read-only" : "");ret = pbl\_usbd\_enable();if (ret != 0) {fprintf(stderr, "msc: USB failed (%d)\n", ret);return ret;}/\* The device now serves the host from the controller's interrupt and the system work queue. \*/printf("msc: ready\n");return 0;}
```

## Output

```
msc: RAM disk of \d+ KiB, FAT with README.TXT
msc: ready
```

Generated from PebbleOS [d278268fe](https://github.com/coredevices/pebbleos/commit/d278268fec28d152cdda8d97da06154ada5fbe3a). This page is maintained in the pebbleos repository: [docs/samples/subsys/usb/msc.md](https://github.com/coredevices/pebbleos/blob/d278268fec28d152cdda8d97da06154ada5fbe3a/docs/samples/subsys/usb/msc.md).
