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