Mistake on this page?
Report an issue in GitHub or email us

USBMSD

USBMSD class hierarchy

You can use the USBMSD interface to emulate a mass storage device over USB. You can use this class to store or load data to and from a storage chip, such as SDcard or Flash. This class implements the MSD protocol and takes in a BlockDevice to interact with a storage chip.

USBMSD class reference

Public Member Functions
 USBMSD (BlockDevice *bd, bool connect_blocking=true, uint16_t vendor_id=0x0703, uint16_t product_id=0x0104, uint16_t product_release=0x0001)
 Constructor. More...
 USBMSD (USBPhy *phy, BlockDevice *bd, uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
 Fully featured constructor. More...
virtual ~USBMSD ()
 Destroy this object. More...
bool connect ()
 Connect the USB MSD device. More...
void disconnect ()
 Disconnect the USB MSD device. More...
void process ()
 Perform USB processing. More...
void attach (mbed::Callback< void()> cb)
 Called when USBMSD needs to perform processing. More...
bool media_removed ()
 Check if MSD device was removed/unmounted on the host side. More...
void init ()
 Initialize this instance. More...
void deinit ()
 Power down this instance. More...
bool configured ()
 Check if the device is configured. More...
void sof_enable ()
 Enable the start of frame interrupt. More...
void sof_disable ()
 Disable the start of frame interrupt. More...
bool endpoint_add (usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_t type, mbed::Callback< void()> callback=nullptr)
 Add an endpoint. More...
template<typename T >
bool endpoint_add (usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_t type, void(T::*callback)())
 Add an endpoint. More...
void endpoint_remove (usb_ep_t endpoint)
 Remove an endpoint. More...
void endpoint_remove_all ()
 Remove all non-zero endpoints. More...
void endpoint_stall (usb_ep_t endpoint)
 Stall an endpoint. More...
void endpoint_unstall (usb_ep_t endpoint)
 Un-stall an endpoint. More...
uint32_t endpoint_max_packet_size (usb_ep_t endpoint)
 Get the current maximum size for this endpoint. More...
void endpoint_abort (usb_ep_t endpoint)
 Abort the current transfer on this endpoint. More...
bool read_start (usb_ep_t endpoint, uint8_t *buffer, uint32_t size)
 start a read on the given endpoint More...
uint32_t read_finish (usb_ep_t endpoint)
 Get the status of a read. More...
bool write_start (usb_ep_t endpoint, uint8_t *buffer, uint32_t size)
 Write a data to the given endpoint. More...
uint32_t write_finish (usb_ep_t endpoint)
 Get the status of a write. More...
Protected Member Functions
virtual void callback_power (bool powered)
 Called by USBDevice layer on power state change. More...
virtual void callback_sof (int frame_number)
 Called by USBDevice layer on each new USB frame. More...
virtual void callback_reset ()
 Called by USBDevice layer on bus reset. More...
void complete_request (RequestResult result, uint8_t *data=NULL, uint32_t size=0)
 Called to complete the setup stage of a callback request. More...
void complete_request_xfer_done (bool success)
 Called to complete the data stage of a callback request. More...
void complete_set_configuration (bool success)
 Called to complete a set configuration command. More...
void complete_set_interface (bool success)
 Called to complete a set interface command. More...
uint8_t * find_descriptor (uint8_t descriptor_type, uint8_t index=0)
 Find a descriptor type inside the configuration descriptor. More...
const usb_ep_table_tendpoint_table ()
 Get the endpoint table of this device. More...
virtual void start_process ()
 Callback called to indicate the USB processing needs to be done. More...
virtual void lock ()
 Acquire exclusive access to this instance USBDevice. More...
virtual void unlock ()
 Release exclusive access to this instance USBDevice. More...
virtual void assert_locked ()
 Assert that the current thread of execution holds the lock. More...

USBMSD SDBlockDevice example

/*
 * Copyright (c) 2006-2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */
#include "mbed.h"
#include "SDBlockDevice.h"
#include "USBMSD.h"

SDBlockDevice sd(PTE3, PTE1, PTE2, PTE4);
USBMSD usb(&sd);

int main()
{

    while (true) {
        usb.process();
    }

    return 0;
}

USBMSD HeapBlockDevice example

/*
 * Copyright (c) 2006-2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */
#include "mbed.h"
#include "USBMSD.h"
#include "FATFileSystem.h"

#define DEFAULT_BLOCK_SIZE  512
#define HEAP_BLOCK_DEVICE_SIZE (128 * DEFAULT_BLOCK_SIZE)

FATFileSystem heap_fs("heap_fs");
HeapBlockDevice bd(HEAP_BLOCK_DEVICE_SIZE, DEFAULT_BLOCK_SIZE);

int main()
{
    bd.init();

    FATFileSystem::format(&bd);

    int err = heap_fs.mount(&bd);

    if (err) {
        printf("%s filesystem mount failed\ntry to reformat device... \r\n", heap_fs.getName());
        err = heap_fs.reformat(&bd);
    }

    // If still error, then report failure
    if (err) {
        printf("Error: Unable to format/mount the device.\r\n");
        while (1);
    }

    USBMSD usb(&bd);

    while (true) {
        usb.process();
    }

    return 0;
}

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.