123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426 |
- #include "usbh_usr.h"
- #include "bsp_fatfs.h"
- #include "ff.h"
- #include "usbh_msc_bot.h"
- #include "usbh_msc_core.h"
- #include "usbh_msc_scsi.h"
- #include <string.h>
- #define usb_printf printf
- USB_OTG_CORE_HANDLE USB_OTG_Core;
- USBH_HOST USB_Host;
- uint8_t USBH_USR_ApplicationState = USH_USR_FS_INIT;
- USBH_Usr_cb_TypeDef USR_cb =
- {
- USBH_USR_Init,
- USBH_USR_DeInit,
- USBH_USR_DeviceAttached,
- USBH_USR_ResetDevice,
- USBH_USR_DeviceDisconnected,
- USBH_USR_OverCurrentDetected,
- USBH_USR_DeviceSpeedDetected,
- USBH_USR_Device_DescAvailable,
- USBH_USR_DeviceAddressAssigned,
- USBH_USR_Configuration_DescAvailable,
- USBH_USR_Manufacturer_String,
- USBH_USR_Product_String,
- USBH_USR_SerialNum_String,
- USBH_USR_EnumerationDone,
- USBH_USR_UserInput,
- USBH_USR_MSC_Application,
- USBH_USR_DeviceNotSupported,
- USBH_USR_UnrecoveredError};
- const uint8_t MSG_HOST_INIT[] = "> Host Library Initialized\r\n";
- const uint8_t MSG_DEV_ATTACHED[] = "> Device Attached \r\n";
- const uint8_t MSG_DEV_DISCONNECTED[] = "> Device Disconnected\r\n";
- const uint8_t MSG_DEV_ENUMERATED[] = "> Enumeration completed \r\n";
- const uint8_t MSG_DEV_HIGHSPEED[] = "> High speed device detected\r\n";
- const uint8_t MSG_DEV_FULLSPEED[] = "> Full speed device detected\r\n";
- const uint8_t MSG_DEV_LOWSPEED[] = "> Low speed device detected\r\n";
- const uint8_t MSG_DEV_ERROR[] = "> Device fault \r\n";
- const uint8_t MSG_MSC_CLASS[] = "> Mass storage device connected\r\n";
- const uint8_t MSG_HID_CLASS[] = "> HID device connected\r\n";
- const uint8_t MSG_DISK_SIZE[] = "> Size of the disk in MBytes: \r\n";
- const uint8_t MSG_LUN[] = "> LUN Available in the device:\r\n";
- const uint8_t MSG_ROOT_CONT[] = "> Exploring disk flash ...\r\n";
- const uint8_t MSG_WR_PROTECT[] = "> The disk is write protected\r\n";
- const uint8_t MSG_UNREC_ERROR[] = "> UNRECOVERED ERROR STATE\r\n";
- void USBH_USR_Init(void)
- {
- static uint8_t startup = 0;
- if (startup == 0)
- {
- startup = 1;
- #ifdef USE_USB_OTG_HS
- usb_printf("> USB OTG HS MSC Host\r\n");
- #else
- usb_printf("> USB OTG FS MSC Host\r\n");
- #endif
- usb_printf("> USB Host library started.\r\n");
- usb_printf(" USB Host Library v2.1.0\r\n");
- }
- }
- void USBH_USR_DeviceAttached(void)
- {
- usb_printf((char *)MSG_DEV_ATTACHED);
- }
- void USBH_USR_UnrecoveredError(void)
- {
- usb_printf((char *)MSG_UNREC_ERROR);
- }
- void USBH_USR_DeviceDisconnected(void)
- {
- usb_printf((char *)MSG_DEV_DISCONNECTED);
- }
- void USBH_USR_ResetDevice(void)
- {
-
- usb_printf("> USBH_USR_ResetDevice \r\n");
- }
- void USBH_USR_DeviceSpeedDetected(uint8_t DeviceSpeed)
- {
- if (DeviceSpeed == HPRT0_PRTSPD_HIGH_SPEED)
- {
- usb_printf((char *)MSG_DEV_HIGHSPEED);
- }
- else if (DeviceSpeed == HPRT0_PRTSPD_FULL_SPEED)
- {
- usb_printf((char *)MSG_DEV_FULLSPEED);
- }
- else if (DeviceSpeed == HPRT0_PRTSPD_LOW_SPEED)
- {
- usb_printf((char *)MSG_DEV_LOWSPEED);
- }
- else
- {
- usb_printf((char *)MSG_DEV_ERROR);
- }
- }
- void USBH_USR_Device_DescAvailable(void *DeviceDesc)
- {
- USBH_DevDesc_TypeDef *hs;
- hs = DeviceDesc;
- usb_printf("> VID : %04Xh\r\n", (uint32_t)(*hs).idVendor);
- usb_printf("> PID : %04Xh\r\n", (uint32_t)(*hs).idProduct);
- }
- void USBH_USR_DeviceAddressAssigned(void)
- {
- }
- void USBH_USR_Configuration_DescAvailable(USBH_CfgDesc_TypeDef *cfgDesc,
- USBH_InterfaceDesc_TypeDef *itfDesc,
- USBH_EpDesc_TypeDef *epDesc)
- {
- USBH_InterfaceDesc_TypeDef *id;
- id = itfDesc;
- if ((*id).bInterfaceClass == 0x08)
- {
- usb_printf((char *)MSG_MSC_CLASS);
- }
- else if ((*id).bInterfaceClass == 0x03)
- {
- usb_printf((char *)MSG_HID_CLASS);
- }
- }
- void USBH_USR_Manufacturer_String(void *ManufacturerString)
- {
- usb_printf("> Manufacturer : %sr\r\n", (char *)ManufacturerString);
- }
- void USBH_USR_Product_String(void *ProductString)
- {
- usb_printf("> Product : %s\r\n", (char *)ProductString);
- }
- void USBH_USR_SerialNum_String(void *SerialNumString)
- {
- usb_printf("> Serial Number : %s\r\n", (char *)SerialNumString);
- }
- void USBH_USR_EnumerationDone(void)
- {
-
- usb_printf((void *)MSG_DEV_ENUMERATED);
- }
- void USBH_USR_DeviceNotSupported(void)
- {
- usb_printf("> Device not supported.\r\n");
- }
- USBH_USR_Status USBH_USR_UserInput(void)
- {
- #if 1
-
- return USBH_USR_RESP_OK;
- #else
- USBH_USR_Status usbh_usr_status;
- usbh_usr_status = USBH_USR_NO_RESP;
- #if 0
-
- if(STM_EVAL_PBGetState(Button_KEY) == RESET)
- {
- usbh_usr_status = USBH_USR_RESP_OK;
- }
- #endif
- return usbh_usr_status;
- #endif
- }
- void USBH_USR_OverCurrentDetected(void)
- {
- usb_printf("> Overcurrent detected.\r\n");
- }
- int USBH_USR_MSC_Application(void)
- {
- FRESULT res;
- switch (USBH_USR_ApplicationState)
- {
- case USH_USR_FS_INIT:
- res = f_mount(&FatFs[2], "2:", 1);
- if (res == FR_NO_FILESYSTEM)
- {
- res = f_mkfs("2:", 0, 0);
- if (res == FR_OK)
- {
- usb_printf("USB format ok\r\n");
- res = f_mount(NULL, "2:", 1);
- res = f_mount(&FatFs[2], "2:", 1);
- }
- else
- {
- usb_printf("USB format fail\r\n");
- }
- }
- if (res != FR_OK)
- {
- usb_printf("!!!USB mount file system fail (%d)\r\n", res);
- return (-1);
- }
- usb_printf("USB file system mount ok !\r\n");
- USBH_USR_ApplicationState = USH_USR_FS_READLIST;
- break;
- case USH_USR_FS_READLIST:
- while (USB_OTG_Core.host.ConnSts == 0)
- ;
- USBH_USR_ApplicationState = USH_USR_FS_WRITEFILE;
- break;
- case USH_USR_FS_WRITEFILE:
- while (USB_OTG_Core.host.ConnSts == 0)
- ;
- USBH_USR_ApplicationState = USH_USR_FS_DRAW;
- break;
- case USH_USR_FS_DRAW:
- break;
- default:
- break;
- }
- return (0);
- }
- void USBH_USR_DeInit(void)
- {
- USBH_USR_ApplicationState = USH_USR_FS_INIT;
- }
|