RZ/A Flexible Software Package Documentation  Release v3.3.0

 
USB Host Mass Storage Class Driver (r_usb_hmsc)

Functions

fsp_err_t R_USB_HMSC_StorageCommand (usb_ctrl_t *const p_api_ctrl, uint8_t *buf, uint8_t command, uint8_t destination)
 Processing for MassStorage(ATAPI) command. More...
 
fsp_err_t R_USB_HMSC_DriveNumberGet (usb_ctrl_t *const p_api_ctrl, uint8_t *p_drive, uint8_t destination)
 Get number of Storage drive. More...
 
fsp_err_t R_USB_HMSC_SemaphoreGet (void)
 Get a semaphore. (RTOS only) More...
 
fsp_err_t R_USB_HMSC_SemaphoreRelease (void)
 Release a semaphore. (RTOS only) More...
 
fsp_err_t R_USB_HMSC_StorageReadSector (uint16_t drive_number, uint8_t *const buff, uint32_t sector_number, uint16_t sector_count)
 Read sector information. More...
 
fsp_err_t R_USB_HMSC_StorageWriteSector (uint16_t drive_number, uint8_t const *const buff, uint32_t sector_number, uint16_t sector_count)
 Write sector information. More...
 

Detailed Description

This module provides a USB Host Mass Storage Class (HMSC) driver. It implements the USB HMSC Interface.

Overview

The r_usb_hmsc module, when used in combination with the r_usb_basic module, operates as a USB Host Mass Storage Class (HMSC) driver. It is built on the USB Mass Storage Class Bulk-Only Transport (BOT) protocol. It is possible to communicate with BOT-compatible USB storage devices by combining this module with a file system and storage device driver.

Note
This module should be used in combination with the FreeRTOS+FAT File System.

Features

The r_usb_hmsc module has the following key features:

Class Requests

The class requests supported by this driver are shown below.

Request Description
GetMaxLun Gets the maximum number of units that are supported.
MassStorageReset Cancels a protocol error.

Storage Commands

This driver supports the following storage commands:

Configuration

Refer to the USB (r_usb_basic) module.

Clock Configuration

Refer to the USB (r_usb_basic) module.

Pin Configuration

Refer to the USB (r_usb_basic) module.

Usage Notes

Warning
Due to the wide variety of USB mass storage device implementations, this driver is not guaranteed to work with all devices. When implementing the driver it is important to verify correct operation with the mass storage devices that the end user is expected to use.

For Bare Metal

  1. To use FreeRTOS+FAT without FreeRTOS, copy FreeRTOSConfigMinimal.h to one of your project's include paths and rename it FreeRTOSConfig.h.
  2. In configurator, enter the appropriate values in the Main stack size and Heap size fields.
  3. In the Bare Metal version, specify "NULL" in the Callback item.
    r_usb_hmsc_baremetal_setting.png
    For Bare Metal Setting

Limitations

  1. Some MSC devices may be unable to connect because they are not recognized as storage devices.
  2. MSC devices that return values of 1 or higher in response to the GetMaxLun command (mass storage class command) are not supported.
  3. Only USB storage devices with a sector size of 512 bytes can be connected.
  4. A device that does not respond to the READ_CAPACITY command operates as a device with a sector size of 512 bytes.
  5. This module must be incorporated into a project using r_usb_basic and does not provide any public APIs.
  6. This driver does not support Low-speed.
  7. This driver does not support FreeRTOS operation.

Examples

USB HMSC Example

Example Operating Environment

The following shows an example operating environment for the HMSC.

Refer to the associated instruction manuals for details on setting up the evaluation board and using the emulator, etc.

r_usb_hmsc_operating_environment.png
Example Operating Environment

Application Specifications

The main functions of the application are as follows:

  1. Performs enumeration and drive recognition processing on MSC devices.
  2. After the above processsing finisihes, the application writes the file to the MSC device once.
  3. After writing the above file, the APL repeatedly reads the file. It continues to read the file repeatedly until the switch is pressed again.

Example Code

