app_usbd.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919
  1. /**
  2. * Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(APP_USBD)
  42. #include "app_usbd.h"
  43. #include "app_usbd_core.h"
  44. #include "app_usbd_request.h"
  45. #include "nrf_power.h"
  46. #include "nrf_drv_clock.h"
  47. #include "nrf_drv_power.h"
  48. #if APP_USBD_CONFIG_EVENT_QUEUE_ENABLE
  49. #include "nrf_atfifo.h"
  50. #include "nrf_atomic.h"
  51. #endif
  52. #define NRF_LOG_MODULE_NAME app_usbd
  53. #if APP_USBD_CONFIG_LOG_ENABLED
  54. #define NRF_LOG_LEVEL APP_USBD_CONFIG_LOG_LEVEL
  55. #define NRF_LOG_INFO_COLOR APP_USBD_CONFIG_INFO_COLOR
  56. #define NRF_LOG_DEBUG_COLOR APP_USBD_CONFIG_DEBUG_COLOR
  57. #else //APP_USBD_CONFIG_LOG_ENABLED
  58. #define NRF_LOG_LEVEL 0
  59. #endif //APP_USBD_CONFIG_LOG_ENABLED
  60. #include "nrf_log.h"
  61. NRF_LOG_MODULE_REGISTER();
  62. /* Base variables tests */
  63. /* Check event of app_usbd_event_type_t enumerator */
  64. STATIC_ASSERT((int32_t)APP_USBD_EVT_FIRST_POWER == (int32_t)NRF_DRV_USBD_EVT_CNT);
  65. STATIC_ASSERT(sizeof(app_usbd_event_type_t) == sizeof(nrf_drv_usbd_event_type_t));
  66. STATIC_ASSERT(sizeof(app_usbd_descriptor_header_t) == 2);
  67. STATIC_ASSERT(sizeof(app_usbd_descriptor_device_t) == 18);
  68. STATIC_ASSERT(sizeof(app_usbd_descriptor_configuration_t) == 9);
  69. STATIC_ASSERT(sizeof(app_usbd_descriptor_iface_t) == 9);
  70. STATIC_ASSERT(sizeof(app_usbd_descriptor_ep_t) == 7);
  71. STATIC_ASSERT(sizeof(app_usbd_descriptor_iad_t) == 8);
  72. STATIC_ASSERT(sizeof(app_usbd_setup_t) == sizeof(nrf_drv_usbd_setup_t));
  73. /**
  74. * @internal
  75. * @defgroup app_usbd_internals USBD library internals
  76. * @ingroup app_usbd
  77. *
  78. * Internal variables, auxiliary macros and functions of USBD library.
  79. * @{
  80. */
  81. #if (APP_USBD_PROVIDE_SOF_TIMESTAMP) || defined(__SDK_DOXYGEN__)
  82. /**
  83. * @brief The last received frame number.
  84. */
  85. static uint16_t m_last_frame;
  86. #endif
  87. /**
  88. * @brief Variable type for endpoint configuration.
  89. *
  90. * Each endpoint would have assigned this type of configuration structure.
  91. */
  92. typedef struct
  93. {
  94. /**
  95. * @brief The class instance.
  96. *
  97. * The pointer to the class instance that is connected to the endpoint.
  98. */
  99. app_usbd_class_inst_t const * p_cinst;
  100. /**
  101. * @brief Endpoint event handler.
  102. *
  103. * Event handler for the endpoint.
  104. * It is set to event handler for the class instance during connection by default,
  105. * but it can be then updated for as a reaction for @ref APP_USBD_EVT_ATTACHED event.
  106. * This way we can speed up the interpretation of endpoint related events.
  107. */
  108. app_usbd_ep_event_handler_t event_handler;
  109. }app_usbd_ep_conf_t;
  110. /**
  111. * @brief Internal event with SOF counter.
  112. */
  113. typedef struct
  114. {
  115. app_usbd_internal_evt_t evt; //!< Internal event type
  116. #if (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE) \
  117. || defined(__SDK_DOXYGEN__)
  118. uint16_t sof_cnt; //!< Number of the SOF events that appears before current event
  119. uint16_t start_frame; //!< Number of the SOF frame that starts this event
  120. #endif // (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE)
  121. } app_usbd_internal_queue_evt_t;
  122. #if (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE) || defined(__SDK_DOXYGEN__)
  123. /**
  124. * @brief Event queue.
  125. *
  126. * The queue with events to be processed.
  127. */
  128. NRF_ATFIFO_DEF(m_event_queue, app_usbd_internal_queue_evt_t, APP_USBD_CONFIG_EVENT_QUEUE_SIZE);
  129. #if (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE) \
  130. || defined(__SDK_DOXYGEN__)
  131. /** @brief SOF events counter */
  132. static nrf_atomic_u32_t m_sof_events_cnt;
  133. /** @brief SOF Frame counter */
  134. static uint16_t m_event_frame;
  135. /* Limit of SOF events stacked until warning message. */
  136. #define APP_USBD_SOF_WARNING_LIMIT 500
  137. #endif // (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE)
  138. // || defined(__SDK_DOXYGEN__)
  139. #endif
  140. /**
  141. * @brief Instances connected with IN endpoints.
  142. *
  143. * Array of instance pointers connected with every IN endpoint.
  144. * @sa m_epout_instances
  145. */
  146. static app_usbd_ep_conf_t m_epin_conf[NRF_USBD_EPIN_CNT];
  147. /**
  148. * @brief Instances connected with OUT endpoints.
  149. *
  150. * Array of instance pointers connected with every OUT endpoint.
  151. * @sa m_epin_instances
  152. */
  153. static app_usbd_ep_conf_t m_epout_conf[NRF_USBD_EPIN_CNT];
  154. /**
  155. * @brief Beginning of classes list.
  156. *
  157. * All enabled in current configuration instances are connected into
  158. * a single linked list chain.
  159. * This variable points to first element.
  160. * Core class instance (connected to endpoint 0) is not listed here.
  161. */
  162. static app_usbd_class_inst_t const * m_p_first_cinst;
  163. /**
  164. * @brief Classes list that requires SOF events.
  165. *
  166. * Pointer to first class that requires SOF events.
  167. */
  168. static app_usbd_class_inst_t const * m_p_first_sof_cinst;
  169. /**
  170. * @brief Classes list that require SOF events in interrupt.
  171. *
  172. * Pointer to first class that requires SOF events in interrupt.
  173. */
  174. static app_usbd_class_inst_t const * m_p_first_sof_interrupt_cinst;
  175. /**
  176. * @brief Default configuration (when NULL is passed to @ref app_usbd_init).
  177. */
  178. static const app_usbd_config_t m_default_conf = {
  179. #if (!(APP_USBD_CONFIG_EVENT_QUEUE_ENABLE)) || defined(__SDK_DOXYGEN__)
  180. .ev_handler = app_usbd_event_execute,
  181. #endif
  182. #if (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE) || defined(__SDK_DOXYGEN__)
  183. .ev_isr_handler = NULL,
  184. #endif
  185. .ev_state_proc = NULL,
  186. .enable_sof = false
  187. };
  188. /**
  189. * @brief SUSPEND state machine states.
  190. *
  191. * The enumeration of internal SUSPEND state machine states.
  192. */
  193. typedef enum
  194. {
  195. SUSTATE_STOPPED, /**< The USB driver was not started */
  196. SUSTATE_STARTED, /**< The USB driver was started - waiting for USB RESET */
  197. SUSTATE_ACTIVE, /**< Active state */
  198. SUSTATE_SUSPENDING, /**< Suspending - waiting for the user to acknowledge */
  199. SUSTATE_SUSPEND, /**< Suspended */
  200. SUSTATE_RESUMING, /**< Resuming - waiting for clock */
  201. SUSTATE_WAKINGUP_WAITING_HFCLK_WREQ, /**< Waking up - waiting for clock and WUREQ from driver */
  202. SUSTATE_WAKINGUP_WAITING_HFCLK, /**< Waking up - waiting for HFCLK (WUREQ detected) */
  203. SUSTATE_WAKINGUP_WAITING_WREQ, /**< Waking up - waiting for WREQ (HFCLK active) */
  204. }app_usbd_sustate_t;
  205. /**
  206. * @brief Current suspend state.
  207. *
  208. * The state of the suspend state machine.
  209. */
  210. static app_usbd_sustate_t m_sustate;
  211. /**
  212. * @brief Remote wake-up register/unregister.
  213. *
  214. * Counter incremented when appended instance required remote wake-up functionality.
  215. * It should be decremented when the class is removed.
  216. * When this counter is not zero, remote wake-up functionality is activated inside core.
  217. */
  218. static uint8_t m_rwu_registered_counter;
  219. /**
  220. * @brief Current configuration.
  221. */
  222. static app_usbd_config_t m_current_conf;
  223. /**
  224. * @brief Class interface call: event handler
  225. *
  226. * @ref app_usbd_class_interface_t::event_handler
  227. *
  228. * @param[in] p_cinst Class instance.
  229. * @param[in] p_event Event passed to class instance.
  230. *
  231. * @return Standard error code @ref ret_code_t
  232. * @retval NRF_SUCCESS event handled successfully.
  233. * @retval NRF_ERROR_NOT_SUPPORTED unsupported event.
  234. * */
  235. static inline ret_code_t class_event_handler(app_usbd_class_inst_t const * const p_cinst,
  236. app_usbd_complex_evt_t const * const p_event)
  237. {
  238. ASSERT(p_cinst != NULL);
  239. ASSERT(p_cinst->p_class_methods != NULL);
  240. ASSERT(p_cinst->p_class_methods->event_handler != NULL);
  241. return p_cinst->p_class_methods->event_handler(p_cinst, p_event);
  242. }
  243. #if (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE) || defined(__SDK_DOXYGEN__)
  244. static inline void class_sof_interrupt_handler(app_usbd_class_inst_t const * const p_cinst,
  245. app_usbd_complex_evt_t const * const p_event)
  246. {
  247. ASSERT(p_cinst != NULL);
  248. ASSERT(p_cinst->p_data != NULL);
  249. ASSERT(p_cinst->p_data->sof_handler != NULL);
  250. p_cinst->p_data->sof_handler(p_event->drv_evt.data.sof.framecnt);
  251. }
  252. /**
  253. * @brief User event handler call (passed via configuration).
  254. *
  255. * @param p_event Handler of an event that is going to be added into queue.
  256. * @param queued The event is visible in the queue.
  257. */
  258. static inline void user_event_handler(app_usbd_internal_evt_t const * const p_event, bool queued)
  259. {
  260. if ((m_current_conf.ev_isr_handler) != NULL)
  261. {
  262. m_current_conf.ev_isr_handler(p_event, queued);
  263. }
  264. }
  265. #endif
  266. /**
  267. * @brief User event processor call (passed via configuration).
  268. *
  269. * @param event Event type.
  270. */
  271. static inline void user_event_state_proc(app_usbd_event_type_t event)
  272. {
  273. if ((m_current_conf.ev_state_proc) != NULL)
  274. {
  275. m_current_conf.ev_state_proc(event);
  276. }
  277. }
  278. /**
  279. * @brief Find a specified descriptor.
  280. *
  281. * @param[in] p_cinst Class instance.
  282. * @param[in] desc_type Descriptor type @ref app_usbd_descriptor_t
  283. * @param[in] desc_index Descriptor index.
  284. * @param[out] p_desc Pointer to escriptor.
  285. * @param[out] p_desc_len Length of descriptor.
  286. *
  287. * @return Standard error code @ref ret_code_t
  288. * @retval NRF_SUCCESS Descriptor successfully found.
  289. * @retval NRF_ERROR_NOT_FOUND Descriptor not found.
  290. * */
  291. ret_code_t app_usbd_class_descriptor_find(app_usbd_class_inst_t const * const p_cinst,
  292. uint8_t desc_type,
  293. uint8_t desc_index,
  294. uint8_t * p_desc,
  295. size_t * p_desc_len)
  296. {
  297. app_usbd_class_descriptor_ctx_t siz;
  298. APP_USBD_CLASS_DESCRIPTOR_INIT(&siz);
  299. uint32_t total_size = 0;
  300. while(p_cinst->p_class_methods->feed_descriptors(&siz, p_cinst, NULL, sizeof(uint8_t)))
  301. {
  302. total_size++;
  303. }
  304. uint8_t cur_len = 0;
  305. uint32_t cur_size = 0;
  306. uint8_t index = 0;
  307. app_usbd_class_descriptor_ctx_t descr;
  308. APP_USBD_CLASS_DESCRIPTOR_INIT(&descr);
  309. while(cur_size < total_size)
  310. {
  311. /* First byte of a descriptor is its size */
  312. UNUSED_RETURN_VALUE(p_cinst->p_class_methods->feed_descriptors(&descr,
  313. p_cinst,
  314. &cur_len,
  315. sizeof(uint8_t)));
  316. /* Second byte is type of the descriptor */
  317. uint8_t type;
  318. UNUSED_RETURN_VALUE(p_cinst->p_class_methods->feed_descriptors(&descr,
  319. p_cinst,
  320. &type,
  321. sizeof(uint8_t)));
  322. if(type == desc_type)
  323. {
  324. if(index == desc_index)
  325. {
  326. /* Copy the length of descriptor to *p_desc_len */
  327. *p_desc_len = cur_len;
  328. /* Two first bytes of descriptor have already been fed - copy them to *p_desc */
  329. *p_desc++ = cur_len;
  330. *p_desc++ = desc_type;
  331. /* Copy the rest of descriptor to *p_desc */
  332. UNUSED_RETURN_VALUE(p_cinst->p_class_methods->feed_descriptors(&descr,
  333. p_cinst,
  334. p_desc,
  335. cur_len-2));
  336. return NRF_SUCCESS;
  337. }
  338. else
  339. {
  340. index++;
  341. }
  342. }
  343. /* Fast-forward through unmatched descriptor */
  344. UNUSED_RETURN_VALUE(p_cinst->p_class_methods->feed_descriptors(&descr,
  345. p_cinst,
  346. NULL,
  347. cur_len-2));
  348. cur_size += cur_len;
  349. }
  350. return NRF_ERROR_NOT_FOUND;
  351. }
  352. /**
  353. * @brief Access into selected endpoint configuration structure.
  354. *
  355. * @param ep Endpoint address.
  356. * @return A pointer to the endpoint configuration structure.
  357. *
  358. * @note This function would assert when endpoint number is not correct and debugging is enabled.
  359. */
  360. static app_usbd_ep_conf_t * app_usbd_ep_conf_access(nrf_drv_usbd_ep_t ep)
  361. {
  362. if (NRF_USBD_EPIN_CHECK(ep))
  363. {
  364. uint8_t nr = NRF_USBD_EP_NR_GET(ep);
  365. ASSERT(nr < NRF_USBD_EPIN_CNT);
  366. return &m_epin_conf[nr];
  367. }
  368. else
  369. {
  370. uint8_t nr = NRF_USBD_EP_NR_GET(ep);
  371. ASSERT(nr < NRF_USBD_EPOUT_CNT);
  372. return &m_epout_conf[nr];
  373. }
  374. }
  375. /**
  376. * @brief Accessing instance connected with selected endpoint.
  377. *
  378. * @param ep Endpoint number.
  379. *
  380. * @return The pointer to the instance connected with endpoint.
  381. */
  382. static inline app_usbd_class_inst_t const * app_usbd_ep_instance_get(nrf_drv_usbd_ep_t ep)
  383. {
  384. return app_usbd_ep_conf_access(ep)->p_cinst;
  385. }
  386. /**
  387. * @brief Connect instance with selected endpoint.
  388. *
  389. * This function configures instance connected to endpoint but also sets
  390. * default event handler function pointer.
  391. *
  392. * @param ep Endpoint number.
  393. * @param p_cinst The instance to connect into the selected endpoint.
  394. * NULL if endpoint is going to be disconnected.
  395. *
  396. * @note Disconnecting EP0 is not allowed and protected by assertion.
  397. */
  398. static void app_usbd_ep_instance_set(nrf_drv_usbd_ep_t ep, app_usbd_class_inst_t const * p_cinst)
  399. {
  400. app_usbd_ep_conf_t * p_ep_conf = app_usbd_ep_conf_access(ep);
  401. /* Set instance and default event handler */
  402. p_ep_conf->p_cinst = p_cinst;
  403. if (p_cinst == NULL)
  404. {
  405. ASSERT((ep != NRF_DRV_USBD_EPOUT0) && (ep != NRF_DRV_USBD_EPIN0)); /* EP0 should never be disconnected */
  406. p_ep_conf->event_handler = NULL;
  407. }
  408. else
  409. {
  410. p_ep_conf->event_handler = p_cinst->p_class_methods->event_handler;
  411. }
  412. }
  413. /**
  414. * @brief Call the core handler.
  415. *
  416. * Core instance is special kind of instance that is connected only to endpoint 0.
  417. * It is not present in instance list.
  418. * This auxiliary function makes future changes easier.
  419. * Just call the event instance for core module here.
  420. */
  421. static inline ret_code_t app_usbd_core_handler_call(app_usbd_internal_evt_t const * const p_event)
  422. {
  423. return m_epout_conf[0].event_handler(
  424. m_epout_conf[0].p_cinst,
  425. (app_usbd_complex_evt_t const *)p_event);
  426. }
  427. /**
  428. * @brief Add event for execution.
  429. *
  430. * Dependent on configuration event would be executed in place or would be added into queue
  431. * to be executed later.
  432. *
  433. * @param p_event_input Event to be executed.
  434. */
  435. static inline void app_usbd_event_add(app_usbd_internal_evt_t const * const p_event_input)
  436. {
  437. app_usbd_internal_evt_t const * p_event = p_event_input;
  438. if (p_event->type == APP_USBD_EVT_DRV_SETUP)
  439. {
  440. uint8_t bRequest = nrf_usbd_setup_brequest_get();
  441. uint8_t bmRequestType = nrf_usbd_setup_bmrequesttype_get();
  442. if ((bmRequestType == app_usbd_setup_req_val(
  443. APP_USBD_SETUP_REQREC_DEVICE,
  444. APP_USBD_SETUP_REQTYPE_STD,
  445. APP_USBD_SETUP_REQDIR_OUT))
  446. && (bRequest == APP_USBD_SETUP_STDREQ_SET_ADDRESS))
  447. {
  448. static const app_usbd_internal_evt_t event_setaddress =
  449. {
  450. .type = APP_USBD_EVT_SETUP_SETADDRESS,
  451. };
  452. p_event = &event_setaddress;
  453. }
  454. }
  455. #if (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE)
  456. if (p_event->app_evt.type == APP_USBD_EVT_DRV_SOF)
  457. {
  458. /* Propagate SOF event to classes that need it in interrupt */
  459. app_usbd_class_inst_t const * p_inst = app_usbd_class_sof_interrupt_first_get();
  460. while (NULL != p_inst)
  461. {
  462. class_sof_interrupt_handler(p_inst, (app_usbd_complex_evt_t const *)p_event);
  463. p_inst = app_usbd_class_sof_interrupt_next_get(p_inst);
  464. }
  465. #if (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE)
  466. CRITICAL_REGION_ENTER();
  467. if (m_sof_events_cnt == 0)
  468. {
  469. m_event_frame = p_event->drv_evt.data.sof.framecnt;
  470. }
  471. UNUSED_RETURN_VALUE(nrf_atomic_u32_add(&m_sof_events_cnt, 1));
  472. CRITICAL_REGION_EXIT();
  473. user_event_handler(p_event, true);
  474. if (m_sof_events_cnt == APP_USBD_SOF_WARNING_LIMIT)
  475. {
  476. NRF_LOG_WARNING("Stacked over %d SOF events.", APP_USBD_SOF_WARNING_LIMIT);
  477. }
  478. return;
  479. #endif // (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE)
  480. #if (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_INTERRUPT)
  481. user_event_handler(p_event, false);
  482. app_usbd_event_execute(p_event);
  483. return;
  484. #endif // (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_INTERRUPT)
  485. }
  486. nrf_atfifo_item_put_t cx;
  487. app_usbd_internal_queue_evt_t * p_event_item = nrf_atfifo_item_alloc(m_event_queue, &cx);
  488. if (NULL != p_event_item)
  489. {
  490. bool visible;
  491. p_event_item->evt = *p_event;
  492. #if (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE)
  493. CRITICAL_REGION_ENTER();
  494. p_event_item->start_frame = m_event_frame - m_sof_events_cnt + 1;
  495. p_event_item->sof_cnt = nrf_atomic_u32_fetch_store(&m_sof_events_cnt, 0);
  496. CRITICAL_REGION_EXIT();
  497. #endif // (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE)
  498. visible = nrf_atfifo_item_put(m_event_queue, &cx);
  499. user_event_handler(p_event, visible);
  500. }
  501. else
  502. {
  503. NRF_LOG_ERROR("Event queue full.");
  504. }
  505. #else
  506. m_current_conf.ev_handler(p_event);
  507. #endif
  508. }
  509. /**
  510. * @brief Power event handler.
  511. *
  512. * The function that pushes power events into the queue.
  513. * @param p_event Event from power driver to map into APP_USBD_EVT_POWER_ event.
  514. */
  515. #if APP_USBD_CONFIG_POWER_EVENTS_PROCESS
  516. static void app_usbd_power_event_handler(nrf_drv_power_usb_evt_t event)
  517. {
  518. switch(event)
  519. {
  520. case NRF_DRV_POWER_USB_EVT_DETECTED:
  521. {
  522. static const app_usbd_internal_evt_t ev = {
  523. .type = APP_USBD_EVT_POWER_DETECTED
  524. };
  525. app_usbd_event_add(&ev);
  526. break;
  527. }
  528. case NRF_DRV_POWER_USB_EVT_REMOVED:
  529. {
  530. static const app_usbd_internal_evt_t ev = {
  531. .type = APP_USBD_EVT_POWER_REMOVED
  532. };
  533. app_usbd_event_add(&ev);
  534. break;
  535. }
  536. case NRF_DRV_POWER_USB_EVT_READY:
  537. {
  538. static const app_usbd_internal_evt_t ev = {
  539. .type = APP_USBD_EVT_POWER_READY
  540. };
  541. app_usbd_event_add(&ev);
  542. break;
  543. }
  544. default:
  545. ASSERT(false);
  546. }
  547. }
  548. #endif
  549. /**
  550. * @brief Event handler.
  551. *
  552. * The function that pushes the event into the queue.
  553. * @param p_event Event to push.
  554. */
  555. static void app_usbd_event_handler(nrf_drv_usbd_evt_t const * const p_event)
  556. {
  557. app_usbd_event_add((app_usbd_internal_evt_t const *)p_event);
  558. }
  559. /**
  560. * @brief HF clock ready event handler.
  561. *
  562. * Function that is called when high frequency clock is started.
  563. *
  564. * @param event Event type that comes from clock driver.
  565. */
  566. static void app_usbd_hfclk_ready(nrf_drv_clock_evt_type_t event)
  567. {
  568. ASSERT(NRF_DRV_CLOCK_EVT_HFCLK_STARTED == event);
  569. static const app_usbd_evt_t evt_data = {
  570. .type = APP_USBD_EVT_HFCLK_READY
  571. };
  572. app_usbd_event_add((app_usbd_internal_evt_t const * )&evt_data);
  573. }
  574. /**
  575. * @brief Check if the HFCLK was requested in selected suspend state machine state.
  576. *
  577. *
  578. * @param sustate State to be checked.
  579. *
  580. * @retval true High frequency clock was requested in selected state.
  581. * @retval false High frequency clock was released in selected state.
  582. */
  583. static inline bool app_usbd_sustate_with_requested_hfclk(app_usbd_sustate_t sustate)
  584. {
  585. switch(sustate)
  586. {
  587. case SUSTATE_STOPPED: return false;
  588. case SUSTATE_STARTED: return false;
  589. case SUSTATE_ACTIVE: return true;
  590. case SUSTATE_SUSPENDING: return false;
  591. case SUSTATE_SUSPEND: return false;
  592. case SUSTATE_RESUMING: return true;
  593. case SUSTATE_WAKINGUP_WAITING_HFCLK_WREQ: return true;
  594. case SUSTATE_WAKINGUP_WAITING_HFCLK: return true;
  595. case SUSTATE_WAKINGUP_WAITING_WREQ: return true;
  596. default:
  597. return false;
  598. }
  599. }
  600. /**
  601. * @brief Check it the HFCLK is running in selected suspend state machine state.
  602. *
  603. * @param sustate State to be checked.
  604. *
  605. * @retval true High frequency clock is running in selected state.
  606. * @retval false High frequency clock is released in selected state.
  607. */
  608. static inline bool app_usbd_sustate_with_running_hfclk(app_usbd_sustate_t sustate)
  609. {
  610. switch(sustate)
  611. {
  612. case SUSTATE_STOPPED: return false;
  613. case SUSTATE_STARTED: return false;
  614. case SUSTATE_ACTIVE: return true;
  615. case SUSTATE_SUSPENDING: return false;
  616. case SUSTATE_SUSPEND: return false;
  617. case SUSTATE_RESUMING: return false;
  618. case SUSTATE_WAKINGUP_WAITING_HFCLK_WREQ: return false;
  619. case SUSTATE_WAKINGUP_WAITING_HFCLK: return false;
  620. case SUSTATE_WAKINGUP_WAITING_WREQ: return true;
  621. default:
  622. return false;
  623. }
  624. }
  625. /**
  626. * @brief Get current suspend state machine state.
  627. *
  628. * @return The state of the suspend state machine.
  629. */
  630. static inline app_usbd_sustate_t sustate_get(void)
  631. {
  632. return m_sustate;
  633. }
  634. /**
  635. * @brief Set current suspend state machine state.
  636. *
  637. * @param sustate The requested state of the state machine.
  638. */
  639. static inline void sustate_set(app_usbd_sustate_t sustate)
  640. {
  641. if (app_usbd_sustate_with_requested_hfclk(sustate) != app_usbd_sustate_with_requested_hfclk(m_sustate))
  642. {
  643. if (app_usbd_sustate_with_requested_hfclk(sustate))
  644. {
  645. static nrf_drv_clock_handler_item_t clock_handler_item =
  646. {
  647. .event_handler = app_usbd_hfclk_ready
  648. };
  649. nrf_drv_clock_hfclk_request(&clock_handler_item);
  650. }
  651. else
  652. {
  653. nrf_drv_clock_hfclk_release();
  654. }
  655. }
  656. if (app_usbd_sustate_with_running_hfclk(sustate) != app_usbd_sustate_with_running_hfclk(m_sustate))
  657. {
  658. if (app_usbd_sustate_with_running_hfclk(sustate))
  659. {
  660. nrf_drv_usbd_active_irq_config();
  661. }
  662. else
  663. {
  664. nrf_drv_usbd_suspend_irq_config();
  665. }
  666. }
  667. m_sustate = sustate;
  668. }
  669. /**
  670. * @brief Default selection function for interface.
  671. *
  672. * This function just enables and clears interface endpoints.
  673. *
  674. * @param[in] p_inst Class instance.
  675. * @param[in] iface_idx Interface index.
  676. * @param[in] alternate Interface alternate setting.
  677. *
  678. * @note Currently only alternate setting 0 is supported.
  679. *
  680. * @return Standard error code @ref ret_code_t
  681. * @retval NRF_SUCCESS Endpoints enabled and cleared.
  682. * @retval NRF_ERROR_INVALID_PARAM Unsupported alternate selected.
  683. */
  684. static inline ret_code_t default_iface_select(
  685. app_usbd_class_inst_t const * const p_inst,
  686. uint8_t iface_idx,
  687. uint8_t alternate)
  688. {
  689. ASSERT(iface_idx <= app_usbd_class_iface_count_get(p_inst));
  690. if (alternate != 0)
  691. {
  692. return NRF_ERROR_INVALID_PARAM;
  693. }
  694. app_usbd_class_iface_conf_t const * p_iface = app_usbd_class_iface_get(p_inst, iface_idx);
  695. uint8_t ep_count = app_usbd_class_iface_ep_count_get(p_iface);
  696. for (uint8_t i = 0; i < ep_count; ++i)
  697. {
  698. /* Enable every endpoint */
  699. app_usbd_class_ep_conf_t const * p_ep = app_usbd_class_iface_ep_get(p_iface, i);
  700. app_usbd_ep_enable(p_ep->address);
  701. }
  702. return NRF_SUCCESS;
  703. }
  704. /**
  705. * @brief Default deselection function for interface.
  706. *
  707. * This function just disables all interface endpoints.
  708. *
  709. * @param[in] p_inst Class instance.
  710. * @param[in] iface_idx Interface index.
  711. */
  712. static inline void default_iface_deselect(
  713. app_usbd_class_inst_t const * const p_inst,
  714. uint8_t iface_idx)
  715. {
  716. ASSERT(iface_idx <= app_usbd_class_iface_count_get(p_inst));
  717. app_usbd_class_iface_conf_t const * p_iface = app_usbd_class_iface_get(p_inst, iface_idx);
  718. uint8_t ep_count = app_usbd_class_iface_ep_count_get(p_iface);
  719. for (uint8_t i = 0; i < ep_count; ++i)
  720. {
  721. /* Disable every endpoint */
  722. app_usbd_class_ep_conf_t const * p_ep = app_usbd_class_iface_ep_get(p_iface, i);
  723. app_usbd_ep_disable(p_ep->address);
  724. }
  725. }
  726. /** @} */
  727. #if (APP_USBD_PROVIDE_SOF_TIMESTAMP) || defined(__SDK_DOXYGEN__)
  728. uint32_t app_usbd_sof_timestamp_get(void)
  729. {
  730. return m_last_frame;
  731. }
  732. #endif
  733. ret_code_t app_usbd_init(app_usbd_config_t const * p_config)
  734. {
  735. ASSERT(nrf_drv_clock_init_check());
  736. ret_code_t ret;
  737. #if (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE) || defined(__SDK_DOXYGEN__)
  738. ret = NRF_ATFIFO_INIT(m_event_queue);
  739. if (NRF_SUCCESS != ret)
  740. {
  741. return NRF_ERROR_INTERNAL;
  742. }
  743. #endif
  744. /* This is called at the beginning to secure multiple calls to init function */
  745. ret = nrf_drv_usbd_init(app_usbd_event_handler);
  746. if (NRF_SUCCESS != ret)
  747. {
  748. return ret;
  749. }
  750. /* Clear the variables */
  751. m_sustate = SUSTATE_STOPPED;
  752. m_p_first_cinst = NULL;
  753. m_p_first_sof_cinst = NULL;
  754. memset(m_epin_conf , 0, sizeof(m_epin_conf ));
  755. memset(m_epout_conf, 0, sizeof(m_epout_conf));
  756. /* Save the new configuration */
  757. if (p_config == NULL)
  758. {
  759. m_current_conf = m_default_conf;
  760. }
  761. else
  762. {
  763. m_current_conf = *p_config;
  764. }
  765. #if (!(APP_USBD_CONFIG_EVENT_QUEUE_ENABLE))
  766. if(m_current_conf.ev_handler == NULL)
  767. {
  768. m_current_conf.ev_handler = m_default_conf.ev_handler;
  769. }
  770. #endif
  771. #if APP_USBD_CONFIG_POWER_EVENTS_PROCESS
  772. ret = nrf_drv_power_init(NULL);
  773. if ((ret != NRF_SUCCESS) && (ret != NRF_ERROR_MODULE_ALREADY_INITIALIZED))
  774. {
  775. /* This should never happen */
  776. APP_ERROR_HANDLER(ret);
  777. }
  778. #endif
  779. /*Pin core class to required endpoints*/
  780. uint8_t iface_idx;
  781. app_usbd_class_iface_conf_t const * p_iface;
  782. app_usbd_class_inst_t const * const p_inst = app_usbd_core_instance_access();
  783. iface_idx = 0;
  784. while ((p_iface = app_usbd_class_iface_get(p_inst, iface_idx++)) != NULL)
  785. {
  786. uint8_t ep_idx = 0;
  787. app_usbd_class_ep_conf_t const * p_ep;
  788. while ((p_ep = app_usbd_class_iface_ep_get(p_iface, ep_idx++)) != NULL)
  789. {
  790. app_usbd_ep_instance_set(app_usbd_class_ep_address_get(p_ep), p_inst);
  791. }
  792. }
  793. /* Successfully attached */
  794. const app_usbd_evt_t evt_data = {
  795. .type = APP_USBD_EVT_INST_APPEND
  796. };
  797. ret = class_event_handler(p_inst, (app_usbd_complex_evt_t const *)(&evt_data));
  798. if (NRF_SUCCESS != ret)
  799. {
  800. UNUSED_RETURN_VALUE(nrf_drv_usbd_uninit());
  801. return ret;
  802. }
  803. return NRF_SUCCESS;
  804. }
  805. ret_code_t app_usbd_uninit(void)
  806. {
  807. #if APP_USBD_CONFIG_POWER_EVENTS_PROCESS
  808. nrf_drv_power_usbevt_uninit();
  809. #endif
  810. /* We get this error at very beginning but it would be used at the end of the function */
  811. const ret_code_t ret = nrf_drv_usbd_uninit();
  812. /* Unchain instance list */
  813. app_usbd_class_inst_t const * * pp_inst;
  814. pp_inst = &m_p_first_cinst;
  815. while (NULL != (*pp_inst))
  816. {
  817. app_usbd_class_inst_t const * * pp_next = &app_usbd_class_data_access(*pp_inst)->p_next;
  818. (*pp_inst) = NULL;
  819. pp_inst = pp_next;
  820. }
  821. /* Unchain SOF list */
  822. pp_inst = &m_p_first_sof_cinst;
  823. while (NULL != (*pp_inst))
  824. {
  825. app_usbd_class_inst_t const * * pp_next = &app_usbd_class_data_access(*pp_inst)->p_sof_next;
  826. (*pp_inst) = NULL;
  827. pp_inst = pp_next;
  828. }
  829. /* Unchain SOF interrupt list */
  830. pp_inst = &m_p_first_sof_interrupt_cinst;
  831. while (NULL != (*pp_inst))
  832. {
  833. app_usbd_class_inst_t const * * pp_next = &app_usbd_class_data_access(*pp_inst)->p_sof_next;
  834. (*pp_inst) = NULL;
  835. pp_inst = pp_next;
  836. }
  837. /* Clear all endpoints configurations */
  838. memset(m_epin_conf , 0, sizeof(m_epin_conf ));
  839. memset(m_epout_conf, 0, sizeof(m_epout_conf));
  840. /* Clear current configuration */
  841. memset(&m_current_conf, 0, sizeof(m_current_conf));
  842. return ret;
  843. }
  844. #if APP_USBD_CONFIG_POWER_EVENTS_PROCESS
  845. ret_code_t app_usbd_power_events_enable(void)
  846. {
  847. if (!nrf_drv_usbd_is_initialized() || nrf_drv_usbd_is_enabled())
  848. {
  849. return NRF_ERROR_INVALID_STATE;
  850. }
  851. ASSERT((!APP_USBD_CONFIG_EVENT_QUEUE_ENABLE) || (USBD_CONFIG_IRQ_PRIORITY == POWER_CONFIG_IRQ_PRIORITY));
  852. ret_code_t ret;
  853. static const nrf_drv_power_usbevt_config_t config =
  854. {
  855. .handler = app_usbd_power_event_handler
  856. };
  857. ret = nrf_drv_power_usbevt_init(&config);
  858. APP_ERROR_CHECK(ret);
  859. return NRF_SUCCESS;
  860. }
  861. #endif /* APP_USBD_CONFIG_POWER_EVENTS_PROCESS */
  862. void app_usbd_enable(void)
  863. {
  864. nrf_drv_usbd_enable();
  865. }
  866. void app_usbd_disable(void)
  867. {
  868. ASSERT(!nrf_drv_usbd_is_started());
  869. nrf_drv_usbd_disable();
  870. }
  871. void app_usbd_start(void)
  872. {
  873. ASSERT(nrf_drv_usbd_is_enabled());
  874. /* Check if interface numbers are in correct order */
  875. if (APP_USBD_CONFIG_LOG_ENABLED)
  876. {
  877. uint8_t next_iface = 0;
  878. for (app_usbd_class_inst_t const * * pp_inst = &m_p_first_cinst;
  879. (*pp_inst) != NULL;
  880. pp_inst = &(app_usbd_class_data_access(*pp_inst)->p_next))
  881. {
  882. uint8_t iface_idx = 0;
  883. app_usbd_class_iface_conf_t const * p_iface;
  884. while (NULL != (p_iface = app_usbd_class_iface_get(*pp_inst, iface_idx++)))
  885. {
  886. if (p_iface->number != next_iface)
  887. {
  888. NRF_LOG_WARNING("Unexpected interface number, expected %d, got %d",
  889. next_iface,
  890. p_iface->number);
  891. }
  892. ++next_iface;
  893. }
  894. }
  895. }
  896. /* Power should be already enabled - wait just in case if user calls
  897. * app_usbd_start just after app_usbd_enable without waiting for the event. */
  898. while (!nrf_power_usbregstatus_outrdy_get())
  899. {
  900. /* Wait for the power but terminate the function if USBD power disappears */
  901. if (!nrf_power_usbregstatus_vbusdet_get())
  902. return;
  903. }
  904. static const app_usbd_evt_t evt_data = {
  905. .type = APP_USBD_EVT_START_REQ
  906. };
  907. app_usbd_event_add((app_usbd_internal_evt_t const * )&evt_data);
  908. }
  909. void app_usbd_stop(void)
  910. {
  911. const app_usbd_evt_t evt_data = {
  912. .type = APP_USBD_EVT_STOP_REQ
  913. };
  914. app_usbd_event_add((app_usbd_internal_evt_t const * )&evt_data);
  915. }
  916. void app_usbd_suspend_req(void)
  917. {
  918. const app_usbd_evt_t evt_data = {
  919. .type = APP_USBD_EVT_SUSPEND_REQ
  920. };
  921. app_usbd_event_add((app_usbd_internal_evt_t const * )&evt_data);
  922. }
  923. bool app_usbd_wakeup_req(void)
  924. {
  925. ASSERT(app_usbd_class_rwu_enabled_check());
  926. if (!app_usbd_core_feature_state_get(APP_USBD_SETUP_STDFEATURE_DEVICE_REMOTE_WAKEUP))
  927. return false;
  928. const app_usbd_evt_t evt_data = {
  929. .type = APP_USBD_EVT_WAKEUP_REQ
  930. };
  931. app_usbd_event_add((app_usbd_internal_evt_t const * )&evt_data);
  932. return true;
  933. }
  934. bool app_usbd_active_check(void)
  935. {
  936. return (sustate_get() == SUSTATE_ACTIVE);
  937. }
  938. void app_usbd_event_execute(app_usbd_internal_evt_t const * const p_event)
  939. {
  940. ASSERT(NULL != m_p_first_cinst);
  941. /* If no event queue is implemented, it has to be ensured that this function is never called
  942. * from the context higher than USB interrupt level
  943. * If queue is implemented it would be called always from Thread level
  944. * if the library is used correctly.
  945. * NOTE: Higher interrupt level -> lower priority value.
  946. */
  947. ASSERT(USBD_CONFIG_IRQ_PRIORITY <= current_int_priority_get());
  948. /* Note - there should never be situation that event is generated on disconnected endpoint */
  949. switch (p_event->type)
  950. {
  951. case APP_USBD_EVT_START_REQ:
  952. {
  953. static const app_usbd_evt_t evt_data = {
  954. .type = APP_USBD_EVT_STARTED
  955. };
  956. /* Send event to all classes */
  957. UNUSED_RETURN_VALUE(app_usbd_core_handler_call((app_usbd_internal_evt_t const * )&evt_data));
  958. app_usbd_all_call((app_usbd_complex_evt_t const *)&evt_data);
  959. user_event_state_proc(APP_USBD_EVT_STARTED);
  960. app_usbd_all_iface_deselect();
  961. app_usbd_core_ep0_disable();
  962. nrf_drv_usbd_start((NULL != m_p_first_sof_cinst) || (m_current_conf.enable_sof) || (APP_USBD_PROVIDE_SOF_TIMESTAMP));
  963. sustate_set(SUSTATE_STARTED);
  964. break;
  965. }
  966. case APP_USBD_EVT_STOP_REQ:
  967. {
  968. static const app_usbd_evt_t evt_data = {
  969. .type = APP_USBD_EVT_STOPPED
  970. };
  971. app_usbd_all_iface_deselect();
  972. nrf_drv_usbd_stop();
  973. sustate_set(SUSTATE_STOPPED);
  974. /* Send event to all classes */
  975. app_usbd_all_call((app_usbd_complex_evt_t const * )&evt_data);
  976. UNUSED_RETURN_VALUE(app_usbd_core_handler_call((app_usbd_internal_evt_t const *)&evt_data));
  977. user_event_state_proc(APP_USBD_EVT_STOPPED);
  978. if (app_usbd_sustate_with_requested_hfclk(sustate_get()))
  979. {
  980. nrf_drv_clock_hfclk_release();
  981. }
  982. break;
  983. }
  984. case APP_USBD_EVT_HFCLK_READY:
  985. {
  986. switch(sustate_get())
  987. {
  988. case SUSTATE_RESUMING:
  989. {
  990. sustate_set(SUSTATE_ACTIVE);
  991. break;
  992. }
  993. case SUSTATE_WAKINGUP_WAITING_HFCLK_WREQ:
  994. {
  995. sustate_set(SUSTATE_WAKINGUP_WAITING_WREQ);
  996. break;
  997. }
  998. case SUSTATE_WAKINGUP_WAITING_HFCLK:
  999. {
  1000. sustate_set(SUSTATE_ACTIVE);
  1001. break;
  1002. }
  1003. default:
  1004. break; // Just ignore - it can happen in specific situation
  1005. }
  1006. break;
  1007. }
  1008. case APP_USBD_EVT_SUSPEND_REQ:
  1009. {
  1010. /* Suspend request can be only processed when we are in suspending state */
  1011. if (SUSTATE_SUSPENDING == sustate_get())
  1012. {
  1013. if (nrf_drv_usbd_suspend())
  1014. {
  1015. sustate_set(SUSTATE_SUSPEND);
  1016. }
  1017. }
  1018. break;
  1019. }
  1020. case APP_USBD_EVT_WAKEUP_REQ:
  1021. {
  1022. /* Suspend temporary if no suspend function was called from the application.
  1023. * This makes it possible to generate APP_USBD_EVT_DRV_WUREQ event from the driver */
  1024. if (sustate_get() == SUSTATE_SUSPENDING)
  1025. {
  1026. if (nrf_drv_usbd_suspend())
  1027. {
  1028. sustate_set(SUSTATE_SUSPEND);
  1029. }
  1030. }
  1031. if (nrf_drv_usbd_wakeup_req())
  1032. {
  1033. sustate_set(SUSTATE_WAKINGUP_WAITING_HFCLK_WREQ);
  1034. }
  1035. break;
  1036. }
  1037. case APP_USBD_EVT_DRV_SOF:
  1038. {
  1039. #if (APP_USBD_PROVIDE_SOF_TIMESTAMP) || defined(__SDK_DOXYGEN__)
  1040. m_last_frame = p_event->drv_evt.data.sof.framecnt;
  1041. #endif
  1042. /* Wake up if suspended */
  1043. if ((sustate_get() == SUSTATE_SUSPENDING) || (sustate_get() == SUSTATE_WAKINGUP_WAITING_WREQ))
  1044. {
  1045. static const app_usbd_evt_t evt_data = {
  1046. .type = APP_USBD_EVT_DRV_RESUME
  1047. };
  1048. app_usbd_event_execute((app_usbd_internal_evt_t *)&evt_data);
  1049. }
  1050. user_event_state_proc(APP_USBD_EVT_DRV_SOF);
  1051. app_usbd_class_inst_t const * p_inst = app_usbd_class_sof_first_get();
  1052. while (NULL != p_inst)
  1053. {
  1054. ret_code_t r = class_event_handler(p_inst, (app_usbd_complex_evt_t const *)p_event);
  1055. UNUSED_VARIABLE(r);
  1056. p_inst = app_usbd_class_sof_next_get(p_inst);
  1057. }
  1058. break;
  1059. }
  1060. case APP_USBD_EVT_DRV_RESET:
  1061. {
  1062. app_usbd_all_iface_deselect();
  1063. app_usbd_core_ep0_enable();
  1064. sustate_set(SUSTATE_ACTIVE);
  1065. user_event_state_proc(APP_USBD_EVT_DRV_RESET);
  1066. /* Processing core interface (connected only to EP0) and then all instances from the list */
  1067. UNUSED_RETURN_VALUE(app_usbd_core_handler_call(p_event));
  1068. app_usbd_all_call((app_usbd_complex_evt_t const *)p_event);
  1069. break;
  1070. }
  1071. case APP_USBD_EVT_DRV_RESUME:
  1072. {
  1073. if (sustate_get() == SUSTATE_WAKINGUP_WAITING_WREQ)
  1074. {
  1075. sustate_set(SUSTATE_ACTIVE);
  1076. nrf_drv_usbd_force_bus_wakeup();
  1077. }
  1078. else
  1079. {
  1080. sustate_set(SUSTATE_RESUMING);
  1081. }
  1082. user_event_state_proc(APP_USBD_EVT_DRV_RESUME);
  1083. /* Processing core interface (connected only to EP0) and then all instances from the list */
  1084. UNUSED_RETURN_VALUE(app_usbd_core_handler_call(p_event));
  1085. app_usbd_all_call((app_usbd_complex_evt_t const *)p_event);
  1086. break;
  1087. }
  1088. case APP_USBD_EVT_DRV_WUREQ:
  1089. {
  1090. static const app_usbd_evt_t evt_data = {
  1091. .type = APP_USBD_EVT_DRV_RESUME
  1092. };
  1093. user_event_state_proc(APP_USBD_EVT_DRV_RESUME);
  1094. /* Processing core interface (connected only to EP0) and then all instances from the list */
  1095. UNUSED_RETURN_VALUE(app_usbd_core_handler_call((app_usbd_internal_evt_t const *)&evt_data));
  1096. app_usbd_all_call((app_usbd_complex_evt_t const *)&evt_data);
  1097. switch(sustate_get())
  1098. {
  1099. case SUSTATE_WAKINGUP_WAITING_HFCLK_WREQ:
  1100. sustate_set(SUSTATE_WAKINGUP_WAITING_HFCLK);
  1101. break;
  1102. case SUSTATE_WAKINGUP_WAITING_WREQ:
  1103. sustate_set(SUSTATE_ACTIVE);
  1104. break;
  1105. default:
  1106. {
  1107. /* This should not happen - but try to recover by setting directly active state */
  1108. NRF_LOG_WARNING("Unexpected state on WUREQ event (%u)", sustate_get());
  1109. sustate_set(SUSTATE_ACTIVE);
  1110. }
  1111. }
  1112. break;
  1113. }
  1114. case APP_USBD_EVT_DRV_SUSPEND:
  1115. {
  1116. sustate_set(SUSTATE_SUSPENDING);
  1117. user_event_state_proc(APP_USBD_EVT_DRV_SUSPEND);
  1118. /* Processing all instances from the list and then core interface (connected only to EP0) */
  1119. app_usbd_all_call((app_usbd_complex_evt_t const *)p_event);
  1120. UNUSED_RETURN_VALUE(app_usbd_core_handler_call(p_event));
  1121. break;
  1122. }
  1123. case APP_USBD_EVT_STATE_CHANGED:
  1124. {
  1125. user_event_state_proc(APP_USBD_EVT_STATE_CHANGED);
  1126. /* Processing all instances from the list and then core interface (connected only to EP0) */
  1127. app_usbd_all_call((app_usbd_complex_evt_t const *)p_event);
  1128. break;
  1129. }
  1130. case APP_USBD_EVT_DRV_SETUP:
  1131. {
  1132. UNUSED_RETURN_VALUE(app_usbd_core_handler_call(p_event));
  1133. break;
  1134. }
  1135. case APP_USBD_EVT_SETUP_SETADDRESS:
  1136. {
  1137. UNUSED_RETURN_VALUE(app_usbd_core_handler_call(p_event));
  1138. break;
  1139. }
  1140. case APP_USBD_EVT_DRV_EPTRANSFER:
  1141. {
  1142. app_usbd_ep_conf_t const * p_ep_conf =
  1143. app_usbd_ep_conf_access(p_event->drv_evt.data.eptransfer.ep);
  1144. ASSERT(NULL != p_ep_conf->p_cinst);
  1145. ASSERT(NULL != p_ep_conf->event_handler);
  1146. if (NRF_SUCCESS != p_ep_conf->event_handler(p_ep_conf->p_cinst,
  1147. (app_usbd_complex_evt_t const *)p_event))
  1148. {
  1149. /* If error returned, every bulk/interrupt endpoint would be stalled */
  1150. if (!(0 == NRF_USBD_EP_NR_GET(p_event->drv_evt.data.eptransfer.ep) ||
  1151. NRF_USBD_EPISO_CHECK(p_event->drv_evt.data.eptransfer.ep)))
  1152. {
  1153. nrf_drv_usbd_ep_stall(p_event->drv_evt.data.eptransfer.ep);
  1154. }
  1155. }
  1156. break;
  1157. }
  1158. #if APP_USBD_CONFIG_POWER_EVENTS_PROCESS
  1159. case APP_USBD_EVT_POWER_DETECTED:
  1160. {
  1161. user_event_state_proc(APP_USBD_EVT_POWER_DETECTED);
  1162. app_usbd_all_call((app_usbd_complex_evt_t const *)p_event);
  1163. break;
  1164. }
  1165. case APP_USBD_EVT_POWER_REMOVED:
  1166. {
  1167. user_event_state_proc(APP_USBD_EVT_POWER_REMOVED);
  1168. app_usbd_all_call((app_usbd_complex_evt_t const *)p_event);
  1169. break;
  1170. }
  1171. case APP_USBD_EVT_POWER_READY:
  1172. {
  1173. user_event_state_proc(APP_USBD_EVT_POWER_READY);
  1174. app_usbd_all_call((app_usbd_complex_evt_t const *)p_event);
  1175. break;
  1176. }
  1177. #endif
  1178. default:
  1179. ASSERT(0);
  1180. break;
  1181. }
  1182. }
  1183. #if (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE) || defined(__SDK_DOXYGEN__)
  1184. bool app_usbd_event_queue_process(void)
  1185. {
  1186. #if (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE)
  1187. app_usbd_internal_evt_t sof_event = {
  1188. .app_evt.type = APP_USBD_EVT_DRV_SOF
  1189. };
  1190. #endif // (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE)
  1191. static nrf_atfifo_item_get_t cx;
  1192. static app_usbd_internal_queue_evt_t * p_event_item = NULL;
  1193. if (NULL == p_event_item)
  1194. {
  1195. p_event_item = nrf_atfifo_item_get(m_event_queue, &cx);
  1196. }
  1197. if (NULL != p_event_item)
  1198. {
  1199. #if (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE)
  1200. if (p_event_item->sof_cnt > 0)
  1201. {
  1202. if (p_event_item->start_frame > USBD_FRAMECNTR_FRAMECNTR_Msk)
  1203. {
  1204. p_event_item->start_frame = 0;
  1205. }
  1206. sof_event.drv_evt.data.sof.framecnt = (p_event_item->start_frame)++;
  1207. --(p_event_item->sof_cnt);
  1208. app_usbd_event_execute(&sof_event);
  1209. return true;
  1210. }
  1211. #endif // (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE)
  1212. app_usbd_event_execute(&(p_event_item->evt));
  1213. UNUSED_RETURN_VALUE(nrf_atfifo_item_free(m_event_queue, &cx));
  1214. p_event_item = NULL;
  1215. return true;
  1216. }
  1217. #if (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE)
  1218. else if (m_sof_events_cnt > 0)
  1219. {
  1220. CRITICAL_REGION_ENTER();
  1221. if (m_event_frame > USBD_FRAMECNTR_FRAMECNTR_Msk)
  1222. {
  1223. m_event_frame = 0;
  1224. }
  1225. sof_event.drv_evt.data.sof.framecnt = m_event_frame++;
  1226. UNUSED_RETURN_VALUE(nrf_atomic_u32_sub_hs(&m_sof_events_cnt, 1));
  1227. CRITICAL_REGION_EXIT();
  1228. app_usbd_event_execute(&sof_event);
  1229. return true;
  1230. }
  1231. #endif // (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE)
  1232. else
  1233. {
  1234. return false;
  1235. }
  1236. }
  1237. #endif
  1238. ret_code_t app_usbd_class_append(app_usbd_class_inst_t const * p_cinst)
  1239. {
  1240. ASSERT(NULL != p_cinst);
  1241. ASSERT(NULL != p_cinst->p_class_methods);
  1242. ASSERT(NULL != p_cinst->p_class_methods->event_handler);
  1243. ASSERT(NULL == app_usbd_class_data_access(p_cinst)->p_next);
  1244. /* This should be only called if USBD is disabled
  1245. * We simply assume that USBD is enabled if its interrupts are */
  1246. ASSERT(!nrf_drv_usbd_is_enabled() && nrf_drv_usbd_is_initialized());
  1247. /* Check if all required endpoints are available
  1248. * Checking is splitted from setting to avoid situation that anything
  1249. * is modified and then operation finishes with error */
  1250. uint8_t iface_idx;
  1251. app_usbd_class_iface_conf_t const * p_iface;
  1252. iface_idx = 0;
  1253. while (NULL != (p_iface = app_usbd_class_iface_get(p_cinst, iface_idx++)))
  1254. {
  1255. uint8_t ep_idx = 0;
  1256. app_usbd_class_ep_conf_t const * p_ep;
  1257. while (NULL != (p_ep = app_usbd_class_iface_ep_get(p_iface, ep_idx++)))
  1258. {
  1259. if (NULL != app_usbd_ep_instance_get(app_usbd_class_ep_address_get(p_ep)))
  1260. {
  1261. return NRF_ERROR_BUSY;
  1262. }
  1263. }
  1264. }
  1265. /* Connecting all required endpoints */
  1266. iface_idx = 0;
  1267. while (NULL != (p_iface = app_usbd_class_iface_get(p_cinst, iface_idx++)))
  1268. {
  1269. uint8_t ep_idx = 0;
  1270. app_usbd_class_ep_conf_t const * p_ep;
  1271. while (NULL != (p_ep = app_usbd_class_iface_ep_get(p_iface, ep_idx++)))
  1272. {
  1273. app_usbd_ep_instance_set(app_usbd_class_ep_address_get(p_ep), p_cinst);
  1274. }
  1275. }
  1276. /* Adding pointer to this instance to the end of the chain */
  1277. app_usbd_class_inst_t const * * pp_last = &m_p_first_cinst;
  1278. while (NULL != (*pp_last))
  1279. {
  1280. ASSERT((*pp_last) != p_cinst);
  1281. pp_last = &(app_usbd_class_data_access(*pp_last)->p_next);
  1282. }
  1283. (*pp_last) = p_cinst;
  1284. /* Successfully attached */
  1285. const app_usbd_evt_t evt_data = {.type = APP_USBD_EVT_INST_APPEND };
  1286. return class_event_handler(p_cinst, (app_usbd_complex_evt_t const *)(&evt_data));
  1287. }
  1288. ret_code_t app_usbd_class_remove(app_usbd_class_inst_t const * p_cinst)
  1289. {
  1290. ASSERT(NULL != p_cinst);
  1291. ASSERT(NULL != p_cinst->p_class_methods);
  1292. ASSERT(NULL != p_cinst->p_class_methods->event_handler);
  1293. /* This function should be only called if USBD is disabled */
  1294. ASSERT(!nrf_drv_usbd_is_enabled() && nrf_drv_usbd_is_initialized());
  1295. ret_code_t ret;
  1296. /* Remove this class from the chain */
  1297. app_usbd_class_inst_t const * * pp_last = &m_p_first_cinst;
  1298. while (NULL != (*pp_last))
  1299. {
  1300. if ((*pp_last) == p_cinst)
  1301. {
  1302. /* Inform class instance that removing process is going to be started */
  1303. const app_usbd_evt_t evt_data = {
  1304. .type = APP_USBD_EVT_INST_REMOVE
  1305. };
  1306. ret = class_event_handler(p_cinst, (app_usbd_complex_evt_t const *)(&evt_data));
  1307. if (ret != NRF_SUCCESS)
  1308. {
  1309. return ret;
  1310. }
  1311. /* Breaking chain */
  1312. (*pp_last) = (app_usbd_class_data_access(p_cinst)->p_next);
  1313. app_usbd_class_data_access(p_cinst)->p_next = NULL;
  1314. /* Disconnecting endpoints */
  1315. uint8_t ep_idx;
  1316. for (ep_idx = 0; ep_idx < NRF_USBD_EPIN_CNT; ++ep_idx)
  1317. {
  1318. nrf_drv_usbd_ep_t ep = NRF_DRV_USBD_EPIN(ep_idx);
  1319. if (app_usbd_ep_instance_get(ep) == p_cinst)
  1320. {
  1321. app_usbd_ep_instance_set(ep, NULL);
  1322. }
  1323. }
  1324. for (ep_idx = 0; ep_idx < NRF_USBD_EPOUT_CNT; ++ep_idx)
  1325. {
  1326. nrf_drv_usbd_ep_t ep = NRF_DRV_USBD_EPOUT(ep_idx);
  1327. if (app_usbd_ep_instance_get(ep) == p_cinst)
  1328. {
  1329. app_usbd_ep_instance_set(ep, NULL);
  1330. }
  1331. }
  1332. return NRF_SUCCESS;
  1333. }
  1334. pp_last = &(app_usbd_class_data_access(*pp_last)->p_next);
  1335. }
  1336. return NRF_ERROR_NOT_FOUND;
  1337. }
  1338. ret_code_t app_usbd_class_remove_all(void)
  1339. {
  1340. ret_code_t ret = NRF_SUCCESS;
  1341. while (NULL != m_p_first_cinst)
  1342. {
  1343. ret = app_usbd_class_remove(m_p_first_cinst);
  1344. if (ret != NRF_SUCCESS)
  1345. {
  1346. break;
  1347. }
  1348. }
  1349. return ret;
  1350. }
  1351. ret_code_t app_usbd_ep_handler_set(app_usbd_class_inst_t const * const p_cinst,
  1352. nrf_drv_usbd_ep_t ep,
  1353. app_usbd_ep_event_handler_t handler)
  1354. {
  1355. ASSERT(NULL != p_cinst);
  1356. ASSERT(NULL != handler);
  1357. /* This function should be only called if USBD is disabled */
  1358. ASSERT(!nrf_drv_usbd_is_enabled() && nrf_drv_usbd_is_initialized());
  1359. if (p_cinst != app_usbd_ep_instance_get(ep))
  1360. {
  1361. return NRF_ERROR_INVALID_PARAM;
  1362. }
  1363. (app_usbd_ep_conf_access(ep))->event_handler = handler;
  1364. return NRF_SUCCESS;
  1365. }
  1366. ret_code_t app_usbd_class_sof_register(app_usbd_class_inst_t const * p_cinst)
  1367. {
  1368. ASSERT(NULL != p_cinst);
  1369. ASSERT(NULL != p_cinst->p_class_methods);
  1370. ASSERT(NULL != p_cinst->p_class_methods->event_handler);
  1371. /* This function should be only called if USBD is disabled */
  1372. ASSERT(!nrf_drv_usbd_is_enabled() && nrf_drv_usbd_is_initialized());
  1373. /* Make sure it's not in interrupt SOF list */
  1374. app_usbd_class_inst_t const * * pp_last = &m_p_first_sof_interrupt_cinst;
  1375. while (NULL != (*pp_last))
  1376. {
  1377. ASSERT((*pp_last) != p_cinst);
  1378. pp_last = &(app_usbd_class_data_access(*pp_last)->p_sof_next);
  1379. }
  1380. /* Next SOF event requiring instance has to be NULL now */
  1381. ASSERT(NULL == (app_usbd_class_data_access(p_cinst)->p_sof_next));
  1382. /* Adding pointer to this instance to the end of the chain */
  1383. pp_last = &m_p_first_sof_cinst;
  1384. while (NULL != (*pp_last))
  1385. {
  1386. ASSERT((*pp_last) != p_cinst);
  1387. pp_last = &(app_usbd_class_data_access(*pp_last)->p_sof_next);
  1388. }
  1389. (*pp_last) = p_cinst;
  1390. return NRF_SUCCESS;
  1391. }
  1392. ret_code_t app_usbd_class_sof_unregister(app_usbd_class_inst_t const * p_cinst)
  1393. {
  1394. ASSERT(NULL != p_cinst);
  1395. /** This function should be only called if USBD is disabled */
  1396. ASSERT(!nrf_drv_usbd_is_enabled() && nrf_drv_usbd_is_initialized());
  1397. app_usbd_class_inst_t const * * pp_last = &m_p_first_sof_cinst;
  1398. while (NULL != (*pp_last))
  1399. {
  1400. if ((*pp_last) == p_cinst)
  1401. {
  1402. /* Breaking chain */
  1403. (*pp_last) = (app_usbd_class_data_access(p_cinst)->p_sof_next);
  1404. app_usbd_class_data_access(p_cinst)->p_sof_next = NULL;
  1405. return NRF_SUCCESS;
  1406. }
  1407. pp_last = &(app_usbd_class_data_access(*pp_last)->p_sof_next);
  1408. }
  1409. return NRF_ERROR_NOT_FOUND;
  1410. }
  1411. ret_code_t app_usbd_class_sof_interrupt_register(app_usbd_class_inst_t const * p_cinst, app_usbd_sof_interrupt_handler_t handler)
  1412. {
  1413. ASSERT(NULL != p_cinst);
  1414. ASSERT(NULL != p_cinst->p_class_methods);
  1415. ASSERT(NULL != handler);
  1416. /* This function should be only called if USBD is disabled */
  1417. ASSERT(!nrf_drv_usbd_is_enabled() && nrf_drv_usbd_is_initialized());
  1418. /* Next SOF event requiring instance has to be NULL now */
  1419. ASSERT(NULL == (app_usbd_class_data_access(p_cinst)->p_sof_next));
  1420. app_usbd_class_data_access(p_cinst)->sof_handler = handler;
  1421. /* Make sure it's not in normal SOF list */
  1422. app_usbd_class_inst_t const * * pp_last = &m_p_first_sof_cinst;
  1423. while (NULL != (*pp_last))
  1424. {
  1425. ASSERT((*pp_last) != p_cinst);
  1426. pp_last = &(app_usbd_class_data_access(*pp_last)->p_sof_next);
  1427. }
  1428. /* Adding pointer to this instance to the end of the interrupt chain */
  1429. pp_last = &m_p_first_sof_interrupt_cinst;
  1430. while (NULL != (*pp_last))
  1431. {
  1432. ASSERT((*pp_last) != p_cinst);
  1433. pp_last = &(app_usbd_class_data_access(*pp_last)->p_sof_next);
  1434. }
  1435. (*pp_last) = p_cinst;
  1436. return NRF_SUCCESS;
  1437. }
  1438. ret_code_t app_usbd_class_sof_interrupt_unregister(app_usbd_class_inst_t const * p_cinst)
  1439. {
  1440. ASSERT(NULL != p_cinst);
  1441. /** This function should be only called if USBD is disabled */
  1442. ASSERT(!nrf_drv_usbd_is_enabled() && nrf_drv_usbd_is_initialized());
  1443. app_usbd_class_inst_t const * * pp_last = &m_p_first_sof_interrupt_cinst;
  1444. while (NULL != (*pp_last))
  1445. {
  1446. if ((*pp_last) == p_cinst)
  1447. {
  1448. /* Breaking chain */
  1449. (*pp_last) = (app_usbd_class_data_access(p_cinst)->p_sof_next);
  1450. app_usbd_class_data_access(p_cinst)->p_sof_next = NULL;
  1451. return NRF_SUCCESS;
  1452. }
  1453. pp_last = &(app_usbd_class_data_access(*pp_last)->p_sof_next);
  1454. }
  1455. return NRF_ERROR_NOT_FOUND;
  1456. }
  1457. ret_code_t app_usbd_class_rwu_register(app_usbd_class_inst_t const * const p_inst)
  1458. {
  1459. ASSERT(p_inst != NULL);
  1460. ++m_rwu_registered_counter;
  1461. /*Overflow check*/
  1462. ASSERT(m_rwu_registered_counter != 0);
  1463. return NRF_SUCCESS;
  1464. }
  1465. ret_code_t app_usbd_class_rwu_unregister(app_usbd_class_inst_t const * const p_inst)
  1466. {
  1467. ASSERT(p_inst != NULL);
  1468. /* Usage validation. If counter is 0 unregister is not possible.*/
  1469. ASSERT(m_rwu_registered_counter != 0);
  1470. --m_rwu_registered_counter;
  1471. return NRF_SUCCESS;
  1472. }
  1473. bool app_usbd_class_rwu_enabled_check(void)
  1474. {
  1475. return (m_rwu_registered_counter != 0);
  1476. }
  1477. ret_code_t app_usbd_interface_ep_reset(app_usbd_class_inst_t const * const p_cinst,
  1478. uint8_t iface)
  1479. {
  1480. uint8_t iface_count = app_usbd_class_iface_count_get(p_cinst);
  1481. app_usbd_class_iface_conf_t const * p_iface = NULL;
  1482. for (uint8_t j = 0; j < iface_count; ++j)
  1483. {
  1484. p_iface = app_usbd_class_iface_get(p_cinst, j);
  1485. if (app_usbd_class_iface_number_get(p_iface) == iface)
  1486. {
  1487. break;
  1488. }
  1489. }
  1490. if (p_iface == NULL)
  1491. {
  1492. return NRF_ERROR_NOT_SUPPORTED;
  1493. }
  1494. uint8_t ep_count = app_usbd_class_iface_ep_count_get(p_iface);
  1495. for (uint8_t j = 0; j < ep_count; ++j)
  1496. {
  1497. /*Clear stall for every endpoint*/
  1498. app_usbd_class_ep_conf_t const * p_ep = app_usbd_class_iface_ep_get(p_iface, j);
  1499. if (!NRF_USBD_EPISO_CHECK(p_ep->address))
  1500. {
  1501. nrf_drv_usbd_ep_dtoggle_clear(p_ep->address);
  1502. nrf_drv_usbd_ep_stall_clear(p_ep->address);
  1503. }
  1504. }
  1505. return NRF_SUCCESS;
  1506. }
  1507. void app_usbd_ep_enable(nrf_drv_usbd_ep_t ep)
  1508. {
  1509. if (!NRF_USBD_EPISO_CHECK(ep))
  1510. {
  1511. nrf_drv_usbd_ep_dtoggle_clear(ep);
  1512. nrf_drv_usbd_ep_stall_clear(ep);
  1513. }
  1514. nrf_drv_usbd_ep_enable(ep);
  1515. }
  1516. void app_usbd_ep_disable(nrf_drv_usbd_ep_t ep)
  1517. {
  1518. nrf_drv_usbd_ep_disable(ep);
  1519. }
  1520. app_usbd_class_inst_t const * app_usbd_class_first_get(void)
  1521. {
  1522. return m_p_first_cinst;
  1523. }
  1524. app_usbd_class_inst_t const * app_usbd_class_sof_first_get(void)
  1525. {
  1526. return m_p_first_sof_cinst;
  1527. }
  1528. app_usbd_class_inst_t const * app_usbd_class_sof_interrupt_first_get(void)
  1529. {
  1530. return m_p_first_sof_interrupt_cinst;
  1531. }
  1532. app_usbd_class_inst_t const * app_usbd_iface_find(uint8_t iface, uint8_t * p_iface_idx)
  1533. {
  1534. app_usbd_class_inst_t const * p_inst = app_usbd_class_first_get();
  1535. while (p_inst != NULL)
  1536. {
  1537. uint8_t iface_count = app_usbd_class_iface_count_get(p_inst);
  1538. /* Iterate over interfaces */
  1539. for (uint8_t i = 0; i < iface_count; ++i)
  1540. {
  1541. app_usbd_class_iface_conf_t const * p_iface;
  1542. p_iface = app_usbd_class_iface_get(p_inst, i);
  1543. if (app_usbd_class_iface_number_get(p_iface) == iface)
  1544. {
  1545. if (p_iface_idx != NULL)
  1546. {
  1547. (*p_iface_idx) = i;
  1548. }
  1549. return p_inst;
  1550. }
  1551. }
  1552. p_inst = app_usbd_class_next_get(p_inst);
  1553. }
  1554. return NULL;
  1555. }
  1556. ret_code_t app_usbd_iface_call(
  1557. app_usbd_class_inst_t const * const p_class_inst,
  1558. uint8_t iface_idx,
  1559. app_usbd_complex_evt_t const * const p_event)
  1560. {
  1561. UNUSED_PARAMETER(iface_idx);
  1562. return class_event_handler(p_class_inst, p_event);
  1563. }
  1564. ret_code_t app_usbd_ep_call(nrf_drv_usbd_ep_t ep, app_usbd_complex_evt_t const * const p_event)
  1565. {
  1566. if (NRF_USBD_EP_VALIDATE(ep))
  1567. {
  1568. app_usbd_class_inst_t const * p_inst = app_usbd_ep_conf_access(ep)->p_cinst;
  1569. if (p_inst != NULL)
  1570. {
  1571. return class_event_handler(p_inst, p_event);
  1572. }
  1573. }
  1574. return NRF_ERROR_INVALID_ADDR;
  1575. }
  1576. void app_usbd_all_call(app_usbd_complex_evt_t const * const p_event)
  1577. {
  1578. app_usbd_class_inst_t const * p_inst;
  1579. for (p_inst = app_usbd_class_first_get(); NULL != p_inst;
  1580. p_inst = app_usbd_class_next_get(p_inst))
  1581. {
  1582. UNUSED_RETURN_VALUE(class_event_handler(p_inst, p_event));
  1583. }
  1584. }
  1585. ret_code_t app_usbd_all_until_served_call(app_usbd_complex_evt_t const * const p_event)
  1586. {
  1587. app_usbd_class_inst_t const * p_inst;
  1588. ret_code_t ret = NRF_ERROR_NOT_SUPPORTED;
  1589. /* Try to process via every instance */
  1590. for (p_inst = app_usbd_class_first_get(); NULL != p_inst;
  1591. p_inst = app_usbd_class_next_get(p_inst))
  1592. {
  1593. ret = class_event_handler(p_inst, p_event);
  1594. if (NRF_ERROR_NOT_SUPPORTED != ret)
  1595. {
  1596. /* Processing finished */
  1597. break;
  1598. }
  1599. }
  1600. return ret;
  1601. }
  1602. ret_code_t app_usbd_ep_transfer(
  1603. nrf_drv_usbd_ep_t ep,
  1604. nrf_drv_usbd_transfer_t const * const p_transfer)
  1605. {
  1606. if (!nrf_drv_usbd_ep_enable_check(ep))
  1607. {
  1608. return NRF_ERROR_INVALID_STATE;
  1609. }
  1610. if (m_sustate != SUSTATE_ACTIVE)
  1611. {
  1612. return NRF_ERROR_INVALID_STATE;
  1613. }
  1614. return nrf_drv_usbd_ep_transfer(ep, p_transfer);
  1615. }
  1616. ret_code_t app_usbd_ep_handled_transfer(
  1617. nrf_drv_usbd_ep_t ep,
  1618. nrf_drv_usbd_handler_desc_t const * const p_handler)
  1619. {
  1620. if (!nrf_drv_usbd_ep_enable_check(ep))
  1621. {
  1622. return NRF_ERROR_INVALID_STATE;
  1623. }
  1624. if (m_sustate != SUSTATE_ACTIVE)
  1625. {
  1626. return NRF_ERROR_INVALID_STATE;
  1627. }
  1628. return nrf_drv_usbd_ep_handled_transfer(ep, p_handler);
  1629. }
  1630. ret_code_t app_usbd_iface_select(
  1631. app_usbd_class_inst_t const * const p_inst,
  1632. uint8_t iface_idx,
  1633. uint8_t alternate)
  1634. {
  1635. ret_code_t ret = NRF_ERROR_NOT_SUPPORTED;
  1636. if (p_inst->p_class_methods->iface_select != NULL)
  1637. {
  1638. ret = p_inst->p_class_methods->iface_select(p_inst, iface_idx, alternate);
  1639. }
  1640. if(ret == NRF_ERROR_NOT_SUPPORTED)
  1641. {
  1642. ret = default_iface_select(p_inst, iface_idx, alternate);
  1643. }
  1644. return ret;
  1645. }
  1646. void app_usbd_iface_deselect(
  1647. app_usbd_class_inst_t const * const p_inst,
  1648. uint8_t iface_idx)
  1649. {
  1650. if (p_inst->p_class_methods->iface_deselect != NULL)
  1651. {
  1652. p_inst->p_class_methods->iface_deselect(p_inst, iface_idx);
  1653. }
  1654. default_iface_deselect(p_inst, iface_idx);
  1655. }
  1656. uint8_t app_usbd_iface_selection_get(
  1657. app_usbd_class_inst_t const * const p_inst,
  1658. uint8_t iface_idx)
  1659. {
  1660. uint8_t alt = 0;
  1661. if (p_inst->p_class_methods->iface_selection_get != NULL)
  1662. {
  1663. alt = p_inst->p_class_methods->iface_selection_get(p_inst, iface_idx);
  1664. }
  1665. return alt;
  1666. }
  1667. void app_usbd_all_iface_select_0(void)
  1668. {
  1669. app_usbd_class_inst_t const * p_inst = app_usbd_class_first_get();
  1670. while (p_inst != NULL)
  1671. {
  1672. uint8_t iface_count = app_usbd_class_iface_count_get(p_inst);
  1673. for (uint8_t i = 0; i < iface_count; ++i)
  1674. {
  1675. ret_code_t ret;
  1676. ret = app_usbd_iface_select(p_inst, i, 0);
  1677. ASSERT(ret == NRF_SUCCESS);
  1678. UNUSED_VARIABLE(ret);
  1679. }
  1680. p_inst = app_usbd_class_next_get(p_inst);
  1681. }
  1682. }
  1683. void app_usbd_all_iface_deselect(void)
  1684. {
  1685. app_usbd_class_inst_t const * p_inst = app_usbd_class_first_get();
  1686. while (p_inst != NULL)
  1687. {
  1688. uint8_t iface_count = app_usbd_class_iface_count_get(p_inst);
  1689. for (uint8_t i = 0; i < iface_count; ++i)
  1690. {
  1691. app_usbd_iface_deselect(p_inst, i);
  1692. }
  1693. p_inst = app_usbd_class_next_get(p_inst);
  1694. }
  1695. }
  1696. #endif //NRF_MODULE_ENABLED(APP_USBD)