1*150812a8SEvalZero /*
2*150812a8SEvalZero * Copyright (c) 2015 - 2018, Nordic Semiconductor ASA
3*150812a8SEvalZero * All rights reserved.
4*150812a8SEvalZero *
5*150812a8SEvalZero * Redistribution and use in source and binary forms, with or without
6*150812a8SEvalZero * modification, are permitted provided that the following conditions are met:
7*150812a8SEvalZero *
8*150812a8SEvalZero * 1. Redistributions of source code must retain the above copyright notice, this
9*150812a8SEvalZero * list of conditions and the following disclaimer.
10*150812a8SEvalZero *
11*150812a8SEvalZero * 2. Redistributions in binary form must reproduce the above copyright
12*150812a8SEvalZero * notice, this list of conditions and the following disclaimer in the
13*150812a8SEvalZero * documentation and/or other materials provided with the distribution.
14*150812a8SEvalZero *
15*150812a8SEvalZero * 3. Neither the name of the copyright holder nor the names of its
16*150812a8SEvalZero * contributors may be used to endorse or promote products derived from this
17*150812a8SEvalZero * software without specific prior written permission.
18*150812a8SEvalZero *
19*150812a8SEvalZero * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20*150812a8SEvalZero * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*150812a8SEvalZero * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*150812a8SEvalZero * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23*150812a8SEvalZero * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*150812a8SEvalZero * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*150812a8SEvalZero * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*150812a8SEvalZero * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*150812a8SEvalZero * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*150812a8SEvalZero * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*150812a8SEvalZero * POSSIBILITY OF SUCH DAMAGE.
30*150812a8SEvalZero */
31*150812a8SEvalZero
32*150812a8SEvalZero #ifndef NRFX_COMP_H__
33*150812a8SEvalZero #define NRFX_COMP_H__
34*150812a8SEvalZero
35*150812a8SEvalZero #include <nrfx.h>
36*150812a8SEvalZero #include <hal/nrf_comp.h>
37*150812a8SEvalZero
38*150812a8SEvalZero #ifdef __cplusplus
39*150812a8SEvalZero extern "C" {
40*150812a8SEvalZero #endif
41*150812a8SEvalZero
42*150812a8SEvalZero /**
43*150812a8SEvalZero * @defgroup nrfx_comp COMP driver
44*150812a8SEvalZero * @{
45*150812a8SEvalZero * @ingroup nrf_comp
46*150812a8SEvalZero * @brief Comparator (COMP) peripheral driver.
47*150812a8SEvalZero */
48*150812a8SEvalZero
49*150812a8SEvalZero /**
50*150812a8SEvalZero * @brief Macro to convert the threshold voltage to an integer value
51*150812a8SEvalZero * (needed by the COMP_TH register).
52*150812a8SEvalZero *
53*150812a8SEvalZero * @param[in] vol Voltage to be changed to COMP_TH register value. This value
54*150812a8SEvalZero * must not be smaller than reference voltage divided by 64.
55*150812a8SEvalZero * @param[in] ref Reference voltage.
56*150812a8SEvalZero */
57*150812a8SEvalZero #define NRFX_VOLTAGE_THRESHOLD_TO_INT(vol, ref) \
58*150812a8SEvalZero (uint8_t)(((vol) > ((ref) / 64)) ? (NRFX_ROUNDED_DIV((vol) * 64,(ref)) - 1) : 0)
59*150812a8SEvalZero
60*150812a8SEvalZero /**
61*150812a8SEvalZero * @brief COMP event handler function type.
62*150812a8SEvalZero * @param[in] event COMP event.
63*150812a8SEvalZero */
64*150812a8SEvalZero typedef void (* nrfx_comp_event_handler_t)(nrf_comp_event_t event);
65*150812a8SEvalZero
66*150812a8SEvalZero /** @brief COMP shortcut masks. */
67*150812a8SEvalZero typedef enum
68*150812a8SEvalZero {
69*150812a8SEvalZero NRFX_COMP_SHORT_STOP_AFTER_CROSS_EVT = COMP_SHORTS_CROSS_STOP_Msk, /*!< Shortcut between the CROSS event and the STOP task. */
70*150812a8SEvalZero NRFX_COMP_SHORT_STOP_AFTER_UP_EVT = COMP_SHORTS_UP_STOP_Msk, /*!< Shortcut between the UP event and the STOP task. */
71*150812a8SEvalZero NRFX_COMP_SHORT_STOP_AFTER_DOWN_EVT = COMP_SHORTS_DOWN_STOP_Msk /*!< Shortcut between the DOWN event and the STOP task. */
72*150812a8SEvalZero } nrfx_comp_short_mask_t;
73*150812a8SEvalZero
74*150812a8SEvalZero /** @brief COMP events masks. */
75*150812a8SEvalZero typedef enum
76*150812a8SEvalZero {
77*150812a8SEvalZero NRFX_COMP_EVT_EN_CROSS_MASK = COMP_INTENSET_CROSS_Msk, /*!< CROSS event (generated after VIN+ == VIN-). */
78*150812a8SEvalZero NRFX_COMP_EVT_EN_UP_MASK = COMP_INTENSET_UP_Msk, /*!< UP event (generated when VIN+ crosses VIN- while increasing). */
79*150812a8SEvalZero NRFX_COMP_EVT_EN_DOWN_MASK = COMP_INTENSET_DOWN_Msk, /*!< DOWN event (generated when VIN+ crosses VIN- while decreasing). */
80*150812a8SEvalZero NRFX_COMP_EVT_EN_READY_MASK = COMP_INTENSET_READY_Msk /*!< READY event (generated when the module is ready). */
81*150812a8SEvalZero } nrfx_comp_evt_en_mask_t;
82*150812a8SEvalZero
83*150812a8SEvalZero /** @brief COMP configuration. */
84*150812a8SEvalZero typedef struct
85*150812a8SEvalZero {
86*150812a8SEvalZero nrf_comp_ref_t reference; /**< Reference selection. */
87*150812a8SEvalZero nrf_comp_ext_ref_t ext_ref; /**< External analog reference selection. */
88*150812a8SEvalZero nrf_comp_main_mode_t main_mode; /**< Main operation mode. */
89*150812a8SEvalZero nrf_comp_th_t threshold; /**< Structure holding THDOWN and THUP values needed by the COMP_TH register. */
90*150812a8SEvalZero nrf_comp_sp_mode_t speed_mode; /**< Speed and power mode. */
91*150812a8SEvalZero nrf_comp_hyst_t hyst; /**< Comparator hysteresis.*/
92*150812a8SEvalZero #if defined (COMP_ISOURCE_ISOURCE_Msk) || defined (__NRFX_DOXYGEN__)
93*150812a8SEvalZero nrf_isource_t isource; /**< Current source selected on analog input. */
94*150812a8SEvalZero #endif
95*150812a8SEvalZero nrf_comp_input_t input; /**< Input to be monitored. */
96*150812a8SEvalZero uint8_t interrupt_priority; /**< Interrupt priority. */
97*150812a8SEvalZero } nrfx_comp_config_t;
98*150812a8SEvalZero
99*150812a8SEvalZero /** @brief COMP threshold default configuration. */
100*150812a8SEvalZero #define NRFX_COMP_CONFIG_TH \
101*150812a8SEvalZero { \
102*150812a8SEvalZero .th_down = NRFX_VOLTAGE_THRESHOLD_TO_INT(0.5, 1.8), \
103*150812a8SEvalZero .th_up = NRFX_VOLTAGE_THRESHOLD_TO_INT(1.5, 1.8) \
104*150812a8SEvalZero }
105*150812a8SEvalZero
106*150812a8SEvalZero /** @brief COMP driver default configuration including the COMP HAL configuration. */
107*150812a8SEvalZero #if defined (COMP_ISOURCE_ISOURCE_Msk) || defined (__NRFX_DOXYGEN__)
108*150812a8SEvalZero #define NRFX_COMP_DEFAULT_CONFIG(_input) \
109*150812a8SEvalZero { \
110*150812a8SEvalZero .reference = (nrf_comp_ref_t)NRFX_COMP_CONFIG_REF, \
111*150812a8SEvalZero .main_mode = (nrf_comp_main_mode_t)NRFX_COMP_CONFIG_MAIN_MODE, \
112*150812a8SEvalZero .threshold = NRFX_COMP_CONFIG_TH, \
113*150812a8SEvalZero .speed_mode = (nrf_comp_sp_mode_t)NRFX_COMP_CONFIG_SPEED_MODE, \
114*150812a8SEvalZero .hyst = (nrf_comp_hyst_t)NRFX_COMP_CONFIG_HYST, \
115*150812a8SEvalZero .isource = (nrf_isource_t)NRFX_COMP_CONFIG_ISOURCE, \
116*150812a8SEvalZero .input = (nrf_comp_input_t)_input, \
117*150812a8SEvalZero .interrupt_priority = NRFX_COMP_CONFIG_IRQ_PRIORITY \
118*150812a8SEvalZero }
119*150812a8SEvalZero #else
120*150812a8SEvalZero #define NRFX_COMP_DEFAULT_CONFIG(_input) \
121*150812a8SEvalZero { \
122*150812a8SEvalZero .reference = (nrf_comp_ref_t)NRFX_COMP_CONFIG_REF, \
123*150812a8SEvalZero .main_mode = (nrf_comp_main_mode_t)NRFX_COMP_CONFIG_MAIN_MODE, \
124*150812a8SEvalZero .threshold = NRFX_COMP_CONFIG_TH, \
125*150812a8SEvalZero .speed_mode = (nrf_comp_sp_mode_t)NRFX_COMP_CONFIG_SPEED_MODE, \
126*150812a8SEvalZero .hyst = (nrf_comp_hyst_t)NRFX_COMP_CONFIG_HYST, \
127*150812a8SEvalZero .input = (nrf_comp_input_t)_input, \
128*150812a8SEvalZero .interrupt_priority = NRFX_COMP_CONFIG_IRQ_PRIORITY \
129*150812a8SEvalZero }
130*150812a8SEvalZero #endif
131*150812a8SEvalZero
132*150812a8SEvalZero /**
133*150812a8SEvalZero * @brief Function for initializing the COMP driver.
134*150812a8SEvalZero *
135*150812a8SEvalZero * This function initializes the COMP driver, but does not enable the peripheral or any interrupts.
136*150812a8SEvalZero * To start the driver, call the function @ref nrfx_comp_start() after initialization.
137*150812a8SEvalZero *
138*150812a8SEvalZero * @param[in] p_config Pointer to the structure with initial configuration.
139*150812a8SEvalZero * @param[in] event_handler Event handler provided by the user.
140*150812a8SEvalZero * Must not be NULL.
141*150812a8SEvalZero *
142*150812a8SEvalZero * @retval NRFX_SUCCESS If initialization was successful.
143*150812a8SEvalZero * @retval NRFX_ERROR_INVALID_STATE If the driver has already been initialized.
144*150812a8SEvalZero * @retval NRFX_ERROR_BUSY If the LPCOMP peripheral is already in use.
145*150812a8SEvalZero * This is possible only if @ref nrfx_prs module
146*150812a8SEvalZero * is enabled.
147*150812a8SEvalZero */
148*150812a8SEvalZero nrfx_err_t nrfx_comp_init(nrfx_comp_config_t const * p_config,
149*150812a8SEvalZero nrfx_comp_event_handler_t event_handler);
150*150812a8SEvalZero
151*150812a8SEvalZero
152*150812a8SEvalZero /**
153*150812a8SEvalZero * @brief Function for uninitializing the COMP driver.
154*150812a8SEvalZero *
155*150812a8SEvalZero * This function uninitializes the COMP driver. The COMP peripheral and
156*150812a8SEvalZero * its interrupts are disabled, and local variables are cleaned. After this call, you must
157*150812a8SEvalZero * initialize the driver again by calling nrfx_comp_init() if you want to use it.
158*150812a8SEvalZero *
159*150812a8SEvalZero * @sa nrfx_comp_stop()
160*150812a8SEvalZero */
161*150812a8SEvalZero void nrfx_comp_uninit(void);
162*150812a8SEvalZero
163*150812a8SEvalZero /**
164*150812a8SEvalZero * @brief Function for setting the analog input.
165*150812a8SEvalZero *
166*150812a8SEvalZero * @param[in] psel COMP analog pin selection.
167*150812a8SEvalZero */
168*150812a8SEvalZero void nrfx_comp_pin_select(nrf_comp_input_t psel);
169*150812a8SEvalZero
170*150812a8SEvalZero /**
171*150812a8SEvalZero * @brief Function for starting the COMP peripheral and interrupts.
172*150812a8SEvalZero *
173*150812a8SEvalZero * Before calling this function, the driver must be initialized. This function
174*150812a8SEvalZero * enables the COMP peripheral and its interrupts.
175*150812a8SEvalZero *
176*150812a8SEvalZero * @param[in] comp_evt_en_mask Mask of events to be enabled. This parameter should be built as
177*150812a8SEvalZero * 'or' of elements from @ref nrfx_comp_evt_en_mask_t.
178*150812a8SEvalZero * @param[in] comp_shorts_mask Mask of shorts to be enabled. This parameter should be built as
179*150812a8SEvalZero * 'or' of elements from @ref nrfx_comp_short_mask_t.
180*150812a8SEvalZero *
181*150812a8SEvalZero * @sa nrfx_comp_init()
182*150812a8SEvalZero *
183*150812a8SEvalZero */
184*150812a8SEvalZero void nrfx_comp_start(uint32_t comp_evt_en_mask, uint32_t comp_shorts_mask);
185*150812a8SEvalZero
186*150812a8SEvalZero /**@brief Function for stopping the COMP peripheral.
187*150812a8SEvalZero *
188*150812a8SEvalZero * Before calling this function, the driver must be enabled. This function disables the COMP
189*150812a8SEvalZero * peripheral and its interrupts.
190*150812a8SEvalZero *
191*150812a8SEvalZero * @sa nrfx_comp_uninit()
192*150812a8SEvalZero *
193*150812a8SEvalZero */
194*150812a8SEvalZero void nrfx_comp_stop(void);
195*150812a8SEvalZero
196*150812a8SEvalZero /**
197*150812a8SEvalZero * @brief Function for copying the current state of the comparator result to the RESULT register.
198*150812a8SEvalZero *
199*150812a8SEvalZero * @retval 0 If the input voltage is below the threshold (VIN+ < VIN-).
200*150812a8SEvalZero * @retval 1 If the input voltage is above the threshold (VIN+ > VIN-).
201*150812a8SEvalZero */
202*150812a8SEvalZero uint32_t nrfx_comp_sample(void);
203*150812a8SEvalZero
204*150812a8SEvalZero /**
205*150812a8SEvalZero * @brief Function for getting the address of a COMP task.
206*150812a8SEvalZero *
207*150812a8SEvalZero * @param[in] task COMP task.
208*150812a8SEvalZero *
209*150812a8SEvalZero * @return Address of the given COMP task.
210*150812a8SEvalZero */
nrfx_comp_task_address_get(nrf_comp_task_t task)211*150812a8SEvalZero __STATIC_INLINE uint32_t nrfx_comp_task_address_get(nrf_comp_task_t task)
212*150812a8SEvalZero {
213*150812a8SEvalZero return (uint32_t)nrf_comp_task_address_get(task);
214*150812a8SEvalZero }
215*150812a8SEvalZero
216*150812a8SEvalZero /**
217*150812a8SEvalZero * @brief Function for getting the address of a COMP event.
218*150812a8SEvalZero *
219*150812a8SEvalZero * @param[in] event COMP event.
220*150812a8SEvalZero *
221*150812a8SEvalZero * @return Address of the given COMP event.
222*150812a8SEvalZero */
nrfx_comp_event_address_get(nrf_comp_event_t event)223*150812a8SEvalZero __STATIC_INLINE uint32_t nrfx_comp_event_address_get(nrf_comp_event_t event)
224*150812a8SEvalZero {
225*150812a8SEvalZero return (uint32_t)nrf_comp_event_address_get(event);
226*150812a8SEvalZero }
227*150812a8SEvalZero
228*150812a8SEvalZero
229*150812a8SEvalZero void nrfx_comp_irq_handler(void);
230*150812a8SEvalZero
231*150812a8SEvalZero
232*150812a8SEvalZero /** @} **/
233*150812a8SEvalZero
234*150812a8SEvalZero #ifdef __cplusplus
235*150812a8SEvalZero }
236*150812a8SEvalZero #endif
237*150812a8SEvalZero
238*150812a8SEvalZero #endif // NRFX_COMP_H__
239