1 /*
2  * Copyright (c) 2017 - 2024, 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 NRF_USBD_H__
35 #define NRF_USBD_H__
36 
37 #include <nrfx.h>
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /**
44  * @defgroup nrf_usbd_hal USBD HAL
45  * @{
46  * @ingroup nrf_usbd
47  * @brief   Hardware access layer for managing the Universal Serial Bus Device (USBD)
48  *          peripheral.
49  */
50 
51 /** @brief USBD tasks. */
52 typedef enum
53 {
54     NRF_USBD_TASK_STARTEPIN0    = offsetof(NRF_USBD_Type, TASKS_STARTEPIN[0] ), /**< Captures the EPIN[0].PTR, EPIN[0].MAXCNT, and EPIN[0].CONFIG registers values, and enables control endpoint IN 0 to respond to traffic from host. */
55     NRF_USBD_TASK_STARTEPIN1    = offsetof(NRF_USBD_Type, TASKS_STARTEPIN[1] ), /**< Captures the EPIN[1].PTR, EPIN[1].MAXCNT, and EPIN[1].CONFIG registers values, and enables data endpoint IN 1 to respond to traffic from host. */
56     NRF_USBD_TASK_STARTEPIN2    = offsetof(NRF_USBD_Type, TASKS_STARTEPIN[2] ), /**< Captures the EPIN[2].PTR, EPIN[2].MAXCNT, and EPIN[2].CONFIG registers values, and enables data endpoint IN 2 to respond to traffic from host. */
57     NRF_USBD_TASK_STARTEPIN3    = offsetof(NRF_USBD_Type, TASKS_STARTEPIN[3] ), /**< Captures the EPIN[3].PTR, EPIN[3].MAXCNT, and EPIN[3].CONFIG registers values, and enables data endpoint IN 3 to respond to traffic from host. */
58     NRF_USBD_TASK_STARTEPIN4    = offsetof(NRF_USBD_Type, TASKS_STARTEPIN[4] ), /**< Captures the EPIN[4].PTR, EPIN[4].MAXCNT, and EPIN[4].CONFIG registers values, and enables data endpoint IN 4 to respond to traffic from host. */
59     NRF_USBD_TASK_STARTEPIN5    = offsetof(NRF_USBD_Type, TASKS_STARTEPIN[5] ), /**< Captures the EPIN[5].PTR, EPIN[5].MAXCNT, and EPIN[5].CONFIG registers values, and enables data endpoint IN 5 to respond to traffic from host. */
60     NRF_USBD_TASK_STARTEPIN6    = offsetof(NRF_USBD_Type, TASKS_STARTEPIN[6] ), /**< Captures the EPIN[6].PTR, EPIN[6].MAXCNT, and EPIN[6].CONFIG registers values, and enables data endpoint IN 6 to respond to traffic from host. */
61     NRF_USBD_TASK_STARTEPIN7    = offsetof(NRF_USBD_Type, TASKS_STARTEPIN[7] ), /**< Captures the EPIN[7].PTR, EPIN[7].MAXCNT, and EPIN[7].CONFIG registers values, and enables data endpoint IN 7 to respond to traffic from host. */
62     NRF_USBD_TASK_STARTISOIN    = offsetof(NRF_USBD_Type, TASKS_STARTISOIN   ), /**< Captures the ISOIN.PTR, ISOIN.MAXCNT, and ISOIN.CONFIG registers values, and enables sending data on ISO endpoint 8. */
63     NRF_USBD_TASK_STARTEPOUT0   = offsetof(NRF_USBD_Type, TASKS_STARTEPOUT[0]), /**< Captures the EPOUT[0].PTR, EPOUT[0].MAXCNT, and EPOUT[0].CONFIG registers values, and enables control endpoint 0 to respond to traffic from host. */
64     NRF_USBD_TASK_STARTEPOUT1   = offsetof(NRF_USBD_Type, TASKS_STARTEPOUT[1]), /**< Captures the EPOUT[1].PTR, EPOUT[1].MAXCNT, and EPOUT[1].CONFIG registers values, and enables data endpoint 1 to respond to traffic from host. */
65     NRF_USBD_TASK_STARTEPOUT2   = offsetof(NRF_USBD_Type, TASKS_STARTEPOUT[2]), /**< Captures the EPOUT[2].PTR, EPOUT[2].MAXCNT, and EPOUT[2].CONFIG registers values, and enables data endpoint 2 to respond to traffic from host. */
66     NRF_USBD_TASK_STARTEPOUT3   = offsetof(NRF_USBD_Type, TASKS_STARTEPOUT[3]), /**< Captures the EPOUT[3].PTR, EPOUT[3].MAXCNT, and EPOUT[3].CONFIG registers values, and enables data endpoint 3 to respond to traffic from host. */
67     NRF_USBD_TASK_STARTEPOUT4   = offsetof(NRF_USBD_Type, TASKS_STARTEPOUT[4]), /**< Captures the EPOUT[4].PTR, EPOUT[4].MAXCNT, and EPOUT[4].CONFIG registers values, and enables data endpoint 4 to respond to traffic from host. */
68     NRF_USBD_TASK_STARTEPOUT5   = offsetof(NRF_USBD_Type, TASKS_STARTEPOUT[5]), /**< Captures the EPOUT[5].PTR, EPOUT[5].MAXCNT, and EPOUT[5].CONFIG registers values, and enables data endpoint 5 to respond to traffic from host. */
69     NRF_USBD_TASK_STARTEPOUT6   = offsetof(NRF_USBD_Type, TASKS_STARTEPOUT[6]), /**< Captures the EPOUT[6].PTR, EPOUT[6].MAXCNT, and EPOUT[6].CONFIG registers values, and enables data endpoint 6 to respond to traffic from host. */
70     NRF_USBD_TASK_STARTEPOUT7   = offsetof(NRF_USBD_Type, TASKS_STARTEPOUT[7]), /**< Captures the EPOUT[7].PTR, EPOUT[7].MAXCNT, and EPOUT[7].CONFIG registers values, and enables data endpoint 7 to respond to traffic from host. */
71     NRF_USBD_TASK_STARTISOOUT   = offsetof(NRF_USBD_Type, TASKS_STARTISOOUT  ), /**< Captures the ISOOUT.PTR, ISOOUT.MAXCNT, and ISOOUT.CONFIG registers values, and enables receiving of data on ISO endpoint 8. */
72     NRF_USBD_TASK_EP0RCVOUT     = offsetof(NRF_USBD_Type, TASKS_EP0RCVOUT    ), /**< Allows OUT data stage on the control endpoint 0. */
73     NRF_USBD_TASK_EP0STATUS     = offsetof(NRF_USBD_Type, TASKS_EP0STATUS    ), /**< Allows status stage on the control endpoint 0. */
74     NRF_USBD_TASK_EP0STALL      = offsetof(NRF_USBD_Type, TASKS_EP0STALL     ), /**< STALLs data and status stage on the control endpoint 0. */
75     NRF_USBD_TASK_DRIVEDPDM     = offsetof(NRF_USBD_Type, TASKS_DPDMDRIVE    ), /**< Forces D+ and D-lines to the state defined in the DPDMVALUE register. */
76     NRF_USBD_TASK_NODRIVEDPDM   = offsetof(NRF_USBD_Type, TASKS_DPDMNODRIVE  ), /**< Stops forcing D+ and D- lines to any state (USB engine takes control). */
77 }nrf_usbd_task_t;
78 
79 /** @brief USBD events. */
80 typedef enum
81 {
82     NRF_USBD_EVENT_USBRESET      = offsetof(NRF_USBD_Type, EVENTS_USBRESET   ), /**< Signals that a USB reset condition is detected on the USB lines. */
83     NRF_USBD_EVENT_STARTED       = offsetof(NRF_USBD_Type, EVENTS_STARTED    ), /**< Confirms that the EPIN[n].PTR, EPIN[n].MAXCNT, EPIN[n].CONFIG, or EPOUT[n].PTR, EPOUT[n].MAXCNT, and EPOUT[n].CONFIG registers have been captured on all endpoints reported in the EPSTATUS register. */
84     NRF_USBD_EVENT_ENDEPIN0      = offsetof(NRF_USBD_Type, EVENTS_ENDEPIN[0] ), /**< The whole EPIN[0] buffer has been consumed. The RAM buffer can be accessed safely by software. */
85     NRF_USBD_EVENT_ENDEPIN1      = offsetof(NRF_USBD_Type, EVENTS_ENDEPIN[1] ), /**< The whole EPIN[1] buffer has been consumed. The RAM buffer can be accessed safely by software. */
86     NRF_USBD_EVENT_ENDEPIN2      = offsetof(NRF_USBD_Type, EVENTS_ENDEPIN[2] ), /**< The whole EPIN[2] buffer has been consumed. The RAM buffer can be accessed safely by software. */
87     NRF_USBD_EVENT_ENDEPIN3      = offsetof(NRF_USBD_Type, EVENTS_ENDEPIN[3] ), /**< The whole EPIN[3] buffer has been consumed. The RAM buffer can be accessed safely by software. */
88     NRF_USBD_EVENT_ENDEPIN4      = offsetof(NRF_USBD_Type, EVENTS_ENDEPIN[4] ), /**< The whole EPIN[4] buffer has been consumed. The RAM buffer can be accessed safely by software. */
89     NRF_USBD_EVENT_ENDEPIN5      = offsetof(NRF_USBD_Type, EVENTS_ENDEPIN[5] ), /**< The whole EPIN[5] buffer has been consumed. The RAM buffer can be accessed safely by software. */
90     NRF_USBD_EVENT_ENDEPIN6      = offsetof(NRF_USBD_Type, EVENTS_ENDEPIN[6] ), /**< The whole EPIN[6] buffer has been consumed. The RAM buffer can be accessed safely by software. */
91     NRF_USBD_EVENT_ENDEPIN7      = offsetof(NRF_USBD_Type, EVENTS_ENDEPIN[7] ), /**< The whole EPIN[7] buffer has been consumed. The RAM buffer can be accessed safely by software. */
92     NRF_USBD_EVENT_EP0DATADONE   = offsetof(NRF_USBD_Type, EVENTS_EP0DATADONE), /**< An acknowledged data transfer has taken place on the control endpoint. */
93     NRF_USBD_EVENT_ENDISOIN0     = offsetof(NRF_USBD_Type, EVENTS_ENDISOIN   ), /**< The whole ISOIN buffer has been consumed. The RAM buffer can be accessed safely by software. */
94     NRF_USBD_EVENT_ENDEPOUT0     = offsetof(NRF_USBD_Type, EVENTS_ENDEPOUT[0]), /**< The whole EPOUT[0] buffer has been consumed. The RAM buffer can be accessed safely by software. */
95     NRF_USBD_EVENT_ENDEPOUT1     = offsetof(NRF_USBD_Type, EVENTS_ENDEPOUT[1]), /**< The whole EPOUT[1] buffer has been consumed. The RAM buffer can be accessed safely by software. */
96     NRF_USBD_EVENT_ENDEPOUT2     = offsetof(NRF_USBD_Type, EVENTS_ENDEPOUT[2]), /**< The whole EPOUT[2] buffer has been consumed. The RAM buffer can be accessed safely by software. */
97     NRF_USBD_EVENT_ENDEPOUT3     = offsetof(NRF_USBD_Type, EVENTS_ENDEPOUT[3]), /**< The whole EPOUT[3] buffer has been consumed. The RAM buffer can be accessed safely by software. */
98     NRF_USBD_EVENT_ENDEPOUT4     = offsetof(NRF_USBD_Type, EVENTS_ENDEPOUT[4]), /**< The whole EPOUT[4] buffer has been consumed. The RAM buffer can be accessed safely by software. */
99     NRF_USBD_EVENT_ENDEPOUT5     = offsetof(NRF_USBD_Type, EVENTS_ENDEPOUT[5]), /**< The whole EPOUT[5] buffer has been consumed. The RAM buffer can be accessed safely by software. */
100     NRF_USBD_EVENT_ENDEPOUT6     = offsetof(NRF_USBD_Type, EVENTS_ENDEPOUT[6]), /**< The whole EPOUT[6] buffer has been consumed. The RAM buffer can be accessed safely by software. */
101     NRF_USBD_EVENT_ENDEPOUT7     = offsetof(NRF_USBD_Type, EVENTS_ENDEPOUT[7]), /**< The whole EPOUT[7] buffer has been consumed. The RAM buffer can be accessed safely by software. */
102     NRF_USBD_EVENT_ENDISOOUT0    = offsetof(NRF_USBD_Type, EVENTS_ENDISOOUT  ), /**< The whole ISOOUT buffer has been consumed. The RAM buffer can be accessed safely by software. */
103     NRF_USBD_EVENT_SOF           = offsetof(NRF_USBD_Type, EVENTS_SOF        ), /**< Signals that a SOF (start of frame) condition has been detected on the USB lines. */
104     NRF_USBD_EVENT_USBEVENT      = offsetof(NRF_USBD_Type, EVENTS_USBEVENT   ), /**< An event or an error not covered by the specified events has occurred, check EVENTCAUSE register to find the cause. */
105     NRF_USBD_EVENT_EP0SETUP      = offsetof(NRF_USBD_Type, EVENTS_EP0SETUP   ), /**< A valid SETUP token has been received (and acknowledged) on the control endpoint. */
106     NRF_USBD_EVENT_DATAEP        = offsetof(NRF_USBD_Type, EVENTS_EPDATA     ), /**< A data transfer has occurred on a data endpoint, indicated by the EPDATASTATUS register. */
107 }nrf_usbd_event_t;
108 
109 /** @brief USBD shorts. */
110 typedef enum
111 {
112     NRF_USBD_SHORT_EP0DATADONE_STARTEPIN0_MASK  = USBD_SHORTS_EP0DATADONE_STARTEPIN0_Msk , /**< Shortcut between EP0DATADONE event and STARTEPIN0 task. */
113     NRF_USBD_SHORT_EP0DATADONE_STARTEPOUT0_MASK = USBD_SHORTS_EP0DATADONE_STARTEPOUT0_Msk, /**< Shortcut between EP0DATADONE event and STARTEPOUT0 task. */
114     NRF_USBD_SHORT_EP0DATADONE_EP0STATUS_MASK   = USBD_SHORTS_EP0DATADONE_EP0STATUS_Msk  , /**< Shortcut between EP0DATADONE event and EP0STATUS task. */
115     NRF_USBD_SHORT_ENDEPOUT0_EP0STATUS_MASK     = USBD_SHORTS_ENDEPOUT0_EP0STATUS_Msk    , /**< Shortcut between ENDEPOUT[0] event and EP0STATUS task. */
116     NRF_USBD_SHORT_ENDEPOUT0_EP0RCVOUT_MASK     = USBD_SHORTS_ENDEPOUT0_EP0RCVOUT_Msk    , /**< Shortcut between ENDEPOUT[0] event and EP0RCVOUT task. */
117 }nrf_usbd_short_mask_t;
118 
119 /** @brief USBD interrupts. */
120 typedef enum
121 {
122     NRF_USBD_INT_USBRESET_MASK    = USBD_INTEN_USBRESET_Msk   , /**< Enable or disable interrupt for USBRESET event. */
123     NRF_USBD_INT_STARTED_MASK     = USBD_INTEN_STARTED_Msk    , /**< Enable or disable interrupt for STARTED event. */
124     NRF_USBD_INT_ENDEPIN0_MASK    = USBD_INTEN_ENDEPIN0_Msk   , /**< Enable or disable interrupt for ENDEPIN[0] event. */
125     NRF_USBD_INT_ENDEPIN1_MASK    = USBD_INTEN_ENDEPIN1_Msk   , /**< Enable or disable interrupt for ENDEPIN[1] event. */
126     NRF_USBD_INT_ENDEPIN2_MASK    = USBD_INTEN_ENDEPIN2_Msk   , /**< Enable or disable interrupt for ENDEPIN[2] event. */
127     NRF_USBD_INT_ENDEPIN3_MASK    = USBD_INTEN_ENDEPIN3_Msk   , /**< Enable or disable interrupt for ENDEPIN[3] event. */
128     NRF_USBD_INT_ENDEPIN4_MASK    = USBD_INTEN_ENDEPIN4_Msk   , /**< Enable or disable interrupt for ENDEPIN[4] event. */
129     NRF_USBD_INT_ENDEPIN5_MASK    = USBD_INTEN_ENDEPIN5_Msk   , /**< Enable or disable interrupt for ENDEPIN[5] event. */
130     NRF_USBD_INT_ENDEPIN6_MASK    = USBD_INTEN_ENDEPIN6_Msk   , /**< Enable or disable interrupt for ENDEPIN[6] event. */
131     NRF_USBD_INT_ENDEPIN7_MASK    = USBD_INTEN_ENDEPIN7_Msk   , /**< Enable or disable interrupt for ENDEPIN[7] event. */
132     NRF_USBD_INT_EP0DATADONE_MASK = USBD_INTEN_EP0DATADONE_Msk, /**< Enable or disable interrupt for EP0DATADONE event. */
133     NRF_USBD_INT_ENDISOIN0_MASK   = USBD_INTEN_ENDISOIN_Msk   , /**< Enable or disable interrupt for ENDISOIN[0] event. */
134     NRF_USBD_INT_ENDEPOUT0_MASK   = USBD_INTEN_ENDEPOUT0_Msk  , /**< Enable or disable interrupt for ENDEPOUT[0] event. */
135     NRF_USBD_INT_ENDEPOUT1_MASK   = USBD_INTEN_ENDEPOUT1_Msk  , /**< Enable or disable interrupt for ENDEPOUT[1] event. */
136     NRF_USBD_INT_ENDEPOUT2_MASK   = USBD_INTEN_ENDEPOUT2_Msk  , /**< Enable or disable interrupt for ENDEPOUT[2] event. */
137     NRF_USBD_INT_ENDEPOUT3_MASK   = USBD_INTEN_ENDEPOUT3_Msk  , /**< Enable or disable interrupt for ENDEPOUT[3] event. */
138     NRF_USBD_INT_ENDEPOUT4_MASK   = USBD_INTEN_ENDEPOUT4_Msk  , /**< Enable or disable interrupt for ENDEPOUT[4] event. */
139     NRF_USBD_INT_ENDEPOUT5_MASK   = USBD_INTEN_ENDEPOUT5_Msk  , /**< Enable or disable interrupt for ENDEPOUT[5] event. */
140     NRF_USBD_INT_ENDEPOUT6_MASK   = USBD_INTEN_ENDEPOUT6_Msk  , /**< Enable or disable interrupt for ENDEPOUT[6] event. */
141     NRF_USBD_INT_ENDEPOUT7_MASK   = USBD_INTEN_ENDEPOUT7_Msk  , /**< Enable or disable interrupt for ENDEPOUT[7] event. */
142     NRF_USBD_INT_ENDISOOUT0_MASK  = USBD_INTEN_ENDISOOUT_Msk  , /**< Enable or disable interrupt for ENDISOOUT[0] event. */
143     NRF_USBD_INT_SOF_MASK         = USBD_INTEN_SOF_Msk        , /**< Enable or disable interrupt for SOF event. */
144     NRF_USBD_INT_USBEVENT_MASK    = USBD_INTEN_USBEVENT_Msk   , /**< Enable or disable interrupt for USBEVENT event. */
145     NRF_USBD_INT_EP0SETUP_MASK    = USBD_INTEN_EP0SETUP_Msk   , /**< Enable or disable interrupt for EP0SETUP event. */
146     NRF_USBD_INT_DATAEP_MASK      = USBD_INTEN_EPDATA_Msk     , /**< Enable or disable interrupt for EPDATA event. */
147 }nrf_usbd_int_mask_t;
148 
149 
150 /**
151  * @brief Function for activating the specified USBD task.
152  *
153  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
154  * @param[in] task  Task to be activated.
155  */
156 NRF_STATIC_INLINE void nrf_usbd_task_trigger(NRF_USBD_Type * p_reg, nrf_usbd_task_t task);
157 
158 /**
159  * @brief Function for returning the address of the specified USBD task register.
160  *
161  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
162  * @param[in] task  Task.
163  *
164  * @return Task address.
165  */
166 NRF_STATIC_INLINE uint32_t nrf_usbd_task_address_get(NRF_USBD_Type const * p_reg,
167                                                      nrf_usbd_task_t       task);
168 
169 /**
170  * @brief Function for clearing the specified event.
171  *
172  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
173  * @param[in] event Event.
174  */
175 NRF_STATIC_INLINE void nrf_usbd_event_clear(NRF_USBD_Type * p_reg, nrf_usbd_event_t event);
176 
177 /**
178  * @brief Function for retrieving the state of the USBD event.
179  *
180  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
181  * @param[in] event Event to be checked.
182  *
183  * @retval true  The event has been generated.
184  * @retval false The event has not been generated.
185  */
186 NRF_STATIC_INLINE bool nrf_usbd_event_check(NRF_USBD_Type const * p_reg, nrf_usbd_event_t event);
187 
188 /**
189  * @brief Function for getting and clearing the state of the specified event.
190  *
191  * This function checks the state of the event and clears it.
192  *
193  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
194  * @param[in] event Event.
195  *
196  * @retval true  The event was set.
197  * @retval false The event was not set.
198  */
199 NRF_STATIC_INLINE bool nrf_usbd_event_get_and_clear(NRF_USBD_Type * p_reg, nrf_usbd_event_t event);
200 
201 /**
202  * @brief Function for returning the address of the specified USBD event register.
203  *
204  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
205  * @param[in] event Event.
206  *
207  * @return Address.
208  */
209 NRF_STATIC_INLINE uint32_t nrf_usbd_event_address_get(NRF_USBD_Type const * p_reg,
210                                                       nrf_usbd_event_t      event);
211 
212 /**
213  * @brief Function for setting shortcuts.
214  *
215  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
216  * @param[in] mask  Shortcut mask.
217  */
218 NRF_STATIC_INLINE void nrf_usbd_shorts_enable(NRF_USBD_Type * p_reg, uint32_t mask);
219 
220 /**
221  * @brief Function for clearing shortcuts.
222  *
223  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
224  * @param[in] mask  Shortcut mask.
225  */
226 NRF_STATIC_INLINE void nrf_usbd_shorts_disable(NRF_USBD_Type * p_reg, uint32_t mask);
227 
228 /**
229  * @brief Function for getting the shortcut mask.
230  *
231  * Function returns shortcut register.
232  *
233  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
234  *
235  * @return Flags of the currently enabled shortcuts.
236  */
237 NRF_STATIC_INLINE uint32_t nrf_usbd_shorts_get(NRF_USBD_Type const * p_reg);
238 
239 /**
240  * @brief Function for enabling the selected interrupts.
241  *
242  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
243  * @param[in] mask  Mask of interrupts to be enabled.
244  *                  Use @ref nrf_usbd_int_mask_t values for bit masking.
245  */
246 NRF_STATIC_INLINE void nrf_usbd_int_enable(NRF_USBD_Type * p_reg, uint32_t mask);
247 
248 /**
249  * @brief Function for checking if the specified interrupts are enabled.
250  *
251  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
252  * @param[in] mask  Mask of interrupts to be checked.
253  *                  Use @ref nrf_usbd_int_mask_t values for bit masking.
254  *
255  * @return Mask of enabled interrupts.
256  */
257 NRF_STATIC_INLINE uint32_t nrf_usbd_int_enable_check(NRF_USBD_Type const * p_reg, uint32_t mask);
258 
259 /**
260  * @brief Function for retrieving the information about the enabled interrupts.
261  *
262  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
263  *
264  * @return The flags of the enabled interrupts.
265  */
266 NRF_STATIC_INLINE uint32_t nrf_usbd_int_enable_get(NRF_USBD_Type const * p_reg);
267 
268 /**
269  * @brief Function for disabling the selected interrupts.
270  *
271  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
272  * @param[in] mask  Mask of interrupts to be disabled.
273  *                  Use @ref nrf_usbd_int_mask_t values for bit masking.
274  */
275 NRF_STATIC_INLINE void nrf_usbd_int_disable(NRF_USBD_Type * p_reg, uint32_t mask);
276 
277 
278 /** @} */ /*  End of nrf_usbd_hal */
279 
280 
281 #ifndef NRF_DECLARE_ONLY
282 
283 /* ------------------------------------------------------------------------------------------------
284  *  Internal functions
285  */
286 
287 /**
288  * @internal
289  * @brief Internal function for getting the register address of task or event.
290  *
291  * @param[in] p_reg  Pointer to the structure of registers of the peripheral.
292  * @param[in] offset Offset of the register from the beginning of the instance.
293  *
294  * @attention The offset must be aligned to 4. In other case, hardware fault can occur.
295  * @return Pointer to the register.
296  */
nrf_usbd_getRegPtr(NRF_USBD_Type * p_reg,uint32_t offset)297 NRF_STATIC_INLINE volatile uint32_t* nrf_usbd_getRegPtr(NRF_USBD_Type * p_reg, uint32_t offset)
298 {
299     return (volatile uint32_t*)(((uint8_t *)p_reg) + (uint32_t)offset);
300 }
301 
302 /**
303  * @internal
304  * @brief Internal function for getting the register address of task or event - constant version.
305  *
306  * @param[in] p_reg  Pointer to the structure of registers of the peripheral.
307  * @param[in] offset Offset of the register from the beginning of the instance.
308  *
309  * @attention The offset must be aligned to 4. In other case, hardware fault can occur.
310  * @return Pointer to the register.
311  */
nrf_usbd_getRegPtr_c(NRF_USBD_Type const * p_reg,uint32_t offset)312 NRF_STATIC_INLINE volatile const uint32_t* nrf_usbd_getRegPtr_c(NRF_USBD_Type const * p_reg,
313                                                                 uint32_t              offset)
314 {
315     return (volatile const uint32_t*)(((uint8_t const *)p_reg) + (uint32_t)offset);
316 }
317 
318 /* ------------------------------------------------------------------------------------------------
319  *  Interface functions definitions
320  */
321 
nrf_usbd_task_trigger(NRF_USBD_Type * p_reg,nrf_usbd_task_t task)322 NRF_STATIC_INLINE void nrf_usbd_task_trigger(NRF_USBD_Type * p_reg, nrf_usbd_task_t task)
323 {
324     *(nrf_usbd_getRegPtr(p_reg, (uint32_t)task)) = 1UL;
325     (void)*(nrf_usbd_getRegPtr(p_reg, (uint32_t)task));
326 }
327 
nrf_usbd_task_address_get(NRF_USBD_Type const * p_reg,nrf_usbd_task_t task)328 NRF_STATIC_INLINE uint32_t nrf_usbd_task_address_get(NRF_USBD_Type const * p_reg,
329                                                      nrf_usbd_task_t       task)
330 {
331     return (uint32_t)nrf_usbd_getRegPtr_c(p_reg, (uint32_t)task);
332 }
333 
nrf_usbd_event_clear(NRF_USBD_Type * p_reg,nrf_usbd_event_t event)334 NRF_STATIC_INLINE void nrf_usbd_event_clear(NRF_USBD_Type * p_reg, nrf_usbd_event_t event)
335 {
336     *(nrf_usbd_getRegPtr(p_reg, (uint32_t)event)) = 0UL;
337     (void)*(nrf_usbd_getRegPtr(p_reg, (uint32_t)event));
338 }
339 
nrf_usbd_event_check(NRF_USBD_Type const * p_reg,nrf_usbd_event_t event)340 NRF_STATIC_INLINE bool nrf_usbd_event_check(NRF_USBD_Type const * p_reg, nrf_usbd_event_t event)
341 {
342     return (bool)*nrf_usbd_getRegPtr_c(p_reg, (uint32_t)event);
343 }
344 
nrf_usbd_event_get_and_clear(NRF_USBD_Type * p_reg,nrf_usbd_event_t event)345 NRF_STATIC_INLINE bool nrf_usbd_event_get_and_clear(NRF_USBD_Type * p_reg, nrf_usbd_event_t event)
346 {
347     bool ret = nrf_usbd_event_check(p_reg, event);
348     if (ret)
349     {
350         nrf_usbd_event_clear(p_reg, event);
351     }
352     return ret;
353 }
354 
nrf_usbd_event_address_get(NRF_USBD_Type const * p_reg,nrf_usbd_event_t event)355 NRF_STATIC_INLINE uint32_t nrf_usbd_event_address_get(NRF_USBD_Type const * p_reg,
356                                                       nrf_usbd_event_t      event)
357 {
358     return (uint32_t)nrf_usbd_getRegPtr_c(p_reg, (uint32_t)event);
359 }
360 
nrf_usbd_shorts_enable(NRF_USBD_Type * p_reg,uint32_t mask)361 NRF_STATIC_INLINE void nrf_usbd_shorts_enable(NRF_USBD_Type * p_reg, uint32_t mask)
362 {
363     p_reg->SHORTS |= mask;
364 }
365 
nrf_usbd_shorts_disable(NRF_USBD_Type * p_reg,uint32_t mask)366 NRF_STATIC_INLINE void nrf_usbd_shorts_disable(NRF_USBD_Type * p_reg, uint32_t mask)
367 {
368     if (~0U == mask)
369     {
370         /* Optimized version for "disable all" */
371         p_reg->SHORTS = 0;
372     }
373     else
374     {
375         p_reg->SHORTS &= ~mask;
376     }
377 }
378 
nrf_usbd_shorts_get(NRF_USBD_Type const * p_reg)379 NRF_STATIC_INLINE uint32_t nrf_usbd_shorts_get(NRF_USBD_Type const * p_reg)
380 {
381     return p_reg->SHORTS;
382 }
383 
nrf_usbd_int_enable(NRF_USBD_Type * p_reg,uint32_t mask)384 NRF_STATIC_INLINE void nrf_usbd_int_enable(NRF_USBD_Type * p_reg, uint32_t mask)
385 {
386     p_reg->INTENSET = mask;
387 }
388 
nrf_usbd_int_enable_check(NRF_USBD_Type const * p_reg,uint32_t mask)389 NRF_STATIC_INLINE uint32_t nrf_usbd_int_enable_check(NRF_USBD_Type const * p_reg, uint32_t mask)
390 {
391     return p_reg->INTENSET & mask;
392 }
393 
nrf_usbd_int_enable_get(NRF_USBD_Type const * p_reg)394 NRF_STATIC_INLINE uint32_t nrf_usbd_int_enable_get(NRF_USBD_Type const * p_reg)
395 {
396     return p_reg->INTENSET;
397 }
398 
nrf_usbd_int_disable(NRF_USBD_Type * p_reg,uint32_t mask)399 NRF_STATIC_INLINE void nrf_usbd_int_disable(NRF_USBD_Type * p_reg, uint32_t mask)
400 {
401     p_reg->INTENCLR = mask;
402 }
403 
404 #endif /* NRF_DECLARE_ONLY */
405 
406 /* ------------------------------------------------------------------------------------------------
407  *  End of automatically generated part
408  * ------------------------------------------------------------------------------------------------
409  */
410 /**
411  * @addtogroup nrf_usbd_hal
412  * @{
413  */
414 
415 /**
416  * @brief Frame counter size.
417  *
418  * The number of counts that can be fitted into frame counter.
419  */
420 #define NRF_USBD_FRAMECNTR_SIZE \
421     ( (USBD_FRAMECNTR_FRAMECNTR_Msk >> USBD_FRAMECNTR_FRAMECNTR_Pos) + 1UL )
422 #ifndef USBD_FRAMECNTR_FRAMECNTR_Msk
423 #error USBD_FRAMECNTR_FRAMECNTR_Msk should be changed into USBD_FRAMECNTR_FRAMECNTR_Msk
424 #endif
425 
426 /**
427  * @brief First isochronous endpoint number.
428  *
429  * The number of the first isochronous endpoint.
430  */
431 #define NRF_USBD_EPISO_FIRST 8
432 
433 /**
434  * @brief Total number of IN endpoints.
435  *
436  * Total number of IN endpoint (including ISOCHRONOUS).
437  */
438 #define NRF_USBD_EPIN_CNT 9
439 
440 /**
441  * @brief Total number of OUT endpoints.
442  *
443  * Total number of OUT endpoint (including ISOCHRONOUS).
444  */
445 #define NRF_USBD_EPOUT_CNT 9
446 
447 /** @brief Mask of the direction bit in an endpoint number. */
448 #define NRF_USBD_EP_DIR_Msk (1U << 7)
449 
450 /** @brief The value of direction bit for the IN endpoint direction. */
451 #define NRF_USBD_EP_DIR_IN  (1U << 7)
452 
453 /** @brief The value of direction bit for the OUT endpoint direction. */
454 #define NRF_USBD_EP_DIR_OUT (0U << 7)
455 
456 /**
457  * @brief Macro for making the IN endpoint identifier from endpoint number.
458  *
459  * Macro that sets direction bit to make IN endpoint.
460  *
461  * @param[in] epnr Endpoint number.
462  *
463  * @return IN Endpoint identifier.
464  */
465 #define NRF_USBD_EPIN(epnr)  (((uint8_t)(epnr)) | NRF_USBD_EP_DIR_IN)
466 
467 /**
468  * @brief Macro for making the OUT endpoint identifier from endpoint number.
469  *
470  * Macro that sets direction bit to make OUT endpoint.
471  *
472  * @param[in] epnr Endpoint number.
473  *
474  * @return OUT Endpoint identifier.
475  */
476 #define NRF_USBD_EPOUT(epnr) (((uint8_t)(epnr)) | NRF_USBD_EP_DIR_OUT)
477 
478 /**
479  * @brief Macro for extracting the endpoint number from the specified endpoint identifier.
480  *
481  * Macro that strips out the information about endpoint direction.
482  *
483  * @param[in] ep Endpoint identifier.
484  *
485  * @return Endpoint number.
486  */
487 #define NRF_USBD_EP_NR_GET(ep) ((uint8_t)(((uint8_t)(ep)) & 0xFU))
488 
489 /**
490  * @brief Macro for checking the endpoint direction.
491  *
492  * This macro checks if the specified endpoint has the IN direction.
493  *
494  * @param[in] ep Endpoint identifier.
495  *
496  * @retval true  The endpoint direction is IN.
497  * @retval false The endpoint direction is OUT.
498  */
499 #define NRF_USBD_EPIN_CHECK(ep)  ( (((uint8_t)(ep)) & NRF_USBD_EP_DIR_Msk) == NRF_USBD_EP_DIR_IN  )
500 
501 /**
502  * @brief Macro for checking endpoint direction.
503  *
504  * This macro checks if given endpoint has OUT direction.
505  *
506  * @param[in] ep Endpoint identifier
507  *
508  * @retval true  The endpoint direction is OUT
509  * @retval false The endpoint direction is IN
510  */
511 #define NRF_USBD_EPOUT_CHECK(ep) ( (((uint8_t)(ep)) & NRF_USBD_EP_DIR_Msk) == NRF_USBD_EP_DIR_OUT )
512 
513 /**
514  * @brief Macro for checking if endpoint is isochronous.
515  *
516  * @param[in] ep It can be endpoint identifier or just endpoint number to be checked.
517  *
518  * @retval true  The endpoint is isochronous type.
519  * @retval false The endpoint is bulk of interrupt type.
520  */
521 #define NRF_USBD_EPISO_CHECK(ep) (NRF_USBD_EP_NR_GET(ep) >= NRF_USBD_EPISO_FIRST)
522 
523 /**
524  * @brief Macro for checking if given number is valid endpoint number.
525  *
526  * @param[in] ep Endpoint number to be checked.
527  *
528  * @retval true  The endpoint is valid.
529  * @retval false The endpoint is not valid.
530  */
531 #define NRF_USBD_EP_VALIDATE(ep) (                                              \
532     (NRF_USBD_EPIN_CHECK(ep) && (NRF_USBD_EP_NR_GET(ep) < NRF_USBD_EPIN_CNT))   \
533     ||                                                                          \
534     (NRF_USBD_EPOUT_CHECK(ep) && (NRF_USBD_EP_NR_GET(ep) < NRF_USBD_EPOUT_CNT)) \
535     )
536 
537 /**
538  * @brief Not isochronous data frame received.
539  *
540  * Special value returned by @ref nrf_usbd_episoout_size_get function that means that
541  * data frame was not received at all.
542  * This allows differentiate between situations when zero size data comes or no data comes at all
543  * on isochronous endpoint.
544  */
545 #define NRF_USBD_EPISOOUT_NO_DATA ((size_t)(-1))
546 
547 /** @brief EVENTCAUSE register bit masks. */
548 typedef enum
549 {
550     NRF_USBD_EVENTCAUSE_ISOOUTCRC_MASK    = USBD_EVENTCAUSE_ISOOUTCRC_Msk,      /**< CRC error was detected on isochronous OUT endpoint 8. */
551     NRF_USBD_EVENTCAUSE_SUSPEND_MASK      = USBD_EVENTCAUSE_SUSPEND_Msk,        /**< Signals that the USB lines have been seen idle long enough for the device to enter suspend. */
552     NRF_USBD_EVENTCAUSE_RESUME_MASK       = USBD_EVENTCAUSE_RESUME_Msk,         /**< Signals that a RESUME condition (K state or activity restart) has been detected on the USB lines. */
553     NRF_USBD_EVENTCAUSE_WUREQ_MASK        = USBD_EVENTCAUSE_USBWUALLOWED_Msk,   /**< The USBD peripheral has exited Low Power mode. */
554     NRF_USBD_EVENTCAUSE_READY_MASK        = USBD_EVENTCAUSE_READY_Msk,          /**< MAC is ready for normal operation, rised few us after USBD enabling. */
555 }nrf_usbd_eventcause_mask_t;
556 
557 /** @brief DPDMVALUE register. */
558 typedef enum
559 {
560     /** Generate RESUME signal. Signal is generated for 50&nbsp;us or 5&nbsp;ms,
561      *  depending on bus state. */
562     NRF_USBD_DPDMVALUE_RESUME = USBD_DPDMVALUE_STATE_Resume,
563     /** D+ Forced high, D- forced low (J state) */
564     NRF_USBD_DPDMVALUE_J      = USBD_DPDMVALUE_STATE_J,
565     /** D+ Forced low, D- forced high (K state) */
566     NRF_USBD_DPMVALUE_K       = USBD_DPDMVALUE_STATE_K
567 }nrf_usbd_dpdmvalue_t;
568 
569 /** @brief Data toggle value or operation. */
570 typedef enum
571 {
572     NRF_USBD_DTOGGLE_NOP   = USBD_DTOGGLE_VALUE_Nop,  /**< No operation - do not change the current data toggle on the selected endpoint. */
573     NRF_USBD_DTOGGLE_DATA0 = USBD_DTOGGLE_VALUE_Data0,/**< Data toggle is DATA0 on the selected endpoint. */
574     NRF_USBD_DTOGGLE_DATA1 = USBD_DTOGGLE_VALUE_Data1 /**< Data toggle is DATA1 on the selected endpoint. */
575 }nrf_usbd_dtoggle_t;
576 
577 /** @brief EPSTATUS bit masks. */
578 typedef enum
579 {
580     NRF_USBD_EPSTATUS_EPIN0_MASK  = USBD_EPSTATUS_EPIN0_Msk,
581     NRF_USBD_EPSTATUS_EPIN1_MASK  = USBD_EPSTATUS_EPIN1_Msk,
582     NRF_USBD_EPSTATUS_EPIN2_MASK  = USBD_EPSTATUS_EPIN2_Msk,
583     NRF_USBD_EPSTATUS_EPIN3_MASK  = USBD_EPSTATUS_EPIN3_Msk,
584     NRF_USBD_EPSTATUS_EPIN4_MASK  = USBD_EPSTATUS_EPIN4_Msk,
585     NRF_USBD_EPSTATUS_EPIN5_MASK  = USBD_EPSTATUS_EPIN5_Msk,
586     NRF_USBD_EPSTATUS_EPIN6_MASK  = USBD_EPSTATUS_EPIN6_Msk,
587     NRF_USBD_EPSTATUS_EPIN7_MASK  = USBD_EPSTATUS_EPIN7_Msk,
588 
589     NRF_USBD_EPSTATUS_EPOUT0_MASK = USBD_EPSTATUS_EPOUT0_Msk,
590     NRF_USBD_EPSTATUS_EPOUT1_MASK = USBD_EPSTATUS_EPOUT1_Msk,
591     NRF_USBD_EPSTATUS_EPOUT2_MASK = USBD_EPSTATUS_EPOUT2_Msk,
592     NRF_USBD_EPSTATUS_EPOUT3_MASK = USBD_EPSTATUS_EPOUT3_Msk,
593     NRF_USBD_EPSTATUS_EPOUT4_MASK = USBD_EPSTATUS_EPOUT4_Msk,
594     NRF_USBD_EPSTATUS_EPOUT5_MASK = USBD_EPSTATUS_EPOUT5_Msk,
595     NRF_USBD_EPSTATUS_EPOUT6_MASK = USBD_EPSTATUS_EPOUT6_Msk,
596     NRF_USBD_EPSTATUS_EPOUT7_MASK = USBD_EPSTATUS_EPOUT7_Msk,
597 }nrf_usbd_epstatus_mask_t;
598 
599 /** @brief DATAEPSTATUS bit masks. */
600 typedef enum
601 {
602     NRF_USBD_EPDATASTATUS_EPIN1_MASK  = USBD_EPDATASTATUS_EPIN1_Msk,
603     NRF_USBD_EPDATASTATUS_EPIN2_MASK  = USBD_EPDATASTATUS_EPIN2_Msk,
604     NRF_USBD_EPDATASTATUS_EPIN3_MASK  = USBD_EPDATASTATUS_EPIN3_Msk,
605     NRF_USBD_EPDATASTATUS_EPIN4_MASK  = USBD_EPDATASTATUS_EPIN4_Msk,
606     NRF_USBD_EPDATASTATUS_EPIN5_MASK  = USBD_EPDATASTATUS_EPIN5_Msk,
607     NRF_USBD_EPDATASTATUS_EPIN6_MASK  = USBD_EPDATASTATUS_EPIN6_Msk,
608     NRF_USBD_EPDATASTATUS_EPIN7_MASK  = USBD_EPDATASTATUS_EPIN7_Msk,
609 
610     NRF_USBD_EPDATASTATUS_EPOUT1_MASK = USBD_EPDATASTATUS_EPOUT1_Msk,
611     NRF_USBD_EPDATASTATUS_EPOUT2_MASK = USBD_EPDATASTATUS_EPOUT2_Msk,
612     NRF_USBD_EPDATASTATUS_EPOUT3_MASK = USBD_EPDATASTATUS_EPOUT3_Msk,
613     NRF_USBD_EPDATASTATUS_EPOUT4_MASK = USBD_EPDATASTATUS_EPOUT4_Msk,
614     NRF_USBD_EPDATASTATUS_EPOUT5_MASK = USBD_EPDATASTATUS_EPOUT5_Msk,
615     NRF_USBD_EPDATASTATUS_EPOUT6_MASK = USBD_EPDATASTATUS_EPOUT6_Msk,
616     NRF_USBD_EPDATASTATUS_EPOUT7_MASK = USBD_EPDATASTATUS_EPOUT7_Msk,
617 }nrf_usbd_dataepstatus_mask_t;
618 
619 /** @brief ISOSPLIT configurations. */
620 typedef enum
621 {
622     NRF_USBD_ISOSPLIT_ONEDIR = USBD_ISOSPLIT_SPLIT_OneDir, /**< Full buffer dedicated to either ISO IN or OUT. */
623     NRF_USBD_ISOSPLIT_HALF   = USBD_ISOSPLIT_SPLIT_HalfIN, /**< Buffer divided in half. */
624 }nrf_usbd_isosplit_t;
625 
626 /** @brief ISOINCONFIG configurations. */
627 typedef enum
628 {
629     NRF_USBD_ISOINCONFIG_NORESP   = USBD_ISOINCONFIG_RESPONSE_NoResp,   /**< Endpoint does not respond to an ISO IN token when no data is ready. */
630     NRF_USBD_ISOINCONFIG_ZERODATA = USBD_ISOINCONFIG_RESPONSE_ZeroData, /**< Endpoint responds with a zero-length data packet to an ISO IN token when no data is ready. */
631 }nrf_usbd_isoinconfig_t;
632 
633 /**
634  * @brief Function for enabling the USBD.
635  *
636  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
637  */
638 NRF_STATIC_INLINE void nrf_usbd_enable(NRF_USBD_Type * p_reg);
639 
640 /**
641  * @brief Function for disabling the USBD.
642  *
643  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
644  */
645 NRF_STATIC_INLINE void nrf_usbd_disable(NRF_USBD_Type * p_reg);
646 
647 /**
648  * @brief Function for getting the EVENTCAUSE register.
649  *
650  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
651  *
652  * @return Flag values defined in @ref nrf_usbd_eventcause_mask_t.
653  */
654 NRF_STATIC_INLINE uint32_t nrf_usbd_eventcause_get(NRF_USBD_Type const * p_reg);
655 
656 /**
657  * @brief Function for clearing the EVENTCAUSE flags.
658  *
659  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
660  * @param[in] flags Flags defined in @ref nrf_usbd_eventcause_mask_t.
661  */
662 NRF_STATIC_INLINE void nrf_usbd_eventcause_clear(NRF_USBD_Type * p_reg, uint32_t flags);
663 
664 /**
665  * @brief Function for getting the EVENTCAUSE register and clearing flags that are set.
666  *
667  * The safest way to return current EVENTCAUSE register.
668  * All the flags that are returned would be cleared inside EVENTCAUSE register.
669  *
670  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
671  *
672  * @return Flag values defined in @ref nrf_usbd_eventcause_mask_t
673  */
674 NRF_STATIC_INLINE uint32_t nrf_usbd_eventcause_get_and_clear(NRF_USBD_Type * p_reg);
675 
676 /**
677  * @brief Function for getting the HALTEDEPIN register value.
678  *
679  * @note Use this function for the response for GetStatus() request to endpoint.
680  *       To check whether the endpoint is stalled in the code, use @ref nrf_usbd_ep_is_stall.
681  *
682  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
683  * @param[in] ep    Endpoint number with IN or OUT flag.
684  *
685  * @return The value of the HALTEDEPIN or HALTEDOUT register for the selected endpoint.
686  */
687 NRF_STATIC_INLINE uint32_t nrf_usbd_halted_get(NRF_USBD_Type const * p_reg, uint8_t ep);
688 
689 /**
690  * @brief Function for checking whether the selected endpoint is stalled.
691  *
692  * Function to be used as a syntax sweeter for @ref nrf_usbd_halted_get.
693  *
694  * Also as the isochronous endpoint cannot be halted - it returns always false
695  * if isochronous endpoint is checked.
696  *
697  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
698  * @param[in] ep    Endpoint number with IN or OUT flag.
699  *
700  * @retval true  The endpoint is halted.
701  * @retval false The endpoint is not halted.
702  */
703 NRF_STATIC_INLINE bool nrf_usbd_ep_is_stall(NRF_USBD_Type const * p_reg, uint8_t ep);
704 
705 /**
706  * @brief Function for getting EPSTATUS register value.
707  *
708  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
709  *
710  * @return Flag values defined in @ref nrf_usbd_epstatus_mask_t.
711  */
712 NRF_STATIC_INLINE uint32_t nrf_usbd_epstatus_get(NRF_USBD_Type const * p_reg);
713 
714 /**
715  * @brief Function for clearing EPSTATUS register value.
716  *
717  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
718  * @param[in] flags Flags defined in @ref nrf_usbd_epstatus_mask_t.
719  */
720 NRF_STATIC_INLINE void nrf_usbd_epstatus_clear(NRF_USBD_Type * p_reg, uint32_t flags);
721 
722 /**
723  * @brief Function for getting and clearing EPSTATUS register value.
724  *
725  * Function clears all flags in register set before returning its value.
726  *
727  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
728  *
729  * @return Flag values defined in @ref nrf_usbd_epstatus_mask_t.
730  */
731 NRF_STATIC_INLINE uint32_t nrf_usbd_epstatus_get_and_clear(NRF_USBD_Type * p_reg);
732 
733 /**
734  * @brief Function for getting DATAEPSTATUS register value.
735  *
736  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
737  *
738  * @return Flag values defined in @ref nrf_usbd_dataepstatus_mask_t.
739  */
740 NRF_STATIC_INLINE uint32_t nrf_usbd_epdatastatus_get(NRF_USBD_Type const * p_reg);
741 
742 /**
743  * @brief Function for clearing DATAEPSTATUS register value.
744  *
745  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
746  * @param[in] flags Flags defined in @ref nrf_usbd_dataepstatus_mask_t.
747  */
748 NRF_STATIC_INLINE void nrf_usbd_epdatastatus_clear(NRF_USBD_Type * p_reg, uint32_t flags);
749 
750 /**
751  * @brief Function for getting and clearing DATAEPSTATUS register value.
752  *
753  * Function clears all flags in register set before returning its value.
754  *
755  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
756  *
757  * @return Flag values defined in @ref nrf_usbd_dataepstatus_mask_t.
758  */
759 NRF_STATIC_INLINE uint32_t nrf_usbd_epdatastatus_get_and_clear(NRF_USBD_Type * p_reg);
760 
761 /**
762  * @name Setup command frame functions.
763  *
764  * Functions for setting up command frame part access.
765  * @{
766  */
767     /**
768      * @brief Function for reading BMREQUESTTYPE - part of the SETUP packet.
769      *
770      * @param[in] p_reg Pointer to the structure of registers of the peripheral.
771      *
772      * @return Value of BREQUESTTYPE on the last received SETUP frame.
773      */
774     NRF_STATIC_INLINE uint8_t nrf_usbd_setup_bmrequesttype_get(NRF_USBD_Type const * p_reg);
775 
776     /**
777      * @brief Function for reading BMREQUEST - part of the SETUP packet.
778      *
779      * @param[in] p_reg Pointer to the structure of registers of the peripheral.
780      *
781      * @return Value of BREQUEST on the last received SETUP frame.
782      */
783     NRF_STATIC_INLINE uint8_t nrf_usbd_setup_brequest_get(NRF_USBD_Type const * p_reg);
784 
785     /**
786      * @brief Function for reading WVALUE - part of the SETUP packet.
787      *
788      * @param[in] p_reg Pointer to the structure of registers of the peripheral.
789      *
790      * @return Value of WVALUE on the last received SETUP frame.
791      */
792     NRF_STATIC_INLINE uint16_t nrf_usbd_setup_wvalue_get(NRF_USBD_Type const * p_reg);
793 
794     /**
795      * @brief Function for reading WINDEX - part of the SETUP packet.
796      *
797      * @param[in] p_reg Pointer to the structure of registers of the peripheral.
798      *
799      * @return Value of WINDEX on the last received SETUP frame.
800      */
801     NRF_STATIC_INLINE uint16_t nrf_usbd_setup_windex_get(NRF_USBD_Type const * p_reg);
802 
803     /**
804      * @brief Function for reading WLENGTH - part of the SETUP packet.
805      *
806      * @param[in] p_reg Pointer to the structure of registers of the peripheral.
807      *
808      * @return Value of WLENGTH on the last received SETUP frame.
809      */
810     NRF_STATIC_INLINE uint16_t nrf_usbd_setup_wlength_get(NRF_USBD_Type const * p_reg);
811 /** @} */
812 
813 /**
814  * @brief Function for getting the number of received bytes on the selected endpoint.
815  *
816  * @note This function can be used on bulk, interrupt, and isochronous endpoints.
817  * @note For the function that returns different value for the ISOOUT zero
818  *       transfer or no transfer at all, see the @ref nrf_usbd_episoout_size_get
819  *       function. This function will return 0 for both cases.
820  *
821  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
822  * @param[in] ep    Endpoint identifier.
823  *
824  * @return Number of received bytes.
825  */
826 NRF_STATIC_INLINE size_t nrf_usbd_epout_size_get(NRF_USBD_Type const * p_reg, uint8_t ep);
827 
828 /**
829  * @brief Function for getting number of the received bytes on isochronous endpoint.
830  *
831  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
832  * @param[in] ep    Endpoint identifier, must be an isochronous OUT endpoint.
833  *
834  * @return Number of bytes received or @ref NRF_USBD_EPISOOUT_NO_DATA.
835  */
836 NRF_STATIC_INLINE size_t nrf_usbd_episoout_size_get(NRF_USBD_Type const * p_reg, uint8_t ep);
837 
838 /**
839  * @brief Function for clearing OUT endpoint to accept any new incoming traffic.
840  *
841  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
842  * @param[in] ep    Endpoint identifier. Only OUT interrupt/bulk endpoints are accepted.
843  */
844 NRF_STATIC_INLINE void nrf_usbd_epout_clear(NRF_USBD_Type * p_reg, uint8_t ep);
845 
846 /**
847  * @brief Function for enabling the USB pullup.
848  *
849  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
850  */
851 NRF_STATIC_INLINE void nrf_usbd_pullup_enable(NRF_USBD_Type * p_reg);
852 
853 /**
854  * @brief Function for disabling the USB pullup.
855  *
856  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
857  */
858 NRF_STATIC_INLINE void nrf_usbd_pullup_disable(NRF_USBD_Type * p_reg);
859 
860 /**
861  * @brief Function for returning the current the USB pullup state.
862  *
863  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
864  *
865  * @retval true  The USB pullup is enabled.
866  * @retval false The USB pullup is disabled.
867  */
868 NRF_STATIC_INLINE bool nrf_usbd_pullup_check(NRF_USBD_Type const * p_reg);
869 
870 /**
871  * @brief Function for configuring the value to be forced on the bus on the DRIVEDPDM task.
872  *
873  * The selected state will be forced on the bus when @ref NRF_USBD_TASK_DRIVEDPDM is set.
874  * The state will be removed from the bus on @ref NRF_USBD_TASK_NODRIVEDPDM and
875  * the control will be returned to the USBD peripheral.
876  *
877  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
878  * @param[in] val   State to be set.
879  */
880 NRF_STATIC_INLINE void nrf_usbd_dpdmvalue_set(NRF_USBD_Type * p_reg, nrf_usbd_dpdmvalue_t val);
881 
882 /**
883  * @brief Function for setting the data toggle.
884  *
885  * Configuration of the current state of data toggling.
886  *
887  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
888  * @param[in] ep    Endpoint number with the information about its direction.
889  * @param[in] op    Operation to execute.
890  */
891 NRF_STATIC_INLINE void nrf_usbd_dtoggle_set(NRF_USBD_Type *    p_reg,
892                                             uint8_t            ep,
893                                             nrf_usbd_dtoggle_t op);
894 
895 /**
896  * @brief Function for getting the data toggle.
897  *
898  * Get the current state of data toggling.
899  *
900  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
901  * @param[in] ep    Endpoint number to return the information about current data toggling.
902  *
903  * @retval NRF_USBD_DTOGGLE_DATA0 Data toggle is DATA0 on selected endpoint.
904  * @retval NRF_USBD_DTOGGLE_DATA1 Data toggle is DATA1 on selected endpoint.
905  */
906 NRF_STATIC_INLINE nrf_usbd_dtoggle_t nrf_usbd_dtoggle_get(NRF_USBD_Type * p_reg, uint8_t ep);
907 
908 /**
909  * @brief Function for checking whether the endpoint is enabled.
910  *
911  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
912  * @param[in] ep    Endpoint ID to be checked.
913  *
914  * @retval true  Endpoint is enabled.
915  * @retval false Endpoint is disabled.
916  */
917 NRF_STATIC_INLINE bool nrf_usbd_ep_enable_check(NRF_USBD_Type const * p_reg, uint8_t ep);
918 
919 /**
920  * @brief Function for enabling the selected endpoint.
921  *
922  * The enabled endpoint responds for the tokens on the USB bus.
923  *
924  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
925  * @param[in] ep    Endpoint ID to be enabled.
926  */
927 NRF_STATIC_INLINE void nrf_usbd_ep_enable(NRF_USBD_Type * p_reg, uint8_t ep);
928 
929 /**
930  * @brief Function for disabling the selected endpoint.
931  *
932  * The disabled endpoint does not respond for the tokens on the USB bus.
933  *
934  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
935  * @param[in] ep    Endpoint ID to be disabled.
936  */
937 NRF_STATIC_INLINE void nrf_usbd_ep_disable(NRF_USBD_Type * p_reg, uint8_t ep);
938 
939 /**
940  * @brief Function for disabling all endpoints.
941  *
942  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
943  */
944 NRF_STATIC_INLINE void nrf_usbd_ep_all_disable(NRF_USBD_Type * p_reg);
945 
946 /**
947  * @brief Function for setting the default endpoint configuration.
948  *
949  * @note The default configuration means that all endpoints, except for EP0 IN
950  *       and EP0 OUT, are disabled. Such configuration is used after the USB
951  *       device reset.
952  *
953  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
954  */
955 NRF_STATIC_INLINE void nrf_usbd_ep_default_config(NRF_USBD_Type * p_reg);
956 
957 /**
958  * @brief Function for stalling the selected endpoint.
959  *
960  * @note This function cannot be called on isochronous endpoint.
961  *
962  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
963  * @param[in] ep    Endpoint identifier.
964  */
965 NRF_STATIC_INLINE void nrf_usbd_ep_stall(NRF_USBD_Type * p_reg, uint8_t ep);
966 
967 /**
968  * @brief Function for unstalling the selected endpoint.
969  *
970  * @note This function cannot be called on isochronous endpoint.
971  *
972  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
973  * @param[in] ep    Endpoint identifier
974  */
975 NRF_STATIC_INLINE void nrf_usbd_ep_unstall(NRF_USBD_Type * p_reg, uint8_t ep);
976 
977 /**
978  * @brief Function for configuring the isochronous buffer splitting.
979  *
980  * This function configures isochronous buffer splitting between IN and OUT endpoints.
981  *
982  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
983  * @param[in] split Required configuration.
984  */
985 NRF_STATIC_INLINE void nrf_usbd_isosplit_set(NRF_USBD_Type * p_reg, nrf_usbd_isosplit_t split);
986 
987 /**
988  * @brief Function for getting the isochronous buffer splitting configuration.
989  *
990  * This function gets the current isochronous buffer splitting configuration.
991  *
992  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
993  *
994  * @return Current configuration.
995  */
996 NRF_STATIC_INLINE nrf_usbd_isosplit_t nrf_usbd_isosplit_get(NRF_USBD_Type const * p_reg);
997 
998 /**
999  * @brief Function for getting current frame counter.
1000  *
1001  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
1002  *
1003  * @return Current frame counter.
1004  */
1005 NRF_STATIC_INLINE uint32_t nrf_usbd_framecntr_get(NRF_USBD_Type const * p_reg);
1006 
1007 /**
1008  * @brief Function for entering into the USB low power mode.
1009  *
1010  * After this function is called, the clock source from the USBD is disconnected internally.
1011  * After this function is called, most of the USBD registers cannot be accessed anymore.
1012  *
1013  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
1014  *
1015  * @sa nrf_usbd_lowpower_disable
1016  * @sa nrf_usbd_lowpower_check
1017  */
1018 NRF_STATIC_INLINE void nrf_usbd_lowpower_enable(NRF_USBD_Type * p_reg);
1019 
1020 /**
1021  * @brief Function for exiting from the USB low power mode.
1022  *
1023  * After this function is called, the clock source for the USBD is connected internally.
1024  * The @ref NRF_USBD_EVENTCAUSE_WUREQ_MASK event will be generated and
1025  * then the USBD registers can be accessed.
1026  *
1027  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
1028  *
1029  * @sa nrf_usbd_lowpower_enable
1030  * @sa nrf_usbd_lowpower_check
1031  */
1032 NRF_STATIC_INLINE void nrf_usbd_lowpower_disable(NRF_USBD_Type * p_reg);
1033 
1034 /**
1035  * @brief Function for checking the state of the low power mode.
1036  *
1037  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
1038  *
1039  * @retval true  The USBD is in low power mode.
1040  * @retval false The USBD is not in low power mode.
1041  */
1042 NRF_STATIC_INLINE bool nrf_usbd_lowpower_check(NRF_USBD_Type const * p_reg);
1043 
1044 /**
1045  * @brief Function for configuring ISO IN endpoint response to an IN token when no data is ready to be sent.
1046  *
1047  * @param[in] p_reg  Pointer to the structure of registers of the peripheral.
1048  * @param[in] config Required configuration.
1049  */
1050 NRF_STATIC_INLINE void nrf_usbd_isoinconfig_set(NRF_USBD_Type *        p_reg,
1051                                                 nrf_usbd_isoinconfig_t config);
1052 
1053 /**
1054  * @brief Function for getting the cofiguration of ISO IN endpoint response to an IN token when no data is ready to be sent.
1055  *
1056  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
1057  *
1058  * @return Current configuration.
1059  */
1060 NRF_STATIC_INLINE nrf_usbd_isoinconfig_t nrf_usbd_isoinconfig_get(NRF_USBD_Type const * p_reg);
1061 
1062 /**
1063  * @brief Function for configuring the EasyDMA channel.
1064  *
1065  * Configures EasyDMA for the transfer.
1066  *
1067  * @param[in] p_reg  Pointer to the structure of registers of the peripheral.
1068  * @param[in] ep     Endpoint identifier (with direction).
1069  * @param[in] ptr    Pointer to the data.
1070  * @param[in] maxcnt Number of bytes to transfer.
1071  */
1072 NRF_STATIC_INLINE void nrf_usbd_ep_easydma_set(NRF_USBD_Type * p_reg,
1073                                                uint8_t         ep,
1074                                                uint32_t        ptr,
1075                                                uint32_t        maxcnt);
1076 
1077 /**
1078  * @brief Function for getting number of transferred bytes.
1079  *
1080  * This function gets the number of transferred bytes in the last transaction.
1081  *
1082  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
1083  * @param[in] ep    Endpoint identifier.
1084  *
1085  * @return The content of the AMOUNT register.
1086  */
1087 NRF_STATIC_INLINE uint32_t nrf_usbd_ep_amount_get(NRF_USBD_Type const * p_reg, uint8_t ep);
1088 
1089 
1090 #ifndef NRF_DECLARE_ONLY
1091 
nrf_usbd_enable(NRF_USBD_Type * p_reg)1092 NRF_STATIC_INLINE void nrf_usbd_enable(NRF_USBD_Type * p_reg)
1093 {
1094     p_reg->ENABLE = USBD_ENABLE_ENABLE_Enabled << USBD_ENABLE_ENABLE_Pos;
1095     (void) p_reg->ENABLE;
1096 }
1097 
nrf_usbd_disable(NRF_USBD_Type * p_reg)1098 NRF_STATIC_INLINE void nrf_usbd_disable(NRF_USBD_Type * p_reg)
1099 {
1100     p_reg->ENABLE = USBD_ENABLE_ENABLE_Disabled << USBD_ENABLE_ENABLE_Pos;
1101     (void) p_reg->ENABLE;
1102 }
1103 
nrf_usbd_eventcause_get(NRF_USBD_Type const * p_reg)1104 NRF_STATIC_INLINE uint32_t nrf_usbd_eventcause_get(NRF_USBD_Type const * p_reg)
1105 {
1106     return p_reg->EVENTCAUSE;
1107 }
1108 
nrf_usbd_eventcause_clear(NRF_USBD_Type * p_reg,uint32_t flags)1109 NRF_STATIC_INLINE void nrf_usbd_eventcause_clear(NRF_USBD_Type * p_reg, uint32_t flags)
1110 {
1111     p_reg->EVENTCAUSE = flags;
1112     (void) p_reg->EVENTCAUSE;
1113 }
1114 
nrf_usbd_eventcause_get_and_clear(NRF_USBD_Type * p_reg)1115 NRF_STATIC_INLINE uint32_t nrf_usbd_eventcause_get_and_clear(NRF_USBD_Type * p_reg)
1116 {
1117     uint32_t ret;
1118     ret = nrf_usbd_eventcause_get(p_reg);
1119     nrf_usbd_eventcause_clear(p_reg, ret);
1120     return ret;
1121 }
1122 
nrf_usbd_halted_get(NRF_USBD_Type const * p_reg,uint8_t ep)1123 NRF_STATIC_INLINE uint32_t nrf_usbd_halted_get(NRF_USBD_Type const * p_reg, uint8_t ep)
1124 {
1125     uint8_t epnr = NRF_USBD_EP_NR_GET(ep);
1126     if (NRF_USBD_EPIN_CHECK(ep))
1127     {
1128         return p_reg->HALTED.EPIN[epnr];
1129     }
1130     else
1131     {
1132         return p_reg->HALTED.EPOUT[epnr];
1133     }
1134 }
1135 
nrf_usbd_ep_is_stall(NRF_USBD_Type const * p_reg,uint8_t ep)1136 NRF_STATIC_INLINE bool nrf_usbd_ep_is_stall(NRF_USBD_Type const * p_reg, uint8_t ep)
1137 {
1138     if (NRF_USBD_EPISO_CHECK(ep))
1139         return false;
1140     return USBD_HALTED_EPOUT_GETSTATUS_Halted == nrf_usbd_halted_get(p_reg, ep);
1141 }
1142 
nrf_usbd_epstatus_get(NRF_USBD_Type const * p_reg)1143 NRF_STATIC_INLINE uint32_t nrf_usbd_epstatus_get(NRF_USBD_Type const * p_reg)
1144 {
1145     return p_reg->EPSTATUS;
1146 }
1147 
nrf_usbd_epstatus_clear(NRF_USBD_Type * p_reg,uint32_t flags)1148 NRF_STATIC_INLINE void nrf_usbd_epstatus_clear(NRF_USBD_Type * p_reg, uint32_t flags)
1149 {
1150     p_reg->EPSTATUS = flags;
1151 }
1152 
nrf_usbd_epstatus_get_and_clear(NRF_USBD_Type * p_reg)1153 NRF_STATIC_INLINE uint32_t nrf_usbd_epstatus_get_and_clear(NRF_USBD_Type * p_reg)
1154 {
1155     uint32_t ret;
1156     ret = nrf_usbd_epstatus_get(p_reg);
1157     nrf_usbd_epstatus_clear(p_reg, ret);
1158     return ret;
1159 }
1160 
nrf_usbd_epdatastatus_get(NRF_USBD_Type const * p_reg)1161 NRF_STATIC_INLINE uint32_t nrf_usbd_epdatastatus_get(NRF_USBD_Type const * p_reg)
1162 {
1163     return p_reg->EPDATASTATUS;
1164 }
1165 
nrf_usbd_epdatastatus_clear(NRF_USBD_Type * p_reg,uint32_t flags)1166 NRF_STATIC_INLINE void nrf_usbd_epdatastatus_clear(NRF_USBD_Type * p_reg, uint32_t flags)
1167 {
1168     p_reg->EPDATASTATUS = flags;
1169 }
1170 
nrf_usbd_epdatastatus_get_and_clear(NRF_USBD_Type * p_reg)1171 NRF_STATIC_INLINE uint32_t nrf_usbd_epdatastatus_get_and_clear(NRF_USBD_Type * p_reg)
1172 {
1173     uint32_t ret;
1174     ret = nrf_usbd_epdatastatus_get(p_reg);
1175     nrf_usbd_epdatastatus_clear(p_reg, ret);
1176     return ret;
1177 }
1178 
nrf_usbd_setup_bmrequesttype_get(NRF_USBD_Type const * p_reg)1179 NRF_STATIC_INLINE uint8_t nrf_usbd_setup_bmrequesttype_get(NRF_USBD_Type const * p_reg)
1180 {
1181     return (uint8_t)(p_reg->BMREQUESTTYPE);
1182 }
1183 
nrf_usbd_setup_brequest_get(NRF_USBD_Type const * p_reg)1184 NRF_STATIC_INLINE uint8_t nrf_usbd_setup_brequest_get(NRF_USBD_Type const * p_reg)
1185 {
1186     return (uint8_t)(p_reg->BREQUEST);
1187 }
1188 
nrf_usbd_setup_wvalue_get(NRF_USBD_Type const * p_reg)1189 NRF_STATIC_INLINE uint16_t nrf_usbd_setup_wvalue_get(NRF_USBD_Type const * p_reg)
1190 {
1191     const uint16_t val = (uint16_t)p_reg->WVALUEL;
1192     return (uint16_t)(val | ((p_reg->WVALUEH) << 8));
1193 }
1194 
nrf_usbd_setup_windex_get(NRF_USBD_Type const * p_reg)1195 NRF_STATIC_INLINE uint16_t nrf_usbd_setup_windex_get(NRF_USBD_Type const * p_reg)
1196 {
1197     const uint16_t val = (uint16_t)p_reg->WINDEXL;
1198     return (uint16_t)(val | ((p_reg->WINDEXH) << 8));
1199 }
1200 
nrf_usbd_setup_wlength_get(NRF_USBD_Type const * p_reg)1201 NRF_STATIC_INLINE uint16_t nrf_usbd_setup_wlength_get(NRF_USBD_Type const * p_reg)
1202 {
1203     const uint16_t val = (uint16_t)p_reg->WLENGTHL;
1204     return (uint16_t)(val | ((p_reg->WLENGTHH) << 8));
1205 }
1206 
nrf_usbd_epout_size_get(NRF_USBD_Type const * p_reg,uint8_t ep)1207 NRF_STATIC_INLINE size_t nrf_usbd_epout_size_get(NRF_USBD_Type const * p_reg, uint8_t ep)
1208 {
1209     NRFX_ASSERT(NRF_USBD_EP_VALIDATE(ep));
1210     NRFX_ASSERT(NRF_USBD_EPOUT_CHECK(ep));
1211     if (NRF_USBD_EPISO_CHECK(ep))
1212     {
1213         size_t size_isoout = p_reg->SIZE.ISOOUT;
1214         if ((size_isoout & USBD_SIZE_ISOOUT_ZERO_Msk) ==
1215             (USBD_SIZE_ISOOUT_ZERO_ZeroData << USBD_SIZE_ISOOUT_ZERO_Pos))
1216         {
1217             size_isoout = 0;
1218         }
1219         return size_isoout;
1220     }
1221 
1222     return p_reg->SIZE.EPOUT[NRF_USBD_EP_NR_GET(ep)];
1223 }
1224 
nrf_usbd_episoout_size_get(NRF_USBD_Type const * p_reg,uint8_t ep)1225 NRF_STATIC_INLINE size_t nrf_usbd_episoout_size_get(NRF_USBD_Type const * p_reg, uint8_t ep)
1226 {
1227     NRFX_ASSERT(NRF_USBD_EP_VALIDATE(ep));
1228     NRFX_ASSERT(NRF_USBD_EPOUT_CHECK(ep));
1229     NRFX_ASSERT(NRF_USBD_EPISO_CHECK(ep));
1230 
1231     size_t size_isoout = p_reg->SIZE.ISOOUT;
1232     if (size_isoout == 0)
1233     {
1234         size_isoout = NRF_USBD_EPISOOUT_NO_DATA;
1235     }
1236     else if ((size_isoout & USBD_SIZE_ISOOUT_ZERO_Msk) ==
1237              (USBD_SIZE_ISOOUT_ZERO_ZeroData << USBD_SIZE_ISOOUT_ZERO_Pos))
1238     {
1239         size_isoout = 0;
1240     }
1241     return size_isoout;
1242 }
1243 
nrf_usbd_epout_clear(NRF_USBD_Type * p_reg,uint8_t ep)1244 NRF_STATIC_INLINE void nrf_usbd_epout_clear(NRF_USBD_Type * p_reg, uint8_t ep)
1245 {
1246     NRFX_ASSERT(NRF_USBD_EPOUT_CHECK(ep));
1247     p_reg->SIZE.EPOUT[NRF_USBD_EP_NR_GET(ep)] = 0;
1248     (void) p_reg->SIZE.EPOUT[NRF_USBD_EP_NR_GET(ep)];
1249 }
1250 
nrf_usbd_pullup_enable(NRF_USBD_Type * p_reg)1251 NRF_STATIC_INLINE void nrf_usbd_pullup_enable(NRF_USBD_Type * p_reg)
1252 {
1253     p_reg->USBPULLUP = USBD_USBPULLUP_CONNECT_Enabled << USBD_USBPULLUP_CONNECT_Pos;
1254     (void) p_reg->USBPULLUP;
1255 }
1256 
nrf_usbd_pullup_disable(NRF_USBD_Type * p_reg)1257 NRF_STATIC_INLINE void nrf_usbd_pullup_disable(NRF_USBD_Type * p_reg)
1258 {
1259     p_reg->USBPULLUP = USBD_USBPULLUP_CONNECT_Disabled << USBD_USBPULLUP_CONNECT_Pos;
1260     (void) p_reg->USBPULLUP;
1261 }
1262 
nrf_usbd_pullup_check(NRF_USBD_Type const * p_reg)1263 NRF_STATIC_INLINE bool nrf_usbd_pullup_check(NRF_USBD_Type const * p_reg)
1264 {
1265     return p_reg->USBPULLUP == (USBD_USBPULLUP_CONNECT_Enabled << USBD_USBPULLUP_CONNECT_Pos);
1266 }
1267 
nrf_usbd_dpdmvalue_set(NRF_USBD_Type * p_reg,nrf_usbd_dpdmvalue_t val)1268 NRF_STATIC_INLINE void nrf_usbd_dpdmvalue_set(NRF_USBD_Type * p_reg, nrf_usbd_dpdmvalue_t val)
1269 {
1270     p_reg->DPDMVALUE = ((uint32_t)val) << USBD_DPDMVALUE_STATE_Pos;
1271 }
1272 
nrf_usbd_dtoggle_set(NRF_USBD_Type * p_reg,uint8_t ep,nrf_usbd_dtoggle_t op)1273 NRF_STATIC_INLINE void nrf_usbd_dtoggle_set(NRF_USBD_Type *    p_reg,
1274                                             uint8_t            ep,
1275                                             nrf_usbd_dtoggle_t op)
1276 {
1277     NRFX_ASSERT(NRF_USBD_EP_VALIDATE(ep));
1278     NRFX_ASSERT(!NRF_USBD_EPISO_CHECK(ep));
1279     p_reg->DTOGGLE = ep | (NRF_USBD_DTOGGLE_NOP << USBD_DTOGGLE_VALUE_Pos);
1280     p_reg->DTOGGLE = ep | ((uint32_t)op << USBD_DTOGGLE_VALUE_Pos);
1281     (void) p_reg->DTOGGLE;
1282 }
1283 
nrf_usbd_dtoggle_get(NRF_USBD_Type * p_reg,uint8_t ep)1284 NRF_STATIC_INLINE nrf_usbd_dtoggle_t nrf_usbd_dtoggle_get(NRF_USBD_Type * p_reg, uint8_t ep)
1285 {
1286     uint32_t retval;
1287     /* Select the endpoint to read */
1288     p_reg->DTOGGLE = ep | (NRF_USBD_DTOGGLE_NOP << USBD_DTOGGLE_VALUE_Pos);
1289     retval = ((p_reg->DTOGGLE) & USBD_DTOGGLE_VALUE_Msk) >> USBD_DTOGGLE_VALUE_Pos;
1290     return (nrf_usbd_dtoggle_t)retval;
1291 }
1292 
nrf_usbd_ep_enable_check(NRF_USBD_Type const * p_reg,uint8_t ep)1293 NRF_STATIC_INLINE bool nrf_usbd_ep_enable_check(NRF_USBD_Type const * p_reg, uint8_t ep)
1294 {
1295     NRFX_ASSERT(NRF_USBD_EP_VALIDATE(ep));
1296     uint8_t epnr = NRF_USBD_EP_NR_GET(ep);
1297 
1298     if (NRF_USBD_EPIN_CHECK(ep))
1299     {
1300         return 0 != (p_reg->EPINEN & (1UL << epnr));
1301     }
1302     else
1303     {
1304         return 0 != (p_reg->EPOUTEN & (1UL << epnr));
1305     }
1306 }
1307 
nrf_usbd_ep_enable(NRF_USBD_Type * p_reg,uint8_t ep)1308 NRF_STATIC_INLINE void nrf_usbd_ep_enable(NRF_USBD_Type * p_reg, uint8_t ep)
1309 {
1310     NRFX_ASSERT(NRF_USBD_EP_VALIDATE(ep));
1311     uint8_t epnr = NRF_USBD_EP_NR_GET(ep);
1312 
1313     if (NRF_USBD_EPIN_CHECK(ep))
1314     {
1315         p_reg->EPINEN |= 1UL << epnr;
1316     }
1317     else
1318     {
1319         p_reg->EPOUTEN |= 1UL << epnr;
1320     }
1321 }
1322 
nrf_usbd_ep_disable(NRF_USBD_Type * p_reg,uint8_t ep)1323 NRF_STATIC_INLINE void nrf_usbd_ep_disable(NRF_USBD_Type * p_reg, uint8_t ep)
1324 {
1325     NRFX_ASSERT(NRF_USBD_EP_VALIDATE(ep));
1326     uint8_t epnr = NRF_USBD_EP_NR_GET(ep);
1327 
1328     if (NRF_USBD_EPIN_CHECK(ep))
1329     {
1330         p_reg->EPINEN &= ~(1UL << epnr);
1331     }
1332     else
1333     {
1334         p_reg->EPOUTEN &= ~(1UL << epnr);
1335     }
1336 }
1337 
nrf_usbd_ep_all_disable(NRF_USBD_Type * p_reg)1338 NRF_STATIC_INLINE void nrf_usbd_ep_all_disable(NRF_USBD_Type * p_reg)
1339 {
1340     p_reg->EPINEN  = 0;
1341     p_reg->EPOUTEN = 0;
1342 }
1343 
nrf_usbd_ep_default_config(NRF_USBD_Type * p_reg)1344 void nrf_usbd_ep_default_config(NRF_USBD_Type * p_reg)
1345 {
1346     p_reg->EPINEN  = USBD_EPINEN_IN0_Enable << USBD_EPINEN_IN0_Pos;
1347     p_reg->EPOUTEN = USBD_EPOUTEN_OUT0_Enable << USBD_EPOUTEN_OUT0_Pos;
1348 }
1349 
nrf_usbd_ep_stall(NRF_USBD_Type * p_reg,uint8_t ep)1350 NRF_STATIC_INLINE void nrf_usbd_ep_stall(NRF_USBD_Type * p_reg, uint8_t ep)
1351 {
1352     NRFX_ASSERT(!NRF_USBD_EPISO_CHECK(ep));
1353     p_reg->EPSTALL = (USBD_EPSTALL_STALL_Stall << USBD_EPSTALL_STALL_Pos) | ep;
1354 }
1355 
nrf_usbd_ep_unstall(NRF_USBD_Type * p_reg,uint8_t ep)1356 NRF_STATIC_INLINE void nrf_usbd_ep_unstall(NRF_USBD_Type * p_reg, uint8_t ep)
1357 {
1358     NRFX_ASSERT(!NRF_USBD_EPISO_CHECK(ep));
1359     p_reg->EPSTALL = (USBD_EPSTALL_STALL_UnStall << USBD_EPSTALL_STALL_Pos) | ep;
1360 }
1361 
nrf_usbd_isosplit_set(NRF_USBD_Type * p_reg,nrf_usbd_isosplit_t split)1362 NRF_STATIC_INLINE void nrf_usbd_isosplit_set(NRF_USBD_Type * p_reg, nrf_usbd_isosplit_t split)
1363 {
1364     p_reg->ISOSPLIT = split << USBD_ISOSPLIT_SPLIT_Pos;
1365 }
1366 
nrf_usbd_isosplit_get(NRF_USBD_Type const * p_reg)1367 NRF_STATIC_INLINE nrf_usbd_isosplit_t nrf_usbd_isosplit_get(NRF_USBD_Type const * p_reg)
1368 {
1369     return (nrf_usbd_isosplit_t)
1370         (((p_reg->ISOSPLIT) & USBD_ISOSPLIT_SPLIT_Msk) >> USBD_ISOSPLIT_SPLIT_Pos);
1371 }
1372 
nrf_usbd_framecntr_get(NRF_USBD_Type const * p_reg)1373 NRF_STATIC_INLINE uint32_t nrf_usbd_framecntr_get(NRF_USBD_Type const * p_reg)
1374 {
1375     return p_reg->FRAMECNTR;
1376 }
1377 
nrf_usbd_lowpower_enable(NRF_USBD_Type * p_reg)1378 NRF_STATIC_INLINE void nrf_usbd_lowpower_enable(NRF_USBD_Type * p_reg)
1379 {
1380     p_reg->LOWPOWER = USBD_LOWPOWER_LOWPOWER_LowPower << USBD_LOWPOWER_LOWPOWER_Pos;
1381     (void) p_reg->LOWPOWER;
1382 }
1383 
nrf_usbd_lowpower_disable(NRF_USBD_Type * p_reg)1384 NRF_STATIC_INLINE void nrf_usbd_lowpower_disable(NRF_USBD_Type * p_reg)
1385 {
1386     p_reg->LOWPOWER = USBD_LOWPOWER_LOWPOWER_ForceNormal << USBD_LOWPOWER_LOWPOWER_Pos;
1387     (void) p_reg->LOWPOWER;
1388 }
1389 
nrf_usbd_lowpower_check(NRF_USBD_Type const * p_reg)1390 NRF_STATIC_INLINE bool nrf_usbd_lowpower_check(NRF_USBD_Type const * p_reg)
1391 {
1392     return (p_reg->LOWPOWER != (USBD_LOWPOWER_LOWPOWER_ForceNormal << USBD_LOWPOWER_LOWPOWER_Pos));
1393 }
1394 
nrf_usbd_isoinconfig_set(NRF_USBD_Type * p_reg,nrf_usbd_isoinconfig_t config)1395 NRF_STATIC_INLINE void nrf_usbd_isoinconfig_set(NRF_USBD_Type *        p_reg,
1396                                                 nrf_usbd_isoinconfig_t config)
1397 {
1398     p_reg->ISOINCONFIG = ((uint32_t)config) << USBD_ISOINCONFIG_RESPONSE_Pos;
1399 }
1400 
nrf_usbd_isoinconfig_get(NRF_USBD_Type const * p_reg)1401 NRF_STATIC_INLINE nrf_usbd_isoinconfig_t nrf_usbd_isoinconfig_get(NRF_USBD_Type const * p_reg)
1402 {
1403     return (nrf_usbd_isoinconfig_t) (((p_reg->ISOINCONFIG) & USBD_ISOINCONFIG_RESPONSE_Msk) >>
1404                                      USBD_ISOINCONFIG_RESPONSE_Pos);
1405 }
1406 
nrf_usbd_ep_easydma_set(NRF_USBD_Type * p_reg,uint8_t ep,uint32_t ptr,uint32_t maxcnt)1407 NRF_STATIC_INLINE void nrf_usbd_ep_easydma_set(NRF_USBD_Type * p_reg,
1408                                                uint8_t         ep,
1409                                                uint32_t        ptr,
1410                                                uint32_t        maxcnt)
1411 {
1412     if (NRF_USBD_EPIN_CHECK(ep))
1413     {
1414         if (NRF_USBD_EPISO_CHECK(ep))
1415         {
1416             p_reg->ISOIN.PTR    = ptr;
1417             p_reg->ISOIN.MAXCNT = maxcnt;
1418         }
1419         else
1420         {
1421             uint8_t epnr = NRF_USBD_EP_NR_GET(ep);
1422             p_reg->EPIN[epnr].PTR    = ptr;
1423             p_reg->EPIN[epnr].MAXCNT = maxcnt;
1424         }
1425     }
1426     else
1427     {
1428         if (NRF_USBD_EPISO_CHECK(ep))
1429         {
1430             p_reg->ISOOUT.PTR    = ptr;
1431             p_reg->ISOOUT.MAXCNT = maxcnt;
1432         }
1433         else
1434         {
1435             uint8_t epnr = NRF_USBD_EP_NR_GET(ep);
1436             p_reg->EPOUT[epnr].PTR    = ptr;
1437             p_reg->EPOUT[epnr].MAXCNT = maxcnt;
1438         }
1439     }
1440 }
1441 
nrf_usbd_ep_amount_get(NRF_USBD_Type const * p_reg,uint8_t ep)1442 NRF_STATIC_INLINE uint32_t nrf_usbd_ep_amount_get(NRF_USBD_Type const * p_reg, uint8_t ep)
1443 {
1444     uint32_t ret;
1445 
1446     if (NRF_USBD_EPIN_CHECK(ep))
1447     {
1448         if (NRF_USBD_EPISO_CHECK(ep))
1449         {
1450             ret = p_reg->ISOIN.AMOUNT;
1451         }
1452         else
1453         {
1454             uint8_t epnr = NRF_USBD_EP_NR_GET(ep);
1455             ret = p_reg->EPIN[epnr].AMOUNT;
1456         }
1457     }
1458     else
1459     {
1460         if (NRF_USBD_EPISO_CHECK(ep))
1461         {
1462             ret = p_reg->ISOOUT.AMOUNT;
1463         }
1464         else
1465         {
1466             uint8_t epnr = NRF_USBD_EP_NR_GET(ep);
1467             ret = p_reg->EPOUT[epnr].AMOUNT;
1468         }
1469     }
1470 
1471     return ret;
1472 }
1473 
1474 #endif /* NRF_DECLARE_ONLY */
1475 
1476 /** @} */
1477 
1478 #ifdef __cplusplus
1479 }
1480 #endif
1481 
1482 #endif /* NRF_USBD_H__ */
1483