app_usbd.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /**
  2. * Copyright (c) 2017 - 2019, 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. #ifndef APP_USBD_H__
  41. #define APP_USBD_H__
  42. #include "nrf_drv_usbd.h"
  43. #include "app_usbd_types.h"
  44. #include "app_usbd_class_base.h"
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. * @defgroup app_usbd USB Device high level library
  50. * @ingroup app_common
  51. *
  52. * @brief @tagAPI52840 Module for easy support for any USB device configuration.
  53. *
  54. * This module manages class instances that would create the USB device,
  55. * manages endpoints and interfaces transactions.
  56. * @{
  57. */
  58. /**
  59. * @brief Highest value of Frame Number in SOF packets.
  60. */
  61. #define APP_USBD_SOF_MAX 2047
  62. /**
  63. * @brief True if SOF timestamping is really provided.
  64. *
  65. * SOF timestamping is really provided if it was requested and if the logger is enabled.
  66. */
  67. #if ((APP_USBD_CONFIG_SOF_TIMESTAMP_PROVIDE) && (NRF_LOG_ENABLED))
  68. #define APP_USBD_PROVIDE_SOF_TIMESTAMP 1
  69. #else
  70. #define APP_USBD_PROVIDE_SOF_TIMESTAMP 0
  71. #endif
  72. /**
  73. * @brief SOF event handling modes.
  74. */
  75. #define APP_USBD_SOF_HANDLING_NORMAL_QUEUE 0 //!< Push SOF events into event queue.
  76. #define APP_USBD_SOF_HANDLING_COMPRESS_QUEUE 1 //!< Compress SOF events.
  77. #define APP_USBD_SOF_HANDLING_INTERRUPT 2 //!< Handle SOF events in interrupt.
  78. /**
  79. * @brief Configuration passed to @ref app_usbd_init.
  80. */
  81. typedef struct {
  82. #if (!(APP_USBD_CONFIG_EVENT_QUEUE_ENABLE)) || defined(__SDK_DOXYGEN__)
  83. /**
  84. * @brief User defined event handler.
  85. *
  86. * This function is called on every event from the interrupt.
  87. * It is prepared for external user function that would queue events to be processed
  88. * from the main context.
  89. * It should be used with operating systems with its own implementation of the queue.
  90. *
  91. * @param p_event The event structure pointer.
  92. *
  93. * @note This field is available only when USB internal queue is disabled
  94. * (see @ref APP_USBD_CONFIG_EVENT_QUEUE_ENABLE).
  95. */
  96. void (*ev_handler)(app_usbd_internal_evt_t const * const p_event);
  97. #endif
  98. #if (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE) || defined(__SDK_DOXYGEN__)
  99. /**
  100. * @brief User defined event handler.
  101. *
  102. * This function is called on every event from the interrupt.
  103. *
  104. * @param p_event The event structure pointer.
  105. * @param queued The event is visible in the queue.
  106. * If queue conflict is detected the event might not be accessible inside queue
  107. * until all write operations finish.
  108. * See @ref nrf_atfifo for more details.
  109. *
  110. * @note This field is available only when USBD internal queue is configured
  111. * (see @ref APP_USBD_CONFIG_EVENT_QUEUE_ENABLE).
  112. *
  113. * @note If is set to NULL no event would be called from interrupt.
  114. * @note This function is called before event is processed.
  115. * It means that if the event type is @ref APP_USBD_EVT_DRV_SETUP,
  116. * there would not be setup field present in the event structure.
  117. */
  118. void (*ev_isr_handler)(app_usbd_internal_evt_t const * const p_event, bool queued);
  119. #endif
  120. /**
  121. * @brief User defined event processor
  122. *
  123. * This function is called while state event is processed.
  124. *
  125. * * @note This field is available only when USBD internal queue is configured
  126. * (see @ref APP_USBD_CONFIG_EVENT_QUEUE_ENABLE).
  127. *
  128. * @param event Event type.
  129. * Only following events are sent into this function:
  130. * - APP_USBD_EVT_DRV_SOF
  131. * - APP_USBD_EVT_DRV_RESET - Note that it also exits suspend
  132. * - APP_USBD_EVT_DRV_SUSPEND
  133. * - APP_USBD_EVT_DRV_RESUME - It is also generated when remote wakeup is generated
  134. * - APP_USBD_EVT_START
  135. * - APP_USBD_EVT_STOP
  136. * - APP_USBD_EVT_STATE_CHANGED
  137. * - APP_USBD_EVT_POWER_DETECTED
  138. * - APP_USBD_EVT_POWER_REMOVED
  139. * - APP_USBD_EVT_POWER_READY
  140. */
  141. void (*ev_state_proc)(app_usbd_event_type_t event);
  142. /**
  143. * @brief SOF processing required by the user event processing.
  144. *
  145. * This flag would enable SOF processing for the user events regardless of the fact if any
  146. * of the implemented class requires SOF event.
  147. *
  148. * @note SOF event would be enabled anyway if any of the appended class requires SOF processing.
  149. */
  150. bool enable_sof;
  151. } app_usbd_config_t;
  152. #if (APP_USBD_PROVIDE_SOF_TIMESTAMP) || defined(__SDK_DOXYGEN__)
  153. /**
  154. * @brief Timestamp function for the logger.
  155. *
  156. * @return Current frame number taken directly from the last processed SOF.
  157. */
  158. uint32_t app_usbd_sof_timestamp_get(void);
  159. #endif
  160. /**
  161. * @brief USB library initialization.
  162. *
  163. * Call this function before any configuration or class attachment.
  164. * USBD peripheral would be ready to accept commands, and library would be ready,
  165. * but it would not be connected to the bus.
  166. * Call @ref app_usbd_enable to enable USBD communication with the host.
  167. *
  168. * @param p_config Configuration. NULL pointer might be passed here and default
  169. * configuration will be applied then.
  170. */
  171. ret_code_t app_usbd_init(app_usbd_config_t const * p_config);
  172. /**
  173. * @brief USB library un-initialization.
  174. *
  175. * @note Currently not supported.
  176. */
  177. ret_code_t app_usbd_uninit(void);
  178. #if (APP_USBD_CONFIG_POWER_EVENTS_PROCESS) || defined(__SDK_DOXYGEN__)
  179. /**
  180. * @brief Function to start USB related power events processing.
  181. *
  182. * This function should be called after @ref app_usbd_init and after all the
  183. * required classes were appended (@ref app_usbd_class_append).
  184. *
  185. * @retval NRF_SUCCESS Power events successfully initialized.
  186. * @retval NRF_ERROR_INVALID_STATE The state of the driver does not allow to enable
  187. * the power events processing.
  188. */
  189. ret_code_t app_usbd_power_events_enable(void);
  190. #endif
  191. /**
  192. * @brief Enable USBD.
  193. *
  194. * USBD is enabled.
  195. * Since now the high frequency clock may be requested when USB RESET would be detected.
  196. */
  197. void app_usbd_enable(void);
  198. /**
  199. * @brief Disable USBD.
  200. *
  201. * Disabled USDB peripheral cannot be accessed but also stops requesting
  202. * High Frequency clock and releases power regulator.
  203. *
  204. * @note This function cannot be called when USB is started. Stop it first.
  205. */
  206. void app_usbd_disable(void);
  207. /**
  208. * @brief Request USBD to start.
  209. *
  210. * The function sends start request to the event queue.
  211. * If the queue is enabled (@ref APP_USBD_CONFIG_EVENT_QUEUE_ENABLE) it would be processed
  212. * when the queue is processed.
  213. * If queue is disabled it would be processed immediately inside this function.
  214. * It means that if queue is disabled this function cannot be called from interrupt with priority
  215. * higher than USB interrupt.
  216. *
  217. * When start is processed it would:
  218. * 1. Start library.
  219. * 2. Enable interrupts.
  220. * 3. Enable USB pull-ups.
  221. *
  222. * @note
  223. * In some specific circumstances the library can be left not started and this function would
  224. * silently exit.
  225. * This may happen if some glitches appears on USB power line or if the plug was disconnected before
  226. * whole starting process finishes.
  227. * User would get the event from POWER peripheral then.
  228. * Also no @ref APP_USBD_EVT_STARTED event would be generated to the classes and user event handler.
  229. * For the safe code it is recommended to wait for @ref APP_USBD_EVT_STARTED event if anything
  230. * has to be initialized after USB driver is started (just before enabling the interrupts).
  231. * If library is properly started the @ref APP_USBD_EVT_STARTED event passed to the user handler
  232. * from this function body.
  233. */
  234. void app_usbd_start(void);
  235. /**
  236. * @brief Stop USB.
  237. *
  238. * The function sends stop request to the event queue.
  239. * If the queue is enabled (@ref APP_USBD_CONFIG_EVENT_QUEUE_ENABLE) it would be processed
  240. * when the queue is processed.
  241. * If queue is disabled it would be processed immediately inside this function.
  242. * It means that if queue is disabled this function cannot be called from interrupt with priority
  243. * higher than USB interrupt.
  244. *
  245. * When the event is processed interrupts and USB pull-ups are disabled.
  246. * The peripheral itself is left enabled so it can be programmed,
  247. * but a HOST sees it as a peripheral disconnection.
  248. *
  249. * @note
  250. * If the library is not started when this function is called it exits silently - also
  251. * no @ref APP_USBD_EVT_STOPPED is generated.
  252. */
  253. void app_usbd_stop(void);
  254. /**
  255. * @brief Request library to suspend.
  256. *
  257. * This function send suspend request to the event queue.
  258. *
  259. * @note This function should only be called after @ref APP_USBD_EVT_DRV_SUSPEND os received.
  260. * Internal suspend request processing would give no effect if the bus is not in suspend state.
  261. */
  262. void app_usbd_suspend_req(void);
  263. /**
  264. * @brief Request library to wake-up.
  265. *
  266. * This function send wakeup request to the event queue.
  267. *
  268. * @note Calling this function does not mean that peripheral is active - the wakeup request is sent
  269. * into message queue and needs to be processed.
  270. *
  271. * @retval true Wakeup generation has been started.
  272. * @retval false No wakeup would be generated becouse it is disabled by the host.
  273. */
  274. bool app_usbd_wakeup_req(void);
  275. /**
  276. * @brief Get information whether there is an active connection.
  277. *
  278. * Function to check if the communication with the bus is possible.
  279. *
  280. * @retval true The bus is active.
  281. * @retval false There is no connection or bus is suspended.
  282. */
  283. bool app_usbd_active_check(void);
  284. /**
  285. * @brief USBD event processor.
  286. *
  287. * Function to be called on each event to be processed by the library.
  288. */
  289. void app_usbd_event_execute(app_usbd_internal_evt_t const * const p_event);
  290. #if (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE) || defined(__SDK_DOXYGEN__)
  291. /**
  292. * @brief Function that process events from the queue.
  293. *
  294. * @note This function calls @ref app_usbd_event_execute internally.
  295. *
  296. * @retval true Event was processed.
  297. * @retval false The event queue is empty.
  298. */
  299. bool app_usbd_event_queue_process(void);
  300. #endif
  301. /**
  302. * @brief Add class instance.
  303. *
  304. * This function connects given instance into internal class instance chain and
  305. * into all required endpoints.
  306. * The instance event handler would be connected into endpoint by default,
  307. * but this can be overwritten by @ref app_usbd_ep_handler_set.
  308. *
  309. * After successful attachment @ref APP_USBD_EVT_INST_APPEND would be passed to class instance.
  310. *
  311. * @note This function can only be called after USBD library is initialized but still disabled.
  312. * Assertion would be generated otherwise.
  313. *
  314. * @param[in,out] p_cinst Instance to connect. Chain data would be written into writable instance data.
  315. *
  316. * @retval NRF_SUCCESS Instance successfully added.
  317. * @retval NRF_ERROR_BUSY Endpoint(s) not available.
  318. */
  319. ret_code_t app_usbd_class_append(app_usbd_class_inst_t const * p_cinst);
  320. /**
  321. * @brief Remove class instance.
  322. *
  323. * Instance is removed from instance chain.
  324. * Instance and event handlers are removed also from endpoints.
  325. * Endpoints used by by the class instance are left disabled.
  326. *
  327. * @note This function can only be called after USBD library is initialized but still disabled.
  328. * Assertion would be generated otherwise.
  329. *
  330. * @param p_cinst Instance pointer to remove.
  331. *
  332. * @retval NRF_SUCCESS Instance successfully removed.
  333. * @retval NRF_ERROR_NOT_FOUND Instance not found in the instance chain.
  334. */
  335. ret_code_t app_usbd_class_remove(app_usbd_class_inst_t const * p_cinst);
  336. /**
  337. * @brief Remove all class instances.
  338. *
  339. * This function basically calls @ref app_usbd_class_remove
  340. * on instances chain as long as there is any element left.
  341. *
  342. * @note This function can only be called after USBD library is initialized but still disabled.
  343. * Assertion would be generated otherwise.
  344. *
  345. * @sa app_usbd_class_remove
  346. *
  347. * @return Is should always return @ref NRF_SUCCESS.
  348. * Any error value returned would mean there is an error inside the library.
  349. */
  350. ret_code_t app_usbd_class_remove_all(void);
  351. /**
  352. * @brief Change endpoint handler.
  353. *
  354. * This function may be called for the endpoint only if the class instance is
  355. * already properly attached by the @ref app_usbd_class_append function.
  356. *
  357. * The endpoint event handler function can be only overwritten by the class instance
  358. * that was connected into the endpoint.
  359. *
  360. * @note This function can only be called after USBD library is initialized but still disabled.
  361. * Assertion would be generated otherwise.
  362. *
  363. * @param[in] p_cinst Instance of a class that wish to set new event handler.
  364. * It has to match currently configured instance for the selected endpoint.
  365. * In other situation error would be returned.
  366. * @param[in] ep Endpoint address to configure.
  367. * @param[in] handler Event handler function to set.
  368. *
  369. * @retval NRF_SUCCESS New handler successfully set
  370. * @retval NRF_ERROR_INVALID_PARAM p_cinst is not the same as currently set for the endpoint
  371. */
  372. ret_code_t app_usbd_ep_handler_set(app_usbd_class_inst_t const * p_cinst,
  373. nrf_drv_usbd_ep_t ep,
  374. app_usbd_ep_event_handler_t handler);
  375. /**
  376. * @brief Register class instance as the one that requires SOF events.
  377. *
  378. * This function should be called in reaction on APP_USBD_EVT_INST_APPEND event.
  379. * Connect the class instance to the list of instances that requires SOF processing.
  380. * If none of the appended instances requires SOF event - it is disabled.
  381. *
  382. * @param p_cinst Instance that requires SOF event.
  383. *
  384. * @retval NRF_SUCCESS Instance linked into SOF processing list.
  385. *
  386. * @sa app_usbd_class_sof_unregister
  387. */
  388. ret_code_t app_usbd_class_sof_register(app_usbd_class_inst_t const * p_cinst);
  389. /**
  390. * @brief Unregister class instance from SOF processing instances list.
  391. *
  392. * Every class that calls @ref app_usbd_class_sof_register have to call also unregistering function
  393. * in reaction to @ref APP_USBD_EVT_INST_REMOVE event.
  394. *
  395. * @param p_cinst Instance to be unregistered from SOF event processing list.
  396. *
  397. * @retval NRF_SUCCESS Instance linked into SOF processing list.
  398. * @retval NRF_ERROR_NOT_FOUND Instance not found in the SOF processing list.
  399. *
  400. * @sa app_usbd_class_sof_register
  401. */
  402. ret_code_t app_usbd_class_sof_unregister(app_usbd_class_inst_t const * p_cinst);
  403. /**
  404. * @brief Register class instance as the one that requires SOF events in interrupt.
  405. *
  406. * This function should be called in reaction on APP_USBD_EVT_INST_APPEND event.
  407. * Connect the class instance to the list of instances that requires SOF processing.
  408. * If none of the appended instances requires SOF event - it is disabled.
  409. *
  410. * @param p_cinst Instance that requires SOF event.
  411. * @param handler Handler to SOF event
  412. *
  413. * @retval NRF_SUCCESS Instance linked into SOF processing list.
  414. *
  415. * @sa app_usbd_class_sof_interrupt_unregister
  416. */
  417. ret_code_t app_usbd_class_sof_interrupt_register(app_usbd_class_inst_t const * p_cinst,
  418. app_usbd_sof_interrupt_handler_t handler);
  419. /**
  420. * @brief Unregister class instance from SOF processing in interrupt instances list.
  421. *
  422. * Every class that calls @ref app_usbd_class_sof_interrupt_register have to call
  423. * also unregistering function in reaction to @ref APP_USBD_EVT_INST_REMOVE event.
  424. *
  425. * @param p_cinst Instance to be unregistered from SOF processing in interrupt list.
  426. *
  427. * @retval NRF_SUCCESS Instance linked into SOF processing in interrupt list.
  428. * @retval NRF_ERROR_NOT_FOUND Instance not found in the SOF processing in interrupt list.
  429. *
  430. * @sa app_usbd_class_sof_interrupt_register
  431. */
  432. ret_code_t app_usbd_class_sof_interrupt_unregister(app_usbd_class_inst_t const * p_cinst);
  433. /**
  434. * @brief Register class on remote wake-up feature.
  435. *
  436. * @param[in] p_inst Instance of the class.
  437. *
  438. * @retval NRF_SUCCESS Instance that requires remote wake-up registered.
  439. */
  440. ret_code_t app_usbd_class_rwu_register(app_usbd_class_inst_t const * const p_inst);
  441. /**
  442. * @brief Unregister class from remote wake-up feature.
  443. *
  444. * @param[in] p_inst Instance of the class.
  445. *
  446. * @retval NRF_SUCCESS Instance that requires remote wake-up removed.
  447. */
  448. ret_code_t app_usbd_class_rwu_unregister(app_usbd_class_inst_t const * const p_inst);
  449. /**
  450. * @brief Check if there is any class with remote wakeup.
  451. *
  452. * The function checks internal registered class with remote wakeup counter.
  453. *
  454. * @sa app_usbd_class_rwu_register, app_usbd_class_rwu_unregister
  455. *
  456. * @retval true The remote wakeup functionality is required by some class instance.
  457. * @retval false There is no class instance that requires wakeup functionality.
  458. */
  459. bool app_usbd_class_rwu_enabled_check(void);
  460. /**
  461. * @brief Find a specified descriptor.
  462. *
  463. * @param[in] p_cinst Class instance.
  464. * @param[in] desc_type Descriptor type @ref app_usbd_descriptor_t
  465. * @param[in] desc_index Descriptor index.
  466. * @param[out] p_desc Pointer to escriptor.
  467. * @param[out] p_desc_len Length of descriptor.
  468. *
  469. * @return Standard error code @ref ret_code_t
  470. * @retval NRF_SUCCESS Descriptor successfully found.
  471. * @retval NRF_ERROR_NOT_FOUND Descriptor not found.
  472. * */
  473. ret_code_t app_usbd_class_descriptor_find(app_usbd_class_inst_t const * const p_cinst,
  474. uint8_t desc_type,
  475. uint8_t desc_index,
  476. uint8_t * p_desc,
  477. size_t * p_desc_len);
  478. /**
  479. * @brief Standard set interface request handle.
  480. *
  481. * This function should be called when processing SET_INTERFACE request.
  482. *
  483. * @param[in] p_cinst Instance of a class.
  484. * @param[in] iface Interface number.
  485. *
  486. * @return Standard error code.
  487. *
  488. * @note Selected interface to reset has to be part of given class.
  489. * */
  490. ret_code_t app_usbd_interface_ep_reset(app_usbd_class_inst_t const * const p_cinst,
  491. uint8_t iface);
  492. /**
  493. * @brief Enable selected endpoint.
  494. *
  495. * Selected endpoint is enabled and cleared.
  496. *
  497. * @param ep Endpoint number.
  498. */
  499. void app_usbd_ep_enable(nrf_drv_usbd_ep_t ep);
  500. /**
  501. * @brief Disable selected endpoint.
  502. *
  503. * @param ep Endpoint number.
  504. */
  505. void app_usbd_ep_disable(nrf_drv_usbd_ep_t ep);
  506. /**
  507. * @name Iterate through classes lists
  508. *
  509. * Functions that helps to iterate through internally chained classes.
  510. * @{
  511. */
  512. /**
  513. * @brief Get first class instance in the list.
  514. *
  515. * Get first instance from the list of active class instances.
  516. * That instance may be used then in @ref app_usbd_class_next_get function.
  517. *
  518. * @return First instance in the list or NULL if there are no instances available.
  519. */
  520. app_usbd_class_inst_t const * app_usbd_class_first_get(void);
  521. /**
  522. * @brief Get next instance in the list.
  523. *
  524. * Get the next instance from the list of active instances.
  525. * Used to iterate through all instances.
  526. *
  527. * @param[in] p_cinst The current instance from with next one is required.
  528. *
  529. * @return Next instance to the given one or NULL if there is no more instances in the list.
  530. */
  531. static inline app_usbd_class_inst_t const * app_usbd_class_next_get(
  532. app_usbd_class_inst_t const * const p_cinst)
  533. {
  534. ASSERT(NULL != p_cinst);
  535. return app_usbd_class_data_access(p_cinst)->p_next;
  536. }
  537. /**
  538. * @brief Get first instance in SOF list.
  539. *
  540. * Start iteration through the list of instances that require SOF event processing.
  541. *
  542. * @return First instance in the list or NULL if the list is empty.
  543. *
  544. * @sa app_usbd_class_first_get
  545. */
  546. app_usbd_class_inst_t const * app_usbd_class_sof_first_get(void);
  547. /**
  548. * @brief Get next instance in the SOF list.
  549. *
  550. * Get the next instance from the list of instances requiring SOF event processing.
  551. * Used to iterate through all SOF instances.
  552. *
  553. * @param p_cinst The current instance from with next one is required.
  554. *
  555. * @return Next instance to the given one or NULL if there is no more instances in the list.
  556. */
  557. static inline app_usbd_class_inst_t const * app_usbd_class_sof_next_get(
  558. app_usbd_class_inst_t const * const p_cinst)
  559. {
  560. ASSERT(NULL != p_cinst);
  561. return app_usbd_class_data_access(p_cinst)->p_sof_next;
  562. }
  563. /**
  564. * @brief Get first instance in SOF interrupt list.
  565. *
  566. * Start iteration through the list of instances that require SOF processing in interrupt.
  567. *
  568. * @return First instance in the list or NULL if the list is empty.
  569. *
  570. * @sa app_usbd_class_first_get
  571. */
  572. app_usbd_class_inst_t const * app_usbd_class_sof_interrupt_first_get(void);
  573. /**
  574. * @brief Get next instance in the SOF interrupt list.
  575. *
  576. * Get the next instance from the list of instances requiring SOF processing in interrupt.
  577. * Used to iterate through all SOF instances that have SOF handlers.
  578. *
  579. * @param p_cinst The current instance from with next one is required.
  580. *
  581. * @return Next instance to the given one or NULL if there is no more instances in the list.
  582. */
  583. static inline app_usbd_class_inst_t const * app_usbd_class_sof_interrupt_next_get(
  584. app_usbd_class_inst_t const * const p_cinst)
  585. {
  586. ASSERT(NULL != p_cinst);
  587. return app_usbd_class_data_access(p_cinst)->p_sof_next;
  588. }
  589. /** @} */
  590. /**
  591. * @brief Search for selected interface.
  592. *
  593. * Function searches for the given interface number and returns the class that contains it.
  594. * Optionally it can return interface index inside class instance.
  595. *
  596. * @param[in] iface Interface number.
  597. * @param[out] p_iface_idx Pointer to a variable that would hold interface index inside returned
  598. * class instance.
  599. *
  600. * @return Pointer to the class structure that cointain given interface or NULL if not found.
  601. */
  602. app_usbd_class_inst_t const * app_usbd_iface_find(uint8_t iface, uint8_t * p_iface_idx);
  603. /**
  604. * @name Communicate with interfaces, endpoints and instances inside usbd library
  605. *
  606. * @{
  607. */
  608. /**
  609. * @brief Call interface event handler.
  610. *
  611. * Call event handler for selected interface.
  612. * @param[in,out] p_class_inst Class instance that holds selected interface.
  613. * @param[in] iface_idx Index of the interface in class structure.
  614. * @param[in] p_event Event structure to be processed.
  615. *
  616. * @return Operation status.
  617. */
  618. ret_code_t app_usbd_iface_call(
  619. app_usbd_class_inst_t const * const p_class_inst,
  620. uint8_t iface_idx,
  621. app_usbd_complex_evt_t const * const p_event);
  622. /**
  623. * @brief Call endpoint event handler.
  624. *
  625. * Call event handler for the selected endpoint.
  626. * @param[in] ep Endpoint number.
  627. * @param[in] p_event Event structure to send.
  628. *
  629. * @return Operation status.
  630. */
  631. ret_code_t app_usbd_ep_call(nrf_drv_usbd_ep_t ep, app_usbd_complex_evt_t const * const p_event);
  632. /**
  633. * @brief Auxiliary function that process event by every instance in the list.
  634. *
  635. * This function ignores the result of called handler.
  636. *
  637. * @param p_event Event to pass to every instance.
  638. */
  639. void app_usbd_all_call(app_usbd_complex_evt_t const * const p_event);
  640. /**
  641. * @brief Call interface event handlers and stop when served.
  642. *
  643. * Call event handlers from instances as long as we get result different than @ref NRF_ERROR_NOT_SUPPORTED
  644. * @param[in] p_event Event structure to send.
  645. *
  646. * @return Operation status or @ref NRF_ERROR_NOT_SUPPORTED if none of instances in the list can support given event.
  647. */
  648. ret_code_t app_usbd_all_until_served_call(app_usbd_complex_evt_t const * const p_event);
  649. /** @} */
  650. /**
  651. * @brief Endpoint transfer.
  652. *
  653. * @param ep Endpoint number.
  654. * @param p_transfer Description of the transfer to be performed.
  655. * The direction of the transfer is determined by the
  656. * endpoint number.
  657. *
  658. * @retval NRF_ERROR_INVALID_STATE The state of the USB device does not allow
  659. * data transfer on the endpoint.
  660. * @return Values returned by @ref nrf_drv_usbd_ep_transfer.
  661. *
  662. * @sa app_usbd_ep_handled_transfer
  663. */
  664. ret_code_t app_usbd_ep_transfer(
  665. nrf_drv_usbd_ep_t ep,
  666. nrf_drv_usbd_transfer_t const * const p_transfer);
  667. /**
  668. * @brief Set up an endpoint handled transfer.
  669. *
  670. * Configures a transfer handled by the feedback function.
  671. *
  672. * @param ep Endpoint number.
  673. * @param p_handler Function called when the next chunk of data is requested.
  674. *
  675. * @retval NRF_ERROR_INVALID_STATE The state of the USB device does not allow
  676. * data transfer on the endpoint.
  677. * @return Values returned by @ref nrf_drv_usbd_ep_handled_transfer.
  678. */
  679. ret_code_t app_usbd_ep_handled_transfer(
  680. nrf_drv_usbd_ep_t ep,
  681. nrf_drv_usbd_handler_desc_t const * const p_handler);
  682. /**
  683. * @brief Select interface
  684. *
  685. * Select the given interface.
  686. * This function calls class interface selection function or default
  687. * interface selection function.
  688. *
  689. * After calling this function interface should be functional.
  690. *
  691. * @param[in,out] p_inst Instance of the class.
  692. * @param[in] iface_idx Index of the interface inside class structure.
  693. * @param[in] alternate Alternate setting that should be selected.
  694. *
  695. * @return Standard error code.
  696. */
  697. ret_code_t app_usbd_iface_select(
  698. app_usbd_class_inst_t const * const p_inst,
  699. uint8_t iface_idx,
  700. uint8_t alternate);
  701. /**
  702. * @brief Deselect interface.
  703. *
  704. * Disable the given interface.
  705. * This function calls class interface deselection function or
  706. * default interface selection function.
  707. *
  708. * After calling this function all the endpoints from the interface
  709. * have to be disabled.
  710. *
  711. * @param[in,out] p_inst Instance of the class.
  712. * @param[in] iface_idx Index of the interface inside class structure.
  713. */
  714. void app_usbd_iface_deselect(
  715. app_usbd_class_inst_t const * const p_inst,
  716. uint8_t iface_idx);
  717. /**
  718. * @brief Get selected interface.
  719. *
  720. * Function retieves currently selected interface.
  721. * If the class contains @ref app_usbd_class_methods_t::iface_selection_get it is called.
  722. * It it does not contain this function this function would return default, 0 value.
  723. *
  724. * @param[in] p_inst Instance of the class.
  725. * @param[in] iface_idx Index of the interface inside class structure.
  726. *
  727. * @return Selected alternate interface setting.
  728. */
  729. uint8_t app_usbd_iface_selection_get(
  730. app_usbd_class_inst_t const * const p_inst,
  731. uint8_t iface_idx);
  732. /**
  733. * @brief Select alternate configuration 0 for all interfaces.
  734. *
  735. * Auxiliary function that clears settings for all interfaces leaving them enabled.
  736. */
  737. void app_usbd_all_iface_select_0(void);
  738. /**
  739. * @brief Deselect all interfaces.
  740. *
  741. * Auxiliary function to disable all interfaces.
  742. */
  743. void app_usbd_all_iface_deselect(void);
  744. /** @} */
  745. #ifdef __cplusplus
  746. }
  747. #endif
  748. #endif /* APP_USBD_H__ */