usbh_usr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /**
  2. ******************************************************************************
  3. * @file usbh_usr.c
  4. * @author MCD Application Team
  5. * @version V2.1.0
  6. * @date 19-March-2012
  7. * @brief This file includes the usb host library user callbacks
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  12. *
  13. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14. * You may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at:
  16. *
  17. * http://www.st.com/software_license_agreement_liberty_v2
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. ******************************************************************************
  26. */
  27. /* Includes ------------------------------------------------------------------*/
  28. #include "usbh_usr.h"
  29. #include "bsp_fatfs.h"
  30. #include "ff.h" /* FATFS */
  31. #include "usbh_msc_bot.h"
  32. #include "usbh_msc_core.h"
  33. #include "usbh_msc_scsi.h"
  34. #include <string.h>
  35. #define usb_printf printf
  36. //#define usb_printf printf(...)
  37. USB_OTG_CORE_HANDLE USB_OTG_Core;
  38. USBH_HOST USB_Host;
  39. uint8_t USBH_USR_ApplicationState = USH_USR_FS_INIT;
  40. /* Points to the DEVICE_PROP structure of current device */
  41. /* The purpose of this register is to speed up the execution */
  42. USBH_Usr_cb_TypeDef USR_cb =
  43. {
  44. USBH_USR_Init,
  45. USBH_USR_DeInit,
  46. USBH_USR_DeviceAttached,
  47. USBH_USR_ResetDevice,
  48. USBH_USR_DeviceDisconnected,
  49. USBH_USR_OverCurrentDetected,
  50. USBH_USR_DeviceSpeedDetected,
  51. USBH_USR_Device_DescAvailable,
  52. USBH_USR_DeviceAddressAssigned,
  53. USBH_USR_Configuration_DescAvailable,
  54. USBH_USR_Manufacturer_String,
  55. USBH_USR_Product_String,
  56. USBH_USR_SerialNum_String,
  57. USBH_USR_EnumerationDone,
  58. USBH_USR_UserInput,
  59. USBH_USR_MSC_Application,
  60. USBH_USR_DeviceNotSupported,
  61. USBH_USR_UnrecoveredError};
  62. /**
  63. * @}
  64. */
  65. /** @defgroup USBH_USR_Private_Constants
  66. * @{
  67. */
  68. /*--------------- LCD Messages ---------------*/
  69. const uint8_t MSG_HOST_INIT[] = "> Host Library Initialized\r\n";
  70. const uint8_t MSG_DEV_ATTACHED[] = "> Device Attached \r\n";
  71. const uint8_t MSG_DEV_DISCONNECTED[] = "> Device Disconnected\r\n";
  72. const uint8_t MSG_DEV_ENUMERATED[] = "> Enumeration completed \r\n";
  73. const uint8_t MSG_DEV_HIGHSPEED[] = "> High speed device detected\r\n";
  74. const uint8_t MSG_DEV_FULLSPEED[] = "> Full speed device detected\r\n";
  75. const uint8_t MSG_DEV_LOWSPEED[] = "> Low speed device detected\r\n";
  76. const uint8_t MSG_DEV_ERROR[] = "> Device fault \r\n";
  77. const uint8_t MSG_MSC_CLASS[] = "> Mass storage device connected\r\n";
  78. const uint8_t MSG_HID_CLASS[] = "> HID device connected\r\n";
  79. const uint8_t MSG_DISK_SIZE[] = "> Size of the disk in MBytes: \r\n";
  80. const uint8_t MSG_LUN[] = "> LUN Available in the device:\r\n";
  81. const uint8_t MSG_ROOT_CONT[] = "> Exploring disk flash ...\r\n";
  82. const uint8_t MSG_WR_PROTECT[] = "> The disk is write protected\r\n";
  83. const uint8_t MSG_UNREC_ERROR[] = "> UNRECOVERED ERROR STATE\r\n";
  84. /**
  85. * @}
  86. */
  87. /** @defgroup USBH_USR_Private_FunctionPrototypes
  88. * @{
  89. */
  90. /**
  91. * @}
  92. */
  93. /** @defgroup USBH_USR_Private_Functions
  94. * @{
  95. */
  96. /**
  97. * @brief USBH_USR_Init
  98. * Displays the message on LCD for host lib initialization
  99. * @param None
  100. * @retval None
  101. */
  102. void USBH_USR_Init(void)
  103. {
  104. static uint8_t startup = 0;
  105. if (startup == 0)
  106. {
  107. startup = 1;
  108. #ifdef USE_USB_OTG_HS
  109. usb_printf("> USB OTG HS MSC Host\r\n");
  110. #else
  111. usb_printf("> USB OTG FS MSC Host\r\n");
  112. #endif
  113. usb_printf("> USB Host library started.\r\n");
  114. usb_printf(" USB Host Library v2.1.0\r\n");
  115. }
  116. }
  117. /**
  118. * @brief USBH_USR_DeviceAttached
  119. * Displays the message on LCD on device attached
  120. * @param None
  121. * @retval None
  122. */
  123. void USBH_USR_DeviceAttached(void)
  124. {
  125. usb_printf((char *)MSG_DEV_ATTACHED);
  126. }
  127. /**
  128. * @brief USBH_USR_UnrecoveredError
  129. * @param None
  130. * @retval None
  131. */
  132. void USBH_USR_UnrecoveredError(void)
  133. {
  134. usb_printf((char *)MSG_UNREC_ERROR);
  135. }
  136. /**
  137. * @brief USBH_DisconnectEvent
  138. * Device disconnect event
  139. * @param None
  140. * @retval Staus
  141. */
  142. void USBH_USR_DeviceDisconnected(void)
  143. {
  144. usb_printf((char *)MSG_DEV_DISCONNECTED);
  145. }
  146. /**
  147. * @brief USBH_USR_ResetUSBDevice
  148. * @param None
  149. * @retval None
  150. */
  151. void USBH_USR_ResetDevice(void)
  152. {
  153. /* callback for USB-Reset */
  154. usb_printf("> USBH_USR_ResetDevice \r\n");
  155. }
  156. /**
  157. * @brief USBH_USR_DeviceSpeedDetected
  158. * Displays the message on LCD for device speed
  159. * @param Device speed
  160. * @retval None
  161. */
  162. void USBH_USR_DeviceSpeedDetected(uint8_t DeviceSpeed)
  163. {
  164. if (DeviceSpeed == HPRT0_PRTSPD_HIGH_SPEED)
  165. {
  166. usb_printf((char *)MSG_DEV_HIGHSPEED);
  167. }
  168. else if (DeviceSpeed == HPRT0_PRTSPD_FULL_SPEED)
  169. {
  170. usb_printf((char *)MSG_DEV_FULLSPEED);
  171. }
  172. else if (DeviceSpeed == HPRT0_PRTSPD_LOW_SPEED)
  173. {
  174. usb_printf((char *)MSG_DEV_LOWSPEED);
  175. }
  176. else
  177. {
  178. usb_printf((char *)MSG_DEV_ERROR);
  179. }
  180. }
  181. /**
  182. * @brief USBH_USR_Device_DescAvailable
  183. * Displays the message on LCD for device descriptor
  184. * @param device descriptor
  185. * @retval None
  186. */
  187. void USBH_USR_Device_DescAvailable(void *DeviceDesc)
  188. {
  189. USBH_DevDesc_TypeDef *hs;
  190. hs = DeviceDesc;
  191. usb_printf("> VID : %04Xh\r\n", (uint32_t)(*hs).idVendor);
  192. usb_printf("> PID : %04Xh\r\n", (uint32_t)(*hs).idProduct);
  193. }
  194. /**
  195. * @brief USBH_USR_DeviceAddressAssigned
  196. * USB device is successfully assigned the Address
  197. * @param None
  198. * @retval None
  199. */
  200. void USBH_USR_DeviceAddressAssigned(void)
  201. {
  202. }
  203. /**
  204. * @brief USBH_USR_Conf_Desc
  205. * Displays the message on LCD for configuration descriptor
  206. * @param Configuration descriptor
  207. * @retval None
  208. */
  209. void USBH_USR_Configuration_DescAvailable(USBH_CfgDesc_TypeDef *cfgDesc,
  210. USBH_InterfaceDesc_TypeDef *itfDesc,
  211. USBH_EpDesc_TypeDef *epDesc)
  212. {
  213. USBH_InterfaceDesc_TypeDef *id;
  214. id = itfDesc;
  215. if ((*id).bInterfaceClass == 0x08)
  216. {
  217. usb_printf((char *)MSG_MSC_CLASS);
  218. }
  219. else if ((*id).bInterfaceClass == 0x03)
  220. {
  221. usb_printf((char *)MSG_HID_CLASS);
  222. }
  223. }
  224. /**
  225. * @brief USBH_USR_Manufacturer_String
  226. * Displays the message on LCD for Manufacturer String
  227. * @param Manufacturer String
  228. * @retval None
  229. */
  230. void USBH_USR_Manufacturer_String(void *ManufacturerString)
  231. {
  232. usb_printf("> Manufacturer : %sr\r\n", (char *)ManufacturerString);
  233. }
  234. /**
  235. * @brief USBH_USR_Product_String
  236. * Displays the message on LCD for Product String
  237. * @param Product String
  238. * @retval None
  239. */
  240. void USBH_USR_Product_String(void *ProductString)
  241. {
  242. usb_printf("> Product : %s\r\n", (char *)ProductString);
  243. }
  244. /**
  245. * @brief USBH_USR_SerialNum_String
  246. * Displays the message on LCD for SerialNum_String
  247. * @param SerialNum_String
  248. * @retval None
  249. */
  250. void USBH_USR_SerialNum_String(void *SerialNumString)
  251. {
  252. usb_printf("> Serial Number : %s\r\n", (char *)SerialNumString);
  253. }
  254. /**
  255. * @brief EnumerationDone
  256. * User response request is displayed to ask application jump to class
  257. * @param None
  258. * @retval None
  259. */
  260. void USBH_USR_EnumerationDone(void)
  261. {
  262. /* Enumeration complete */
  263. usb_printf((void *)MSG_DEV_ENUMERATED);
  264. }
  265. /**
  266. * @brief USBH_USR_DeviceNotSupported
  267. * Device is not supported
  268. * @param None
  269. * @retval None
  270. */
  271. void USBH_USR_DeviceNotSupported(void)
  272. {
  273. usb_printf("> Device not supported.\r\n");
  274. }
  275. /**
  276. * @brief USBH_USR_UserInput
  277. * User Action for application state entry
  278. * @param None
  279. * @retval USBH_USR_Status : User response for key button
  280. */
  281. USBH_USR_Status USBH_USR_UserInput(void)
  282. {
  283. #if 1
  284. /* HOST_ENUMERATION 和 HOST_CLASS_REQUEST
  285. 在枚举成功和类请求之间等待用户输入。
  286. 此处直接返回OK,无需等待。
  287. */
  288. return USBH_USR_RESP_OK;
  289. #else
  290. USBH_USR_Status usbh_usr_status;
  291. usbh_usr_status = USBH_USR_NO_RESP;
  292. #if 0
  293. /*Key B3 is in polling mode to detect user action */
  294. if(STM_EVAL_PBGetState(Button_KEY) == RESET)
  295. {
  296. usbh_usr_status = USBH_USR_RESP_OK;
  297. }
  298. #endif
  299. return usbh_usr_status;
  300. #endif
  301. }
  302. /**
  303. * @brief USBH_USR_OverCurrentDetected
  304. * Over Current Detected on VBUS
  305. * @param None
  306. * @retval Staus
  307. */
  308. void USBH_USR_OverCurrentDetected(void)
  309. {
  310. usb_printf("> Overcurrent detected.\r\n");
  311. }
  312. /**
  313. * @brief USBH_USR_MSC_Application
  314. * Demo application for mass storage
  315. * @param None
  316. * @retval Staus
  317. */
  318. int USBH_USR_MSC_Application(void)
  319. {
  320. FRESULT res; /* 文件操作结果 */
  321. switch (USBH_USR_ApplicationState)
  322. {
  323. case USH_USR_FS_INIT:
  324. res = f_mount(&FatFs[2], "2:", 1); // 文件系统挂载时会对NAND初始化
  325. if (res == FR_NO_FILESYSTEM) // TF 卡没有文件系统
  326. {
  327. res = f_mkfs("2:", 0, 0); // 格式化
  328. if (res == FR_OK)
  329. {
  330. usb_printf("USB format ok\r\n");
  331. res = f_mount(NULL, "2:", 1); /* 格式化后,先取消挂载 */
  332. res = f_mount(&FatFs[2], "2:", 1); /* 重新挂载 */
  333. }
  334. else
  335. {
  336. usb_printf("USB format fail\r\n");
  337. }
  338. }
  339. if (res != FR_OK)
  340. {
  341. usb_printf("!!!USB mount file system fail (%d)\r\n", res);
  342. return (-1);
  343. }
  344. usb_printf("USB file system mount ok !\r\n");
  345. USBH_USR_ApplicationState = USH_USR_FS_READLIST;
  346. break;
  347. case USH_USR_FS_READLIST:
  348. while (USB_OTG_Core.host.ConnSts == 0)
  349. ;
  350. USBH_USR_ApplicationState = USH_USR_FS_WRITEFILE;
  351. break;
  352. case USH_USR_FS_WRITEFILE:
  353. while (USB_OTG_Core.host.ConnSts == 0)
  354. ;
  355. USBH_USR_ApplicationState = USH_USR_FS_DRAW;
  356. break;
  357. case USH_USR_FS_DRAW:
  358. break;
  359. default:
  360. break;
  361. }
  362. return (0);
  363. }
  364. /**
  365. * @brief USBH_USR_DeInit
  366. * Deint User state and associated variables
  367. * @param None
  368. * @retval None
  369. */
  370. void USBH_USR_DeInit(void)
  371. {
  372. USBH_USR_ApplicationState = USH_USR_FS_INIT;
  373. }
  374. /**
  375. * @}
  376. */
  377. /**
  378. * @}
  379. */
  380. /**
  381. * @}
  382. */
  383. /**
  384. * @}
  385. */
  386. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/