📄 usb_mode.c
字号:
USB_End_Ms();
}
#endif
}
#ifdef PICTBRIDGE_SUPPORT
else if ((type == USB_IMAGE) && (gUsbDevice.device_type != USB_IMAGE))
{
/* initailize IMAGE class */
USB_Register_CreateFunc("IMAGE", USB_Image_If_Create, USB_Image_If_Reset);
USB_Register_Device_Code(USB_IMAGE_DEVICE_CODE, USB_IMAGE_SUBCLASS_CODE,
USB_IMAGE_PROTOCOL_CODE, g_USBImage.image_param->desc_product);
/* get 4-byte alignment image buffer and xml buffer */
g_USBImage.image_buffer = (USB_IMAGE_BUFFER *)parameter;
g_USBImage.dps_request_tx_data = (kal_uint8 *)g_USBImage.image_buffer + USB_IMAGE_MAX_BUFFERSIZE;
g_USBImage.dps_response_tx_data = (kal_uint8 *)g_USBImage.dps_request_tx_data + USB_IMAGE_MAX_XMLBUFFERSIZE;
g_USBImage.dps_request_rx_data = (kal_uint8 *)g_USBImage.dps_response_tx_data + USB_IMAGE_MAX_XMLBUFFERSIZE;
g_USBImage.dps_response_rx_data = (kal_uint8 *)g_USBImage.dps_request_rx_data + USB_IMAGE_MAX_XMLBUFFERSIZE;
USB_Init(USB_IMAGE, b_enable);
}
#endif
else
{
EXT_ASSERT(0, type, gUsbDevice.device_type, 0);
}
}
/* The enable parameter decides whether to turn off D+ at this time */
void USB_Release_Type(kal_bool b_enable, kal_bool b_plugout)
{
USB_DEVICE_TYPE device_type;
// if (gUsbDevice.device_type != USB_UNKOWN)
// device_type = USB_IMAGE;
device_type = gUsbDevice.device_type;
gUsbDevice.is_configured_now = KAL_FALSE;
/* note that device type should be set to unknown here,
in case UART owner uses other UART functions */
gUsbDevice.device_type = USB_UNKOWN;
if(b_enable == KAL_TRUE)
{
/* power down USB */
USB_PDNmode(KAL_TRUE); /* disable D+ pull high */
}
/* stop and release resource*/
if (device_type == USB_MASS_STORAGE)
{
USB_Free_DMA_Channel(g_UsbMS.txpipe->byEP);
USB_Release_Ms_Status();
if(INT_USBBoot() == KAL_FALSE) /* normal mode */
{
#ifdef __USB_IN_NORMAL_MODE__
if(kal_if_hisr() == KAL_FALSE)
{
USB_End_Normal_Mode_Ms();
}
else
{
#ifdef __OTG_ENABLE__
OTG_Hdlr_Send_Msg(OTG_HDLR_TASK_END_NORMAL_MS, 0, KAL_FALSE);
#else
EXT_ASSERT(0, 0, 0, 0);
#endif
}
#else
EXT_ASSERT(0, 0, 0, 0);
#endif
}
else
{
#ifdef __NVRAM_IN_USB_MS__
if(kal_if_hisr() == KAL_FALSE)
{
USB_End_Ms();
}
else
{
#ifdef __OTG_ENABLE__
OTG_Hdlr_Send_Msg(OTG_HDLR_TASK_END_MS, 0, KAL_FALSE);
#else
EXT_ASSERT(0, 0, 0, 0);
#endif
}
#endif
}
}
else if(device_type == USB_CDC_ACM)
{
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner == USB_ACM_OWNER_DSP)
{
USB_DSPIRDBG_Stop();
}
else
#endif /* __DSPIRDBG__*/
{
/* notify UART owner that virtual UART cable is plug out*/
/* Note that this should be called before release ACM status */
if((g_UsbACM.send_UARTilm == KAL_TRUE) && (USB2UARTPort.ownerid != MOD_DRV_HISR))
{
USB_Send_Msg(USB_SEND_MSG_UART_PLUG_OUT, 0, KAL_TRUE);
}
}
USB_Free_DMA_Channel(g_UsbACM.txpipe->byEP);
/* release global variable g_UsbACM resource*/
USB_Release_Acm_Status();
}
#ifdef WEBCAM_SUPPORT
else if(device_type == USB_VIDEO)
{
if(g_UsbMode.cable_type==USB_MODE_CABLE_PLUGOUT)/*real plug out, not these two messages result in*/
{
/* Update video state machine */
USBVideo_Plug_Out_Hdlr();
}
USB_Free_DMA_Channel(g_USBVideo.txpipe->byEP);
USB_Release_Video_Status();
}
#endif /* WEBCAM_SUPPORT */
#ifdef PICTBRIDGE_SUPPORT
else if(device_type == USB_IMAGE)
{
if(g_UsbMode.cable_type == USB_MODE_CABLE_PLUGOUT) /*real plug out */
{
/* Update image state machine */
USB_Image_Plug_Out_Hdlr();
}
USB_Free_DMA_Channel(g_USBImage.txpipe->byEP);
USB_Release_Image_Status();
}
#endif /* PICTBRIDGE_SUPPORT */
if(device_type!=USB_UNKOWN)
{
/* If cable is plug out second time in mass storage mode, do not send these msgs */
USB_DeRegister_CreateFunc();
USB_DeRegister_Device_Code();
/* release global variable gUsbDevice resource*/
USB_Release_Device_Status();
}
if(b_plugout == KAL_TRUE)
{
#ifndef __PRODUCTION_RELEASE__
kal_prompt_trace(MOD_USB, "USB plug out");
#endif /* __PRODUCTION_RELEASE__ */
USB_Release();
if((gUsbDevice.usb_comport_boot == KAL_FALSE)||(INT_USBBoot() == KAL_TRUE))
{
#ifndef OTG_NORMAL_APP_FIXED
/* Notify UEM, MS mode must send this message to shut down */
USB_Send_Msg(USB_SEND_MSG_PLUG_OUT, 0, KAL_TRUE);
#endif
}
}
}
#ifdef __OTG_ENABLE__
static void OTG_Host_Stop_Hdlr(void)
{
USBD_Detach(USB_HCD_DETATCH, 0);
if(g_UsbMode.b_start_hnp == KAL_TRUE)
{
#ifndef OTG_NORMAL_APP_FIXED
/*Notify UEM*/
USB_Send_Msg(USB_SEND_MSG_PLUG_OUT, 0, KAL_TRUE);
#endif
}
return;
}
void OTG_Hdlr_Select(OTG_HDLR_TASK_TYPE type, kal_uint32 *parameter, kal_bool b_param)
{
switch(type)
{
case OTG_HDLR_TASK_INIT_MS:
USB_Init_Ms(g_UsbMS.b_ms_first_plugin);
g_UsbMS.b_ms_first_plugin = KAL_FALSE;
break;
case OTG_HDLR_TASK_END_MS:
USB_End_Ms();
break;
case OTG_HDLR_TASK_INIT_NOMAL_MS:
USB_Init_Normal_Mode_Ms(parameter, b_param);
break;
case OTG_HDLR_TASK_END_NORMAL_MS:
USB_End_Normal_Mode_Ms();
break;
default:
break;
}
}
#endif
/************************************************************
mass storage mode functions
*************************************************************/
#ifdef __USB_IN_NORMAL_MODE__
static FS_DEVICE_TYPE_ENUM USB_Ms_Dev_To_Fs_Type(USB_STORAGE_DEV_TYPE type)
{
FS_DEVICE_TYPE_ENUM fs_type;
switch(type)
{
case USB_STORAGE_DEV_NOR:
fs_type = FS_DEVICE_TYPE_NOR;
break;
case USB_STORAGE_DEV_NAND:
fs_type = FS_DEVICE_TYPE_NAND;
break;
case USB_STORAGE_DEV_CARD:
fs_type = FS_DEVICE_TYPE_CARD;
break;
default:
EXT_ASSERT(0, type, 0, 0);
break;
}
return fs_type;
}
#ifdef __USB_IN_NORMAL_MODE_IMPROVE__
static kal_bool usbms_read_sysdrv( void *data, kal_uint32 LBA, kal_uint16 sec_len)
{
kal_uint32 received_file_size;
kal_int32 result;
result = FS_Seek(g_UsbMS.system_drive_handle, (LBA*512), FS_FILE_BEGIN);
if(result < 0)
EXT_ASSERT(0, result, 0, 0);
result = FS_Read(g_UsbMS.system_drive_handle, data, (sec_len*512), &received_file_size);
if(result != FS_NO_ERROR)
EXT_ASSERT(0, result, 0, 0);
return KAL_TRUE;
}
static kal_bool usbms_write_sysdrv(void *data, kal_uint32 LBA, kal_uint16 sec_len)
{
kal_uint32 file_size;
kal_int32 result;
result = FS_GetFileSize(g_UsbMS.system_drive_handle, &file_size);
if(result != FS_NO_ERROR)
EXT_ASSERT(0, result, 0, 0);
if((LBA + sec_len)*512 > (file_size) )
EXT_ASSERT(0, (LBA + sec_len)*512, file_size, 0);
result = FS_Seek(g_UsbMS.system_drive_handle, (LBA*512), FS_FILE_BEGIN);
if(result < 0)
EXT_ASSERT(0, result, 0, 0);
result = FS_Write(g_UsbMS.system_drive_handle, data, (sec_len*512), &file_size);
if(result != FS_NO_ERROR)
EXT_ASSERT(0, result, 0, 0);
return KAL_TRUE;
}
static kal_bool usbms_read_capacity_sysdrv(kal_uint32 *max_lba, kal_uint32 *sec_len)
{
kal_uint32 *lba;
kal_int32 result;
*sec_len =512;
result = FS_GetFileSize(g_UsbMS.system_drive_handle, lba);
if(result != FS_NO_ERROR)
EXT_ASSERT(0, result, 0, 0);
*max_lba = (*lba)/512 - 1;
return KAL_TRUE;
}
static kal_uint8 usbms_checkmedia_exit_sysdrv(void)
{
return 0;
}
static kal_bool usbms_format_sysdrv()
{
return KAL_TRUE;
}
static kal_bool usbms_prevmedia_removal_sysdrv(kal_bool enable)
{
return KAL_TRUE;
}
/*
static kal_bool usbms_read_formatcapacity_sysdrv(kal_uint32 *no_of_blks, kal_uint32 *blk_len)
{
return KAL_TRUE;
}
*/
static USB_STORAGE_DEV_TYPE usbms_dev_type_sysdrv(void)
{
/* it must be NAND or NOR flash */
if(g_UsbMS.device_type >= USB_STORAGE_DEV_CARD)
EXT_ASSERT(0, g_UsbMS.device_type, 0, 0);
return g_UsbMS.device_type;
}
/* If we want to export the drive which system drive in the same disk, then we need to build this fake MS APIs to replace NOR or NAND flash APIs */
USB_DiskDriver_STRUCT USB_SYSTEM_drv =
{
usbms_read_sysdrv,
usbms_write_sysdrv,
usbms_format_sysdrv,
usbms_checkmedia_exit_sysdrv,
usbms_prevmedia_removal_sysdrv,
usbms_read_capacity_sysdrv,
// usbms_read_formatcapacity_sysdrv,
usbms_dev_type_sysdrv
};
#endif
#endif
static void USB_Init_Normal_Mode_Ms(kal_uint32* mem_addr, kal_bool b_enable)
{
#ifdef __USB_IN_NORMAL_MODE__
kal_int32 index;
USB_STORAGE_DEV_TYPE dev_type;
FS_DEVICE_TYPE_ENUM fs_type;
kal_int32 result;
kal_uint8 max_lun = USB_Ms_Get_Max_LUN();
/* This function can not be executed in HISR*/
if(kal_if_hisr()==KAL_TRUE)
EXT_ASSERT(0, 0, 0, 0);
/* get disk buffer */
g_UsbMS.disk_buffer = (void*)mem_addr;
/* lock file system */
result = FS_LockFAT(FS_EXPORT_ENUM);
if(result < 0)
{
gUsbDevice.usb_config_result = KAL_FALSE;
return;
}
/* query whether each device can be exported or not (system driver disk)*/
for(index = max_lun; index >= 0; index--)
{
/* get each device type and translate to file system type */
dev_type = USB_Ms_Dev_Type(index);
fs_type = USB_Ms_Dev_To_Fs_Type(dev_type);
/* query whether it can be exported or not */
result = FS_GetDevStatus(fs_type, FS_EXPORT_STATE_ENUM);
if(result == FS_NO_ERROR)
{
/* device is not exported, means it is system drive inside */
#ifdef __USB_IN_NORMAL_MODE_IMPROVE__
WCHAR temp_path1[] = L"x:\\";
WCHAR temp_path2[] = L"\\\\.\\x:";
kal_int16 drvLetter;
result = FS_GetDevPartitions(fs_type);
if (result <= 1)
{
/* There're no partition on this physical device */
EXT_ASSERT(0, result, fs_type, 0);
}
/* get the drive letter we want to export */
drvLetter = FS_GetDrive(FS_DRIVE_V_NORMAL, 1, FS_NO_ALT_DRIVE);
temp_path1[0] = drvLetter;
if (fs_type != FS_GetDevType(temp_path1))
{
drvLetter = FS_GetDrive(FS_DRIVE_V_NORMAL, 2, FS_NO_ALT_DRIVE);
temp_path1[0] = drvLetter;
if (fs_type != FS_GetDevType(temp_path1))
EXT_ASSERT(0, fs_type, FS_GetDrive(FS_DRIVE_V_NORMAL, 1, FS_NO_ALT_DRIVE),
FS_GetDrive(FS_DRIVE_V_NORMAL, 2, FS_NO_ALT_DRIVE));
}
temp_path2[4] = drvLetter;
g_UsbMS.system_drive_handle = FS_Open(temp_path2, FS_READ_WRITE);
if(g_UsbMS.system_drive_handle < 0)
EXT_ASSERT(0, g_UsbMS.system_drive_handle, 0, 0);
g_UsbMS.device_type = dev_type;
USB_Ms_Change_Register_DiskDriver(index, &USB_SYSTEM_drv);
#else
USB_Ms_DeRegister_DiskDriver(index);
#endif /* __USB_IN_NORMAL_MODE_IMPROVE__ */
}
else if(result != FS_DEVICE_EXPORTED_ERROR)
{
EXT_ASSERT(0, result, dev_type, fs_type);
}
}
/* Initialize storage function */
USB_Register_CreateFunc("MASS STORAGE", USB_Ms_If_Create, USB_Ms_If_Reset);
USB_Register_Device_Code(USB_MS_DEVICE_CODE, USB_MS_SUBCLASS_CODE,
USB_MS_PROTOCOL_CODE, g_UsbMS.ms_param->desc_product);
/* initialize MASS STORAGE MODE */
USB_Init(USB_MASS_STORAGE, b_enable);
#else /* __USB_IN_NORMAL_MODE__ */
EXT_ASSERT(0, 0, 0, 0);
#endif
}
static void USB_End_Normal_Mode_Ms(void)
{
#ifdef __USB_IN_NORMAL_MODE__
/* This function can not be executed in HISR*/
if(kal_if_hisr()==KAL_TRUE)
EXT_ASSERT(0, 0, 0, 0);
#ifdef __USB_IN_NORMAL_MODE_IMPROVE__
FS_Close(g_UsbMS.system_drive_handle);
/* for debugging */
g_UsbMS.system_drive_handle = -1;
#endif
FS_LockFAT(FS_REMOUNT_ENUM);
#else
EXT_ASSERT(0, 0, 0, 0);
#endif
}
void USB_Init_Ms(kal_bool bFirst)
{
#ifdef __P_PROPRIETARY_COPYRIGHT__
/* under construction !*/
#endif
/* This function can not be executed in HISR*/
if(kal_if_hisr() == KAL_TRUE)
EXT_ASSERT(0, 0, 0, 0);
#ifdef OTG_NORMAL_APP_FIXED
ASSERT(0);
#endif
#ifdef __NVRAM_IN_USB_MS__
/* Notify UEM*/
USB_Send_Msg(USB_SEND_MSG_PLUG_IN, 0, KAL_TRUE);
#else
if(bFirst)
{
/* Notify UEM*/
USB_Send_Msg(USB_SEND_MSG_PLUG_IN, 0, KAL_TRUE);
}
#endif
/* If not first time plug in, only notify UEM*/
if(bFirst)
{
/* get unique string from nvram and construct serial string*/
USB_Get_Serial_Value();
#ifdef __P_PROPRIETARY_COPYRIGHT__
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif
/* must lock file system before turn on USB power */
FS_LockFAT(FS_LOCK_USB_ENUM); //lock file system in ms mode
USB_Register_CreateFunc("MASS STORAGE", USB_Ms_If_Create, USB_Ms_If_Reset);
USB_Register_Device_Code(USB_MS_DEVICE_CODE, USB_MS_SUBCLASS_CODE,
USB_MS_PROTOCOL_CODE, g_UsbMS.ms_param->desc_product);
/* initialize MASS STORAGE MODE */
USB_Init(USB_MASS_STORAGE, KAL_TRUE);
}
}
static void USB_End_Ms(void)
{
#ifdef __NVRAM_IN_USB_MS__
/* This function can not be executed in HISR*/
if(kal_if_hisr()==KAL_TRUE)
EXT_ASSERT(0, 0, 0, 0);
FS_LockFAT(FS_UNLOCK_USB_ENUM);
#else
EXT_ASSERT(0, 0, 0, 0);
#endif
}
void USB_Init_ACM_In_Meta(void)
{
static kal_bool b_init_acm_meta = KAL_FALSE;
if(b_init_acm_meta==KAL_FALSE)
{
b_init_acm_meta = KAL_TRUE;
/* init ft acm parameters*/
USB_Acm_FT_Init();
/* initailize CDC_ACM */
USB_Register_CreateFunc("ACM DATA", USB_Acm_DataIf_Create,USB_Acm_DataIf_Reset);
USB_Register_CreateFunc("ACM COMMU.", USB_Acm_CommIf_Create,USB_Acm_CommIf_Reset);
USB_Register_Device_Code(USB_ACM_DEVICE_CODE, USB_ACM_SUBCLASS_CODE,
USB_ACM_PROTOCOL_CODE, g_UsbACM.acm_param->desc_product);
USB_Init(USB_CDC_ACM, KAL_TRUE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -