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