1 /*
2  * Copyright (c) 2019 - 2020, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  *    list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  *    contributors may be used to endorse or promote products derived from this
17  *    software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef NRF_USBREG_H__
33 #define NRF_USBREG_H__
34 
35 #include <nrfx.h>
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /**
42  * @defgroup nrf_usbreg_hal USBREG HAL
43  * @{
44  * @ingroup nrf_usbd
45  * @ingroup nrf_power
46  * @brief   Hardware access layer for managing the USB regulator peripheral.
47  */
48 
49 /** @brief USBREG events. */
50 typedef enum
51 {
52     NRF_USBREG_EVENT_USBDETECTED = offsetof(NRF_USBREG_Type, EVENTS_USBDETECTED), /**< Voltage supply detected on VBUS. */
53     NRF_USBREG_EVENT_USBREMOVED  = offsetof(NRF_USBREG_Type, EVENTS_USBREMOVED),  /**< Voltage supply removed from VBUS. */
54     NRF_USBREG_EVENT_USBPWRRDY   = offsetof(NRF_USBREG_Type, EVENTS_USBPWRRDY)    /**< USB 3.3&nbsp;V supply ready. */
55 } nrf_usbreg_event_t;
56 
57 /** @brief USBREG interrupts. */
58 typedef enum
59 {
60     NRF_USBREG_INT_USBDETECTED = USBREG_INTEN_USBDETECTED_Msk, /**< Interrupt on USBDETECTED. */
61     NRF_USBREG_INT_USBREMOVED  = USBREG_INTEN_USBREMOVED_Msk,  /**< Interrupt on USBREMOVED. */
62     NRF_USBREG_INT_USBPWRRDY   = USBREG_INTEN_USBPWRRDY_Msk    /**< Interrupt on USBPWRRDY. */
63 } nrf_usbreg_int_mask_t;
64 
65 /** @brief USBREGSTATUS register bit masks. */
66 typedef enum
67 {
68     NRF_USBREG_STATUS_VBUSDETECT_MASK = USBREG_USBREGSTATUS_VBUSDETECT_Msk, /**< USB detected or removed.     */
69     NRF_USBREG_STATUS_OUTPUTRDY_MASK  = USBREG_USBREGSTATUS_OUTPUTRDY_Msk   /**< USB 3.3&nbsp;V supply ready. */
70 } nrf_usbreg_status_mask_t;
71 
72 /**
73  * @brief Function for clearing the specified USBREG event.
74  *
75  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
76  * @param[in] event Event to be cleared.
77  */
78 NRF_STATIC_INLINE void nrf_usbreg_event_clear(NRF_USBREG_Type *  p_reg,
79                                               nrf_usbreg_event_t event);
80 
81 /**
82  * @brief Function for retrieving the state of the USBREG event.
83  *
84  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
85  * @param[in] event Event to be checked.
86  *
87  * @retval true  The event has been generated.
88  * @retval false The event has not been generated.
89  */
90 NRF_STATIC_INLINE bool nrf_usbreg_event_check(NRF_USBREG_Type const * p_reg,
91                                               nrf_usbreg_event_t      event);
92 
93 /**
94  * @brief Function for enabling specified interrupts.
95  *
96  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
97  * @param[in] mask  Mask of interrupts to be enabled.
98  */
99 NRF_STATIC_INLINE void nrf_usbreg_int_enable(NRF_USBREG_Type * p_reg, uint32_t mask);
100 
101 /**
102  * @brief Function for disabling specified interrupts.
103  *
104  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
105  * @param[in] mask  Mask of interrupts to be disabled.
106  */
107 NRF_STATIC_INLINE void nrf_usbreg_int_disable(NRF_USBREG_Type * p_reg, uint32_t mask);
108 
109 /**
110  * @brief Function for checking if the specified interrupts are enabled.
111  *
112  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
113  * @param[in] mask  Mask of interrupts to be checked.
114  *
115  * @return Mask of enabled interrupts.
116  */
117 NRF_STATIC_INLINE uint32_t nrf_usbreg_int_enable_check(NRF_USBREG_Type const * p_reg,
118                                                        uint32_t                mask);
119 
120 /**
121  * @brief Function for getting the whole USBREGSTATUS register.
122  *
123  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
124  *
125  * @return The USBREGSTATUS register value.
126  *         Use @ref nrf_usbreg_status_mask_t values for bit masking.
127  */
128 NRF_STATIC_INLINE uint32_t nrf_usbreg_status_get(NRF_USBREG_Type const * p_reg);
129 
130 #ifndef NRF_DECLARE_ONLY
131 
nrf_usbreg_event_clear(NRF_USBREG_Type * p_reg,nrf_usbreg_event_t event)132 NRF_STATIC_INLINE void nrf_usbreg_event_clear(NRF_USBREG_Type *  p_reg,
133                                               nrf_usbreg_event_t event)
134 {
135     *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
136 }
137 
nrf_usbreg_event_check(NRF_USBREG_Type const * p_reg,nrf_usbreg_event_t event)138 NRF_STATIC_INLINE bool nrf_usbreg_event_check(NRF_USBREG_Type const * p_reg,
139                                               nrf_usbreg_event_t      event)
140 {
141     return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
142 }
143 
nrf_usbreg_int_enable(NRF_USBREG_Type * p_reg,uint32_t mask)144 NRF_STATIC_INLINE void nrf_usbreg_int_enable(NRF_USBREG_Type * p_reg, uint32_t mask)
145 {
146     p_reg->INTENSET = mask;
147 }
148 
nrf_usbreg_int_disable(NRF_USBREG_Type * p_reg,uint32_t mask)149 NRF_STATIC_INLINE void nrf_usbreg_int_disable(NRF_USBREG_Type * p_reg, uint32_t mask)
150 {
151     p_reg->INTENCLR = mask;
152 }
153 
nrf_usbreg_int_enable_check(NRF_USBREG_Type const * p_reg,uint32_t mask)154 NRF_STATIC_INLINE uint32_t nrf_usbreg_int_enable_check(NRF_USBREG_Type const * p_reg,
155                                                        uint32_t                mask)
156 {
157     return p_reg->INTENSET & mask;
158 }
159 
nrf_usbreg_status_get(NRF_USBREG_Type const * p_reg)160 NRF_STATIC_INLINE uint32_t nrf_usbreg_status_get(NRF_USBREG_Type const * p_reg)
161 {
162     return p_reg->USBREGSTATUS;
163 }
164 
165 #endif // NRF_DECLARE_ONLY
166 
167 /** @} */
168 
169 #ifdef __cplusplus
170 }
171 #endif
172 
173 #endif // NRF_USBREG_H__
174