usb_device_dci.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  1. /*
  2. * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016 - 2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <usb/include/usb_device_config.h>
  31. #include <usb/include/usb.h>
  32. #include "usb_device.h"
  33. #include "usb_device_dci.h"
  34. #include "fsl_device_registers.h"
  35. #if ((defined(USB_DEVICE_CONFIG_NUM)) && (USB_DEVICE_CONFIG_NUM > 0U))
  36. #if ((defined(USB_DEVICE_CONFIG_KHCI)) && (USB_DEVICE_CONFIG_KHCI > 0U))
  37. #include "usb_device_khci.h"
  38. #endif
  39. #if ((defined(USB_DEVICE_CONFIG_EHCI)) && (USB_DEVICE_CONFIG_EHCI > 0U))
  40. #include "usb_device_ehci.h"
  41. #endif
  42. #if (((defined(USB_DEVICE_CONFIG_LPCIP3511FS)) && (USB_DEVICE_CONFIG_LPCIP3511FS > 0U)) || \
  43. ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U)))
  44. #include "usb_device_lpcip3511.h"
  45. #endif
  46. #if (defined(USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
  47. #include "fsl_cache.h"
  48. #endif
  49. /*******************************************************************************
  50. * Definitions
  51. ******************************************************************************/
  52. /*******************************************************************************
  53. * Prototypes
  54. ******************************************************************************/
  55. static usb_status_t USB_DeviceAllocateHandle(uint8_t controllerId, usb_device_struct_t **handle);
  56. static usb_status_t USB_DeviceFreeHandle(usb_device_struct_t *handle);
  57. static usb_status_t USB_DeviceGetControllerInterface(
  58. uint8_t controllerId, const usb_device_controller_interface_struct_t **controllerInterface);
  59. static usb_status_t USB_DeviceTransfer(usb_device_handle handle,
  60. uint8_t endpointAddress,
  61. uint8_t *buffer,
  62. uint32_t length);
  63. static usb_status_t USB_DeviceControl(usb_device_handle handle, usb_device_control_type_t type, void *param);
  64. static usb_status_t USB_DeviceResetNotification(usb_device_struct_t *handle,
  65. usb_device_callback_message_struct_t *message);
  66. #if (defined(USB_DEVICE_CONFIG_LOW_POWER_MODE) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
  67. static usb_status_t USB_DeviceSuspendNotification(usb_device_struct_t *handle,
  68. usb_device_callback_message_struct_t *message);
  69. static usb_status_t USB_DeviceResumeNotification(usb_device_struct_t *handle,
  70. usb_device_callback_message_struct_t *message);
  71. #if (defined(USB_DEVICE_CONFIG_LPM_L1) && (USB_DEVICE_CONFIG_LPM_L1 > 0U))
  72. static usb_status_t USB_DeviceSleepNotification(usb_device_struct_t *handle,
  73. usb_device_callback_message_struct_t *message);
  74. #endif
  75. #endif /* USB_DEVICE_CONFIG_LOW_POWER_MODE */
  76. #if (defined(USB_DEVICE_CONFIG_DETACH_ENABLE) && (USB_DEVICE_CONFIG_DETACH_ENABLE > 0U))
  77. static usb_status_t USB_DeviceDetachNotification(usb_device_struct_t *handle,
  78. usb_device_callback_message_struct_t *message);
  79. static usb_status_t USB_DeviceAttachNotification(usb_device_struct_t *handle,
  80. usb_device_callback_message_struct_t *message);
  81. #endif
  82. static usb_status_t USB_DeviceNotification(usb_device_struct_t *handle, usb_device_callback_message_struct_t *message);
  83. /*******************************************************************************
  84. * Variables
  85. ******************************************************************************/
  86. USB_GLOBAL static usb_device_struct_t s_UsbDevice[USB_DEVICE_CONFIG_NUM];
  87. /*******************************************************************************
  88. * Code
  89. ******************************************************************************/
  90. /*!
  91. * @brief Allocate a device handle.
  92. *
  93. * This function allocates a device handle.
  94. *
  95. * @param controllerId The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
  96. * @param handle It is out parameter, is used to return pointer of the device handle to the caller.
  97. *
  98. * @retval kStatus_USB_Success Get a device handle successfully.
  99. * @retval kStatus_USB_Busy Cannot allocate a device handle.
  100. * @retval kStatus_USB_Error The device has been initialized.
  101. */
  102. static usb_status_t USB_DeviceAllocateHandle(uint8_t controllerId, usb_device_struct_t **handle)
  103. {
  104. uint32_t count;
  105. int i = 0;
  106. for(i=0;i<USB_DEVICE_CONFIG_NUM;i++)
  107. {
  108. rt_memset(&s_UsbDevice[i],0,sizeof(s_UsbDevice[i]));
  109. }
  110. USB_OSA_SR_ALLOC();
  111. USB_OSA_ENTER_CRITICAL();
  112. /* Check the controller is initialized or not. */
  113. for (count = 0U; count < USB_DEVICE_CONFIG_NUM; count++)
  114. {
  115. if ((NULL != s_UsbDevice[count].controllerHandle) && (controllerId == s_UsbDevice[count].controllerId))
  116. {
  117. USB_OSA_EXIT_CRITICAL();
  118. return kStatus_USB_Error;
  119. }
  120. }
  121. /* Get a free device handle. */
  122. for (count = 0U; count < USB_DEVICE_CONFIG_NUM; count++)
  123. {
  124. if (NULL == s_UsbDevice[count].controllerHandle)
  125. {
  126. s_UsbDevice[count].controllerId = controllerId;
  127. *handle = &s_UsbDevice[count];
  128. USB_OSA_EXIT_CRITICAL();
  129. return kStatus_USB_Success;
  130. }
  131. }
  132. USB_OSA_EXIT_CRITICAL();
  133. return kStatus_USB_Busy;
  134. }
  135. /*!
  136. * @brief Free a device handle.
  137. *
  138. * This function frees a device handle.
  139. *
  140. * @param handle The device handle.
  141. *
  142. * @retval kStatus_USB_Success Free device handle successfully.
  143. */
  144. static usb_status_t USB_DeviceFreeHandle(usb_device_struct_t *handle)
  145. {
  146. USB_OSA_SR_ALLOC();
  147. USB_OSA_ENTER_CRITICAL();
  148. handle->controllerHandle = NULL;
  149. handle->controllerId = 0U;
  150. USB_OSA_EXIT_CRITICAL();
  151. return kStatus_USB_Success;
  152. }
  153. #if ((defined(USB_DEVICE_CONFIG_KHCI)) && (USB_DEVICE_CONFIG_KHCI > 0U))
  154. /* KHCI device driver interface */
  155. static const usb_device_controller_interface_struct_t s_UsbDeviceKhciInterface = {
  156. USB_DeviceKhciInit, USB_DeviceKhciDeinit, USB_DeviceKhciSend,
  157. USB_DeviceKhciRecv, USB_DeviceKhciCancel, USB_DeviceKhciControl
  158. };
  159. #endif
  160. #if ((defined(USB_DEVICE_CONFIG_EHCI)) && (USB_DEVICE_CONFIG_EHCI > 0U))
  161. /* EHCI device driver interface */
  162. static const usb_device_controller_interface_struct_t s_UsbDeviceEhciInterface = {
  163. USB_DeviceEhciInit, USB_DeviceEhciDeinit, USB_DeviceEhciSend,
  164. USB_DeviceEhciRecv, USB_DeviceEhciCancel, USB_DeviceEhciControl
  165. };
  166. #endif
  167. #if (((defined(USB_DEVICE_CONFIG_LPCIP3511FS)) && (USB_DEVICE_CONFIG_LPCIP3511FS > 0U)) || \
  168. ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U)))
  169. /* EHCI device driver interface */
  170. static const usb_device_controller_interface_struct_t s_UsbDeviceLpc3511IpInterface = {
  171. USB_DeviceLpc3511IpInit, USB_DeviceLpc3511IpDeinit, USB_DeviceLpc3511IpSend,
  172. USB_DeviceLpc3511IpRecv, USB_DeviceLpc3511IpCancel, USB_DeviceLpc3511IpControl
  173. };
  174. #endif
  175. /*!
  176. * @brief Get the controller interface handle.
  177. *
  178. * This function is used to get the controller interface handle.
  179. *
  180. * @param controllerId The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
  181. * @param controllerInterface It is out parameter, is used to return pointer of the device controller handle to the
  182. * caller.
  183. *
  184. * @retval kStatus_USB_Success Get a device handle successfully.
  185. * @retval kStatus_USB_ControllerNotFound The controller id is invalided.
  186. */
  187. static usb_status_t USB_DeviceGetControllerInterface(
  188. uint8_t controllerId, const usb_device_controller_interface_struct_t **controllerInterface)
  189. {
  190. usb_status_t error = kStatus_USB_ControllerNotFound;
  191. switch (controllerId)
  192. {
  193. #if ((defined(USB_DEVICE_CONFIG_KHCI)) && (USB_DEVICE_CONFIG_KHCI > 0U))
  194. /* Get the KHCI controller driver interface */
  195. case kUSB_ControllerKhci0:
  196. case kUSB_ControllerKhci1:
  197. *controllerInterface = (const usb_device_controller_interface_struct_t *)&s_UsbDeviceKhciInterface;
  198. error = kStatus_USB_Success;
  199. break;
  200. #endif
  201. #if ((defined(USB_DEVICE_CONFIG_EHCI)) && (USB_DEVICE_CONFIG_EHCI > 0U))
  202. /* Get the EHCI controller driver interface */
  203. case kUSB_ControllerEhci0:
  204. case kUSB_ControllerEhci1:
  205. error = kStatus_USB_Success;
  206. *controllerInterface = (const usb_device_controller_interface_struct_t *)&s_UsbDeviceEhciInterface;
  207. break;
  208. #endif
  209. #if (((defined(USB_DEVICE_CONFIG_LPCIP3511FS)) && (USB_DEVICE_CONFIG_LPCIP3511FS > 0U)) || \
  210. ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U)))
  211. /* Get the EHCI controller driver interface */
  212. case kUSB_ControllerLpcIp3511Fs0:
  213. case kUSB_ControllerLpcIp3511Fs1:
  214. case kUSB_ControllerLpcIp3511Hs0:
  215. case kUSB_ControllerLpcIp3511Hs1:
  216. error = kStatus_USB_Success;
  217. *controllerInterface = (const usb_device_controller_interface_struct_t *)&s_UsbDeviceLpc3511IpInterface;
  218. break;
  219. #endif
  220. default:
  221. break;
  222. }
  223. return error;
  224. }
  225. /*!
  226. * @brief Start a new transfer.
  227. *
  228. * This function is used to start a new transfer.
  229. *
  230. * @param handle The device handle. It equals the value returned from USB_DeviceInit.
  231. * @param endpointAddress Endpoint address. Bit7 is direction, 0U - USB_OUT, 1U - USB_IN.
  232. * @param buffer The memory address to be transferred, or the memory address to hold the data need to be
  233. * sent.
  234. * @param length The length of the data.
  235. *
  236. * @retval kStatus_USB_Success Get a device handle successfully.
  237. * @retval kStatus_USB_InvalidHandle The device handle is invalided.
  238. * @retval kStatus_USB_ControllerNotFound The controller interface is not found.
  239. * @retval kStatus_USB_Error The device is doing reset.
  240. */
  241. static usb_status_t USB_DeviceTransfer(usb_device_handle handle,
  242. uint8_t endpointAddress,
  243. uint8_t *buffer,
  244. uint32_t length)
  245. {
  246. usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
  247. usb_status_t error = kStatus_USB_Error;
  248. uint8_t endpoint = endpointAddress & USB_ENDPOINT_NUMBER_MASK;
  249. uint8_t direction = (endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >>
  250. USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT;
  251. USB_OSA_SR_ALLOC();
  252. if (NULL == deviceHandle)
  253. {
  254. return kStatus_USB_InvalidHandle;
  255. }
  256. if (NULL != deviceHandle->controllerInterface)
  257. {
  258. if (deviceHandle->epCallback[(uint8_t)((uint32_t)endpoint << 1U) | direction].isBusy)
  259. {
  260. return kStatus_USB_Busy;
  261. }
  262. USB_OSA_ENTER_CRITICAL();
  263. deviceHandle->epCallback[(uint8_t)((uint32_t)endpoint << 1U) | direction].isBusy = 1U;
  264. USB_OSA_EXIT_CRITICAL();
  265. if (endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK)
  266. {
  267. #if (defined(USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
  268. if (length)
  269. {
  270. DCACHE_CleanByRange((uint32_t)buffer, length);
  271. }
  272. #endif
  273. /* Call the controller send interface. */
  274. error = deviceHandle->controllerInterface->deviceSend(deviceHandle->controllerHandle, endpointAddress,
  275. buffer, length);
  276. }
  277. else
  278. {
  279. #if (defined(USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
  280. if (length)
  281. {
  282. DCACHE_CleanInvalidateByRange((uint32_t)buffer, length);
  283. }
  284. #endif
  285. /* Call the controller receive interface. */
  286. error = deviceHandle->controllerInterface->deviceRecv(deviceHandle->controllerHandle, endpointAddress,
  287. buffer, length);
  288. }
  289. if (kStatus_USB_Success != error)
  290. {
  291. USB_OSA_ENTER_CRITICAL();
  292. deviceHandle->epCallback[(uint8_t)((uint32_t)endpoint << 1U) | direction].isBusy = 0U;
  293. USB_OSA_EXIT_CRITICAL();
  294. }
  295. }
  296. else
  297. {
  298. error = kStatus_USB_ControllerNotFound;
  299. }
  300. return error;
  301. }
  302. /*!
  303. * @brief Control the status of the selected item.
  304. *
  305. * This function is used to control the status of the selected item..
  306. *
  307. * @param handle The device handle. It equals the value returned from USB_DeviceInit.
  308. * @param type The control type, please refer to the enumeration usb_device_control_type_t.
  309. * @param param The param type is determined by the selected item.
  310. *
  311. * @retval kStatus_USB_Success Get a device handle successfully.
  312. * @retval kStatus_USB_InvalidHandle The device handle is invalided.
  313. * @retval kStatus_USB_ControllerNotFound The controller interface is not found.
  314. * @retval kStatus_USB_Error Unsupport type.
  315. * Or, the param is NULL pointer.
  316. */
  317. static usb_status_t USB_DeviceControl(usb_device_handle handle, usb_device_control_type_t type, void *param)
  318. {
  319. usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
  320. usb_status_t error = kStatus_USB_Error;
  321. if (NULL == deviceHandle)
  322. {
  323. return kStatus_USB_InvalidHandle;
  324. }
  325. if (NULL != deviceHandle->controllerInterface)
  326. {
  327. /* Call the controller control interface. */
  328. error = deviceHandle->controllerInterface->deviceControl(deviceHandle->controllerHandle, type, param);
  329. }
  330. else
  331. {
  332. error = kStatus_USB_ControllerNotFound;
  333. }
  334. return error;
  335. }
  336. /*!
  337. * @brief Handle the reset notification.
  338. *
  339. * This function is used to handle the reset notification.
  340. *
  341. * @param handle The device handle. It equals the value returned from USB_DeviceInit.
  342. * @param message The device callback message handle.
  343. *
  344. * @retval kStatus_USB_Success Get a device handle successfully.
  345. */
  346. static usb_status_t USB_DeviceResetNotification(usb_device_struct_t *handle,
  347. usb_device_callback_message_struct_t *message)
  348. {
  349. #if (defined(USB_DEVICE_CONFIG_USE_TASK) && (USB_DEVICE_CONFIG_USE_TASK > 0U))
  350. USB_OSA_SR_ALLOC();
  351. #endif
  352. handle->isResetting = 1U;
  353. #if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U))
  354. /* Clear remote wakeup feature */
  355. handle->remotewakeup = 0U;
  356. #endif
  357. #if (defined(USB_DEVICE_CONFIG_USE_TASK) && (USB_DEVICE_CONFIG_USE_TASK > 0U))
  358. USB_OSA_ENTER_CRITICAL();
  359. handle->epCallbackDirectly = 1;
  360. USB_OSA_EXIT_CRITICAL();
  361. #endif
  362. /* Set the controller to default status. */
  363. USB_DeviceControl(handle, kUSB_DeviceControlSetDefaultStatus, NULL);
  364. #if (defined(USB_DEVICE_CONFIG_USE_TASK) && (USB_DEVICE_CONFIG_USE_TASK > 0U))
  365. USB_OSA_ENTER_CRITICAL();
  366. handle->epCallbackDirectly = 0;
  367. USB_OSA_EXIT_CRITICAL();
  368. #endif
  369. handle->state = kUSB_DeviceStateDefault;
  370. handle->deviceAddress = 0U;
  371. for (uint32_t count = 0U; count < (USB_DEVICE_CONFIG_ENDPOINTS * 2U); count++)
  372. {
  373. handle->epCallback[count].callbackFn = (usb_device_endpoint_callback_t)NULL;
  374. handle->epCallback[count].callbackParam = NULL;
  375. handle->epCallback[count].isBusy = 0U;
  376. }
  377. /* Call device callback to notify the application that the USB bus reset signal detected. */
  378. handle->deviceCallback(handle, kUSB_DeviceEventBusReset, NULL);
  379. handle->isResetting = 0U;
  380. return kStatus_USB_Success;
  381. }
  382. #if (defined(USB_DEVICE_CONFIG_LOW_POWER_MODE) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
  383. /*!
  384. * @brief Handle the suspend notification.
  385. *
  386. * This function is used to handle the suspend notification.
  387. *
  388. * @param handle The device handle. It equals the value returned from USB_DeviceInit.
  389. * @param message The device callback message handle.
  390. *
  391. * @return A USB error code or kStatus_USB_Success.
  392. */
  393. static usb_status_t USB_DeviceSuspendNotification(usb_device_struct_t *handle,
  394. usb_device_callback_message_struct_t *message)
  395. {
  396. /* Call device callback to notify the application that the USB bus suspend signal detected. */
  397. return handle->deviceCallback(handle, kUSB_DeviceEventSuspend, NULL);
  398. }
  399. /*!
  400. * @brief Handle the resume notification.
  401. *
  402. * This function is used to handle the resume notification.
  403. *
  404. * @param handle The device handle. It equals the value returned from USB_DeviceInit.
  405. * @param message The device callback message handle.
  406. *
  407. * @return A USB error code or kStatus_USB_Success.
  408. */
  409. static usb_status_t USB_DeviceResumeNotification(usb_device_struct_t *handle,
  410. usb_device_callback_message_struct_t *message)
  411. {
  412. /* Call device callback to notify the application that the USB bus resume signal detected. */
  413. return handle->deviceCallback(handle, kUSB_DeviceEventResume, NULL);
  414. }
  415. #if (defined(USB_DEVICE_CONFIG_LPM_L1) && (USB_DEVICE_CONFIG_LPM_L1 > 0U))
  416. /*!
  417. * @brief Handle the suspend notification.
  418. *
  419. * This function is used to handle the suspend notification.
  420. *
  421. * @param handle The device handle. It equals the value returned from USB_DeviceInit.
  422. * @param message The device callback message handle.
  423. *
  424. * @return A USB error code or kStatus_USB_Success.
  425. */
  426. static usb_status_t USB_DeviceSleepNotification(usb_device_struct_t *handle,
  427. usb_device_callback_message_struct_t *message)
  428. {
  429. /* Call device callback to notify the application that the USB bus suspend signal detected. */
  430. return handle->deviceCallback(handle, kUSB_DeviceEventSleeped, NULL);
  431. }
  432. #endif
  433. /*!
  434. * @brief Handle the remotewakeup notification.
  435. *
  436. * This function is used to handle the remotewakeup notification.
  437. *
  438. * @param handle The device handle. It equals the value returned from USB_DeviceInit.
  439. * @param flag The buffer pointer to store remotewakeup flag.
  440. *
  441. * @return A USB error code or kStatus_USB_Success.
  442. */
  443. usb_status_t USB_DeviceGetRemoteWakeUp(usb_device_struct_t *handle, uint8_t **flag)
  444. {
  445. /* Call device callback to notify the application that the USB bus suspend signal detected. */
  446. return USB_DeviceControl(handle, kUSB_DeviceControlGetRemoteWakeUp, flag);
  447. }
  448. #endif /* USB_DEVICE_CONFIG_LOW_POWER_MODE */
  449. #if (defined(USB_DEVICE_CONFIG_ERROR_HANDLING) && (USB_DEVICE_CONFIG_ERROR_HANDLING > 0U))
  450. usb_status_t USB_DeviceErrorNotification(usb_device_struct_t *handle, usb_device_callback_message_struct_t *message)
  451. {
  452. /* Call device callback to notify the application that the USB bus error signal detected. */
  453. return handle->deviceCallback(handle, kUSB_DeviceEventError, NULL);
  454. }
  455. #endif /* USB_DEVICE_CONFIG_ERROR_HANDLING */
  456. #if (defined(USB_DEVICE_CONFIG_DETACH_ENABLE) && (USB_DEVICE_CONFIG_DETACH_ENABLE > 0U))
  457. /*!
  458. * @brief Handle the detach notification.
  459. *
  460. * This function is used to handle the detach notification.
  461. *
  462. * @param handle The device handle. It equals the value returned from USB_DeviceInit.
  463. * @param message The device callback message handle.
  464. *
  465. * @return A USB error code or kStatus_USB_Success.
  466. */
  467. static usb_status_t USB_DeviceDetachNotification(usb_device_struct_t *handle,
  468. usb_device_callback_message_struct_t *message)
  469. {
  470. /* Call device callback to notify the application that the device is disconnected from a host. */
  471. return handle->deviceCallback(handle, kUSB_DeviceEventDetach, NULL);
  472. }
  473. /*!
  474. * @brief Handle the attach notification.
  475. *
  476. * This function is used to handle the attach notification.
  477. *
  478. * @param handle The device handle. It equals the value returned from USB_DeviceInit.
  479. * @param message The device callback message handle.
  480. *
  481. * @return A USB error code or kStatus_USB_Success.
  482. */
  483. static usb_status_t USB_DeviceAttachNotification(usb_device_struct_t *handle,
  484. usb_device_callback_message_struct_t *message)
  485. {
  486. /* Call device callback to notify the application that the device is connected to a host. */
  487. return handle->deviceCallback(handle, kUSB_DeviceEventAttach, NULL);
  488. }
  489. #endif
  490. #if (defined(USB_DEVICE_CHARGER_DETECT_ENABLE) && (USB_DEVICE_CHARGER_DETECT_ENABLE > 0U)) && \
  491. ((defined(FSL_FEATURE_SOC_USBDCD_COUNT) && (FSL_FEATURE_SOC_USBDCD_COUNT > 0U)) || \
  492. (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U)))
  493. /*!
  494. * @brief Handle the dcd module timeout notification.
  495. *
  496. * This function is used to handle the dcd module timeout notification.
  497. *
  498. * @param handle The device handle. It equals the value returned from USB_DeviceInit.
  499. * @param message The device callback message handle.
  500. *
  501. * @return A USB error code or kStatus_USB_Success.
  502. */
  503. static usb_status_t USB_DeviceDcdTimeOutNotification(usb_device_struct_t *handle,
  504. usb_device_callback_message_struct_t *message)
  505. {
  506. /* Call device callback to notify the application that the device charger detect timeout happened. */
  507. return handle->deviceCallback(handle, kUSB_DeviceEventDcdTimeOut, NULL);
  508. }
  509. /*!
  510. * @brief Handle the dcd module unknown port type notification.
  511. *
  512. * This function is used to handle the dcd module unknown port type notification.
  513. *
  514. * @param handle The device handle. It equals the value returned from USB_DeviceInit.
  515. * @param message The device callback message handle.
  516. *
  517. * @return A USB error code or kStatus_USB_Success.
  518. */
  519. static usb_status_t USB_DeviceDcdUnknownPortTypeNotification(usb_device_struct_t *handle,
  520. usb_device_callback_message_struct_t *message)
  521. {
  522. /* Call device callback to notify the application that the device charger detect unknown port type happened. */
  523. return handle->deviceCallback(handle, kUSB_DeviceEventDcdUnknownType, NULL);
  524. }
  525. /*!
  526. * @brief Handle the SDP facility is detected notification.
  527. *
  528. * This function is used to handle the SDP facility is detectednotification.
  529. *
  530. * @param handle The device handle. It equals the value returned from USB_DeviceInit.
  531. * @param message The device callback message handle.
  532. *
  533. * @return A USB error code or kStatus_USB_Success.
  534. */
  535. static usb_status_t USB_DeviceDcdSDPDetectNotification(usb_device_struct_t *handle,
  536. usb_device_callback_message_struct_t *message)
  537. {
  538. /* Call device callback to notify the application that the SDP facility is detected. */
  539. return handle->deviceCallback(handle, kUSB_DeviceEventSDPDetected, NULL);
  540. }
  541. /*!
  542. * @brief Handle the charging port is detected notification.
  543. *
  544. * This function is used to handle the charging port is detectednotification.
  545. *
  546. * @param handle The device handle. It equals the value returned from USB_DeviceInit.
  547. * @param message The device callback message handle.
  548. *
  549. * @return A USB error code or kStatus_USB_Success.
  550. */
  551. static usb_status_t USB_DeviceDcdChargingPortDetectNotification(usb_device_struct_t *handle,
  552. usb_device_callback_message_struct_t *message)
  553. {
  554. /* Call device callback to notify the application that the charing port is detected. */
  555. return handle->deviceCallback(handle, kUSB_DeviceEventChargingPortDetected, NULL);
  556. }
  557. /*!
  558. * @brief Handle the CDP facility is detected notification.
  559. *
  560. * This function is used to handle the CDP facility is detectednotification.
  561. *
  562. * @param handle The device handle. It equals the value returned from USB_DeviceInit.
  563. * @param message The device callback message handle.
  564. *
  565. * @return A USB error code or kStatus_USB_Success.
  566. */
  567. static usb_status_t USB_DeviceDcdChargingHostDetectNotification(usb_device_struct_t *handle,
  568. usb_device_callback_message_struct_t *message)
  569. {
  570. /* Call device callback to notify the application that the CDP facility is detected. */
  571. return handle->deviceCallback(handle, kUSB_DeviceEventChargingHostDetected, NULL);
  572. }
  573. /*!
  574. * @brief Handle the DCP facility is detected notification.
  575. *
  576. * This function is used to handle the DCP facility is detectednotification.
  577. *
  578. * @param handle The device handle. It equals the value returned from USB_DeviceInit.
  579. * @param message The device callback message handle.
  580. *
  581. * @return A USB error code or kStatus_USB_Success.
  582. */
  583. static usb_status_t USB_DeviceDcdDedicatedChargerDetectNotification(usb_device_struct_t *handle,
  584. usb_device_callback_message_struct_t *message)
  585. {
  586. /* Call device callback to notify the application that the DCP facility is detected. */
  587. return handle->deviceCallback(handle, kUSB_DeviceEventDedicatedChargerDetected, NULL);
  588. }
  589. #endif
  590. /*!
  591. * @brief Handle the attach notification.
  592. *
  593. * This function is used to handle the attach notification.
  594. *
  595. * @param handle The device handle. It equals the value returned from USB_DeviceInit.
  596. * @param message The device callback message handle.
  597. *
  598. * @return A USB error code or kStatus_USB_Success.
  599. */
  600. static usb_status_t USB_DeviceNotification(usb_device_struct_t *handle, usb_device_callback_message_struct_t *message)
  601. {
  602. uint8_t endpoint = message->code & USB_ENDPOINT_NUMBER_MASK;
  603. uint8_t direction = (message->code & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >>
  604. USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT;
  605. usb_status_t error = kStatus_USB_Error;
  606. switch (message->code)
  607. {
  608. case kUSB_DeviceNotifyBusReset:
  609. error = USB_DeviceResetNotification(handle, message);
  610. break;
  611. #if (defined(USB_DEVICE_CONFIG_LOW_POWER_MODE) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
  612. case kUSB_DeviceNotifySuspend:
  613. error = USB_DeviceSuspendNotification(handle, message);
  614. break;
  615. case kUSB_DeviceNotifyResume:
  616. error = USB_DeviceResumeNotification(handle, message);
  617. break;
  618. #if (defined(USB_DEVICE_CONFIG_LPM_L1) && (USB_DEVICE_CONFIG_LPM_L1 > 0U))
  619. case kUSB_DeviceNotifyLPMSleep:
  620. error = USB_DeviceSleepNotification(handle, message);
  621. break;
  622. #endif
  623. #endif
  624. #if (defined(USB_DEVICE_CONFIG_ERROR_HANDLING) && (USB_DEVICE_CONFIG_ERROR_HANDLING > 0U))
  625. case kUSB_DeviceNotifyError:
  626. error = USB_DeviceErrorNotification(handle, message);
  627. break;
  628. #endif
  629. #if USB_DEVICE_CONFIG_DETACH_ENABLE
  630. case kUSB_DeviceNotifyDetach:
  631. error = USB_DeviceDetachNotification(handle, message);
  632. break;
  633. case kUSB_DeviceNotifyAttach:
  634. error = USB_DeviceAttachNotification(handle, message);
  635. break;
  636. #endif
  637. #if (defined(USB_DEVICE_CHARGER_DETECT_ENABLE) && (USB_DEVICE_CHARGER_DETECT_ENABLE > 0U)) && \
  638. ((defined(FSL_FEATURE_SOC_USBDCD_COUNT) && (FSL_FEATURE_SOC_USBDCD_COUNT > 0U)) || \
  639. (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U)))
  640. case kUSB_DeviceNotifyDcdTimeOut:
  641. error = USB_DeviceDcdTimeOutNotification(handle, message);
  642. break;
  643. case kUSB_DeviceNotifyDcdUnknownPortType:
  644. error = USB_DeviceDcdUnknownPortTypeNotification(handle, message);
  645. break;
  646. case kUSB_DeviceNotifySDPDetected:
  647. error = USB_DeviceDcdSDPDetectNotification(handle, message);
  648. break;
  649. case kUSB_DeviceNotifyChargingPortDetected:
  650. error = USB_DeviceDcdChargingPortDetectNotification(handle, message);
  651. break;
  652. case kUSB_DeviceNotifyChargingHostDetected:
  653. error = USB_DeviceDcdChargingHostDetectNotification(handle, message);
  654. break;
  655. case kUSB_DeviceNotifyDedicatedChargerDetected:
  656. error = USB_DeviceDcdDedicatedChargerDetectNotification(handle, message);
  657. break;
  658. #endif
  659. default:
  660. if (endpoint < USB_DEVICE_CONFIG_ENDPOINTS)
  661. {
  662. if (handle->epCallback[(uint8_t)((uint32_t)endpoint << 1U) | direction].callbackFn)
  663. {
  664. usb_device_endpoint_callback_message_struct_t endpointCallbackMessage;
  665. endpointCallbackMessage.buffer = message->buffer;
  666. endpointCallbackMessage.length = message->length;
  667. endpointCallbackMessage.isSetup = message->isSetup;
  668. if (message->isSetup)
  669. {
  670. handle->epCallback[0].isBusy = 0U;
  671. handle->epCallback[1].isBusy = 0U;
  672. }
  673. else
  674. {
  675. handle->epCallback[(uint8_t)((uint32_t)endpoint << 1U) | direction].isBusy = 0U;
  676. }
  677. /* Call endpoint callback */
  678. error = handle->epCallback[(uint8_t)((uint32_t)endpoint << 1U) | direction].callbackFn(
  679. handle, &endpointCallbackMessage,
  680. handle->epCallback[(uint8_t)((uint32_t)endpoint << 1U) | direction].callbackParam);
  681. }
  682. }
  683. break;
  684. }
  685. return error;
  686. }
  687. /*!
  688. * @brief Notify the device that the controller status changed.
  689. *
  690. * This function is used to notify the device that the controller status changed.
  691. *
  692. * @param handle The device handle. It equals the value returned from USB_DeviceInit.
  693. * @param message The device callback message handle.
  694. *
  695. * @return A USB error code or kStatus_USB_Success.
  696. */
  697. usb_status_t USB_DeviceNotificationTrigger(void *handle, void *msg)
  698. {
  699. usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
  700. usb_device_callback_message_struct_t *message = (usb_device_callback_message_struct_t *)msg;
  701. if ((NULL == msg) || (NULL == handle))
  702. {
  703. return kStatus_USB_InvalidHandle;
  704. }
  705. /* The device callback is invalid or not. */
  706. if (!deviceHandle->deviceCallback)
  707. {
  708. return kStatus_USB_Error;
  709. }
  710. #if (defined(USB_DEVICE_CONFIG_USE_TASK) && (USB_DEVICE_CONFIG_USE_TASK > 0U))
  711. if (deviceHandle->epCallbackDirectly)
  712. {
  713. if ((message->code & USB_ENDPOINT_NUMBER_MASK) && (!(message->code & 0x70U)))
  714. {
  715. return USB_DeviceNotification(deviceHandle, message);
  716. }
  717. }
  718. /* Add the message to message queue when the device task is enabled. */
  719. if (kStatus_USB_OSA_Success != USB_OsaMsgqSend(deviceHandle->notificationQueue, (void *)message))
  720. {
  721. return kStatus_USB_Busy;
  722. }
  723. return kStatus_USB_Success;
  724. #else
  725. /* Handle the notification by calling USB_DeviceNotification. */
  726. return USB_DeviceNotification(deviceHandle, message);
  727. #endif
  728. }
  729. /*!
  730. * @brief Initialize the USB device stack.
  731. *
  732. * This function initializes the USB device module specified by the controllerId.
  733. *
  734. * @param controllerId The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
  735. * @param deviceCallback Function pointer of the device callback.
  736. * @param handle It is out parameter, is used to return pointer of the device handle to the caller.
  737. *
  738. * @retval kStatus_USB_Success The device is initialized successfully.
  739. * @retval kStatus_USB_InvalidHandle The handle is a NULL pointer.
  740. * @retval kStatus_USB_Busy Cannot allocate a device handle.
  741. * @retval kStatus_USB_ControllerNotFound Cannot find the controller according to the controller id.
  742. * @retval kStatus_USB_InvalidControllerInterface The controller driver interfaces is invaild, There is an empty
  743. * interface entity.
  744. * @retval kStatus_USB_Error The macro USB_DEVICE_CONFIG_ENDPOINTS is more than IP's endpoint number.
  745. * Or, the device has been initialized.
  746. * Or, the message queue is created failed.
  747. */
  748. usb_status_t USB_DeviceInit(uint8_t controllerId, usb_device_callback_t deviceCallback, usb_device_handle *handle)
  749. {
  750. usb_device_struct_t *deviceHandle = NULL;
  751. usb_status_t error;
  752. uint32_t count;
  753. if (NULL == handle)
  754. {
  755. return kStatus_USB_InvalidHandle;
  756. }
  757. /* Allocate a device handle by using the controller id. */
  758. error = USB_DeviceAllocateHandle(controllerId, &deviceHandle);
  759. if (kStatus_USB_Success != error)
  760. {
  761. return error;
  762. }
  763. /* Save the device callback */
  764. deviceHandle->deviceCallback = deviceCallback;
  765. /* Save the controller id */
  766. deviceHandle->controllerId = controllerId;
  767. /* Clear the device address */
  768. deviceHandle->deviceAddress = 0U;
  769. /* Clear the device reset state */
  770. deviceHandle->isResetting = 0U;
  771. /* Initialize the enpoints */
  772. for (count = 0U; count < (USB_DEVICE_CONFIG_ENDPOINTS * 2U); count++)
  773. {
  774. deviceHandle->epCallback[count].callbackFn = (usb_device_endpoint_callback_t)NULL;
  775. deviceHandle->epCallback[count].callbackParam = NULL;
  776. deviceHandle->epCallback[count].isBusy = 0U;
  777. }
  778. /* Get the controller interface according to the controller id */
  779. error = USB_DeviceGetControllerInterface(controllerId, &deviceHandle->controllerInterface);
  780. if (kStatus_USB_Success != error)
  781. {
  782. USB_DeviceFreeHandle(deviceHandle);
  783. return error;
  784. }
  785. if (NULL == deviceHandle->controllerInterface)
  786. {
  787. USB_DeviceFreeHandle(deviceHandle);
  788. return kStatus_USB_ControllerNotFound;
  789. }
  790. if (((usb_device_controller_init_t)NULL == deviceHandle->controllerInterface->deviceInit) ||
  791. ((usb_device_controller_deinit_t)NULL == deviceHandle->controllerInterface->deviceDeinit) ||
  792. ((usb_device_controller_send_t)NULL == deviceHandle->controllerInterface->deviceSend) ||
  793. ((usb_device_controller_recv_t)NULL == deviceHandle->controllerInterface->deviceRecv) ||
  794. ((usb_device_controller_cancel_t)NULL == deviceHandle->controllerInterface->deviceCancel) ||
  795. ((usb_device_controller_control_t)NULL == deviceHandle->controllerInterface->deviceControl))
  796. {
  797. USB_DeviceFreeHandle(deviceHandle);
  798. return kStatus_USB_InvalidControllerInterface;
  799. }
  800. #if USB_DEVICE_CONFIG_USE_TASK
  801. /* Create a message queue when the device handle is enabled. */
  802. if (kStatus_USB_OSA_Success !=
  803. USB_OsaMsgqCreate(&deviceHandle->notificationQueue, USB_DEVICE_CONFIG_MAX_MESSAGES,
  804. (1U + (sizeof(usb_device_callback_message_struct_t) - 1U) / sizeof(uint32_t))))
  805. {
  806. USB_DeviceDeinit(deviceHandle);
  807. return kStatus_USB_Error;
  808. }
  809. #endif
  810. *handle = deviceHandle;
  811. /* Initialize the controller */
  812. error = deviceHandle->controllerInterface->deviceInit(controllerId, deviceHandle, &deviceHandle->controllerHandle);
  813. if (kStatus_USB_Success != error)
  814. {
  815. USB_DeviceDeinit(deviceHandle);
  816. *handle = NULL;
  817. return error;
  818. }
  819. /* Set the device to deafult state */
  820. deviceHandle->state = kUSB_DeviceStateDefault;
  821. return error;
  822. }
  823. /*!
  824. * @brief Enable the device functionality.
  825. *
  826. * The function enables the device functionality, so that the device can be recognized by the host when the device
  827. * detects that it has been connected to a host.
  828. *
  829. * @param handle The device handle got from USB_DeviceInit.
  830. *
  831. * @retval kStatus_USB_Success The device is run successfully.
  832. * @retval kStatus_USB_ControllerNotFound Cannot find the controller.
  833. * @retval kStatus_USB_InvalidHandle The device handle is a NULL pointer. Or the controller handle is invalid.
  834. *
  835. */
  836. usb_status_t USB_DeviceRun(usb_device_handle handle)
  837. {
  838. return USB_DeviceControl(handle, kUSB_DeviceControlRun, NULL);
  839. }
  840. /*!
  841. * @brief Disable the device functionality.
  842. *
  843. * The function disables the device functionality, after this function called, even the device is detached to the host,
  844. * and the device can't work.
  845. *
  846. * @param handle The device handle got from USB_DeviceInit.
  847. *
  848. * @retval kStatus_USB_Success The device is stopped successfully.
  849. * @retval kStatus_USB_ControllerNotFound Cannot find the controller.
  850. * @retval kStatus_USB_InvalidHandle The device handle is a NULL pointer. Or the controller handle is invalid.
  851. */
  852. usb_status_t USB_DeviceStop(usb_device_handle handle)
  853. {
  854. return USB_DeviceControl(handle, kUSB_DeviceControlStop, NULL);
  855. }
  856. /*!
  857. * @brief De-initialize the device controller.
  858. *
  859. * The function de-initializes the device controller specified by the handle.
  860. *
  861. * @param handle The device handle got from USB_DeviceInit.
  862. *
  863. * @retval kStatus_USB_Success The device is stopped successfully.
  864. * @retval kStatus_USB_InvalidHandle The device handle is a NULL pointer. Or the controller handle is invalid.
  865. */
  866. usb_status_t USB_DeviceDeinit(usb_device_handle handle)
  867. {
  868. usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
  869. if (NULL == deviceHandle)
  870. {
  871. return kStatus_USB_InvalidHandle;
  872. }
  873. /* De-initialize the controller */
  874. if (NULL != deviceHandle->controllerInterface)
  875. {
  876. deviceHandle->controllerInterface->deviceDeinit(deviceHandle->controllerHandle);
  877. deviceHandle->controllerInterface = (usb_device_controller_interface_struct_t *)NULL;
  878. }
  879. #if USB_DEVICE_CONFIG_USE_TASK
  880. /* Destroy the message queue. */
  881. if (NULL != deviceHandle->notificationQueue)
  882. {
  883. USB_OsaMsgqDestroy(deviceHandle->notificationQueue);
  884. deviceHandle->notificationQueue = NULL;
  885. }
  886. #endif
  887. /* Free the device handle. */
  888. USB_DeviceFreeHandle(deviceHandle);
  889. return kStatus_USB_Success;
  890. }
  891. /*!
  892. * @brief Send data through a specified endpoint.
  893. *
  894. * The function is used to send data through a specified endpoint.
  895. *
  896. * @param handle The device handle got from USB_DeviceInit.
  897. * @param endpointAddress Endpoint index.
  898. * @param buffer The memory address to hold the data need to be sent.
  899. * @param length The data length need to be sent.
  900. *
  901. * @retval kStatus_USB_Success The send request is sent successfully.
  902. * @retval kStatus_USB_InvalidHandle The handle is a NULL pointer. Or the controller handle is invalid.
  903. * @retval kStatus_USB_Busy Cannot allocate dtds for current tansfer in EHCI driver.
  904. * @retval kStatus_USB_ControllerNotFound Cannot find the controller.
  905. * @retval kStatus_USB_Error The device is doing reset.
  906. *
  907. * @note The return value just means if the sending request is successful or not; the transfer done is notified by the
  908. * corresponding callback function.
  909. * Currently, only one transfer request can be supported for one specific endpoint.
  910. * If there is a specific requirement to support multiple transfer requests for one specific endpoint, the application
  911. * should implement a queue in the application level.
  912. * The subsequent transfer could begin only when the previous transfer is done (get notification through the endpoint
  913. * callback).
  914. */
  915. usb_status_t USB_DeviceSendRequest(usb_device_handle handle, uint8_t endpointAddress, uint8_t *buffer, uint32_t length)
  916. {
  917. return USB_DeviceTransfer(handle, (endpointAddress & USB_ENDPOINT_NUMBER_MASK) |
  918. (USB_IN << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT),
  919. buffer, length);
  920. }
  921. /*!
  922. * @brief Receive data through a specified endpoint.
  923. *
  924. * The function is used to receive data through a specified endpoint.
  925. *
  926. * @param handle The device handle got from USB_DeviceInit.
  927. * @param endpointAddress Endpoint index.
  928. * @param buffer The memory address to save the received data.
  929. * @param length The data length want to be received.
  930. *
  931. * @retval kStatus_USB_Success The receive request is sent successfully.
  932. * @retval kStatus_USB_InvalidHandle The handle is a NULL pointer. Or the controller handle is invalid.
  933. * @retval kStatus_USB_Busy Cannot allocate dtds for current tansfer in EHCI driver.
  934. * @retval kStatus_USB_ControllerNotFound Cannot find the controller.
  935. * @retval kStatus_USB_Error The device is doing reset.
  936. *
  937. * @note The return value just means if the receiving request is successful or not; the transfer done is notified by the
  938. * corresponding callback function.
  939. * Currently, only one transfer request can be supported for one specific endpoint.
  940. * If there is a specific requirement to support multiple transfer requests for one specific endpoint, the application
  941. * should implement a queue in the application level.
  942. * The subsequent transfer could begin only when the previous transfer is done (get notification through the endpoint
  943. * callback).
  944. */
  945. usb_status_t USB_DeviceRecvRequest(usb_device_handle handle, uint8_t endpointAddress, uint8_t *buffer, uint32_t length)
  946. {
  947. return USB_DeviceTransfer(handle, (endpointAddress & USB_ENDPOINT_NUMBER_MASK) |
  948. (USB_OUT << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT),
  949. buffer, length);
  950. }
  951. /*!
  952. * @brief Cancel the pending transfer in a specified endpoint.
  953. *
  954. * The function is used to cancel the pending transfer in a specified endpoint.
  955. *
  956. * @param handle The device handle got from USB_DeviceInit.
  957. * @param endpointAddress Endpoint address, bit7 is the direction of endpoint, 1U - IN, abd 0U - OUT.
  958. *
  959. * @retval kStatus_USB_Success The transfer is cancelled.
  960. * @retval kStatus_USB_InvalidHandle The handle is a NULL pointer. Or the controller handle is invalid.
  961. * @retval kStatus_USB_ControllerNotFound Cannot find the controller.
  962. */
  963. usb_status_t USB_DeviceCancel(usb_device_handle handle, uint8_t endpointAddress)
  964. {
  965. usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
  966. usb_status_t error = kStatus_USB_Error;
  967. if (NULL == deviceHandle)
  968. {
  969. return kStatus_USB_InvalidHandle;
  970. }
  971. if (NULL != deviceHandle->controllerInterface)
  972. {
  973. error = deviceHandle->controllerInterface->deviceCancel(deviceHandle->controllerHandle, endpointAddress);
  974. }
  975. else
  976. {
  977. error = kStatus_USB_ControllerNotFound;
  978. }
  979. return error;
  980. }
  981. /*!
  982. * @brief Initialize a specified endpoint.
  983. *
  984. * The function is used to initialize a specified endpoint and the corresponding endpoint callback is also initialized.
  985. *
  986. * @param handle The device handle got from USB_DeviceInit.
  987. * @param epInit Endpoint initizlization structure. Please refer to the structure usb_device_endpoint_init_struct_t.
  988. * @param epCallback Endpoint callback structure. Please refer to the structure
  989. * usb_device_endpoint_callback_struct_t.
  990. *
  991. * @retval kStatus_USB_Success The endpoint is initialized successfully.
  992. * @retval kStatus_USB_InvalidHandle The handle is a NULL pointer. Or the controller handle is invalid.
  993. * @retval kStatus_USB_InvalidParameter The epInit or epCallback is NULL pointer. Or the endpoint number is
  994. * more than USB_DEVICE_CONFIG_ENDPOINTS.
  995. * @retval kStatus_USB_Busy The endpoint is busy in EHCI driver.
  996. * @retval kStatus_USB_ControllerNotFound Cannot find the controller.
  997. */
  998. usb_status_t USB_DeviceInitEndpoint(usb_device_handle handle,
  999. usb_device_endpoint_init_struct_t *epInit,
  1000. usb_device_endpoint_callback_struct_t *epCallback)
  1001. {
  1002. usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
  1003. uint8_t endpoint;
  1004. uint8_t direction;
  1005. if (!deviceHandle)
  1006. {
  1007. return kStatus_USB_InvalidHandle;
  1008. }
  1009. if ((!epInit) || (!epCallback))
  1010. {
  1011. return kStatus_USB_InvalidParameter;
  1012. }
  1013. endpoint = epInit->endpointAddress & USB_ENDPOINT_NUMBER_MASK;
  1014. direction = (epInit->endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >>
  1015. USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT;
  1016. if (endpoint < USB_DEVICE_CONFIG_ENDPOINTS)
  1017. {
  1018. deviceHandle->epCallback[(uint8_t)((uint32_t)endpoint << 1U) | direction].callbackFn = epCallback->callbackFn;
  1019. deviceHandle->epCallback[(uint8_t)((uint32_t)endpoint << 1U) | direction].callbackParam =
  1020. epCallback->callbackParam;
  1021. deviceHandle->epCallback[(uint8_t)((uint32_t)endpoint << 1U) | direction].isBusy = 0U;
  1022. }
  1023. else
  1024. {
  1025. return kStatus_USB_InvalidParameter;
  1026. }
  1027. return USB_DeviceControl(handle, kUSB_DeviceControlEndpointInit, epInit);
  1028. }
  1029. /*!
  1030. * @brief De-initizlize a specified endpoint.
  1031. *
  1032. * The function is used to de-initizlize a specified endpoint.
  1033. *
  1034. * @param handle The device handle got from USB_DeviceInit.
  1035. * @param endpointAddress Endpoint address, bit7 is the direction of endpoint, 1U - IN, abd 0U - OUT.
  1036. *
  1037. * @retval kStatus_USB_Success The endpoint is de-initialized successfully.
  1038. * @retval kStatus_USB_InvalidHandle The handle is a NULL pointer. Or the controller handle is invalid.
  1039. * @retval kStatus_USB_InvalidParameter The endpoint number is more than USB_DEVICE_CONFIG_ENDPOINTS.
  1040. * @retval kStatus_USB_Busy The endpoint is busy in EHCI driver.
  1041. * @retval kStatus_USB_ControllerNotFound Cannot find the controller.
  1042. */
  1043. usb_status_t USB_DeviceDeinitEndpoint(usb_device_handle handle, uint8_t endpointAddress)
  1044. {
  1045. usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
  1046. uint8_t endpoint = endpointAddress & USB_ENDPOINT_NUMBER_MASK;
  1047. uint8_t direction = (endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >>
  1048. USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT;
  1049. usb_status_t error = kStatus_USB_Error;
  1050. #if (defined(USB_DEVICE_CONFIG_USE_TASK) && (USB_DEVICE_CONFIG_USE_TASK > 0U))
  1051. USB_OSA_SR_ALLOC();
  1052. #endif
  1053. if (!deviceHandle)
  1054. {
  1055. return kStatus_USB_InvalidHandle;
  1056. }
  1057. #if (defined(USB_DEVICE_CONFIG_USE_TASK) && (USB_DEVICE_CONFIG_USE_TASK > 0U))
  1058. USB_OSA_ENTER_CRITICAL();
  1059. deviceHandle->epCallbackDirectly = 1;
  1060. USB_OSA_EXIT_CRITICAL();
  1061. #endif
  1062. error = USB_DeviceControl(handle, kUSB_DeviceControlEndpointDeinit, &endpointAddress);
  1063. #if (defined(USB_DEVICE_CONFIG_USE_TASK) && (USB_DEVICE_CONFIG_USE_TASK > 0U))
  1064. USB_OSA_ENTER_CRITICAL();
  1065. deviceHandle->epCallbackDirectly = 0;
  1066. USB_OSA_EXIT_CRITICAL();
  1067. #endif
  1068. if (endpoint < USB_DEVICE_CONFIG_ENDPOINTS)
  1069. {
  1070. deviceHandle->epCallback[(uint8_t)((uint32_t)endpoint << 1U) | direction].callbackFn =
  1071. (usb_device_endpoint_callback_t)NULL;
  1072. deviceHandle->epCallback[(uint8_t)((uint32_t)endpoint << 1U) | direction].callbackParam = NULL;
  1073. deviceHandle->epCallback[(uint8_t)((uint32_t)endpoint << 1U) | direction].isBusy = 0U;
  1074. }
  1075. else
  1076. {
  1077. return kStatus_USB_InvalidParameter;
  1078. }
  1079. return error;
  1080. }
  1081. /*!
  1082. * @brief Stall a specified endpoint.
  1083. *
  1084. * The function is used to stall a specified endpoint.
  1085. *
  1086. * @param handle The device handle got from USB_DeviceInit.
  1087. * @param endpointAddress Endpoint address, bit7 is the direction of endpoint, 1U - IN, abd 0U - OUT.
  1088. *
  1089. * @retval kStatus_USB_Success The endpoint is stalled successfully.
  1090. * @retval kStatus_USB_InvalidHandle The handle is a NULL pointer. Or the controller handle is invalid.
  1091. * @retval kStatus_USB_InvalidParameter The endpoint number is more than USB_DEVICE_CONFIG_ENDPOINTS.
  1092. * @retval kStatus_USB_ControllerNotFound Cannot find the controller.
  1093. */
  1094. usb_status_t USB_DeviceStallEndpoint(usb_device_handle handle, uint8_t endpointAddress)
  1095. {
  1096. if ((endpointAddress & USB_ENDPOINT_NUMBER_MASK) < USB_DEVICE_CONFIG_ENDPOINTS)
  1097. {
  1098. return USB_DeviceControl(handle, kUSB_DeviceControlEndpointStall, &endpointAddress);
  1099. }
  1100. else
  1101. {
  1102. return kStatus_USB_InvalidParameter;
  1103. }
  1104. }
  1105. /*!
  1106. * @brief Un-stall a specified endpoint.
  1107. *
  1108. * The function is used to un-stall a specified endpoint.
  1109. *
  1110. * @param handle The device handle got from USB_DeviceInit.
  1111. * @param endpointAddress Endpoint address, bit7 is the direction of endpoint, 1U - IN, abd 0U - OUT.
  1112. *
  1113. * @retval kStatus_USB_Success The endpoint is un-stalled successfully.
  1114. * @retval kStatus_USB_InvalidHandle The handle is a NULL pointer. Or the controller handle is invalid.
  1115. * @retval kStatus_USB_InvalidParameter The endpoint number is more than USB_DEVICE_CONFIG_ENDPOINTS.
  1116. * @retval kStatus_USB_ControllerNotFound Cannot find the controller.
  1117. */
  1118. usb_status_t USB_DeviceUnstallEndpoint(usb_device_handle handle, uint8_t endpointAddress)
  1119. {
  1120. if ((endpointAddress & USB_ENDPOINT_NUMBER_MASK) < USB_DEVICE_CONFIG_ENDPOINTS)
  1121. {
  1122. return USB_DeviceControl(handle, kUSB_DeviceControlEndpointUnstall, &endpointAddress);
  1123. }
  1124. else
  1125. {
  1126. return kStatus_USB_InvalidParameter;
  1127. }
  1128. }
  1129. /*!
  1130. * @brief Get the status of the selected item.
  1131. *
  1132. * The function is used to get the status of the selected item.
  1133. *
  1134. * @param handle The device handle got from USB_DeviceInit.
  1135. * @param type The selected item. Please refer to the structure usb_device_status_t.
  1136. * @param param The param type is determined by the selected item.
  1137. *
  1138. * @retval kStatus_USB_Success Get status successfully.
  1139. * @retval kStatus_USB_InvalidHandle The handle is a NULL pointer. Or the controller handle is invalid.
  1140. * @retval kStatus_USB_InvalidParameter The param is NULL pointer.
  1141. * @retval kStatus_USB_ControllerNotFound Cannot find the controller.
  1142. * @retval kStatus_USB_Error Unsupported type.
  1143. */
  1144. usb_status_t USB_DeviceGetStatus(usb_device_handle handle, usb_device_status_t type, void *param)
  1145. {
  1146. uint8_t *temp8;
  1147. usb_status_t error = kStatus_USB_Error;
  1148. if (NULL == param)
  1149. {
  1150. return kStatus_USB_InvalidParameter;
  1151. }
  1152. switch (type)
  1153. {
  1154. case kUSB_DeviceStatusSpeed:
  1155. error = USB_DeviceControl(handle, kUSB_DeviceControlGetSpeed, param);
  1156. break;
  1157. case kUSB_DeviceStatusOtg:
  1158. error = USB_DeviceControl(handle, kUSB_DeviceControlGetOtgStatus, param);
  1159. break;
  1160. case kUSB_DeviceStatusDeviceState:
  1161. temp8 = (uint8_t *)param;
  1162. error = kStatus_USB_Success;
  1163. *temp8 = ((usb_device_struct_t *)handle)->state;
  1164. break;
  1165. case kUSB_DeviceStatusAddress:
  1166. temp8 = (uint8_t *)param;
  1167. error = kStatus_USB_Success;
  1168. *temp8 = ((usb_device_struct_t *)handle)->deviceAddress;
  1169. break;
  1170. case kUSB_DeviceStatusDevice:
  1171. error = USB_DeviceControl(handle, kUSB_DeviceControlGetDeviceStatus, param);
  1172. break;
  1173. case kUSB_DeviceStatusEndpoint:
  1174. error = USB_DeviceControl(handle, kUSB_DeviceControlGetEndpointStatus, param);
  1175. break;
  1176. case kUSB_DeviceStatusSynchFrame:
  1177. error = USB_DeviceControl(handle, kUSB_DeviceControlGetSynchFrame, param);
  1178. break;
  1179. #if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U))
  1180. case kUSB_DeviceStatusRemoteWakeup:
  1181. temp8 = (uint8_t *)param;
  1182. error = kStatus_USB_Success;
  1183. *temp8 = ((usb_device_struct_t *)handle)->remotewakeup;
  1184. break;
  1185. #endif
  1186. default:
  1187. break;
  1188. }
  1189. return error;
  1190. }
  1191. /*!
  1192. * @brief Set the status of the selected item.
  1193. *
  1194. * The function is used to set the status of the selected item.
  1195. *
  1196. * @param handle The device handle got from USB_DeviceInit.
  1197. * @param type The selected item. Please refer to the structure usb_device_status_t.
  1198. * @param param The param type is determined by the selected item.
  1199. *
  1200. * @retval kStatus_USB_Success Set status successfully.
  1201. * @retval kStatus_USB_InvalidHandle The handle is a NULL pointer. Or the controller handle is invalid.
  1202. * @retval kStatus_USB_ControllerNotFound Cannot find the controller.
  1203. * @retval kStatus_USB_Error Unsupported type, or the param is NULL pointer.
  1204. */
  1205. usb_status_t USB_DeviceSetStatus(usb_device_handle handle, usb_device_status_t type, void *param)
  1206. {
  1207. usb_status_t error = kStatus_USB_Error;
  1208. switch (type)
  1209. {
  1210. #if (defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U) || \
  1211. (defined(USB_DEVICE_CONFIG_LPCIP3511HS) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))) && \
  1212. (defined(USB_DEVICE_CONFIG_USB20_TEST_MODE) && (USB_DEVICE_CONFIG_USB20_TEST_MODE > 0U))
  1213. case kUSB_DeviceStatusTestMode:
  1214. error = USB_DeviceControl(handle, kUSB_DeviceControlSetTestMode, param);
  1215. break;
  1216. #endif
  1217. case kUSB_DeviceStatusOtg:
  1218. error = USB_DeviceControl(handle, kUSB_DeviceControlSetOtgStatus, param);
  1219. break;
  1220. case kUSB_DeviceStatusDeviceState:
  1221. if (NULL != param)
  1222. {
  1223. error = kStatus_USB_Success;
  1224. ((usb_device_struct_t *)handle)->state = (uint8_t)(*(uint8_t *)param);
  1225. }
  1226. break;
  1227. case kUSB_DeviceStatusAddress:
  1228. if (kUSB_DeviceStateAddressing != ((usb_device_struct_t *)handle)->state)
  1229. {
  1230. if (NULL != param)
  1231. {
  1232. error = kStatus_USB_Success;
  1233. ((usb_device_struct_t *)handle)->deviceAddress = (uint8_t)(*(uint8_t *)param);
  1234. ((usb_device_struct_t *)handle)->state = kUSB_DeviceStateAddressing;
  1235. }
  1236. }
  1237. else
  1238. {
  1239. error = USB_DeviceControl(handle, kUSB_DeviceControlSetDeviceAddress,
  1240. &((usb_device_struct_t *)handle)->deviceAddress);
  1241. }
  1242. break;
  1243. case kUSB_DeviceStatusBusResume:
  1244. error = USB_DeviceControl(handle, kUSB_DeviceControlResume, param);
  1245. break;
  1246. case kUSB_DeviceStatusBusSleepResume:
  1247. error = USB_DeviceControl(handle, kUSB_DeviceControlSleepResume, param);
  1248. break;
  1249. #if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U))
  1250. case kUSB_DeviceStatusRemoteWakeup:
  1251. if (NULL != param)
  1252. {
  1253. error = kStatus_USB_Success;
  1254. ((usb_device_struct_t *)handle)->remotewakeup = (uint8_t)(*(uint8_t *)param);
  1255. }
  1256. break;
  1257. #endif
  1258. case kUSB_DeviceStatusBusSuspend:
  1259. error = USB_DeviceControl(handle, kUSB_DeviceControlSuspend, param);
  1260. break;
  1261. case kUSB_DeviceStatusBusSleep:
  1262. error = USB_DeviceControl(handle, kUSB_DeviceControlSleep, param);
  1263. break;
  1264. default:
  1265. break;
  1266. }
  1267. return error;
  1268. }
  1269. #if (defined(USB_DEVICE_CHARGER_DETECT_ENABLE) && (USB_DEVICE_CHARGER_DETECT_ENABLE > 0U)) && \
  1270. ((defined(FSL_FEATURE_SOC_USBDCD_COUNT) && (FSL_FEATURE_SOC_USBDCD_COUNT > 0U)) || \
  1271. (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U)))
  1272. /*!
  1273. * @brief Initializes the device dcd module.
  1274. *
  1275. * The function initializes the device dcd module.
  1276. *
  1277. * @param handle The device handle got from USB_DeviceInit.
  1278. *
  1279. * @retval kStatus_USB_Success The device is run successfully.
  1280. * @retval kStatus_USB_ControllerNotFound Cannot find the controller.
  1281. * @retval kStatus_USB_InvalidHandle The device handle is a NULL pointer. Or the controller handle is invalid.
  1282. *
  1283. */
  1284. usb_status_t USB_DeviceDcdInitModule(usb_device_handle handle, void *time_param)
  1285. {
  1286. return USB_DeviceControl(handle, kUSB_DeviceControlDcdInitModule, time_param);
  1287. }
  1288. /*!
  1289. * @brief De-initializes the device dcd module.
  1290. *
  1291. * The function de-intializes the device dcd module.
  1292. *
  1293. * @param handle The device handle got from USB_DeviceInit.
  1294. *
  1295. * @retval kStatus_USB_Success The device is run successfully.
  1296. * @retval kStatus_USB_InvalidHandle The device handle is a NULL pointer. Or the controller handle is invalid.
  1297. *
  1298. */
  1299. usb_status_t USB_DeviceDcdDeinitModule(usb_device_handle handle)
  1300. {
  1301. return USB_DeviceControl(handle, kUSB_DeviceControlDcdDeinitModule, NULL);
  1302. }
  1303. #endif
  1304. #if USB_DEVICE_CONFIG_USE_TASK
  1305. /*!
  1306. * @brief Device task function.
  1307. *
  1308. * The function is used to handle controller message.
  1309. * This function should not be called in applicartion directly.
  1310. *
  1311. * @param handle The device handle got from USB_DeviceInit.
  1312. */
  1313. void USB_DeviceTaskFunction(void *deviceHandle)
  1314. {
  1315. usb_device_struct_t *handle = (usb_device_struct_t *)deviceHandle;
  1316. static usb_device_callback_message_struct_t message;
  1317. if (deviceHandle)
  1318. {
  1319. /* Get the message from the queue */
  1320. if (kStatus_USB_OSA_Success == USB_OsaMsgqRecv(handle->notificationQueue, (uint32_t *)&message, 0U))
  1321. {
  1322. /* Handle the message */
  1323. USB_DeviceNotification(handle, &message);
  1324. }
  1325. }
  1326. }
  1327. #endif
  1328. /*!
  1329. * @brief Get dvice stack version function.
  1330. *
  1331. * The function is used to get dvice stack version.
  1332. *
  1333. * @param[out] version The version structure pointer to keep the device stack version.
  1334. *
  1335. */
  1336. void USB_DeviceGetVersion(uint32_t *version)
  1337. {
  1338. if (version)
  1339. {
  1340. *version =
  1341. (uint32_t)USB_MAKE_VERSION(USB_STACK_VERSION_MAJOR, USB_STACK_VERSION_MINOR, USB_STACK_VERSION_BUGFIX);
  1342. }
  1343. }
  1344. #if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U))
  1345. /*!
  1346. * @brief Update the hardware tick.
  1347. *
  1348. * The function is used to update the hardware tick.
  1349. *
  1350. * @param[in] handle The device handle got from #USB_DeviceInit.
  1351. * @param[in] tick Current hardware tick.
  1352. *
  1353. */
  1354. usb_status_t USB_DeviceUpdateHwTick(usb_device_handle handle, uint64_t tick)
  1355. {
  1356. usb_device_struct_t *deviceHandle;
  1357. usb_status_t status = kStatus_USB_Success;
  1358. if (handle == NULL)
  1359. {
  1360. return kStatus_USB_InvalidHandle;
  1361. }
  1362. deviceHandle = (usb_device_struct_t *)handle;
  1363. deviceHandle->hwTick = tick;
  1364. return status;
  1365. }
  1366. #endif
  1367. #endif /* USB_DEVICE_CONFIG_NUM */