Implement application specific behavior of a Mass Storage Class (MSC) USB Device.
More...
|
| User API |
| User API reference of the Mass Storage Class.
|
|
| Configuration |
| Configuration of the USB Device MSC Class in µVision.
|
|
Implement application specific behavior of a Mass Storage Class (MSC) USB Device.
The MSC class in the USB Component is used for data storage.
Refer to:
The USB Component allows multiple instances of the MSC class. Each MSC class instance has a separate files and interface functions:
- A configuration file USBD_Config_MSC_n.h.
- An application specific user source code file which may be implemented with the user code template USBD_User_MSC_n.c.
- Functions that start with the prefix USBD_MSCn_ are available for each instance of a MSC class.
This documentation uses n as a placeholder for the instance number 0 - 3. Most applications only require one instance of a MSC class. For the first MSC class instance the instance number is 0:
- USBD_Config_MSC_0.h
- USBD_User_MSC_0.c
- The function prefix is USBD_MSC0_
Software Structure
The handling for the MSC class endpoint events is implemented in USBD_MSCn_Thread which is started by USBD_Initialize. Each instance of a MSC class runs an instance of USBD_MSCn_Thread which calls the data functions USBD_MSCn_Read and USBD_MSCn_Write.
Implementation
To create an USB Device with a MSC class:
Media Ownership
Sometimes, it is required to implement the ownership control over attached media and changing the ownership between USB and File System. This is required if you have a device that connects to a PC as a USB MSC device while the storage media also needs to be accessible to a user application. Using the two functions USBD_MSCn_SetMediaOwnerUSB and USBD_MSCn_SetMediaOwnerFS you can change the owner of the media to either the USB (the host PC) or the File System (the user application). The user code template USBD_MSC.c provides means to manage the ownership.
The following picture shows the connection of the device to the PC and the user application running on the device:
USB MSC Device connected to a PC with a user application accessing the attached storage medium
In the file USBD_MSC.c the variable usbd_msc0_media_own
is used to set the ownership of the media device to the application or the File System. In the file USBD_User_MSC.c the variable is used to initialize it at the beginning of the application (in the USBD_MSCn_Initialize function) and to check the ownership of the media (in the USBD_MSCn_CheckMedia function). The application then only makes use of the two functions USBD_MSCn_SetMediaOwnerUSB and USBD_MSCn_SetMediaOwnerFS as explained in this code example.
User Code Templates
There are two user code templates available that help to add support for a MSC device:
- USBD_User_MSC.c contains all the callback functions that need to be implemented by the user.
- USBD_MSC.c is a code template for the application specific functionality of a USB Device MSC instance and implements ownership control for attached media devices.
User Code Template USBD_User_MSC.c
The following source code can be used to implement the application specific behavior of a USB MSC Device.
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "rl_usb.h"
#define USE_FILE_SYSTEM 1 // 1 = File System is used, 0 = File System is not used
#define MEDIA_DRIVE "M0:"
#if (USE_FILE_SYSTEM == 1) // If File System is used
#include "rl_fs.h"
#define MEDIA_OWN_USB (1U ) // Media owned by USB (bit mask)
#define MEDIA_OWN_CHG (1U << 1) // Media ownership change requested (bit mask)
volatile uint8_t usbd_mscn_media_own;
static int32_t drv_id;
static bool media_ok;
#else
static uint32_t memory [8192/4];
static uint32_t block_buf[ 512/4];
extern
const uint8_t memory_disk_image[4096];
#endif
#if (USE_FILE_SYSTEM == 1) // If File System is used
uint32_t param_status;
usbd_mscn_media_own = MEDIA_OWN_USB;
media_ok = false;
if (finit (MEDIA_DRIVE) != fsOK) {
return;
}
drv_id = fs_ioc_get_id (MEDIA_DRIVE);
if (drv_id < 0U) { return; }
param_status = 0U;
if (fs_ioc_device_ctrl (drv_id, fsDevCtrlCodeControlMedia, ¶m_status) != fsOK) {
return;
}
if (fs_ioc_lock (drv_id)) {
return;
}
media_ok = true;
#else
memcpy (memory, memory_disk_image, sizeof(memory_disk_image));
#endif
}
}
#if (USE_FILE_SYSTEM == 1) // If File System is used
fsIOC_Cache cache_info;
if (fs_ioc_get_cache(drv_id, &cache_info) != fsOK) {
return false;
}
*buffer = (uint32_t)cache_info.buffer;
*size = cache_info.size;
#else
*buffer = (uint32_t)block_buf;
*size = sizeof(block_buf);
#endif
return true;
}
#if (USE_FILE_SYSTEM == 1) // If File System is used
fsMediaInfo media_info;
if (fs_ioc_read_info(drv_id, &media_info) != fsOK) {
return false;
}
*block_count = media_info.block_cnt;
*block_size = media_info.read_blen;
#else
*block_count = sizeof(memory)/512U;
*block_size = 512U;
#endif
return true;
}
#if (USE_FILE_SYSTEM == 1) // If File System is used
if (fs_ioc_read_sector (drv_id, lba, buf, cnt) != fsOK) {
return false;
}
#else
memcpy (buf, &memory[lba * (512U/4U)], cnt * 512U);
#endif
return true;
}
#if (USE_FILE_SYSTEM == 1) // If File System is used
if (fs_ioc_write_sector (drv_id, lba, buf, cnt) != fsOK) {
return false;
}
#else
memcpy (&memory[lba * (512U/4U)], buf, cnt * 512U);
#endif
return true;
}
#if (USE_FILE_SYSTEM == 1) // If File System is used
uint32_t param_status;
uint8_t media_state;
static uint8_t media_ready_ex = 0U;
uint8_t own;
media_state = 0U;
switch (fs_ioc_device_ctrl (drv_id, fsDevCtrlCodeCheckMedia, ¶m_status)) {
case fsOK:
if (param_status & FS_MEDIA_NOCHKMEDIA) {
media_state = USBD_MSC_MEDIA_READY;
break;
}
if (param_status & FS_MEDIA_INSERTED) {
media_state = USBD_MSC_MEDIA_READY;
}
if (param_status & FS_MEDIA_PROTECTED) {
media_state |= USBD_MSC_MEDIA_PROTECTED;
}
break;
default:
break;
}
own = usbd_mscn_media_own;
if (own & MEDIA_OWN_CHG) {
if (own & MEDIA_OWN_USB) {
funmount (MEDIA_DRIVE);
} else {
fs_ioc_unlock (drv_id);
}
}
if ((own & MEDIA_OWN_CHG) ||
(media_state ^ media_ready_ex)) {
if (media_state & USBD_MSC_MEDIA_READY) {
if (own & MEDIA_OWN_USB){
media_ok = false;
param_status = 0U;
if (fs_ioc_device_ctrl (drv_id, fsDevCtrlCodeControlMedia, ¶m_status) == fsOK) {
if (fs_ioc_lock (drv_id) == 0) {
media_ok = true;
}
}
} else {
if (fmount (MEDIA_DRIVE) == fsOK) {
media_ok = true;
}
}
}
if (own & MEDIA_OWN_CHG) {
usbd_mscn_media_own &= ~MEDIA_OWN_CHG;
}
media_ready_ex = media_state & USBD_MSC_MEDIA_READY;
}
if ((!media_ok) || (!(usbd_mscn_media_own & MEDIA_OWN_USB))) {
return 0U;
}
return media_state;
#else
return USBD_MSC_MEDIA_READY;
#endif
}
User Code Template USBD_MSC_n.c
The following source code can be used to implement ownership control for attached media devices.
#include "stdint.h"
#include "stdbool.h"
#include "cmsis_os.h"
#include "USBD_MSC_n.h"
extern volatile uint8_t usbd_mscn_media_own;
uint32_t timeout_cnt;
timeout_cnt = 300U;
usbd_mscn_media_own = USBD_MSCn_MEDIA_OWN_CHG | USBD_MSCn_MEDIA_OWN_USB;
while (usbd_mscn_media_own & USBD_MSCn_MEDIA_OWN_CHG) {
osDelay(10);
if ((--timeout_cnt) == 0) { return USBD_MSCn_ERROR; }
}
return USBD_MSCn_OK;
}
uint32_t timeout_cnt;
timeout_cnt = 300U;
usbd_mscn_media_own = USBD_MSCn_MEDIA_OWN_CHG;
while (usbd_mscn_media_own & USBD_MSCn_MEDIA_OWN_CHG) {
osDelay(10);
if ((--timeout_cnt) == 0) { return USBD_MSCn_ERROR; }
}
return USBD_MSCn_OK;
}
Code Example
This code snippet shows how to use the two functions in a user application:
:
switch (DeviceState) {
case DEV_IDLE:
break;
case DEV_START_DOING_SOMETHING:
USBD_MSC0_SetMediaOwnerFS();
DeviceState = DEV_DO_IT;
break;
case DEV_STOP_DOING_SOMETHING:
USBD_MSC0_SetMediaOwnerUSB();
DeviceState = DEV_IDLE;
break;
}
: