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 NRFX_USBREG_H__
33 #define NRFX_USBREG_H__
34 
35 #include <nrfx.h>
36 #include <hal/nrf_usbreg.h>
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 /**
43  * @defgroup nrfx_usbreg USBREG driver
44  * @{
45  * @ingroup nrf_usbd
46  * @ingroup nrf_power
47  * @brief   USB regulators (USBREG) peripheral driver.
48  */
49 
50 /** @brief Events from USB power system */
51 typedef enum {
52     NRFX_USBREG_EVT_DETECTED, /**< USB power detected on the connector (plugged in). */
53     NRFX_USBREG_EVT_REMOVED,  /**< USB power removed from the connector. */
54     NRFX_USBREG_EVT_READY     /**< USB power regulator ready. */
55 } nrfx_usbreg_evt_t;
56 
57 /**
58  * @brief USB power state
59  *
60  * The single enumerator that holds all data about current state of USB
61  * related POWER.
62  *
63  * Organized this way that higher power state has higher numeric value
64  */
65 typedef enum {
66     NRFX_USBREG_STATE_DISCONNECTED, /**< No power on USB lines detected. */
67     NRFX_USBREG_STATE_CONNECTED,    /**< The USB power is detected, but USB power regulator is not ready. */
68     NRFX_USBREG_STATE_READY         /**< From the power viewpoint, USB is ready for working. */
69 } nrfx_usbreg_state_t;
70 
71 /**
72  * @brief Event handler for the USB-related power events.
73  *
74  * @param event Event type
75  */
76 typedef void (*nrfx_usbreg_event_handler_t)(nrfx_usbreg_evt_t event);
77 
78 /**
79  * @brief The configuration of the USB-related power events.
80  *
81  * Configuration used to enable and configure USB power event handling.
82  */
83 typedef struct
84 {
85     nrfx_usbreg_event_handler_t handler;      //!< Event processing.
86     uint8_t                     irq_priority; //!< Priority of the USBREG interrupt.
87 } nrfx_usbreg_config_t;
88 
89 /**
90  * @brief Function for getting the USBREG handler.
91  *
92  * @return Handler of the USB power.
93  */
94 nrfx_usbreg_event_handler_t nrfx_usbreg_handler_get(void);
95 
96 /**
97  * @brief Function for initializing the processing of USBREG events.
98  *
99  * Configures and sets up the USB power event processing.
100  *
101  * @param[in] p_config Configuration structure. Must not be NULL.
102  *
103  * @sa nrfx_usbreg_uninit
104  */
105 void nrfx_usbreg_init(nrfx_usbreg_config_t const *p_config);
106 
107 /** @brief Function for enabling the processing of USBREG events. */
108 void nrfx_usbreg_enable(void);
109 
110 /** @brief Function for disabling the processing of USBREG events. */
111 void nrfx_usbreg_disable(void);
112 
113 /**
114  * @brief Function for uninitalizing the processing of USBREG events.
115  *
116  * @sa nrfx_usbreg_init
117  */
118 void nrfx_usbreg_uninit(void);
119 
120 /**
121  * @brief Function for getting the status of USBREG.
122  *
123  * @return Current USB power status.
124  */
125 NRFX_STATIC_INLINE nrfx_usbreg_state_t nrfx_usbreg_usbstatus_get(void);
126 
127 #ifndef NRFX_DECLARE_ONLY
nrfx_usbreg_usbstatus_get(void)128 NRFX_STATIC_INLINE nrfx_usbreg_state_t nrfx_usbreg_usbstatus_get(void)
129 {
130     uint32_t status = nrf_usbreg_status_get(NRF_USBREGULATOR);
131     if (0 == (status & NRF_USBREG_STATUS_VBUSDETECT_MASK))
132     {
133         return NRFX_USBREG_STATE_DISCONNECTED;
134     }
135     if (0 == (status & NRF_USBREG_STATUS_OUTPUTRDY_MASK))
136     {
137         return NRFX_USBREG_STATE_CONNECTED;
138     }
139     return NRFX_USBREG_STATE_READY;
140 }
141 #endif // NRFX_DECLARE_ONLY
142 
143 /** @} */
144 
145 void nrfx_usbreg_irq_handler(void);
146 
147 #ifdef __cplusplus
148 }
149 #endif
150 
151 #endif // NRFX_USBREG_H__
152