Note
In order to get the example code below to be workable, Has CWD in FreeRTOS+FAT configuration below should be enabled.
r_usb_hmsc_freertos+fat_setting.png
FreeRTOS+FAT configuration for Exmaple Code
#define RM_FREERTOS_PLUS_FAT_EXAMPLE_FILE_NAME "TEST_FILE.txt"
#define RM_FREERTOS_PLUS_FAT_EXAMPLE_BUFFER_SIZE_BYTES (10240)
#define RM_FREERTOS_PLUS_FAT_EXAMPLE_PARTITION_NUMBER (0)
#define RM_FREERTOS_PLUS_FAT_EXAMPLE_SUPPORT_USB
typedef enum
{
STATE_ATTACH, STATE_DATA_READY, STATE_DATA_WRITE, STATE_FILE_READ, STATE_DETACH, STATE_ERROR,
} state_t;
extern rm_freertos_plus_fat_instance_ctrl_t g_rm_freertos_plus_fat0_ctrl;
extern const rm_freertos_plus_fat_cfg_t g_rm_freertos_plus_fat0_cfg;
uint8_t g_file_data[RM_FREERTOS_PLUS_FAT_EXAMPLE_BUFFER_SIZE_BYTES];
uint8_t g_read_buffer[RM_FREERTOS_PLUS_FAT_EXAMPLE_BUFFER_SIZE_BYTES];
static uint16_t g_state = STATE_DETACH;
void usb_hmsc_baremetal_example (void)
{
uint16_t i;
uint16_t k;
fsp_err_t err;
FF_FILE * pxSourceFile;
FF_Disk_t disk;
usb_status_t event;
usb_event_info_t event_info;
FF_Error_t ff_err;
size_t size_return;
int close_err;
for (i = 0; i < RM_FREERTOS_PLUS_FAT_EXAMPLE_BUFFER_SIZE_BYTES; i++)
{
g_file_data[i] = (uint8_t) i;
}
/* Open media driver.*/
RM_FREERTOS_PLUS_FAT_Open(&g_rm_freertos_plus_fat0_ctrl, &g_rm_freertos_plus_fat0_cfg);
/* When using USB media, enable RM_FREERTOS_PLUS_FAT_EXAMPLE_SUPPORT_USB macro. */
#ifdef RM_FREERTOS_PLUS_FAT_EXAMPLE_SUPPORT_USB
while (1)
{
g_usb_on_usb.eventGet(&event_info, &event);
switch (event)
{
{
/* Initialize the media and the disk. If the media is removable, it must be inserted before calling
* RM_FREERTOS_PLUS_FAT_MediaInit. */
p_instance_ctrl = event_info.p_context;
p_instance_ctrl->device_address = event_info.device_address;
RM_FREERTOS_PLUS_FAT_MediaInit(&g_rm_freertos_plus_fat0_ctrl, &device);
/* Initialize one disk for each partition used in the application. */
RM_FREERTOS_PLUS_FAT_DiskInit(&g_rm_freertos_plus_fat0_ctrl, &g_rm_freertos_plus_fat0_disk_cfg, &disk);
/* Mount each disk. This assumes the disk is already partitioned and formatted. */
FF_Mount(&disk, RM_FREERTOS_PLUS_FAT_EXAMPLE_PARTITION_NUMBER);
/* Add the disk to the file system. */
FF_FS_Add("/", &disk);
/* Open a source file for writing. */
pxSourceFile = ff_fopen((const char *) RM_FREERTOS_PLUS_FAT_EXAMPLE_FILE_NAME, "w");
/* Write file data. */
ff_fwrite(g_file_data, sizeof(g_file_data), 1, pxSourceFile);
/* Close the file. */
ff_fclose(pxSourceFile);
g_state = STATE_FILE_READ;
break;
}
{
g_state = STATE_DETACH;
RM_FREERTOS_PLUS_FAT_DiskDeinit(&g_rm_freertos_plus_fat0_ctrl, &disk);
break;
}
default:
{
break;
}
}
if (STATE_FILE_READ == g_state)
{
pxSourceFile = ff_fopen((const char *) RM_FREERTOS_PLUS_FAT_EXAMPLE_FILE_NAME, "r");
for (k = 0; k < RM_FREERTOS_PLUS_FAT_EXAMPLE_BUFFER_SIZE_BYTES; k++)
{
g_read_buffer[k] = (uint8_t) 0;
}
/* Read file data. */
size_return = ff_fread(g_read_buffer, sizeof(g_file_data), 1, pxSourceFile);
/* Close the file. */
close_err = ff_fclose(pxSourceFile);
}
}
#endif
}

Function Documentation

◆ R_USB_HMSC_StorageCommand()

fsp_err_t R_USB_HMSC_StorageCommand ( usb_ctrl_t *const  p_api_ctrl,
uint8_t *  buf,
uint8_t  command,
uint8_t  destination 
)

Processing for MassStorage(ATAPI) command.

Return values
FSP_SUCCESSSuccess.
FSP_ERR_USB_FAILEDThe function could not be completed successfully.
FSP_ERR_ASSERTIONParameter Null pointer error.
FSP_ERR_USB_PARAMETERParameter error.

◆ R_USB_HMSC_DriveNumberGet()

fsp_err_t R_USB_HMSC_DriveNumberGet ( usb_ctrl_t *const  p_api_ctrl,
uint8_t *  p_drive,
uint8_t  destination 
)

Get number of Storage drive.

Return values
FSP_SUCCESSSuccess.
FSP_ERR_USB_FAILEDThe function could not be completed successfully.
FSP_ERR_ASSERTIONParameter Null pointer error.
FSP_ERR_USB_PARAMETERParameter error.

◆ R_USB_HMSC_SemaphoreGet()

fsp_err_t R_USB_HMSC_SemaphoreGet ( void  )

Get a semaphore. (RTOS only)

If this function is called in the OS less execution environment, a failure is returned.

Return values
FSP_SUCCESSSuccess.
FSP_ERR_USB_FAILEDThe function could not be completed successfully.

◆ R_USB_HMSC_SemaphoreRelease()

fsp_err_t R_USB_HMSC_SemaphoreRelease ( void  )

Release a semaphore. (RTOS only)

If this function is called in the OS less execution environment, a failure is returned.

Return values
FSP_SUCCESSSuccess.
FSP_ERR_USB_FAILEDThe function could not be completed successfully.

◆ R_USB_HMSC_StorageReadSector()

fsp_err_t R_USB_HMSC_StorageReadSector ( uint16_t  drive_number,
uint8_t *const  buff,
uint32_t  sector_number,
uint16_t  sector_count 
)

Read sector information.

Return values
FSP_SUCCESSSuccess.
FSP_ERR_USB_FAILEDThe function could not be completed successfully.
FSP_ERR_ASSERTIONParameter Null pointer error.
FSP_ERR_USB_PARAMETERParameter error.
Note
The address specified in the argument buff must be 4-byte aligned.

◆ R_USB_HMSC_StorageWriteSector()

fsp_err_t R_USB_HMSC_StorageWriteSector ( uint16_t  drive_number,
uint8_t const *const  buff,
uint32_t  sector_number,
uint16_t  sector_count 
)

Write sector information.

Return values
FSP_SUCCESSSuccess.
FSP_ERR_USB_FAILEDThe function could not be completed successfully.
FSP_ERR_ASSERTIONParameter Null pointer error.
FSP_ERR_USB_PARAMETERParameter error.
Note
The address specified in the argument buff must be 4-byte aligned.