nrf_drv_usbd.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. /**
  2. * Copyright (c) 2016 - 2018, 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 NRF_DRV_USBD_H__
  41. #define NRF_DRV_USBD_H__
  42. #include "sdk_errors.h"
  43. #include "nrf_usbd.h"
  44. #include <stdint.h>
  45. #include <stdbool.h>
  46. #include "app_util.h"
  47. #include "nrf_drv_usbd_errata.h"
  48. #ifdef __cplusplus
  49. extern "C" {
  50. #endif
  51. /**
  52. * @defgroup nrf_drv_usbd USB Device HAL and driver
  53. * @ingroup nrf_drivers
  54. * @brief @tagAPI52840 USB Device APIs.
  55. * @details The USB Device HAL provides basic APIs for accessing
  56. * the registers of the USBD.
  57. * The USB Device driver provides APIs on a higher level.
  58. *
  59. * @{
  60. */
  61. /**
  62. * @name Possible schemes of DMA scheduling
  63. *
  64. * Definition of available configuration constants used by DMA scheduler
  65. * @{
  66. */
  67. /**
  68. * @brief Highly prioritized access
  69. *
  70. * Endpoint with lower number has always higher priority and its data would
  71. * be transfered first.
  72. * OUT endpoints ale processed before IN endpoints
  73. */
  74. #define NRF_DRV_USBD_DMASCHEDULER_PRIORITIZED 0
  75. /**
  76. * @brief Round robin scheme
  77. *
  78. * All endpoints are processed in round-robin scheme.
  79. * It means that when one endpoint is processed next in order would be
  80. * the nearest with lower number.
  81. * When no endpoints with lower number requires processing - then
  82. * all endpoints from 0 are tested.
  83. */
  84. #define NRF_DRV_USBD_DMASCHEDULER_ROUNDROBIN 1
  85. /** @} */
  86. /**
  87. * @brief Number of bytes in the endpoint
  88. *
  89. * Constant that informs about endpoint size
  90. */
  91. #define NRF_DRV_USBD_EPSIZE 64
  92. /**
  93. * @brief Number of bytes for isochronous endpoints
  94. *
  95. * Number of bytes for isochronous endpoints in total.
  96. * This number would be shared between IN and OUT endpoint.
  97. * It may be also assigned totaly to one endpoint.
  98. * @sa nrf_usbd_isosplit_set
  99. * @sa nrf_usbd_isosplit_get
  100. */
  101. #define NRF_DRV_USBD_ISOSIZE 1024
  102. /**
  103. * @brief The size of internal feeder buffer.
  104. *
  105. * @sa nrf_drv_usbd_feeder_buffer_get
  106. */
  107. #define NRF_DRV_USBD_FEEDER_BUFFER_SIZE NRF_DRV_USBD_EPSIZE
  108. /**
  109. * @name Macros for creating endpoint identifiers
  110. *
  111. * Auxiliary macros to be used to create Endpoint identifier that is compatible
  112. * with USB specification.
  113. * @{
  114. */
  115. /**
  116. * @brief Create identifier for IN endpoint
  117. *
  118. * Simple macro to create IN endpoint identifier for given endpoint number.
  119. *
  120. * @param[in] n Endpoint number.
  121. *
  122. * @return Endpoint identifier that connects endpoint number and endpoint direction.
  123. */
  124. #define NRF_DRV_USBD_EPIN(n) ((nrf_drv_usbd_ep_t)NRF_USBD_EPIN(n))
  125. /**
  126. * @brief Create identifier for OUT endpoint
  127. *
  128. * Simple macro to create OUT endpoint identifier for given endpoint number.
  129. *
  130. * @param[in] n Endpoint number.
  131. *
  132. * @return Endpoint identifier that connects endpoint number and endpoint direction.
  133. */
  134. #define NRF_DRV_USBD_EPOUT(n) ((nrf_drv_usbd_ep_t)NRF_USBD_EPOUT(n))
  135. /** @} */
  136. /**
  137. * @brief Endpoint identifier
  138. *
  139. * Endpoint identifier used in the driver.
  140. * This endpoint number is consistent with USB 2.0 specification.
  141. */
  142. typedef enum
  143. {
  144. NRF_DRV_USBD_EPOUT0 = NRF_USBD_EPOUT(0), /**< Endpoint OUT 0 */
  145. NRF_DRV_USBD_EPOUT1 = NRF_USBD_EPOUT(1), /**< Endpoint OUT 1 */
  146. NRF_DRV_USBD_EPOUT2 = NRF_USBD_EPOUT(2), /**< Endpoint OUT 2 */
  147. NRF_DRV_USBD_EPOUT3 = NRF_USBD_EPOUT(3), /**< Endpoint OUT 3 */
  148. NRF_DRV_USBD_EPOUT4 = NRF_USBD_EPOUT(4), /**< Endpoint OUT 4 */
  149. NRF_DRV_USBD_EPOUT5 = NRF_USBD_EPOUT(5), /**< Endpoint OUT 5 */
  150. NRF_DRV_USBD_EPOUT6 = NRF_USBD_EPOUT(6), /**< Endpoint OUT 6 */
  151. NRF_DRV_USBD_EPOUT7 = NRF_USBD_EPOUT(7), /**< Endpoint OUT 7 */
  152. NRF_DRV_USBD_EPOUT8 = NRF_USBD_EPOUT(8), /**< Endpoint OUT 8 */
  153. NRF_DRV_USBD_EPIN0 = NRF_USBD_EPIN(0), /**< Endpoint IN 0 */
  154. NRF_DRV_USBD_EPIN1 = NRF_USBD_EPIN(1), /**< Endpoint IN 1 */
  155. NRF_DRV_USBD_EPIN2 = NRF_USBD_EPIN(2), /**< Endpoint IN 2 */
  156. NRF_DRV_USBD_EPIN3 = NRF_USBD_EPIN(3), /**< Endpoint IN 3 */
  157. NRF_DRV_USBD_EPIN4 = NRF_USBD_EPIN(4), /**< Endpoint IN 4 */
  158. NRF_DRV_USBD_EPIN5 = NRF_USBD_EPIN(5), /**< Endpoint IN 5 */
  159. NRF_DRV_USBD_EPIN6 = NRF_USBD_EPIN(6), /**< Endpoint IN 6 */
  160. NRF_DRV_USBD_EPIN7 = NRF_USBD_EPIN(7), /**< Endpoint IN 7 */
  161. NRF_DRV_USBD_EPIN8 = NRF_USBD_EPIN(8), /**< Endpoint IN 8 */
  162. }nrf_drv_usbd_ep_t;
  163. /**
  164. * @brief Events generated by the library
  165. *
  166. * Enumeration of possible events that may be generated by the library.
  167. */
  168. typedef enum
  169. {
  170. NRF_DRV_USBD_EVT_SOF, /**< Start Of Frame event on USB bus detected */
  171. NRF_DRV_USBD_EVT_RESET, /**< Reset condition on USB bus detected */
  172. NRF_DRV_USBD_EVT_SUSPEND, /**< This device should go to suspend mode now */
  173. NRF_DRV_USBD_EVT_RESUME, /**< This device should resume from suspend now */
  174. NRF_DRV_USBD_EVT_WUREQ, /**< Wakeup request - the USBD peripheral is ready to generate WAKEUP signal after exiting low power mode. */
  175. NRF_DRV_USBD_EVT_SETUP, /**< Setup frame received and decoded */
  176. NRF_DRV_USBD_EVT_EPTRANSFER, /**<
  177. * For Rx (OUT: Host->Device):
  178. * 1. The packet has been received but there is no buffer prepared for transfer already.
  179. * 2. Whole transfer has been finished
  180. *
  181. * For Tx (IN: Device->Host):
  182. * The last packet from requested transfer has been transfered over USB bus and acknowledged
  183. */
  184. NRF_DRV_USBD_EVT_CNT /**< Number of defined events */
  185. }nrf_drv_usbd_event_type_t;
  186. /**
  187. * @brief Possible endpoint error codes
  188. *
  189. * Error codes that may be returned with @ref NRF_DRV_USBD_EVT_EPTRANSFER
  190. */
  191. typedef enum
  192. {
  193. NRF_USBD_EP_OK, /**< No error */
  194. NRF_USBD_EP_WAITING, /**< Data received, no buffer prepared already - waiting for configured transfer */
  195. NRF_USBD_EP_OVERLOAD, /**< Received number of bytes cannot fit given buffer
  196. * This error would also be returned when next_transfer function has been defined
  197. * but currently received data cannot fit completely in current buffer.
  198. * No data split from single endpoint transmission is supported.
  199. *
  200. * When this error is reported - data is left inside endpoint buffer.
  201. * Clear endpoint or prepare new buffer and read it.
  202. */
  203. NRF_USBD_EP_ABORTED, /**< EP0 transfer can be aborted when new setup comes.
  204. * Any other transfer can be aborted by USB reset or library stopping.
  205. */
  206. }nrf_drv_usbd_ep_status_t;
  207. /**
  208. * @brief Event structure
  209. *
  210. * Structure passed to event handler
  211. */
  212. typedef struct
  213. {
  214. nrf_drv_usbd_event_type_t type;
  215. union
  216. {
  217. struct{
  218. uint16_t framecnt; //!< Current value of frame counter
  219. }sof; //!< Data aviable for @ref NRF_DRV_USBD_EVT_SOF
  220. struct{
  221. nrf_drv_usbd_ep_t ep; //!< Endpoint number
  222. }isocrc;
  223. struct{
  224. nrf_drv_usbd_ep_t ep; //!< Endpoint number
  225. nrf_drv_usbd_ep_status_t status; //!< Status for the endpoint
  226. }eptransfer;
  227. }data;
  228. }nrf_drv_usbd_evt_t;
  229. /**
  230. * @brief USBD event callback function type.
  231. *
  232. * @param[in] p_event Event information structure.
  233. */
  234. typedef void (*nrf_drv_usbd_event_handler_t)(nrf_drv_usbd_evt_t const * const p_event);
  235. /**
  236. * @brief Universal data pointer.
  237. *
  238. * Universal data pointer that can be used for any type of transfer.
  239. */
  240. typedef union
  241. {
  242. void const * tx; //!< Constant TX buffer pointer.
  243. void * rx; //!< Writable RX buffer pointer.
  244. uint32_t ptr; //!< Numeric value used internally by the library.
  245. }nrf_drv_usbd_data_ptr_t;
  246. /**
  247. * @brief Structure to be filled with information about the next transfer.
  248. *
  249. * This is used mainly for transfer feeders and consumers.
  250. * It describes a single endpoint transfer and therefore the size of the buffer
  251. * can never be higher than the endpoint size.
  252. */
  253. typedef struct
  254. {
  255. nrf_drv_usbd_data_ptr_t p_data; //!< Union with available data pointers used by the library.
  256. size_t size; //!< Size of the requested transfer.
  257. }nrf_drv_usbd_ep_transfer_t;
  258. /**
  259. * @brief Flags for the current transfer.
  260. *
  261. * Flags configured for the transfer that can be merged using the bitwise 'or' operator (|).
  262. */
  263. typedef enum
  264. {
  265. NRF_DRV_USBD_TRANSFER_ZLP_FLAG = 1U << 0, //!< Add a zero-length packet.
  266. }nrf_drv_usbd_transfer_flags_t;
  267. /**
  268. * @brief Total transfer configuration.
  269. *
  270. * This structure is used to configure total transfer information.
  271. * It is used by internal built-in feeders and consumers.
  272. */
  273. typedef struct
  274. {
  275. nrf_drv_usbd_data_ptr_t p_data; //!< Union with available data pointers used by the library.
  276. size_t size; //!< Total size of the requested transfer.
  277. uint32_t flags; //!< Transfer flags.
  278. /**< Use the @ref nrf_drv_usbd_transfer_flags_t values. */
  279. }nrf_drv_usbd_transfer_t;
  280. /**
  281. * @brief Auxiliary macro for declaring IN transfer description with flags.
  282. *
  283. * The base macro for creating transfers with any configuration option.
  284. *
  285. * @param name Instance name.
  286. * @param tx_buff Buffer to transfer.
  287. * @param tx_size Transfer size.
  288. * @param tx_flags Flags for the transfer (see @ref nrf_drv_usbd_transfer_flags_t).
  289. *
  290. * @return Configured variable with total transfer description.
  291. */
  292. #define NRF_DRV_USBD_TRANSFER_IN_FLAGS(name, tx_buff, tx_size, tx_flags) \
  293. const nrf_drv_usbd_transfer_t name = { \
  294. .p_data = { .tx = (tx_buff) }, \
  295. .size = (tx_size), \
  296. .flags = (tx_flags) \
  297. }
  298. /**
  299. * @brief Helper macro for declaring IN transfer description
  300. *
  301. * Normal transfer mode, no ZLP would be automatically generated.
  302. *
  303. * @sa nrf_drv_usbd_transfer_t
  304. * @sa NRF_DRV_USBD_TRANSFER_IN_ZLP
  305. *
  306. * @param name Instance name
  307. * @param tx_buff Buffer to transfer
  308. * @param tx_size Transfer size
  309. *
  310. * @return Configured variable with total transfer description
  311. *
  312. */
  313. #define NRF_DRV_USBD_TRANSFER_IN(name, tx_buff, tx_size) \
  314. NRF_DRV_USBD_TRANSFER_IN_FLAGS(name, tx_buff, tx_size, 0)
  315. /**
  316. * @brief Helper macro for declaring IN transfer description
  317. *
  318. * ZLP mode - Zero Length Packet would be generated on the end of the transfer
  319. * (always!).
  320. *
  321. * @sa nrf_drv_usbd_transfer_t
  322. * @sa NRF_DRV_USBD_TRANSFER_IN
  323. *
  324. * @param name Instance name
  325. * @param tx_buff Buffer to transfer
  326. * @param tx_size Transfer size
  327. *
  328. * @return Configured variable with total transfer description
  329. */
  330. #define NRF_DRV_USBD_TRANSFER_IN_ZLP(name, tx_buff, tx_size) \
  331. NRF_DRV_USBD_TRANSFER_IN_FLAGS( \
  332. name, \
  333. tx_buff, \
  334. tx_size, \
  335. NRF_DRV_USBD_TRANSFER_ZLP_FLAG)
  336. /**
  337. * @brief Helper macro for declaring OUT transfer item (@ref nrf_drv_usbd_transfer_t)
  338. *
  339. * @param name Instance name
  340. * @param rx_buff Buffer to transfer
  341. * @param rx_size Transfer size
  342. * */
  343. #define NRF_DRV_USBD_TRANSFER_OUT(name, rx_buff, rx_size) \
  344. const nrf_drv_usbd_transfer_t name = { \
  345. .p_data = { .rx = (rx_buff) }, \
  346. .size = (rx_size), \
  347. .flags = 0 \
  348. }
  349. /**
  350. * @brief USBD transfer feeder.
  351. *
  352. * Pointer for a transfer feeder.
  353. * Transfer feeder is a feedback function used to prepare a single
  354. * TX (Device->Host) endpoint transfer.
  355. *
  356. * The transfers provided by the feeder must be simple:
  357. * - The size of the transfer provided by this function is limited to a single endpoint buffer.
  358. * Bigger transfers are not handled automatically in this case.
  359. * - Flash transfers are not automatically supported- you must copy them to the RAM buffer before.
  360. *
  361. * @note
  362. * This function may use @ref nrf_drv_usbd_feeder_buffer_get to gain a temporary buffer
  363. * that can be used to prepare transfer.
  364. *
  365. * @param[out] p_next Structure with the data for the next transfer to be filled.
  366. * Required only if the function returns true.
  367. * @param[in,out] p_context Context variable configured with the transfer.
  368. * @param[in] ep_size The endpoint size.
  369. *
  370. * @retval false The current transfer is the last one - you do not need to call
  371. * the function again.
  372. * @retval true There is more data to be prepared and when the current transfer
  373. * finishes, the feeder function is expected to be called again.
  374. */
  375. typedef bool (*nrf_drv_usbd_feeder_t)(
  376. nrf_drv_usbd_ep_transfer_t * p_next,
  377. void * p_context,
  378. size_t ep_size);
  379. /**
  380. * @brief USBD transfer consumer.
  381. *
  382. * Pointer for a transfer consumer.
  383. * Transfer consumer is a feedback function used to prepare a single
  384. * RX (Host->Device) endpoint transfer.
  385. *
  386. * The transfer must provide a buffer big enough to fit the whole data from the endpoint.
  387. * Otherwise, the NRF_USBD_EP_OVERLOAD event is generated.
  388. *
  389. * @param[out] p_next Structure with the data for the next transfer to be filled.
  390. * Required only if the function returns true.
  391. * @param[in,out] p_context Context variable configured with the transfer.
  392. * @param[in] ep_size The endpoint size.
  393. * @param[in] data_size Number of received bytes in the endpoint buffer.
  394. *
  395. * @retval false Current transfer is the last one - you do not need to call
  396. * the function again.
  397. * @retval true There is more data to be prepared and when current transfer
  398. * finishes, the feeder function is expected to be called again.
  399. */
  400. typedef bool (*nrf_drv_usbd_consumer_t)(
  401. nrf_drv_usbd_ep_transfer_t * p_next,
  402. void * p_context,
  403. size_t ep_size,
  404. size_t data_size);
  405. /**
  406. * @brief Universal transfer handler.
  407. *
  408. * Union with feeder and consumer function pointer.
  409. */
  410. typedef union
  411. {
  412. nrf_drv_usbd_feeder_t feeder; //!< Feeder function pointer.
  413. nrf_drv_usbd_consumer_t consumer; //!< Consumer function pointer.
  414. }nrf_drv_usbd_handler_t;
  415. /**
  416. * @brief USBD transfer descriptor.
  417. *
  418. * Universal structure that may hold the setup for callback configuration for
  419. * IN or OUT type of the transfer.
  420. */
  421. typedef struct
  422. {
  423. nrf_drv_usbd_handler_t handler; //!< Handler for the current transfer, function pointer.
  424. void * p_context; //!< Context for the transfer handler.
  425. }nrf_drv_usbd_handler_desc_t;
  426. /**
  427. * @brief Setup packet structure
  428. *
  429. * Structure that contains interpreted SETUP packet.
  430. */
  431. typedef struct
  432. {
  433. uint8_t bmRequestType; //!< byte 0
  434. uint8_t bmRequest; //!< byte 1
  435. uint16_t wValue; //!< byte 2
  436. uint16_t wIndex; //!< byte 4, 5
  437. uint16_t wLength; //!< byte 6, 7
  438. }nrf_drv_usbd_setup_t;
  439. /**
  440. * @brief Library initialization
  441. *
  442. * @param[in] event_handler Event handler provided by the user.
  443. */
  444. ret_code_t nrf_drv_usbd_init(nrf_drv_usbd_event_handler_t const event_handler);
  445. /**
  446. * @brief Library deinitialization
  447. */
  448. ret_code_t nrf_drv_usbd_uninit(void);
  449. /**
  450. * @brief Enable the USBD port
  451. *
  452. * After calling this function USBD peripheral would be enabled.
  453. * The USB LDO would be enabled.
  454. * Enabled USBD peripheral would request HFCLK.
  455. * This function does not enable external oscillator, so if it is not enabled by other part of the
  456. * program after enabling USBD driver HFINT would be used for the USBD peripheral.
  457. * It is perfectly fine until USBD is started. See @ref nrf_drv_usbd_start.
  458. *
  459. * In normal situation this function should be called in reaction to USBDETECTED
  460. * event from POWER peripheral.
  461. *
  462. * Interrupts and USB pins pull-up would stay disabled until @ref nrf_drv_usbd_start
  463. * function is called.
  464. */
  465. void nrf_drv_usbd_enable(void);
  466. /**
  467. * @brief Disable the USBD port
  468. *
  469. * After calling this function USBD peripheral would be disabled.
  470. * No events would be detected or processed by the library.
  471. * Clock for the peripheral would be disconnected.
  472. */
  473. void nrf_drv_usbd_disable(void);
  474. /**
  475. * @brief Start USB functionality
  476. *
  477. * After calling this function USBD peripheral should be fully functional
  478. * and all new incoming events / interrupts would be processed by the library.
  479. *
  480. * Also only after calling this function host sees new connected device.
  481. *
  482. * Call this function when USBD power LDO regulator is ready - on USBPWRRDY event
  483. * from POWER peripheral.
  484. *
  485. * Before USBD interrupts are enabled, external HFXO is requested.
  486. *
  487. * @param enable_sof The flag that is used to enable SOF processing.
  488. * If it is false, SOF interrupt is left disabled and will not be generated.
  489. * This improves power saving if SOF is not required.
  490. *
  491. * @note If the isochronous endpoints are going to be used,
  492. * it is required to enable the SOF.
  493. * In other case any isochronous endpoint would stay busy
  494. * after first transmission.
  495. */
  496. void nrf_drv_usbd_start(bool enable_sof);
  497. /**
  498. * @brief Stop USB functionality
  499. *
  500. * This function disables USBD pull-up and interrupts.
  501. *
  502. * The HFXO request is released in this function.
  503. *
  504. * @note
  505. * This function can also be used to logically disconnect USB from the HOST that
  506. * would force it to enumerate device after calling @ref nrf_drv_usbd_start.
  507. */
  508. void nrf_drv_usbd_stop(void);
  509. /**
  510. * @brief Check if driver is initialized
  511. *
  512. * @retval false Driver is not initialized
  513. * @retval true Driver is initialized
  514. */
  515. bool nrf_drv_usbd_is_initialized(void);
  516. /**
  517. * @brief Check if driver is enabled
  518. *
  519. * @retval false Driver is disabled
  520. * @retval true Driver is enabled
  521. */
  522. bool nrf_drv_usbd_is_enabled(void);
  523. /**
  524. * @brief Check if driver is started
  525. *
  526. * @retval false Driver is not started
  527. * @retval true Driver is started (fully functional)
  528. * @note The USBD peripheral interrupt state is checked
  529. */
  530. bool nrf_drv_usbd_is_started(void);
  531. /**
  532. * @brief Suspend USBD operation
  533. *
  534. * The USBD peripheral is forced to go into the low power mode.
  535. * The function has to be called in the reaction to @ref NRF_DRV_USBD_EVT_SUSPEND event
  536. * when the firmware is ready.
  537. *
  538. * After successful call of this function most of the USBD registers would be unavailable.
  539. *
  540. * @note Check returned value for the feedback if suspending was successful.
  541. *
  542. * @retval true USBD peripheral successfully suspended
  543. * @retval false USBD peripheral was not suspended due to resume detection.
  544. *
  545. */
  546. bool nrf_drv_usbd_suspend(void);
  547. /**
  548. * @brief Start wake up procedure
  549. *
  550. * The USBD peripheral is forced to quit the low power mode.
  551. * After calling this function all the USBD registers would be available.
  552. *
  553. * The hardware starts measuring time when wake up is possible.
  554. * This may take 0-5&nbsp;ms depending on how long the SUSPEND state was kept on the USB line.
  555. * When NRF_DRV_USBD_EVT_WUREQ event is generated it means that Wake Up signaling has just been
  556. * started on the USB lines.
  557. *
  558. * @note Do not expect only @ref NRF_DRV_USBD_EVT_WUREQ event.
  559. * There always may appear @ref NRF_DRV_USBD_EVT_RESUME event.
  560. * @note NRF_DRV_USBD_EVT_WUREQ event means that Remote WakeUp signal
  561. * has just begun to be generated.
  562. * This may take up to 20&nbsp;ms for the bus to become active.
  563. *
  564. * @retval true WakeUp procedure started.
  565. * @retval false No WakeUp procedure started - bus is already active.
  566. */
  567. bool nrf_drv_usbd_wakeup_req(void);
  568. /**
  569. * @brief Check if USBD is in SUSPEND mode
  570. *
  571. * @note This is the information about peripheral itself, not about the bus state.
  572. *
  573. * @retval true USBD peripheral is suspended
  574. * @retval false USBD peripheral is active
  575. */
  576. bool nrf_drv_usbd_suspend_check(void);
  577. /**
  578. * @brief Enable only interrupts that should be processed in SUSPEND mode
  579. *
  580. * Auxiliary function to help with SUSPEND mode integration.
  581. * It enables only the interrupts that can be properly processed without stable HFCLK.
  582. *
  583. * Normally all the interrupts are enabled.
  584. * Use this function to suspend interrupt processing that may require stable HFCLK until the
  585. * clock is enabled.
  586. *
  587. * @sa nrf_drv_usbd_active_irq_config
  588. */
  589. void nrf_drv_usbd_suspend_irq_config(void);
  590. /**
  591. * @brief Default active interrupt configuration
  592. *
  593. * Default interrupt configuration.
  594. * Use in a pair with @ref nrf_drv_usbd_active_irq_config.
  595. *
  596. * @sa nrf_drv_usbd_suspend_irq_config
  597. */
  598. void nrf_drv_usbd_active_irq_config(void);
  599. /**
  600. * @brief Check the bus state
  601. *
  602. * This function checks if the bus state is suspended
  603. *
  604. * @note The value returned by this function changes on SUSPEND and RESUME event processing.
  605. *
  606. * @retval true USBD bus is suspended
  607. * @retval false USBD bus is active
  608. */
  609. bool nrf_drv_usbd_bus_suspend_check(void);
  610. /**
  611. * @brief Force the bus state to active
  612. */
  613. void nrf_drv_usbd_force_bus_wakeup(void);
  614. /**
  615. * @brief Configure packet size that should be supported by the endpoint
  616. *
  617. * The real endpoint buffer size is always the same.
  618. * This value sets max packet size that would be transmitted over the endpoint.
  619. * This is required by the library
  620. *
  621. * @param[in] ep Endpoint number
  622. * @param[in] size Required maximum packet size
  623. *
  624. * @note Endpoint size is always set to @ref NRF_DRV_USBD_EPSIZE or @ref NRF_DRV_USBD_ISOSIZE / 2
  625. * when @ref nrf_drv_usbd_ep_enable function is called.
  626. */
  627. void nrf_drv_usbd_ep_max_packet_size_set(nrf_drv_usbd_ep_t ep, uint16_t size);
  628. /**
  629. * @brief Get configured endpoint packet size
  630. *
  631. * Function to get configured endpoint size on the buffer.
  632. *
  633. * @param[in] ep Endpoint number
  634. *
  635. * @return Maximum pocket size configured on selected endpoint
  636. */
  637. uint16_t nrf_drv_usbd_ep_max_packet_size_get(nrf_drv_usbd_ep_t ep);
  638. /**
  639. * @brief Check if the selected endpoint is enabled.
  640. *
  641. * @param ep Endpoint number to check.
  642. *
  643. * @retval true Endpoint is enabled.
  644. * @retval false Endpoint is disabled.
  645. */
  646. bool nrf_drv_usbd_ep_enable_check(nrf_drv_usbd_ep_t ep);
  647. /**
  648. * @brief Enable selected endpoint
  649. *
  650. * This function enables endpoint itself and its interrupts.
  651. * @param ep Endpoint number to enable
  652. *
  653. * @note
  654. * Max packet size is set to endpoint default maximum value.
  655. *
  656. * @sa nrf_drv_usbd_ep_max_packet_size_set
  657. */
  658. void nrf_drv_usbd_ep_enable(nrf_drv_usbd_ep_t ep);
  659. /**
  660. * @brief Disable selected endpoint
  661. *
  662. * This function disables endpoint itself and its interrupts.
  663. * @param ep Endpoint number to disable
  664. */
  665. void nrf_drv_usbd_ep_disable(nrf_drv_usbd_ep_t ep);
  666. /**
  667. * @brief Disable all endpoints except for EP0
  668. *
  669. * Disable all endpoints that can be disabled in USB device while it is still active.
  670. */
  671. void nrf_drv_usbd_ep_default_config(void);
  672. /**
  673. * @brief Start sending data over endpoint
  674. *
  675. * Function initializes endpoint transmission.
  676. * This is asynchronous function - it finishes immediately after configuration
  677. * for transmission is prepared.
  678. *
  679. * @note Data buffer pointed by p_data have to be kept active till
  680. * @ref NRF_DRV_USBD_EVT_EPTRANSFER event is generated.
  681. *
  682. * @param[in] ep Endpoint number.
  683. * For IN endpoint sending would be initiated.
  684. * For OUT endpoint receiving would be initiated.
  685. * @param[in] p_transfer
  686. *
  687. * @retval NRF_ERROR_BUSY Selected endpoint is pending.
  688. * @retval NRF_ERROR_INVALID_ADDR Unexpected transfer on EPIN0 or EPOUT0.
  689. * @retval NRF_ERROR_FORBIDDEN Endpoint stalled.
  690. * @retval NRF_SUCCESS Transfer queued or started.
  691. */
  692. ret_code_t nrf_drv_usbd_ep_transfer(
  693. nrf_drv_usbd_ep_t ep,
  694. nrf_drv_usbd_transfer_t const * const p_transfer);
  695. /**
  696. * @brief Start sending data over the endpoint using the transfer handler function.
  697. *
  698. * This function initializes an endpoint transmission.
  699. * Just before data is transmitted, the transfer handler
  700. * is called and it prepares a data chunk.
  701. *
  702. * @param[in] ep Endpoint number.
  703. * For an IN endpoint, sending is initiated.
  704. * For an OUT endpoint, receiving is initiated.
  705. * @param p_handler Transfer handler - feeder for IN direction and consumer for
  706. * OUT direction.
  707. *
  708. * @retval NRF_ERROR_BUSY Selected endpoint is pending.
  709. * @retval NRF_ERROR_INVALID_ADDR Unexpected transfer on EPIN0 or EPOUT0.
  710. * @retval NRF_ERROR_FORBIDDEN Endpoint stalled.
  711. * @retval NRF_SUCCESS Transfer queued or started.
  712. */
  713. ret_code_t nrf_drv_usbd_ep_handled_transfer(
  714. nrf_drv_usbd_ep_t ep,
  715. nrf_drv_usbd_handler_desc_t const * const p_handler);
  716. /**
  717. * @brief Get the temporary buffer to be used by the feeder.
  718. *
  719. * This buffer is used for TX transfers and it can be reused automatically
  720. * when the transfer is finished.
  721. * Use it for transfer preparation.
  722. *
  723. * May be used inside the feeder configured in @ref nrf_drv_usbd_ep_handled_transfer.
  724. *
  725. * @return Pointer to the buffer that can be used temporarily.
  726. *
  727. * @sa NRF_DRV_USBD_FEEDER_BUFFER_SIZE
  728. */
  729. void * nrf_drv_usbd_feeder_buffer_get(void);
  730. /**
  731. * @brief Get the information about last finished or current transfer
  732. *
  733. * Function returns the status of the last buffer set for transfer on selected endpoint.
  734. * The status considers last buffer set by @ref nrf_drv_usbd_ep_transfer function or
  735. * by transfer callback function.
  736. *
  737. * @param[in] ep Endpoint number.
  738. * @param[out] p_size Information about the current/last transfer size.
  739. *
  740. * @retval NRF_SUCCESS Transfer already finished
  741. * @retval NRF_ERROR_BUSY Ongoing transfer
  742. * @retval NRF_ERROR_DATA_SIZE Too much of data received that cannot fit into buffer and cannot be splited into chunks.
  743. * This may happen if buffer size is not a multiplication of endpoint buffer size.
  744. */
  745. ret_code_t nrf_drv_usbd_ep_status_get(nrf_drv_usbd_ep_t ep, size_t * p_size);
  746. /**
  747. * @brief Get number of received bytes
  748. *
  749. * Get the number of received bytes.
  750. * The function behavior is undefined when called on IN endpoint.
  751. *
  752. * @param ep Endpoint number.
  753. *
  754. * @return Number of received bytes
  755. */
  756. size_t nrf_drv_usbd_epout_size_get(nrf_drv_usbd_ep_t ep);
  757. /**
  758. * @brief Check if endpoint buffer is ready or is under USB IP control
  759. *
  760. * Function to test if endpoint is busy.
  761. * Endpoint that is busy cannot be accessed by MCU.
  762. * It means that:
  763. * - OUT (TX) endpoint: Last uploaded data is still in endpoint and is waiting
  764. * to be received by the host.
  765. * - IN (RX) endpoint: Endpoint is ready to receive data from the host
  766. * and the endpoint does not have any data.
  767. * When endpoint is not busy:
  768. * - OUT (TX) endpoint: New data can be uploaded.
  769. * - IN (RX) endpoint: New data can be downloaded using @ref nrf_drv_usbd_ep_transfer
  770. * function.
  771. */
  772. bool nrf_drv_usbd_ep_is_busy(nrf_drv_usbd_ep_t ep);
  773. /**
  774. * @brief Stall endpoint
  775. *
  776. * Stall endpoit to send error information during next transfer request from
  777. * the host.
  778. *
  779. * @note To stall endpoint it is safer to use @ref nrf_drv_usbd_setup_stall
  780. * @note Stalled endpoint would not be cleared when DMA transfer finishes.
  781. *
  782. * @param ep Endpoint number to stall
  783. *
  784. */
  785. void nrf_drv_usbd_ep_stall(nrf_drv_usbd_ep_t ep);
  786. /**
  787. * @brief Clear stall flag on endpoint
  788. *
  789. * This function clears endpoint that is stalled.
  790. * @note
  791. * If it is OUT endpoint (receiving) it would be also prepared for reception.
  792. * It means that busy flag would be set.
  793. * @note
  794. * In endpoint (transmitting) would not be cleared - it gives possibility to
  795. * write new data before transmitting.
  796. */
  797. void nrf_drv_usbd_ep_stall_clear(nrf_drv_usbd_ep_t ep);
  798. /**
  799. * @brief Check if endpoint is stalled
  800. *
  801. * This function gets stall state of selected endpoint
  802. *
  803. * @param ep Endpoint number to check
  804. */
  805. bool nrf_drv_usbd_ep_stall_check(nrf_drv_usbd_ep_t ep);
  806. /**
  807. * @brief Clear current endpoint data toggle
  808. *
  809. * @param ep Endpoint number to clear
  810. */
  811. void nrf_drv_usbd_ep_dtoggle_clear(nrf_drv_usbd_ep_t ep);
  812. /**
  813. * @brief Get parsed setup data
  814. *
  815. * Function fills the parsed setup data structure.
  816. *
  817. * @param[out] p_setup Pointer to data structure that would be filled by
  818. * parsed data.
  819. */
  820. void nrf_drv_usbd_setup_get(nrf_drv_usbd_setup_t * const p_setup);
  821. /**
  822. * @brief Clear only for data transmission on setup endpoint
  823. *
  824. * This function may be called if any more data in control write transfer is expected.
  825. * Clears only OUT endpoint to be able to take another OUT data token.
  826. * It does not allow STATUS stage.
  827. * @sa nrf_drv_usbd_setup_clear
  828. */
  829. void nrf_drv_usbd_setup_data_clear(void);
  830. /**
  831. * @brief Clear setup endpoint
  832. *
  833. * This function acknowledges setup when SETUP command was received and processed.
  834. * It has to be called if no data respond for the SETUP command is sent.
  835. */
  836. void nrf_drv_usbd_setup_clear(void);
  837. /**
  838. * @brief Stall setup endpoint
  839. *
  840. * Mark and error on setup endpoint.
  841. */
  842. void nrf_drv_usbd_setup_stall(void);
  843. /**
  844. * @note
  845. * This function locks interrupts that may be costly.
  846. * It is good idea to test if the endpoint is still busy before calling this function:
  847. * @code
  848. (m_ep_dma_waiting & (1U << ep2bit(ep)))
  849. * @endcode
  850. * This function would check it again, but it makes it inside critical section.
  851. */
  852. void nrf_drv_usbd_ep_abort(nrf_drv_usbd_ep_t ep);
  853. /**
  854. * @brief Get the information about expected transfer SETUP data direction
  855. *
  856. * Function returns the information about last expected transfer direction.
  857. *
  858. * @retval NRF_DRV_USBD_EPOUT0 Expecting OUT (Host->Device) direction or no data
  859. * @retval NRF_DRV_USBD_EPIN0 Expecting IN (Device->Host) direction
  860. */
  861. nrf_drv_usbd_ep_t nrf_drv_usbd_last_setup_dir_get(void);
  862. /**
  863. * @brief Drop transfer on OUT endpoint
  864. *
  865. * @param[in] ep OUT endpoint ID
  866. */
  867. void nrf_drv_usbd_transfer_out_drop(nrf_drv_usbd_ep_t ep);
  868. #ifdef __cplusplus
  869. }
  870. #endif
  871. /** @} */
  872. #endif /* NRF_DRV_USBD_H__ */