1 /*
2  * Copyright (c) 2019 - 2023, 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  */
101 NRF_STATIC_INLINE void nrf_usbreg_int_enable(NRF_USBREG_Type * p_reg, uint32_t mask);
102 
103 /**
104  * @brief Function for disabling specified interrupts.
105  *
106  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
107  * @param[in] mask  Mask of interrupts to be disabled.
108  */
109 NRF_STATIC_INLINE void nrf_usbreg_int_disable(NRF_USBREG_Type * p_reg, uint32_t mask);
110 
111 /**
112  * @brief Function for checking if the specified interrupts are enabled.
113  *
114  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
115  * @param[in] mask  Mask of interrupts to be checked.
116  *
117  * @return Mask of enabled interrupts.
118  */
119 NRF_STATIC_INLINE uint32_t nrf_usbreg_int_enable_check(NRF_USBREG_Type const * p_reg,
120                                                        uint32_t                mask);
121 
122 /**
123  * @brief Function for getting the whole USBREGSTATUS register.
124  *
125  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
126  *
127  * @return The USBREGSTATUS register value.
128  *         Use @ref nrf_usbreg_status_mask_t values for bit masking.
129  */
130 NRF_STATIC_INLINE uint32_t nrf_usbreg_status_get(NRF_USBREG_Type const * p_reg);
131 
132 #ifndef NRF_DECLARE_ONLY
133 
nrf_usbreg_event_clear(NRF_USBREG_Type * p_reg,nrf_usbreg_event_t event)134 NRF_STATIC_INLINE void nrf_usbreg_event_clear(NRF_USBREG_Type *  p_reg,
135                                               nrf_usbreg_event_t event)
136 {
137     *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
138 }
139 
nrf_usbreg_event_check(NRF_USBREG_Type const * p_reg,nrf_usbreg_event_t event)140 NRF_STATIC_INLINE bool nrf_usbreg_event_check(NRF_USBREG_Type const * p_reg,
141                                               nrf_usbreg_event_t      event)
142 {
143     return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
144 }
145 
nrf_usbreg_int_enable(NRF_USBREG_Type * p_reg,uint32_t mask)146 NRF_STATIC_INLINE void nrf_usbreg_int_enable(NRF_USBREG_Type * p_reg, uint32_t mask)
147 {
148     p_reg->INTENSET = mask;
149 }
150 
nrf_usbreg_int_disable(NRF_USBREG_Type * p_reg,uint32_t mask)151 NRF_STATIC_INLINE void nrf_usbreg_int_disable(NRF_USBREG_Type * p_reg, uint32_t mask)
152 {
153     p_reg->INTENCLR = mask;
154 }
155 
nrf_usbreg_int_enable_check(NRF_USBREG_Type const * p_reg,uint32_t mask)156 NRF_STATIC_INLINE uint32_t nrf_usbreg_int_enable_check(NRF_USBREG_Type const * p_reg,
157                                                        uint32_t                mask)
158 {
159     return p_reg->INTENSET & mask;
160 }
161 
nrf_usbreg_status_get(NRF_USBREG_Type const * p_reg)162 NRF_STATIC_INLINE uint32_t nrf_usbreg_status_get(NRF_USBREG_Type const * p_reg)
163 {
164     return p_reg->USBREGSTATUS;
165 }
166 
167 #endif // NRF_DECLARE_ONLY
168 
169 /** @} */
170 
171 #ifdef __cplusplus
172 }
173 #endif
174 
175 #endif // NRF_USBREG_H__
176