• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..--

boards/29-Dec-2025-14099

src/29-Dec-2025-215162

CMakeLists.txtD29-Dec-2025431 1612

KconfigD29-Dec-20251.7 KiB7856

README.rstD29-Dec-202510.7 KiB299230

flashdisk.overlayD29-Dec-2025236 1513

prj.confD29-Dec-2025379 1713

ramdisk.overlayD29-Dec-2025222 1513

sample.yamlD29-Dec-20252.8 KiB117116

README.rst

1.. zephyr:code-sample:: usb-mass
2   :name: USB Mass Storage
3   :relevant-api: usbd_api usbd_msc_device file_system_api
4
5   Expose board's RAM or FLASH as a USB disk using USB Mass Storage driver.
6
7Overview
8********
9
10This sample app demonstrates use of a USB Mass Storage driver by the Zephyr
11project. This very simple driver enumerates a board with either RAM or FLASH
12into an USB disk.
13
14Requirements
15************
16
17This project requires a USB device driver, and either 96KiB of RAM or a FLASH device.
18
19Building and Running
20********************
21
22This sample can be built for multiple boards, customized through overlays found
23in :zephyr_file:`samples/subsys/usb/mass/boards` in the Zephyr project tree.
24The selection between a RAM-based or a FLASH-based disk and file system
25can be chosen passing Kconfig configuration via the -D command-line switch.
26
27RAM-disk Example without any file system
28========================================
29
30The default configurations selects RAM-based disk without any file system.
31This example only needs additional 96KiB RAM for the RAM-disk and is intended
32for testing USB mass storage class implementation.
33
34.. zephyr-app-commands::
35   :zephyr-app: samples/subsys/usb/mass
36   :board: reel_board
37   :gen-args: -DEXTRA_DTC_OVERLAY_FILE="ramdisk.overlay"
38   :goals: build
39   :compact:
40
41
42FAT FS Example
43==============
44
45If more than 96KiB RAM are available, FAT files system can be used with a RAM-disk.
46
47.. zephyr-app-commands::
48   :zephyr-app: samples/subsys/usb/mass
49   :board: reel_board
50   :gen-args: -DEXTRA_DTC_OVERLAY_FILE="ramdisk.overlay" -DCONFIG_APP_MSC_STORAGE_RAM=y
51   :goals: build
52   :compact:
53
54Alternatively, FAT file system can be used on the SoC internal flash when the
55storage partition is large enough.
56
57.. zephyr-app-commands::
58   :zephyr-app: samples/subsys/usb/mass
59   :board: frdm_k64f
60   :gen-args: -DEXTRA_DTC_OVERLAY_FILE="flashdisk.overlay" -DCONFIG_APP_MSC_STORAGE_FLASH_FATFS=y -DCONFIG_DISK_DRIVER_SDMMC=n
61   :goals: build
62   :compact:
63
64The internal flash storage partition on the SoC may be too small for the FAT
65file system. If the board has an external flash device, however, it can be used
66for the FAT file system. The sample includes a few board overlay files for
67configuring an external flash device. For example, there is an overlay file for
68Adafruit Feather nRF52840 Express that allows you to use an external 16 MiBy
69QSPI flash chip with a 2 MiBy FAT partition.
70
71.. zephyr-app-commands::
72   :zephyr-app: samples/subsys/usb/mass
73   :board: adafruit_feather_nrf52840_sense
74   :gen-args: -DCONFIG_APP_MSC_STORAGE_FLASH_FATFS=y
75   :goals: build
76   :compact:
77
78After you have built and flashed the sample app image to your board, plug the
79board into a host device, for example, a PC running Linux.
80The board will be detected as shown by the Linux journalctl command:
81
82.. code-block:: console
83
84    $ journalctl -k -n 17
85    usb 2-2.4: new full-speed USB device number 29 using xhci_hcd
86    usb 2-2.4: New USB device found, idVendor=2fe3, idProduct=0008, bcdDevice= 2.03
87    usb 2-2.4: New USB device strings: Mfr=1, Product=2, SerialNumber=3
88    usb 2-2.4: Product: Zephyr MSC sample
89    usb 2-2.4: Manufacturer: ZEPHYR
90    usb 2-2.4: SerialNumber: 86FE679A598AC47A
91    usb-storage 2-2.4:1.0: USB Mass Storage device detected
92    scsi host3: usb-storage 2-2.4:1.0
93    scsi 3:0:0:0: Direct-Access     ZEPHYR   ZEPHYR USB DISK  0.01 PQ: 0 ANSI: 0 CCS
94    sd 3:0:0:0: Attached scsi generic sg4 type 0
95    sd 3:0:0:0: [sdb] 256 512-byte logical blocks: (131 kB/128 KiB)
96    sd 3:0:0:0: [sdb] Write Protect is off
97    sd 3:0:0:0: [sdb] Mode Sense: 03 00 00 00
98    sd 3:0:0:0: [sdb] No Caching mode page found
99    sd 3:0:0:0: [sdb] Assuming drive cache: write through
100     sdb:
101    sd 3:0:0:0: [sdb] Attached SCSI removable disk
102
103The output to the console will look something like this
104(file system contents will be different):
105
106.. code-block:: none
107
108    *** Booting Zephyr OS build zephyr-v2.3.0-1991-g4c8d1496eafb  ***
109    Area 4 at 0x0 on GD25Q16 for 2097152 bytes
110    Mount /NAND:: 0
111    /NAND:: bsize = 512 ; frsize = 1024 ; blocks = 2028 ; bfree = 1901
112    /NAND: opendir: 0
113      F 0 SAMPLE.TXT
114    End of files
115    [00:00:00.077,423] <inf> main: The device is put in USB mass storage mode.
116
117On most operating systems the drive will be automatically mounted.
118
119SD Card Example
120===============
121
122This example requires SD card support, see :ref:`disk_access_api`, and
123a SD card formatted with FAT filesystem.
124
125If a board with SD card controller is available, the example can be built as
126follows:
127
128.. zephyr-app-commands::
129   :zephyr-app: samples/subsys/usb/mass
130   :board: mimxrt1050_evk
131   :gen-args: -DCONFIG_APP_MSC_STORAGE_SDCARD=y
132   :goals: build
133   :compact:
134
135In case the board has no support for SD card controller, but the card can
136be connected to SPI using e.g. a shield, example can be built as follows:
137
138.. zephyr-app-commands::
139   :zephyr-app: samples/subsys/usb/mass
140   :board: nrf52840dk/nrf52840
141   :shield: waveshare_epaper_gdeh0154a07
142   :gen-args: -DCONFIG_APP_MSC_STORAGE_SDCARD=y
143   :goals: build
144   :compact:
145
146Depending on the size of the media it can take time until the file system has
147initialized the card and it is available via USB. It should also be noted that
148the transfer speed over SPI is very slow.
149
150.. code-block:: none
151
152   *** Booting Zephyr OS build v2.5.0-rc3-73-gd85067f0a759  ***
153   Mount /SD:: 0
154   [00:00:00.281,585] <inf> sdhc_spi: Found a ~3751 MiB SDHC card.
155   [00:00:00.282,867] <inf> sdhc_spi: Manufacturer ID=27 OEM='SM' Name='00000' Revision=0x10 Serial=0x16fdd47b
156   [00:00:00.308,654] <inf> sdhc_spi: Found a ~3751 MiB SDHC card.
157   [00:00:00.309,906] <inf> sdhc_spi: Manufacturer ID=27 OEM='SM' Name='00000' Revision=0x10 Serial=0x16fdd47b
158   /SD:: bsize = 512 ; frsize = 32768 ; blocks = 119776 ; bfree = 119773
159   /SD: opendir: 0
160     D 0 42
161     F 1111 TEST.TXT
162   End of files
163   [00:00:18.588,043] <inf> main: The device is put in USB mass storage mode.
164
165LittleFS Example
166================
167
168The sample can be built for any board that has a storage partition defined.
169
170.. zephyr-app-commands::
171   :zephyr-app: samples/subsys/usb/mass
172   :board: reel_board
173   :gen-args: -DEXTRA_DTC_OVERLAY_FILE="flashdisk.overlay" -DCONFIG_APP_MSC_STORAGE_FLASH_LITTLEFS=y
174   :goals: build
175   :compact:
176
177If more storage space is needed, the board can be configured to use an external
178flash device. This can be done using the same approach as in the FAT file
179system example. In this example, the nRF52840DK board is configured to use an
180external 64 MiB QSPI flash chip with a 128 KiBy `littlefs`_ partition
181compatible with the one produced by the :zephyr:code-sample:`littlefs` sample.
182
183.. zephyr-app-commands::
184   :zephyr-app: samples/subsys/usb/mass
185   :board: nrf52840dk/nrf52840
186   :gen-args: -DCONFIG_APP_MSC_STORAGE_FLASH_LITTLEFS=y
187   :goals: build
188   :compact:
189
190After you have built and flashed the sample app image to your board,
191connect the board's two USB connectors (debug and nRF USB) to a host
192running a littlefs-FUSE-capable operating system.  The output to the
193console will look something like this (file system contents will be
194different):
195
196.. code-block:: none
197
198    *** Booting Zephyr OS build zephyr-v2.2.0-1966-g7815942d5fc5  ***
199    Area 4 at 0x0 on MX25R64 for 65536 bytes
200    [00:00:00.005,310] <inf> main: The device is put in USB mass storage mode.
201
202    [00:00:00.009,002] <inf> littlefs: LittleFS version 2.2, disk version 2.0
203    [00:00:00.009,063] <inf> littlefs: FS at MX25R64:0x0 is 16 0x1000-byte blocks with 512 cye
204    [00:00:00.009,063] <inf> littlefs: sizes: rd 16 ; pr 16 ; ca 64 ; la 32
205    [00:00:00.011,718] <inf> littlefs: /lfs mounted
206    Mount /lfs: 0
207    /lfs: bsize = 16 ; frsize = 4096 ; blocks = 16 ; bfree = 13
208    /lfs opendir: 0
209      F 8 hi
210      F 128 linux
211      F 5 newfile
212    End of files
213
214For information on mounting littlefs file system on Linux or FreeBSD
215systems refer to the "littlefs Usage" section below.
216
217littlefs Usage
218==============
219
220While a FAT-based file system can be mounted by many systems automatically,
221mounting the littlefs file system on a Linux or FreeBSD system can be
222accomplished using the `littlefs-FUSE`_ utility.
223
224First determine the local device name from the system log, e.g.:
225
226.. code-block:: none
227
228    Apr 25 08:10:25 tirzah kernel: [570310.921039] scsi 17:0:0:0: Direct-Access     ZEPHYR   ZEPHYR USB DISK  0.01 PQ: 0 ANSI: 0 CCS
229    Apr 25 08:10:25 tirzah kernel: [570310.921550] sd 17:0:0:0: Attached scsi generic sg4 type 0
230    Apr 25 08:10:25 tirzah kernel: [570310.922277] sd 17:0:0:0: [sdd] 256 512-byte logical blocks: (131 kB/128 KiB)
231    Apr 25 08:10:25 tirzah kernel: [570310.922696] sd 17:0:0:0: [sdd] Write Protect is off
232
233This shows that the block device associated with the USB drive is
234``/dev/sdd``:
235
236.. code-block:: shell
237
238    tirzah[447]$ ll /dev/sdd
239    brw-rw---- 1 root disk 8, 48 Apr 25 08:10 /dev/sdd
240
241This can be mounted as a file system with the following commands:
242
243.. code-block:: shell
244
245   sudo chmod a+rw /dev/sdd   # required to allow user access
246   mkdir /tmp/lfs
247   lfs \
248          --read_size=16 \
249          --prog_size=16 \
250          --block_size=4096 \
251          --block_count=32 \
252          --cache_size=64 \
253          --lookahead_size=32 \
254          /dev/sdd /tmp/lfs
255
256which produces output like this (disk contents will vary):
257
258.. code-block:: none
259
260    tirzah[467]$ ls -l /tmp/lfs
261    total 0
262    -rwxrwxrwx 0 root root   8 Dec 31  1969 hi
263    -rwxrwxrwx 0 root root 128 Dec 31  1969 linux
264    -rwxrwxrwx 0 root root   5 Dec 31  1969 newfile
265
266``lfs`` is a mount command and you should take care to unmount the
267device before removing the USB drive:
268
269.. code-block:: shell
270
271   umount /tmp/lfs
272
273littlefs parameter selection
274----------------------------
275
276Be aware that the parameters passed to :command:`lfs` in the example
277above **must** exactly match the corresponding parameters used to
278initialize the file system.  The required parameters can be observed
279from the Zephyr mount log messages:
280
281.. code-block:: none
282
283    [00:00:00.009,002] <inf> littlefs: LittleFS version 2.2, disk version 2.0
284    [00:00:00.009,063] <inf> littlefs: FS at MX25R64:0x0 is 16 0x1000-byte blocks with 512 cye
285    [00:00:00.009,063] <inf> littlefs: sizes: rd 16 ; pr 16 ; ca 64 ; la 32
286
287* ``--read_size`` corresponds to the ``rd`` size and is 16;
288* ``--prog_size`` corresponds to the ``pr`` size and is 16;
289* ``--block_size`` comes from ``0x1000-byte blocks`` and is 4096 (0x1000);
290* ``--block_count`` comes from ``16 0x1000-byte blocks`` and is 16;
291* ``--cache_size`` comes from the ``ca`` size and is 64;
292* ``--lookahead_size`` comes from the ``la`` size and is 32
293
294If any of the parameters are inconsistent between the Zephyr and Linux
295specification the file system will not mount correctly.
296
297.. _littlefs: https://github.com/littlefs-project/littlefs
298.. _littlefs-FUSE: https://github.com/littlefs-project/littlefs-fuse
299