xref: /nrf52832-nimble/nordic/nrfx/drivers/src/nrfx_comp.c (revision 150812a83cab50279bd772ef6db1bfaf255f2c5b)
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 #include <nrfx.h>
33*150812a8SEvalZero 
34*150812a8SEvalZero #if NRFX_CHECK(NRFX_COMP_ENABLED)
35*150812a8SEvalZero 
36*150812a8SEvalZero #include <nrfx_comp.h>
37*150812a8SEvalZero #include "prs/nrfx_prs.h"
38*150812a8SEvalZero 
39*150812a8SEvalZero #define NRFX_LOG_MODULE COMP
40*150812a8SEvalZero #include <nrfx_log.h>
41*150812a8SEvalZero 
42*150812a8SEvalZero #define EVT_TO_STR(event)                                     \
43*150812a8SEvalZero     (event == NRF_COMP_EVENT_READY ? "NRF_COMP_EVENT_READY" : \
44*150812a8SEvalZero     (event == NRF_COMP_EVENT_DOWN  ? "NRF_COMP_EVENT_DOWN"  : \
45*150812a8SEvalZero     (event == NRF_COMP_EVENT_UP    ? "NRF_COMP_EVENT_UP"    : \
46*150812a8SEvalZero     (event == NRF_COMP_EVENT_CROSS ? "NRF_COMP_EVENT_CROSS" : \
47*150812a8SEvalZero                                      "UNKNOWN ERROR"))))
48*150812a8SEvalZero 
49*150812a8SEvalZero 
50*150812a8SEvalZero static nrfx_comp_event_handler_t    m_comp_event_handler = NULL;
51*150812a8SEvalZero static nrfx_drv_state_t             m_state = NRFX_DRV_STATE_UNINITIALIZED;
52*150812a8SEvalZero 
comp_execute_handler(nrf_comp_event_t event,uint32_t event_mask)53*150812a8SEvalZero static void comp_execute_handler(nrf_comp_event_t event, uint32_t event_mask)
54*150812a8SEvalZero {
55*150812a8SEvalZero     if (nrf_comp_event_check(event) && nrf_comp_int_enable_check(event_mask))
56*150812a8SEvalZero     {
57*150812a8SEvalZero         nrf_comp_event_clear(event);
58*150812a8SEvalZero         NRFX_LOG_DEBUG("Event: %s.", EVT_TO_STR(event));
59*150812a8SEvalZero 
60*150812a8SEvalZero         m_comp_event_handler(event);
61*150812a8SEvalZero     }
62*150812a8SEvalZero }
63*150812a8SEvalZero 
nrfx_comp_irq_handler(void)64*150812a8SEvalZero void nrfx_comp_irq_handler(void)
65*150812a8SEvalZero {
66*150812a8SEvalZero     comp_execute_handler(NRF_COMP_EVENT_READY, COMP_INTENSET_READY_Msk);
67*150812a8SEvalZero     comp_execute_handler(NRF_COMP_EVENT_DOWN,  COMP_INTENSET_DOWN_Msk);
68*150812a8SEvalZero     comp_execute_handler(NRF_COMP_EVENT_UP,    COMP_INTENSET_UP_Msk);
69*150812a8SEvalZero     comp_execute_handler(NRF_COMP_EVENT_CROSS, COMP_INTENSET_CROSS_Msk);
70*150812a8SEvalZero }
71*150812a8SEvalZero 
72*150812a8SEvalZero 
nrfx_comp_init(nrfx_comp_config_t const * p_config,nrfx_comp_event_handler_t event_handler)73*150812a8SEvalZero nrfx_err_t nrfx_comp_init(nrfx_comp_config_t const * p_config,
74*150812a8SEvalZero                           nrfx_comp_event_handler_t  event_handler)
75*150812a8SEvalZero {
76*150812a8SEvalZero     NRFX_ASSERT(p_config);
77*150812a8SEvalZero     NRFX_ASSERT(event_handler);
78*150812a8SEvalZero     nrfx_err_t err_code;
79*150812a8SEvalZero 
80*150812a8SEvalZero     if (m_state != NRFX_DRV_STATE_UNINITIALIZED)
81*150812a8SEvalZero     { // COMP driver is already initialized
82*150812a8SEvalZero         err_code = NRFX_ERROR_INVALID_STATE;
83*150812a8SEvalZero         NRFX_LOG_WARNING("Function: %s, error code: %s.",
84*150812a8SEvalZero                          __func__,
85*150812a8SEvalZero                          NRFX_LOG_ERROR_STRING_GET(err_code));
86*150812a8SEvalZero         return err_code;
87*150812a8SEvalZero     }
88*150812a8SEvalZero 
89*150812a8SEvalZero     m_comp_event_handler = event_handler;
90*150812a8SEvalZero 
91*150812a8SEvalZero #if NRFX_CHECK(NRFX_PRS_ENABLED)
92*150812a8SEvalZero     if (nrfx_prs_acquire(NRF_COMP, nrfx_comp_irq_handler) != NRFX_SUCCESS)
93*150812a8SEvalZero     {
94*150812a8SEvalZero         err_code = NRFX_ERROR_BUSY;
95*150812a8SEvalZero         NRFX_LOG_WARNING("Function: %s, error code: %s.",
96*150812a8SEvalZero                          __func__,
97*150812a8SEvalZero                          NRFX_LOG_ERROR_STRING_GET(err_code));
98*150812a8SEvalZero         return err_code;
99*150812a8SEvalZero     }
100*150812a8SEvalZero #endif
101*150812a8SEvalZero 
102*150812a8SEvalZero     nrf_comp_task_trigger(NRF_COMP_TASK_STOP);
103*150812a8SEvalZero     nrf_comp_enable();
104*150812a8SEvalZero 
105*150812a8SEvalZero     // Clear events to be sure there are no leftovers.
106*150812a8SEvalZero     nrf_comp_event_clear(NRF_COMP_EVENT_READY);
107*150812a8SEvalZero     nrf_comp_event_clear(NRF_COMP_EVENT_DOWN);
108*150812a8SEvalZero     nrf_comp_event_clear(NRF_COMP_EVENT_UP);
109*150812a8SEvalZero     nrf_comp_event_clear(NRF_COMP_EVENT_CROSS);
110*150812a8SEvalZero 
111*150812a8SEvalZero     nrf_comp_ref_set(p_config->reference);
112*150812a8SEvalZero 
113*150812a8SEvalZero     //If external source is chosen, write to appropriate register.
114*150812a8SEvalZero     if (p_config->reference == COMP_REFSEL_REFSEL_ARef)
115*150812a8SEvalZero     {
116*150812a8SEvalZero         nrf_comp_ext_ref_set(p_config->ext_ref);
117*150812a8SEvalZero     }
118*150812a8SEvalZero 
119*150812a8SEvalZero     nrf_comp_th_set(p_config->threshold);
120*150812a8SEvalZero     nrf_comp_main_mode_set(p_config->main_mode);
121*150812a8SEvalZero     nrf_comp_speed_mode_set(p_config->speed_mode);
122*150812a8SEvalZero     nrf_comp_hysteresis_set(p_config->hyst);
123*150812a8SEvalZero #if defined (COMP_ISOURCE_ISOURCE_Msk)
124*150812a8SEvalZero     nrf_comp_isource_set(p_config->isource);
125*150812a8SEvalZero #endif
126*150812a8SEvalZero     nrf_comp_shorts_disable(NRFX_COMP_SHORT_STOP_AFTER_CROSS_EVT |
127*150812a8SEvalZero                             NRFX_COMP_SHORT_STOP_AFTER_UP_EVT |
128*150812a8SEvalZero                             NRFX_COMP_SHORT_STOP_AFTER_DOWN_EVT);
129*150812a8SEvalZero     nrf_comp_int_disable(COMP_INTENCLR_CROSS_Msk |
130*150812a8SEvalZero                          COMP_INTENCLR_UP_Msk |
131*150812a8SEvalZero                          COMP_INTENCLR_DOWN_Msk |
132*150812a8SEvalZero                          COMP_INTENCLR_READY_Msk);
133*150812a8SEvalZero 
134*150812a8SEvalZero     nrf_comp_input_select(p_config->input);
135*150812a8SEvalZero 
136*150812a8SEvalZero     NRFX_IRQ_PRIORITY_SET(COMP_LPCOMP_IRQn, p_config->interrupt_priority);
137*150812a8SEvalZero     NRFX_IRQ_ENABLE(COMP_LPCOMP_IRQn);
138*150812a8SEvalZero 
139*150812a8SEvalZero     m_state = NRFX_DRV_STATE_INITIALIZED;
140*150812a8SEvalZero 
141*150812a8SEvalZero     err_code = NRFX_SUCCESS;
142*150812a8SEvalZero     NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
143*150812a8SEvalZero     return err_code;
144*150812a8SEvalZero }
145*150812a8SEvalZero 
nrfx_comp_uninit(void)146*150812a8SEvalZero void nrfx_comp_uninit(void)
147*150812a8SEvalZero {
148*150812a8SEvalZero     NRFX_ASSERT(m_state != NRFX_DRV_STATE_UNINITIALIZED);
149*150812a8SEvalZero     NRFX_IRQ_DISABLE(COMP_LPCOMP_IRQn);
150*150812a8SEvalZero     nrf_comp_disable();
151*150812a8SEvalZero #if NRFX_CHECK(NRFX_PRS_ENABLED)
152*150812a8SEvalZero     nrfx_prs_release(NRF_COMP);
153*150812a8SEvalZero #endif
154*150812a8SEvalZero     m_state = NRFX_DRV_STATE_UNINITIALIZED;
155*150812a8SEvalZero     m_comp_event_handler = NULL;
156*150812a8SEvalZero     NRFX_LOG_INFO("Uninitialized.");
157*150812a8SEvalZero }
158*150812a8SEvalZero 
nrfx_comp_pin_select(nrf_comp_input_t psel)159*150812a8SEvalZero void nrfx_comp_pin_select(nrf_comp_input_t psel)
160*150812a8SEvalZero {
161*150812a8SEvalZero     bool comp_enable_state = nrf_comp_enable_check();
162*150812a8SEvalZero     nrf_comp_task_trigger(NRF_COMP_TASK_STOP);
163*150812a8SEvalZero     if (m_state == NRFX_DRV_STATE_POWERED_ON)
164*150812a8SEvalZero     {
165*150812a8SEvalZero         m_state = NRFX_DRV_STATE_INITIALIZED;
166*150812a8SEvalZero     }
167*150812a8SEvalZero     nrf_comp_disable();
168*150812a8SEvalZero     nrf_comp_input_select(psel);
169*150812a8SEvalZero     if (comp_enable_state == true)
170*150812a8SEvalZero     {
171*150812a8SEvalZero         nrf_comp_enable();
172*150812a8SEvalZero     }
173*150812a8SEvalZero }
174*150812a8SEvalZero 
nrfx_comp_start(uint32_t comp_int_mask,uint32_t comp_shorts_mask)175*150812a8SEvalZero void nrfx_comp_start(uint32_t comp_int_mask, uint32_t comp_shorts_mask)
176*150812a8SEvalZero {
177*150812a8SEvalZero     NRFX_ASSERT(m_state == NRFX_DRV_STATE_INITIALIZED);
178*150812a8SEvalZero     nrf_comp_int_enable(comp_int_mask);
179*150812a8SEvalZero     nrf_comp_shorts_enable(comp_shorts_mask);
180*150812a8SEvalZero     nrf_comp_task_trigger(NRF_COMP_TASK_START);
181*150812a8SEvalZero     m_state = NRFX_DRV_STATE_POWERED_ON;
182*150812a8SEvalZero     NRFX_LOG_INFO("Enabled.");
183*150812a8SEvalZero }
184*150812a8SEvalZero 
nrfx_comp_stop(void)185*150812a8SEvalZero void nrfx_comp_stop(void)
186*150812a8SEvalZero {
187*150812a8SEvalZero     NRFX_ASSERT(m_state == NRFX_DRV_STATE_POWERED_ON);
188*150812a8SEvalZero     nrf_comp_shorts_disable(UINT32_MAX);
189*150812a8SEvalZero     nrf_comp_int_disable(UINT32_MAX);
190*150812a8SEvalZero     nrf_comp_task_trigger(NRF_COMP_TASK_STOP);
191*150812a8SEvalZero     m_state = NRFX_DRV_STATE_INITIALIZED;
192*150812a8SEvalZero     NRFX_LOG_INFO("Disabled.");
193*150812a8SEvalZero }
194*150812a8SEvalZero 
nrfx_comp_sample()195*150812a8SEvalZero uint32_t nrfx_comp_sample()
196*150812a8SEvalZero {
197*150812a8SEvalZero     NRFX_ASSERT(m_state == NRFX_DRV_STATE_POWERED_ON);
198*150812a8SEvalZero     nrf_comp_task_trigger(NRF_COMP_TASK_SAMPLE);
199*150812a8SEvalZero     return nrf_comp_result_get();
200*150812a8SEvalZero }
201*150812a8SEvalZero 
202*150812a8SEvalZero #endif // NRFX_CHECK(NRFX_COMP_ENABLED)
203