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: Mass storage

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).

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
#endif

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

Overview

  • Sources
  • Output