1 /*
2  * Copyright (c) 2019 - 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_USBREG_H__
35 #define NRF_USBREG_H__
36 
37 #include <nrfx.h>
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /**
44  * @defgroup nrf_usbreg_hal USBREG HAL
45  * @{
46  * @ingroup nrf_usbd
47  * @ingroup nrf_power
48  * @brief   Hardware access layer for managing the USB regulator peripheral.
49  */
50 
51 /** @brief USBREG events. */
52 typedef enum
53 {
54     NRF_USBREG_EVENT_USBDETECTED = offsetof(NRF_USBREG_Type, EVENTS_USBDETECTED), /**< Voltage supply detected on VBUS. */
55     NRF_USBREG_EVENT_USBREMOVED  = offsetof(NRF_USBREG_Type, EVENTS_USBREMOVED),  /**< Voltage supply removed from VBUS. */
56     NRF_USBREG_EVENT_USBPWRRDY   = offsetof(NRF_USBREG_Type, EVENTS_USBPWRRDY)    /**< USB 3.3&nbsp;V supply ready. */
57 } nrf_usbreg_event_t;
58 
59 /** @brief USBREG interrupts. */
60 typedef enum
61 {
62     NRF_USBREG_INT_USBDETECTED = USBREG_INTEN_USBDETECTED_Msk, /**< Interrupt on USBDETECTED. */
63     NRF_USBREG_INT_USBREMOVED  = USBREG_INTEN_USBREMOVED_Msk,  /**< Interrupt on USBREMOVED. */
64     NRF_USBREG_INT_USBPWRRDY   = USBREG_INTEN_USBPWRRDY_Msk    /**< Interrupt on USBPWRRDY. */
65 } nrf_usbreg_int_mask_t;
66 
67 /** @brief USBREGSTATUS register bit masks. */
68 typedef enum
69 {
70     NRF_USBREG_STATUS_VBUSDETECT_MASK = USBREG_USBREGSTATUS_VBUSDETECT_Msk, /**< USB detected or removed.     */
71     NRF_USBREG_STATUS_OUTPUTRDY_MASK  = USBREG_USBREGSTATUS_OUTPUTRDY_Msk   /**< USB 3.3&nbsp;V supply ready. */
72 } nrf_usbreg_status_mask_t;
73 
74 /**
75  * @brief Function for clearing the specified USBREG event.
76  *
77  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
78  * @param[in] event Event to be cleared.
79  */
80 NRF_STATIC_INLINE void nrf_usbreg_event_clear(NRF_USBREG_Type *  p_reg,
81                                               nrf_usbreg_event_t event);
82 
83 /**
84  * @brief Function for retrieving the state of the USBREG event.
85  *
86  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
87  * @param[in] event Event to be checked.
88  *
89  * @retval true  The event has been generated.
90  * @retval false The event has not been generated.
91  */
92 NRF_STATIC_INLINE bool nrf_usbreg_event_check(NRF_USBREG_Type const * p_reg,
93                                               nrf_usbreg_event_t      event);
94 
95 /**
96  * @brief Function for enabling specified interrupts.
97  *
98  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
99  * @param[in] mask  Mask of interrupts to be enabled.
100  *                  Use @ref nrf_usbreg_int_mask_t values for bit masking.
101  */
102 NRF_STATIC_INLINE void nrf_usbreg_int_enable(NRF_USBREG_Type * p_reg, uint32_t mask);
103 
104 /**
105  * @brief Function for disabling specified interrupts.
106  *
107  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
108  * @param[in] mask  Mask of interrupts to be disabled.
109  *                  Use @ref nrf_usbreg_int_mask_t values for bit masking.
110  */
111 NRF_STATIC_INLINE void nrf_usbreg_int_disable(NRF_USBREG_Type * p_reg, uint32_t mask);
112 
113 /**
114  * @brief Function for checking if the specified interrupts are enabled.
115  *
116  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
117  * @param[in] mask  Mask of interrupts to be checked.
118  *                  Use @ref nrf_usbreg_int_mask_t values for bit masking.
119  *
120  * @return Mask of enabled interrupts.
121  */
122 NRF_STATIC_INLINE uint32_t nrf_usbreg_int_enable_check(NRF_USBREG_Type const * p_reg,
123                                                        uint32_t                mask);
124 
125 /**
126  * @brief Function for getting the whole USBREGSTATUS register.
127  *
128  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
129  *
130  * @return The USBREGSTATUS register value.
131  *         Use @ref nrf_usbreg_status_mask_t values for bit masking.
132  */
133 NRF_STATIC_INLINE uint32_t nrf_usbreg_status_get(NRF_USBREG_Type const * p_reg);
134 
135 #ifndef NRF_DECLARE_ONLY
136 
nrf_usbreg_event_clear(NRF_USBREG_Type * p_reg,nrf_usbreg_event_t event)137 NRF_STATIC_INLINE void nrf_usbreg_event_clear(NRF_USBREG_Type *  p_reg,
138                                               nrf_usbreg_event_t event)
139 {
140     *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
141 }
142 
nrf_usbreg_event_check(NRF_USBREG_Type const * p_reg,nrf_usbreg_event_t event)143 NRF_STATIC_INLINE bool nrf_usbreg_event_check(NRF_USBREG_Type const * p_reg,
144                                               nrf_usbreg_event_t      event)
145 {
146     return nrf_event_check(p_reg, event);
147 }
148 
nrf_usbreg_int_enable(NRF_USBREG_Type * p_reg,uint32_t mask)149 NRF_STATIC_INLINE void nrf_usbreg_int_enable(NRF_USBREG_Type * p_reg, uint32_t mask)
150 {
151     p_reg->INTENSET = mask;
152 }
153 
nrf_usbreg_int_disable(NRF_USBREG_Type * p_reg,uint32_t mask)154 NRF_STATIC_INLINE void nrf_usbreg_int_disable(NRF_USBREG_Type * p_reg, uint32_t mask)
155 {
156     p_reg->INTENCLR = mask;
157 }
158 
nrf_usbreg_int_enable_check(NRF_USBREG_Type const * p_reg,uint32_t mask)159 NRF_STATIC_INLINE uint32_t nrf_usbreg_int_enable_check(NRF_USBREG_Type const * p_reg,
160                                                        uint32_t                mask)
161 {
162     return p_reg->INTENSET & mask;
163 }
164 
nrf_usbreg_status_get(NRF_USBREG_Type const * p_reg)165 NRF_STATIC_INLINE uint32_t nrf_usbreg_status_get(NRF_USBREG_Type const * p_reg)
166 {
167     return p_reg->USBREGSTATUS;
168 }
169 
170 #endif // NRF_DECLARE_ONLY
171 
172 /** @} */
173 
174 #ifdef __cplusplus
175 }
176 #endif
177 
178 #endif // NRF_USBREG_H__
179