1 /*
2  * Copyright (c) 2023 - 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_USBHS_H__
35 #define NRF_USBHS_H__
36 
37 #include <nrfx.h>
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /**
44  * @defgroup nrf_usbhs_hal USBHS HAL
45  * @{
46  * @ingroup nrf_usbhs
47  * @brief   Hardware access layer for managing the Universal Serial Bus High Speed (USBHS) peripheral.
48  */
49 
50 /** @brief USBHS tasks. */
51 typedef enum
52 {
53     NRF_USBHS_TASK_START = offsetof(NRF_USBHS_Type, TASKS_START), ///< Start the USB peripheral.
54 } nrf_usbhs_task_t;
55 
56 /** @brief USBHS events. */
57 typedef enum
58 {
59     NRF_USBHS_EVENT_CORE = offsetof(NRF_USBHS_Type, EVENTS_CORE), ///< Signal that the USB reset condition is detected on the USB lines.
60 } nrf_usbhs_event_t;
61 
62 /** @brief USBHS interrupts. */
63 typedef enum
64 {
65     NRF_USBHS_INT_USBCORE_MASK = USBHS_INTENSET_CORE_Msk, ///< Interrupt on the USBCORE event.
66 } nrf_usbhs_int_mask_t;
67 
68 /**
69  * @brief Function for activating the specified USBHS task.
70  *
71  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
72  * @param[in] task  Task to be activated.
73  */
74 NRF_STATIC_INLINE void nrf_usbhs_task_trigger(NRF_USBHS_Type * p_reg, nrf_usbhs_task_t task);
75 
76 /**
77  * @brief Function for returning the address of the specified USBHS task register.
78  *
79  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
80  * @param[in] task  The specified task.
81  *
82  * @return Task address.
83  */
84 NRF_STATIC_INLINE uint32_t nrf_usbhs_task_address_get(NRF_USBHS_Type const * p_reg,
85                                                       nrf_usbhs_task_t       task);
86 
87 /**
88  * @brief Function for clearing the specified event.
89  *
90  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
91  * @param[in] event Event to be cleared.
92  */
93 NRF_STATIC_INLINE void nrf_usbhs_event_clear(NRF_USBHS_Type * p_reg, nrf_usbhs_event_t event);
94 
95 /**
96  * @brief Function for retrieving the state of the USBHS event.
97  *
98  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
99  * @param[in] event Event to be checked.
100  *
101  * @retval true  The event has been generated.
102  * @retval false The event has not been generated.
103  */
104 NRF_STATIC_INLINE bool nrf_usbhs_event_check(NRF_USBHS_Type const * p_reg, nrf_usbhs_event_t event);
105 
106 /**
107  * @brief Function for getting and clearing the state of the specified event.
108  *
109  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
110  * @param[in] event Event to be cleared.
111  *
112  * @retval true  The event was set.
113  * @retval false The event was not set.
114  */
115 NRF_STATIC_INLINE bool nrf_usbhs_event_get_and_clear(NRF_USBHS_Type * p_reg, nrf_usbhs_event_t event);
116 
117 /**
118  * @brief Function for returning the address of the specified USBHS event register.
119  *
120  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
121  * @param[in] event The specified event.
122  *
123  * @return Address of the event specified as a function parameter.
124  */
125 NRF_STATIC_INLINE uint32_t nrf_usbhs_event_address_get(NRF_USBHS_Type const * p_reg,
126                                                        nrf_usbhs_event_t      event);
127 /**
128  * @brief Function for enabling the selected interrupts.
129  *
130  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
131  * @param[in] mask  Mask of interrupts to be enabled.
132  *                  Use @ref nrf_usbhs_int_mask_t values for bit masking.
133  */
134 NRF_STATIC_INLINE void nrf_usbhs_int_enable(NRF_USBHS_Type * p_reg, uint32_t mask);
135 
136 /**
137  * @brief Function for checking if the specified interrupts are enabled.
138  *
139  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
140  * @param[in] mask  Mask of interrupts to be checked.
141  *                  Use @ref nrf_usbhs_int_mask_t values for bit masking.
142  *
143  * @return Mask of enabled interrupts.
144  */
145 NRF_STATIC_INLINE uint32_t nrf_usbhs_int_enable_check(NRF_USBHS_Type const * p_reg, uint32_t mask);
146 
147 /**
148  * @brief Function for retrieving the information about the enabled interrupts.
149  *
150  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
151  *
152  * @return The flags of the enabled interrupts.
153  */
154 NRF_STATIC_INLINE uint32_t nrf_usbhs_int_enable_get(NRF_USBHS_Type const * p_reg);
155 
156 /**
157  * @brief Function for disabling the selected interrupts.
158  *
159  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
160  * @param[in] mask  Mask of interrupts to be disabled.
161  *                  Use @ref nrf_usbhs_int_mask_t values for bit masking.
162  */
163 NRF_STATIC_INLINE void nrf_usbhs_int_disable(NRF_USBHS_Type * p_reg, uint32_t mask);
164 
165 /**
166  * @brief Function for enabling the USBHS.
167  *
168  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
169  */
170 NRF_STATIC_INLINE void nrf_usbhs_enable(NRF_USBHS_Type * p_reg);
171 
172 /**
173  * @brief Function for disabling the USBHS.
174  *
175  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
176  */
177 NRF_STATIC_INLINE void nrf_usbhs_disable(NRF_USBHS_Type * p_reg);
178 
179 #ifndef NRF_DECLARE_ONLY
180 
nrf_usbhs_task_trigger(NRF_USBHS_Type * p_reg,nrf_usbhs_task_t task)181 NRF_STATIC_INLINE void nrf_usbhs_task_trigger(NRF_USBHS_Type * p_reg, nrf_usbhs_task_t task)
182 {
183     *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL;
184 }
185 
nrf_usbhs_task_address_get(NRF_USBHS_Type const * p_reg,nrf_usbhs_task_t task)186 NRF_STATIC_INLINE uint32_t nrf_usbhs_task_address_get(NRF_USBHS_Type const * p_reg,
187                                                       nrf_usbhs_task_t       task)
188 {
189     return ((uint32_t)p_reg + (uint32_t)task);
190 }
191 
nrf_usbhs_event_clear(NRF_USBHS_Type * p_reg,nrf_usbhs_event_t event)192 NRF_STATIC_INLINE void nrf_usbhs_event_clear(NRF_USBHS_Type * p_reg, nrf_usbhs_event_t event)
193 {
194     *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
195     nrf_event_readback((uint8_t *)p_reg + (uint32_t)event);
196 }
197 
nrf_usbhs_event_check(NRF_USBHS_Type const * p_reg,nrf_usbhs_event_t event)198 NRF_STATIC_INLINE bool nrf_usbhs_event_check(NRF_USBHS_Type const * p_reg, nrf_usbhs_event_t event)
199 {
200     return nrf_event_check(p_reg, event);
201 }
202 
nrf_usbhs_event_get_and_clear(NRF_USBHS_Type * p_reg,nrf_usbhs_event_t event)203 NRF_STATIC_INLINE bool nrf_usbhs_event_get_and_clear(NRF_USBHS_Type * p_reg, nrf_usbhs_event_t event)
204 {
205     bool ret = nrf_usbhs_event_check(p_reg, event);
206     if (ret)
207     {
208         nrf_usbhs_event_clear(p_reg, event);
209     }
210     return ret;
211 }
212 
nrf_usbhs_event_address_get(NRF_USBHS_Type const * p_reg,nrf_usbhs_event_t event)213 NRF_STATIC_INLINE uint32_t nrf_usbhs_event_address_get(NRF_USBHS_Type const * p_reg,
214                                                        nrf_usbhs_event_t      event)
215 {
216     return ((uint32_t)p_reg + (uint32_t)event);
217 }
218 
nrf_usbhs_int_enable(NRF_USBHS_Type * p_reg,uint32_t mask)219 NRF_STATIC_INLINE void nrf_usbhs_int_enable(NRF_USBHS_Type * p_reg, uint32_t mask)
220 {
221     p_reg->INTENSET = mask;
222 }
223 
nrf_usbhs_int_enable_check(NRF_USBHS_Type const * p_reg,uint32_t mask)224 NRF_STATIC_INLINE uint32_t nrf_usbhs_int_enable_check(NRF_USBHS_Type const * p_reg, uint32_t mask)
225 {
226     return p_reg->INTENSET & mask;
227 }
228 
nrf_usbhs_int_enable_get(NRF_USBHS_Type const * p_reg)229 NRF_STATIC_INLINE uint32_t nrf_usbhs_int_enable_get(NRF_USBHS_Type const * p_reg)
230 {
231     return p_reg->INTENSET;
232 }
233 
nrf_usbhs_int_disable(NRF_USBHS_Type * p_reg,uint32_t mask)234 NRF_STATIC_INLINE void nrf_usbhs_int_disable(NRF_USBHS_Type * p_reg, uint32_t mask)
235 {
236     p_reg->INTENCLR = mask;
237 }
238 
nrf_usbhs_enable(NRF_USBHS_Type * p_reg)239 NRF_STATIC_INLINE void nrf_usbhs_enable(NRF_USBHS_Type * p_reg)
240 {
241     p_reg->ENABLE = (USBHS_ENABLE_PHY_Enabled << USBHS_ENABLE_PHY_Pos) |
242                     (USBHS_ENABLE_CORE_Enabled << USBHS_ENABLE_CORE_Pos);
243 }
244 
nrf_usbhs_disable(NRF_USBHS_Type * p_reg)245 NRF_STATIC_INLINE void nrf_usbhs_disable(NRF_USBHS_Type * p_reg)
246 {
247     p_reg->ENABLE = (USBHS_ENABLE_PHY_Disabled << USBHS_ENABLE_PHY_Pos) |
248                     (USBHS_ENABLE_CORE_Disabled << USBHS_ENABLE_CORE_Pos);
249 }
250 
251 #endif /* NRF_DECLARE_ONLY */
252 
253 /** @} */
254 
255 #ifdef __cplusplus
256 }
257 #endif
258 
259 #endif /* NRF_USBHS_H__ */
260