nrfx_usbd.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. #ifndef NRFX_USBD_H__
  41. #define NRFX_USBD_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_usbd.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @defgroup nrfx_usbd USBD driver
  49. * @{
  50. * @ingroup nrf_usbd
  51. * @brief Universal Serial Bus Device (USBD) peripheral driver.
  52. */
  53. /**
  54. * @brief Number of bytes in the endpoint.
  55. */
  56. #define NRFX_USBD_EPSIZE 64
  57. /**
  58. * @brief Number of bytes for isochronous endpoints.
  59. *
  60. * Number of bytes for isochronous endpoints in total.
  61. * This number would be shared between IN and OUT endpoint.
  62. * It may be also assigned totaly to one endpoint.
  63. * @sa nrf_usbd_isosplit_set
  64. * @sa nrf_usbd_isosplit_get
  65. */
  66. #define NRFX_USBD_ISOSIZE 1024
  67. /**
  68. * @brief The size of internal feeder buffer.
  69. *
  70. * @sa nrfx_usbd_feeder_buffer_get
  71. */
  72. #define NRFX_USBD_FEEDER_BUFFER_SIZE NRFX_USBD_EPSIZE
  73. /**
  74. * @name Macros for creating endpoint identifiers.
  75. *
  76. * Auxiliary macros for creating endpoint identifiers compatible with the USB specification.
  77. * @{
  78. * @brief Create identifier for IN endpoint.
  79. *
  80. * Simple macro to create IN endpoint identifier for given endpoint number.
  81. *
  82. * @param[in] n Endpoint number.
  83. *
  84. * @return Endpoint identifier that connects endpoint number and endpoint direction.
  85. */
  86. #define NRFX_USBD_EPIN(n) ((nrfx_usbd_ep_t)NRF_USBD_EPIN(n))
  87. /**
  88. * @brief Create identifier for OUT endpoint.
  89. *
  90. * Simple macro to create OUT endpoint identifier for given endpoint number.
  91. *
  92. * @param[in] n Endpoint number.
  93. *
  94. * @return Endpoint identifier that connects endpoint number and endpoint direction.
  95. */
  96. #define NRFX_USBD_EPOUT(n) ((nrfx_usbd_ep_t)NRF_USBD_EPOUT(n))
  97. /** @} */
  98. /**
  99. * @brief Endpoint identifier.
  100. *
  101. * Endpoint identifier used in the driver.
  102. * This endpoint number is consistent with USB 2.0 specification.
  103. */
  104. typedef enum
  105. {
  106. NRFX_USBD_EPOUT0 = NRF_USBD_EPOUT(0), /**< Endpoint OUT 0 */
  107. NRFX_USBD_EPOUT1 = NRF_USBD_EPOUT(1), /**< Endpoint OUT 1 */
  108. NRFX_USBD_EPOUT2 = NRF_USBD_EPOUT(2), /**< Endpoint OUT 2 */
  109. NRFX_USBD_EPOUT3 = NRF_USBD_EPOUT(3), /**< Endpoint OUT 3 */
  110. NRFX_USBD_EPOUT4 = NRF_USBD_EPOUT(4), /**< Endpoint OUT 4 */
  111. NRFX_USBD_EPOUT5 = NRF_USBD_EPOUT(5), /**< Endpoint OUT 5 */
  112. NRFX_USBD_EPOUT6 = NRF_USBD_EPOUT(6), /**< Endpoint OUT 6 */
  113. NRFX_USBD_EPOUT7 = NRF_USBD_EPOUT(7), /**< Endpoint OUT 7 */
  114. NRFX_USBD_EPOUT8 = NRF_USBD_EPOUT(8), /**< Endpoint OUT 8 */
  115. NRFX_USBD_EPIN0 = NRF_USBD_EPIN(0), /**< Endpoint IN 0 */
  116. NRFX_USBD_EPIN1 = NRF_USBD_EPIN(1), /**< Endpoint IN 1 */
  117. NRFX_USBD_EPIN2 = NRF_USBD_EPIN(2), /**< Endpoint IN 2 */
  118. NRFX_USBD_EPIN3 = NRF_USBD_EPIN(3), /**< Endpoint IN 3 */
  119. NRFX_USBD_EPIN4 = NRF_USBD_EPIN(4), /**< Endpoint IN 4 */
  120. NRFX_USBD_EPIN5 = NRF_USBD_EPIN(5), /**< Endpoint IN 5 */
  121. NRFX_USBD_EPIN6 = NRF_USBD_EPIN(6), /**< Endpoint IN 6 */
  122. NRFX_USBD_EPIN7 = NRF_USBD_EPIN(7), /**< Endpoint IN 7 */
  123. NRFX_USBD_EPIN8 = NRF_USBD_EPIN(8), /**< Endpoint IN 8 */
  124. } nrfx_usbd_ep_t;
  125. /**
  126. * @brief Events generated by the driver.
  127. *
  128. * Enumeration of possible events that may be generated by the driver.
  129. */
  130. typedef enum
  131. {
  132. NRFX_USBD_EVT_SOF, /**< Start Of Frame event on USB bus detected. */
  133. NRFX_USBD_EVT_RESET, /**< Reset condition on USB bus detected. */
  134. NRFX_USBD_EVT_SUSPEND, /**< This device should go to suspend mode now. */
  135. NRFX_USBD_EVT_RESUME, /**< This device should resume from suspend now. */
  136. NRFX_USBD_EVT_WUREQ, /**< Wakeup request - the USBD peripheral is ready to generate
  137. WAKEUP signal after exiting low power mode. */
  138. NRFX_USBD_EVT_SETUP, /**< Setup frame received and decoded. */
  139. NRFX_USBD_EVT_EPTRANSFER, /**< For Rx (OUT: Host->Device):
  140. * 1. The packet has been received but there is no buffer prepared for transfer already.
  141. * 2. Whole transfer has been finished.
  142. *
  143. * For Tx (IN: Device->Host):
  144. * The last packet from requested transfer has been transfered over USB bus and acknowledged.
  145. */
  146. NRFX_USBD_EVT_CNT /**< Number of defined events. */
  147. } nrfx_usbd_event_type_t;
  148. /**
  149. * @brief Endpoint status codes.
  150. *
  151. * Status codes that may be returned by @ref nrfx_usbd_ep_status_get or, except for
  152. * @ref NRFX_USBD_EP_BUSY, reported together with @ref NRFX_USBD_EVT_EPTRANSFER.
  153. */
  154. typedef enum
  155. {
  156. NRFX_USBD_EP_OK, /**< No error occured. */
  157. NRFX_USBD_EP_WAITING, /**< Data received, no buffer prepared already - waiting for configured transfer. */
  158. NRFX_USBD_EP_OVERLOAD, /**< Received number of bytes cannot fit given buffer.
  159. * This error would also be returned when next_transfer function has been defined
  160. * but currently received data cannot fit completely in current buffer.
  161. * No data split from single endpoint transmission is supported.
  162. *
  163. * When this error is reported - data is left inside endpoint buffer.
  164. * Clear endpoint or prepare new buffer and read it.
  165. */
  166. NRFX_USBD_EP_ABORTED, /**< EP0 transfer can be aborted when new setup comes.
  167. * Any other transfer can be aborted by USB reset or driver stopping.
  168. */
  169. NRFX_USBD_EP_BUSY, /**< Transfer is in progress. */
  170. } nrfx_usbd_ep_status_t;
  171. /**
  172. * @brief Event structure.
  173. *
  174. * Structure passed to event handler.
  175. */
  176. typedef struct
  177. {
  178. nrfx_usbd_event_type_t type; /**< Event type. */
  179. union
  180. {
  181. struct {
  182. uint16_t framecnt; /**< Current value of frame counter. */
  183. } sof; /**< Data available for @ref NRFX_USBD_EVT_SOF. */
  184. struct {
  185. nrfx_usbd_ep_t ep; /**< Endpoint number. */
  186. } isocrc;
  187. struct {
  188. nrfx_usbd_ep_t ep; /**< Endpoint number. */
  189. nrfx_usbd_ep_status_t status; /**< Status for the endpoint. */
  190. } eptransfer;
  191. } data; /**< Union to store event data. */
  192. } nrfx_usbd_evt_t;
  193. /**
  194. * @brief USBD event callback function type.
  195. *
  196. * @param[in] p_event Event information structure.
  197. */
  198. typedef void (*nrfx_usbd_event_handler_t)(nrfx_usbd_evt_t const * p_event);
  199. /**
  200. * @brief Universal data pointer.
  201. *
  202. * Universal data pointer that can be used for any type of transfer.
  203. */
  204. typedef union
  205. {
  206. void const * tx; //!< Constant TX buffer pointer.
  207. void * rx; //!< Writable RX buffer pointer.
  208. uint32_t addr; //!< Numeric value used internally by the driver.
  209. } nrfx_usbd_data_ptr_t;
  210. /**
  211. * @brief Structure to be filled with information about the next transfer.
  212. *
  213. * This is used mainly for transfer feeders and consumers.
  214. * It describes a single endpoint transfer and therefore the size of the buffer
  215. * can never be higher than the endpoint size.
  216. */
  217. typedef struct
  218. {
  219. nrfx_usbd_data_ptr_t p_data; //!< Union with available data pointers used by the driver.
  220. size_t size; //!< Size of the requested transfer.
  221. } nrfx_usbd_ep_transfer_t;
  222. /**
  223. * @brief Flags for the current transfer.
  224. *
  225. * Flags configured for the transfer that can be merged using the bitwise 'or' operator (|).
  226. */
  227. typedef enum
  228. {
  229. NRFX_USBD_TRANSFER_ZLP_FLAG = 1U << 0, //!< Add a zero-length packet.
  230. } nrfx_usbd_transfer_flags_t;
  231. /**
  232. * @brief Total transfer configuration.
  233. *
  234. * This structure is used to configure total transfer information.
  235. * It is used by internal built-in feeders and consumers.
  236. */
  237. typedef struct
  238. {
  239. nrfx_usbd_data_ptr_t p_data; //!< Union with available data pointers used by the driver.
  240. size_t size; //!< Total size of the requested transfer.
  241. uint32_t flags; //!< Transfer flags.
  242. /**< Use the @ref nrfx_usbd_transfer_flags_t values. */
  243. } nrfx_usbd_transfer_t;
  244. /**
  245. * @brief Auxiliary macro for declaring IN transfer description with optional flags.
  246. *
  247. * The base macro for creating transfers with any configuration option.
  248. *
  249. * @param name Instance name.
  250. * @param tx_buff Buffer to transfer.
  251. * @param tx_size Transfer size.
  252. * @param tx_flags Flags for the transfer (see @ref nrfx_usbd_transfer_flags_t).
  253. *
  254. * @return Configured variable with total transfer description.
  255. */
  256. #define NRFX_USBD_TRANSFER_IN(name, tx_buff, tx_size, tx_flags) \
  257. const nrfx_usbd_transfer_t name = { \
  258. .p_data = { .tx = (tx_buff) }, \
  259. .size = (tx_size), \
  260. .flags = (tx_flags) \
  261. }
  262. /**
  263. * @brief Helper macro for declaring OUT transfer item (@ref nrfx_usbd_transfer_t).
  264. *
  265. * @param name Instance name.
  266. * @param rx_buff Buffer to transfer.
  267. * @param rx_size Transfer size.
  268. * */
  269. #define NRFX_USBD_TRANSFER_OUT(name, rx_buff, rx_size) \
  270. const nrfx_usbd_transfer_t name = { \
  271. .p_data = { .rx = (rx_buff) }, \
  272. .size = (rx_size), \
  273. .flags = 0 \
  274. }
  275. /**
  276. * @brief USBD transfer feeder.
  277. *
  278. * Pointer for a transfer feeder.
  279. * Transfer feeder is a feedback function used to prepare a single
  280. * TX (Device->Host) endpoint transfer.
  281. *
  282. * The transfers provided by the feeder must be simple:
  283. * - The size of the transfer provided by this function is limited to a single endpoint buffer.
  284. * Bigger transfers are not handled automatically in this case.
  285. * - Flash transfers are not automatically supported- you must copy them to the RAM buffer before.
  286. *
  287. * @note
  288. * This function may use @ref nrfx_usbd_feeder_buffer_get to gain a temporary buffer
  289. * that can be used to prepare transfer.
  290. *
  291. * @param[out] p_next Structure with the data for the next transfer to be filled.
  292. * Required only if the function returns true.
  293. * @param[in,out] p_context Context variable configured with the transfer.
  294. * @param[in] ep_size The endpoint size.
  295. *
  296. * @retval false The current transfer is the last one - you do not need to call
  297. * the function again.
  298. * @retval true There is more data to be prepared and when the current transfer
  299. * finishes, the feeder function is expected to be called again.
  300. */
  301. typedef bool (*nrfx_usbd_feeder_t)(nrfx_usbd_ep_transfer_t * p_next,
  302. void * p_context,
  303. size_t ep_size);
  304. /**
  305. * @brief USBD transfer consumer.
  306. *
  307. * Pointer for a transfer consumer.
  308. * Transfer consumer is a feedback function used to prepare a single
  309. * RX (Host->Device) endpoint transfer.
  310. *
  311. * The transfer must provide a buffer big enough to fit the whole data from the endpoint.
  312. * Otherwise, the NRFX_USBD_EP_OVERLOAD event is generated.
  313. *
  314. * @param[out] p_next Structure with the data for the next transfer to be filled.
  315. * Required only if the function returns true.
  316. * @param[in,out] p_context Context variable configured with the transfer.
  317. * @param[in] ep_size The endpoint size.
  318. * @param[in] data_size Number of received bytes in the endpoint buffer.
  319. *
  320. * @retval false Current transfer is the last one - you do not need to call
  321. * the function again.
  322. * @retval true There is more data to be prepared and when current transfer
  323. * finishes, the feeder function is expected to be called again.
  324. */
  325. typedef bool (*nrfx_usbd_consumer_t)(nrfx_usbd_ep_transfer_t * p_next,
  326. void * p_context,
  327. size_t ep_size,
  328. size_t data_size);
  329. /**
  330. * @brief Universal transfer handler.
  331. *
  332. * Union with feeder and consumer function pointer.
  333. */
  334. typedef union
  335. {
  336. nrfx_usbd_feeder_t feeder; //!< Feeder function pointer.
  337. nrfx_usbd_consumer_t consumer; //!< Consumer function pointer.
  338. } nrfx_usbd_handler_t;
  339. /**
  340. * @brief USBD transfer descriptor.
  341. *
  342. * Universal structure that may hold the setup for callback configuration for
  343. * IN or OUT type of the transfer.
  344. */
  345. typedef struct
  346. {
  347. nrfx_usbd_handler_t handler; //!< Handler for the current transfer, function pointer.
  348. void * p_context; //!< Context for the transfer handler.
  349. } nrfx_usbd_handler_desc_t;
  350. /**
  351. * @brief Setup packet structure.
  352. *
  353. * Structure that contains interpreted SETUP packet as described in USB specification.
  354. */
  355. typedef struct
  356. {
  357. uint8_t bmRequestType; //!< byte 0
  358. uint8_t bRequest; //!< byte 1
  359. uint16_t wValue; //!< byte 2, 3
  360. uint16_t wIndex; //!< byte 4, 5
  361. uint16_t wLength; //!< byte 6, 7
  362. } nrfx_usbd_setup_t;
  363. /**
  364. * @brief Driver initialization.
  365. *
  366. * @param[in] event_handler Event handler provided by the user. Cannot be null.
  367. *
  368. * @retval NRFX_SUCCESS Initialization successful.
  369. * @retval NRFX_ERROR_INVALID_STATE Driver was already initialized.
  370. */
  371. nrfx_err_t nrfx_usbd_init(nrfx_usbd_event_handler_t event_handler);
  372. /**
  373. * @brief Driver deinitialization.
  374. */
  375. void nrfx_usbd_uninit(void);
  376. /**
  377. * @brief Enable the USBD port.
  378. *
  379. * After calling this function USBD peripheral would be enabled.
  380. * The USB LDO would be enabled.
  381. * Enabled USBD peripheral would request HFCLK.
  382. * This function does not enable external oscillator, so if it is not enabled by other part of the
  383. * program after enabling USBD driver HFINT would be used for the USBD peripheral.
  384. * It is perfectly fine until USBD is started. See @ref nrfx_usbd_start.
  385. *
  386. * In normal situation this function should be called in reaction to USBDETECTED
  387. * event from POWER peripheral.
  388. *
  389. * Interrupts and USB pins pull-up would stay disabled until @ref nrfx_usbd_start
  390. * function is called.
  391. */
  392. void nrfx_usbd_enable(void);
  393. /**
  394. * @brief Disable the USBD port.
  395. *
  396. * After calling this function USBD peripheral would be disabled.
  397. * No events would be detected or processed by the driver.
  398. * Clock for the peripheral would be disconnected.
  399. */
  400. void nrfx_usbd_disable(void);
  401. /**
  402. * @brief Start USB functionality.
  403. *
  404. * After calling this function USBD peripheral should be fully functional
  405. * and all new incoming events / interrupts would be processed by the driver.
  406. *
  407. * Also only after calling this function host sees new connected device.
  408. *
  409. * Call this function when USBD power LDO regulator is ready - on USBPWRRDY event
  410. * from POWER peripheral.
  411. *
  412. * Before USBD interrupts are enabled, external HFXO is requested.
  413. *
  414. * @param enable_sof The flag that is used to enable SOF processing.
  415. * If it is false, SOF interrupt is left disabled and will not be generated.
  416. * This improves power saving if SOF is not required.
  417. *
  418. * @note If the isochronous endpoints are going to be used,
  419. * it is required to enable the SOF.
  420. * In other case any isochronous endpoint would stay busy
  421. * after first transmission.
  422. */
  423. void nrfx_usbd_start(bool enable_sof);
  424. /**
  425. * @brief Stop USB functionality.
  426. *
  427. * This function disables USBD pull-up and interrupts.
  428. *
  429. * The HFXO request is released in this function.
  430. *
  431. * @note
  432. * This function can also be used to logically disconnect USB from the HOST that
  433. * would force it to enumerate device after calling @ref nrfx_usbd_start.
  434. */
  435. void nrfx_usbd_stop(void);
  436. /**
  437. * @brief Check if driver is initialized.
  438. *
  439. * @retval false Driver is not initialized.
  440. * @retval true Driver is initialized.
  441. */
  442. bool nrfx_usbd_is_initialized(void);
  443. /**
  444. * @brief Check if driver is enabled.
  445. *
  446. * @retval false Driver is disabled.
  447. * @retval true Driver is enabled.
  448. */
  449. bool nrfx_usbd_is_enabled(void);
  450. /**
  451. * @brief Check if driver is started.
  452. *
  453. * @retval false Driver is not started.
  454. * @retval true Driver is started (fully functional).
  455. * @note The USBD peripheral interrupt state is checked.
  456. */
  457. bool nrfx_usbd_is_started(void);
  458. /**
  459. * @brief Suspend USBD operation.
  460. *
  461. * The USBD peripheral is forced to go into the low power mode.
  462. * The function has to be called in the reaction to @ref NRFX_USBD_EVT_SUSPEND event
  463. * when the firmware is ready.
  464. *
  465. * After successful call of this function most of the USBD registers would be unavailable.
  466. *
  467. * @note Check returned value for the feedback if suspending was successful.
  468. *
  469. * @retval true USBD peripheral successfully suspended.
  470. * @retval false USBD peripheral was not suspended due to resume detection.
  471. */
  472. bool nrfx_usbd_suspend(void);
  473. /**
  474. * @brief Start wake up procedure.
  475. *
  476. * The USBD peripheral is forced to quit the low power mode.
  477. * After calling this function all the USBD registers would be available.
  478. *
  479. * The hardware starts measuring time when wake up is possible.
  480. * This may take 0-5&nbsp;ms depending on how long the SUSPEND state was kept on the USB line.
  481. * When NRFX_USBD_EVT_WUREQ event is generated it means that Wake Up signaling has just been
  482. * started on the USB lines.
  483. *
  484. * @note Do not expect only @ref NRFX_USBD_EVT_WUREQ event.
  485. * There always may appear @ref NRFX_USBD_EVT_RESUME event.
  486. * @note NRFX_USBD_EVT_WUREQ event means that Remote WakeUp signal
  487. * has just begun to be generated.
  488. * This may take up to 20&nbsp;ms for the bus to become active.
  489. *
  490. * @retval true WakeUp procedure started.
  491. * @retval false No WakeUp procedure started - bus is already active.
  492. */
  493. bool nrfx_usbd_wakeup_req(void);
  494. /**
  495. * @brief Check if USBD is in SUSPEND mode.
  496. *
  497. * @note This is the information about peripheral itself, not about the bus state.
  498. *
  499. * @retval true USBD peripheral is suspended.
  500. * @retval false USBD peripheral is active.
  501. */
  502. bool nrfx_usbd_suspend_check(void);
  503. /**
  504. * @brief Enable only interrupts that should be processed in SUSPEND mode.
  505. *
  506. * Auxiliary function to help with SUSPEND mode integration.
  507. * It enables only the interrupts that can be properly processed without stable HFCLK.
  508. *
  509. * Normally all the interrupts are enabled.
  510. * Use this function to suspend interrupt processing that may require stable HFCLK until the
  511. * clock is enabled.
  512. *
  513. * @sa nrfx_usbd_active_irq_config
  514. */
  515. void nrfx_usbd_suspend_irq_config(void);
  516. /**
  517. * @brief Default active interrupt configuration.
  518. *
  519. * Default interrupt configuration.
  520. * Use in a pair with @ref nrfx_usbd_active_irq_config.
  521. *
  522. * @sa nrfx_usbd_suspend_irq_config
  523. */
  524. void nrfx_usbd_active_irq_config(void);
  525. /**
  526. * @brief Check the bus state.
  527. *
  528. * This function checks if the bus state is suspended.
  529. *
  530. * @note The value returned by this function changes on SUSPEND and RESUME event processing.
  531. *
  532. * @retval true USBD bus is suspended.
  533. * @retval false USBD bus is active.
  534. */
  535. bool nrfx_usbd_bus_suspend_check(void);
  536. /**
  537. * @brief Force the bus state to active
  538. */
  539. void nrfx_usbd_force_bus_wakeup(void);
  540. /**
  541. * @brief Configure packet size that should be supported by the endpoint.
  542. *
  543. * The real endpoint buffer size is always the same.
  544. * This value sets max packet size that would be transmitted over the endpoint.
  545. * This is required by the driver.
  546. *
  547. * @param[in] ep Endpoint number.
  548. * @param[in] size Required maximum packet size.
  549. *
  550. * @note Endpoint size is always set to @ref NRFX_USBD_EPSIZE or @ref NRFX_USBD_ISOSIZE / 2
  551. * when @ref nrfx_usbd_ep_enable function is called.
  552. */
  553. void nrfx_usbd_ep_max_packet_size_set(nrfx_usbd_ep_t ep, uint16_t size);
  554. /**
  555. * @brief Get configured endpoint packet size.
  556. *
  557. * Function to get configured endpoint size on the buffer.
  558. *
  559. * @param[in] ep Endpoint number.
  560. *
  561. * @return Maximum pocket size configured on selected endpoint.
  562. */
  563. uint16_t nrfx_usbd_ep_max_packet_size_get(nrfx_usbd_ep_t ep);
  564. /**
  565. * @brief Check if the selected endpoint is enabled.
  566. *
  567. * @param[in] ep Endpoint number to check.
  568. *
  569. * @retval true Endpoint is enabled.
  570. * @retval false Endpoint is disabled.
  571. */
  572. bool nrfx_usbd_ep_enable_check(nrfx_usbd_ep_t ep);
  573. /**
  574. * @brief Enable selected endpoint.
  575. *
  576. * This function enables endpoint itself and its interrupts.
  577. *
  578. * @param[in] ep Endpoint number to enable.
  579. *
  580. * @note
  581. * Max packet size is set to endpoint default maximum value.
  582. *
  583. * @sa nrfx_usbd_ep_max_packet_size_set
  584. */
  585. void nrfx_usbd_ep_enable(nrfx_usbd_ep_t ep);
  586. /**
  587. * @brief Disable selected endpoint.
  588. *
  589. * This function disables endpoint itself and its interrupts.
  590. *
  591. * @param[in] ep Endpoint number to disable.
  592. */
  593. void nrfx_usbd_ep_disable(nrfx_usbd_ep_t ep);
  594. /**
  595. * @brief Disable all endpoints except for EP0.
  596. *
  597. * Disable all endpoints that can be disabled in USB device while it is still active.
  598. */
  599. void nrfx_usbd_ep_default_config(void);
  600. /**
  601. * @brief Start sending data over endpoint.
  602. *
  603. * Function initializes endpoint transmission.
  604. * This is asynchronous function - it finishes immediately after configuration
  605. * for transmission is prepared.
  606. *
  607. * @note Data buffer pointed by p_data have to be kept active till
  608. * @ref NRFX_USBD_EVT_EPTRANSFER event is generated.
  609. *
  610. * @param[in] ep Endpoint number.
  611. * For IN endpoint sending would be initiated.
  612. * For OUT endpoint receiving would be initiated.
  613. * @param[in] p_transfer Transfer parameters.
  614. *
  615. * @retval NRFX_SUCCESS Transfer queued or started.
  616. * @retval NRFX_ERROR_BUSY Selected endpoint is pending.
  617. * @retval NRFX_ERROR_INVALID_ADDR Unexpected transfer on EPIN0 or EPOUT0.
  618. */
  619. nrfx_err_t nrfx_usbd_ep_transfer(nrfx_usbd_ep_t ep,
  620. nrfx_usbd_transfer_t const * p_transfer);
  621. /**
  622. * @brief Start sending data over the endpoint using the transfer handler function.
  623. *
  624. * This function initializes an endpoint transmission.
  625. * Just before data is transmitted, the transfer handler
  626. * is called and it prepares a data chunk.
  627. *
  628. * @param[in] ep Endpoint number.
  629. * For an IN endpoint, sending is initiated.
  630. * For an OUT endpoint, receiving is initiated.
  631. * @param[in] p_handler Transfer handler - feeder for IN direction and consumer for
  632. * OUT direction.
  633. *
  634. * @retval NRFX_SUCCESS Transfer queued or started.
  635. * @retval NRFX_ERROR_BUSY Selected endpoint is pending.
  636. * @retval NRFX_ERROR_INVALID_ADDR Unexpected transfer on EPIN0 or EPOUT0.
  637. */
  638. nrfx_err_t nrfx_usbd_ep_handled_transfer(nrfx_usbd_ep_t ep,
  639. nrfx_usbd_handler_desc_t const * p_handler);
  640. /**
  641. * @brief Get the temporary buffer to be used by the feeder.
  642. *
  643. * This buffer is used for TX transfers and it can be reused automatically
  644. * when the transfer is finished.
  645. * Use it for transfer preparation.
  646. *
  647. * May be used inside the feeder configured in @ref nrfx_usbd_ep_handled_transfer.
  648. *
  649. * @return Pointer to the buffer that can be used temporarily.
  650. *
  651. * @sa NRFX_USBD_FEEDER_BUFFER_SIZE
  652. */
  653. void * nrfx_usbd_feeder_buffer_get(void);
  654. /**
  655. * @brief Get the information about last finished or current transfer.
  656. *
  657. * Function returns the status of the last buffer set for transfer on selected endpoint.
  658. * The status considers last buffer set by @ref nrfx_usbd_ep_transfer function or
  659. * by transfer callback function.
  660. *
  661. * @param[in] ep Endpoint number.
  662. * @param[out] p_size Information about the current/last transfer size.
  663. *
  664. * @return Endpoint status.
  665. *
  666. * @sa nrfx_usbd_ep_status_t
  667. */
  668. nrfx_usbd_ep_status_t nrfx_usbd_ep_status_get(nrfx_usbd_ep_t ep, size_t * p_size);
  669. /**
  670. * @brief Get number of received bytes.
  671. *
  672. * Get the number of received bytes.
  673. * The function behavior is undefined when called on IN endpoint.
  674. *
  675. * @param[in] ep Endpoint number.
  676. *
  677. * @return Number of received bytes.
  678. */
  679. size_t nrfx_usbd_epout_size_get(nrfx_usbd_ep_t ep);
  680. /**
  681. * @brief Check if endpoint buffer is ready or is under USB IP control.
  682. *
  683. * Function to test if endpoint is busy.
  684. * Endpoint that is busy cannot be accessed by MCU.
  685. * It means that:
  686. * - OUT (TX) endpoint: Last uploaded data is still in endpoint and is waiting
  687. * to be received by the host.
  688. * - IN (RX) endpoint: Endpoint is ready to receive data from the host
  689. * and the endpoint does not have any data.
  690. * When endpoint is not busy:
  691. * - OUT (TX) endpoint: New data can be uploaded.
  692. * - IN (RX) endpoint: New data can be downloaded using @ref nrfx_usbd_ep_transfer
  693. * function.
  694. *
  695. * @param[in] ep Endpoint number.
  696. *
  697. * @retval false Endpoint is not busy.
  698. * @retval true Endpoint is busy.
  699. */
  700. bool nrfx_usbd_ep_is_busy(nrfx_usbd_ep_t ep);
  701. /**
  702. * @brief Stall endpoint
  703. *
  704. * Stall endpoit to send error information during next transfer request from
  705. * the host.
  706. *
  707. * @note To stall endpoint it is safer to use @ref nrfx_usbd_setup_stall
  708. * @note Stalled endpoint would not be cleared when DMA transfer finishes.
  709. *
  710. * @param[in] ep Endpoint number to stall.
  711. */
  712. void nrfx_usbd_ep_stall(nrfx_usbd_ep_t ep);
  713. /**
  714. * @brief Clear stall flag on endpoint.
  715. *
  716. * This function clears endpoint that is stalled.
  717. * @note
  718. * If it is OUT endpoint (receiving) it would be also prepared for reception.
  719. * It means that busy flag would be set.
  720. * @note
  721. * In endpoint (transmitting) would not be cleared - it gives possibility to
  722. * write new data before transmitting.
  723. *
  724. * @param[in] ep Endpoint number.
  725. */
  726. void nrfx_usbd_ep_stall_clear(nrfx_usbd_ep_t ep);
  727. /**
  728. * @brief Check if endpoint is stalled.
  729. *
  730. * This function gets stall state of selected endpoint.
  731. *
  732. * @param[in] ep Endpoint number to check.
  733. *
  734. * @retval false Endpoint is not stalled.
  735. * @retval true Endpoint is stalled.
  736. */
  737. bool nrfx_usbd_ep_stall_check(nrfx_usbd_ep_t ep);
  738. /**
  739. * @brief Clear current endpoint data toggle.
  740. *
  741. * @param[in] ep Endpoint number to clear.
  742. */
  743. void nrfx_usbd_ep_dtoggle_clear(nrfx_usbd_ep_t ep);
  744. /**
  745. * @brief Get parsed setup data.
  746. *
  747. * Function fills the parsed setup data structure.
  748. *
  749. * @param[out] p_setup Pointer to data structure that would be filled by
  750. * parsed data.
  751. */
  752. void nrfx_usbd_setup_get(nrfx_usbd_setup_t * p_setup);
  753. /**
  754. * @brief Clear the control endpoint for packet reception during DATA stage.
  755. *
  756. * This function may be called if any more data in control write transfer is expected.
  757. * Clears only OUT endpoint to be able to take another OUT data token.
  758. * It does not allow STATUS stage.
  759. * @sa nrfx_usbd_setup_clear
  760. */
  761. void nrfx_usbd_setup_data_clear(void);
  762. /**
  763. * @brief Clear setup endpoint.
  764. *
  765. * This function acknowledges setup when SETUP command was received and processed.
  766. * It has to be called if no data respond for the SETUP command is sent.
  767. */
  768. void nrfx_usbd_setup_clear(void);
  769. /**
  770. * @brief Stall setup endpoint.
  771. *
  772. * Mark an error on setup endpoint.
  773. */
  774. void nrfx_usbd_setup_stall(void);
  775. /**
  776. * @brief Abort pending transfer on selected endpoint.
  777. *
  778. * @param[in] ep Endpoint number.
  779. */
  780. void nrfx_usbd_ep_abort(nrfx_usbd_ep_t ep);
  781. /**
  782. * @brief Get the information about expected transfer SETUP data direction.
  783. *
  784. * Function returns the information about last expected transfer direction.
  785. *
  786. * @retval NRFX_USBD_EPOUT0 Expecting OUT (Host->Device) direction or no data.
  787. * @retval NRFX_USBD_EPIN0 Expecting IN (Device->Host) direction.
  788. */
  789. nrfx_usbd_ep_t nrfx_usbd_last_setup_dir_get(void);
  790. /**
  791. * @brief Drop transfer on OUT endpoint.
  792. *
  793. * @param[in] ep OUT endpoint ID.
  794. */
  795. void nrfx_usbd_transfer_out_drop(nrfx_usbd_ep_t ep);
  796. /** @} */
  797. void nrfx_usbd_irq_handler(void);
  798. #ifdef __cplusplus
  799. }
  800. #endif
  801. #endif // NRFX_USBD_H__