MSC (Mass Storage)即大容量存贮。
下边这个程序,在设备枚举后,USB主机开始配置。当USB MSC接串被主机接受后,主设备开始始能这个接口,UDI_MSC_ENABLE_EXT()回调函数被调用。这样当事件被接收udi_msc_rrocess_trans()必须被使能。
msc自动处理内存访问通过在主函数调用udi_msc_process_trans();
以下是udi_msc_process_trans();
static void udi_msc_spc_mode_sense(bool b_sense10)
{
// Union of all mode sense structures
union sense_6_10 {
struct {
struct scsi_mode_param_header6 header;
struct spc_control_page_info_execpt sense_data;
} s6;
struct {
struct scsi_mode_param_header10 header;
struct spc_control_page_info_execpt sense_data;
} s10;
};
uint8_t data_sense_lgt;
uint8_t mode;
uint8_t request_lgt;
uint8_t wp;
struct spc_control_page_info_execpt *ptr_mode;
UDC_BSS(4) static union sense_6_10 sense;
// Clear all fields
memset(&sense, 0, sizeof(sense));
// Initialize process
if (b_sense10) {
request_lgt = udi_msc_cbw.CDB[8];
ptr_mode = &sense.s10.sense_data;
data_sense_lgt = sizeof(struct scsi_mode_param_header10);
} else {
request_lgt = udi_msc_cbw.CDB[4];
ptr_mode = &sense.s6.sense_data;
data_sense_lgt = sizeof(struct scsi_mode_param_header6);
}
// No Block descriptor
// Fill page(s)
mode = udi_msc_cbw.CDB[2] & SCSI_MS_MODE_ALL;
if ((SCSI_MS_MODE_INFEXP == mode)
|| (SCSI_MS_MODE_ALL == mode)) {
// Informational exceptions control page (from SPC)
ptr_mode->page_code =
SCSI_MS_MODE_INFEXP;
ptr_mode->page_length =
SPC_MP_INFEXP_PAGE_LENGTH;
ptr_mode->mrie =
SPC_MP_INFEXP_MRIE_NO_SENSE;
data_sense_lgt += sizeof(struct spc_control_page_info_execpt);
}
// Can't send more than mode sense data length
if (request_lgt > data_sense_lgt)
request_lgt = data_sense_lgt;
if (!udi_msc_cbw_validate(request_lgt, USB_CBW_DIRECTION_IN))
return;
// Fill mode parameter header length
wp = (mem_wr_protect(udi_msc_cbw.bCBWLUN)) ? SCSI_MS_SBC_WP : 0;
if (b_sense10) {
sense.s10.header.mode_data_length =
cpu_to_be16((data_sense_lgt - 2));
//sense.s10.header.medium_type = 0;
sense.s10.header.device_specific_parameter = wp;
//sense.s10.header.block_descriptor_length = 0;
} else {
sense.s6.header.mode_data_length = data_sense_lgt - 1;
//sense.s6.header.medium_type = 0;
sense.s6.header.device_specific_parameter = wp;
//sense.s6.header.block_descriptor_length = 0;
}
// Send mode sense data
udi_msc_data_send((uint8_t *) & sense, request_lgt);
}
当我运行这个程序的时候,除了驱动自动加以外还虚拟出了个U盘。这让我吃惊不小,看来USB还真得好好学。
以下是WINDOWS自动装完驱动的提示:
以下是我格式化U盘的画面:
最后我建立个TXT文档:
hello hello
存到hello.txt
放到那个虚拟盘上,没想到,顺利打开。
|