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_LPCOMP_ENABLED)
35*150812a8SEvalZero
36*150812a8SEvalZero #include <nrfx_lpcomp.h>
37*150812a8SEvalZero #include "prs/nrfx_prs.h"
38*150812a8SEvalZero
39*150812a8SEvalZero #define NRFX_LOG_MODULE LPCOMP
40*150812a8SEvalZero #include <nrfx_log.h>
41*150812a8SEvalZero
42*150812a8SEvalZero #define EVT_TO_STR(event) \
43*150812a8SEvalZero (event == NRF_LPCOMP_EVENT_READY ? "NRF_LPCOMP_EVENT_READY" : \
44*150812a8SEvalZero (event == NRF_LPCOMP_EVENT_DOWN ? "NRF_LPCOMP_EVENT_DOWN" : \
45*150812a8SEvalZero (event == NRF_LPCOMP_EVENT_UP ? "NRF_LPCOMP_EVENT_UP" : \
46*150812a8SEvalZero (event == NRF_LPCOMP_EVENT_CROSS ? "NRF_LPCOMP_EVENT_CROSS" : \
47*150812a8SEvalZero "UNKNOWN EVENT"))))
48*150812a8SEvalZero
49*150812a8SEvalZero
50*150812a8SEvalZero static nrfx_lpcomp_event_handler_t m_lpcomp_event_handler = NULL;
51*150812a8SEvalZero static nrfx_drv_state_t m_state = NRFX_DRV_STATE_UNINITIALIZED;
52*150812a8SEvalZero
lpcomp_execute_handler(nrf_lpcomp_event_t event,uint32_t event_mask)53*150812a8SEvalZero static void lpcomp_execute_handler(nrf_lpcomp_event_t event, uint32_t event_mask)
54*150812a8SEvalZero {
55*150812a8SEvalZero if (nrf_lpcomp_event_check(event) && nrf_lpcomp_int_enable_check(event_mask))
56*150812a8SEvalZero {
57*150812a8SEvalZero nrf_lpcomp_event_clear(event);
58*150812a8SEvalZero NRFX_LOG_DEBUG("Event: %s.", EVT_TO_STR(event));
59*150812a8SEvalZero
60*150812a8SEvalZero m_lpcomp_event_handler(event);
61*150812a8SEvalZero }
62*150812a8SEvalZero }
63*150812a8SEvalZero
nrfx_lpcomp_irq_handler(void)64*150812a8SEvalZero void nrfx_lpcomp_irq_handler(void)
65*150812a8SEvalZero {
66*150812a8SEvalZero lpcomp_execute_handler(NRF_LPCOMP_EVENT_READY, LPCOMP_INTENSET_READY_Msk);
67*150812a8SEvalZero lpcomp_execute_handler(NRF_LPCOMP_EVENT_DOWN, LPCOMP_INTENSET_DOWN_Msk);
68*150812a8SEvalZero lpcomp_execute_handler(NRF_LPCOMP_EVENT_UP, LPCOMP_INTENSET_UP_Msk);
69*150812a8SEvalZero lpcomp_execute_handler(NRF_LPCOMP_EVENT_CROSS, LPCOMP_INTENSET_CROSS_Msk);
70*150812a8SEvalZero }
71*150812a8SEvalZero
nrfx_lpcomp_init(nrfx_lpcomp_config_t const * p_config,nrfx_lpcomp_event_handler_t event_handler)72*150812a8SEvalZero nrfx_err_t nrfx_lpcomp_init(nrfx_lpcomp_config_t const * p_config,
73*150812a8SEvalZero nrfx_lpcomp_event_handler_t event_handler)
74*150812a8SEvalZero {
75*150812a8SEvalZero NRFX_ASSERT(p_config);
76*150812a8SEvalZero NRFX_ASSERT(event_handler);
77*150812a8SEvalZero nrfx_err_t err_code;
78*150812a8SEvalZero
79*150812a8SEvalZero if (m_state != NRFX_DRV_STATE_UNINITIALIZED)
80*150812a8SEvalZero { // LPCOMP driver is already initialized
81*150812a8SEvalZero err_code = NRFX_ERROR_INVALID_STATE;
82*150812a8SEvalZero NRFX_LOG_WARNING("Function: %s, error code: %s.",
83*150812a8SEvalZero __func__,
84*150812a8SEvalZero NRFX_LOG_ERROR_STRING_GET(err_code));
85*150812a8SEvalZero return err_code;
86*150812a8SEvalZero }
87*150812a8SEvalZero
88*150812a8SEvalZero m_lpcomp_event_handler = event_handler;
89*150812a8SEvalZero
90*150812a8SEvalZero #if NRFX_CHECK(NRFX_PRS_ENABLED)
91*150812a8SEvalZero if (nrfx_prs_acquire(NRF_LPCOMP, nrfx_lpcomp_irq_handler) != NRFX_SUCCESS)
92*150812a8SEvalZero {
93*150812a8SEvalZero err_code = NRFX_ERROR_BUSY;
94*150812a8SEvalZero NRFX_LOG_WARNING("Function: %s, error code: %s.",
95*150812a8SEvalZero __func__,
96*150812a8SEvalZero NRFX_LOG_ERROR_STRING_GET(err_code));
97*150812a8SEvalZero return err_code;
98*150812a8SEvalZero }
99*150812a8SEvalZero #endif
100*150812a8SEvalZero
101*150812a8SEvalZero nrf_lpcomp_configure(&(p_config->hal));
102*150812a8SEvalZero
103*150812a8SEvalZero nrf_lpcomp_input_select(p_config->input);
104*150812a8SEvalZero
105*150812a8SEvalZero switch (p_config->hal.detection)
106*150812a8SEvalZero {
107*150812a8SEvalZero case NRF_LPCOMP_DETECT_UP:
108*150812a8SEvalZero nrf_lpcomp_int_enable(LPCOMP_INTENSET_UP_Msk);
109*150812a8SEvalZero break;
110*150812a8SEvalZero
111*150812a8SEvalZero case NRF_LPCOMP_DETECT_DOWN:
112*150812a8SEvalZero nrf_lpcomp_int_enable(LPCOMP_INTENSET_DOWN_Msk);
113*150812a8SEvalZero break;
114*150812a8SEvalZero
115*150812a8SEvalZero case NRF_LPCOMP_DETECT_CROSS:
116*150812a8SEvalZero nrf_lpcomp_int_enable(LPCOMP_INTENSET_CROSS_Msk);
117*150812a8SEvalZero break;
118*150812a8SEvalZero
119*150812a8SEvalZero default:
120*150812a8SEvalZero break;
121*150812a8SEvalZero }
122*150812a8SEvalZero nrf_lpcomp_shorts_enable(NRF_LPCOMP_SHORT_READY_SAMPLE_MASK);
123*150812a8SEvalZero
124*150812a8SEvalZero NRFX_IRQ_PRIORITY_SET(LPCOMP_IRQn, p_config->interrupt_priority);
125*150812a8SEvalZero NRFX_IRQ_ENABLE(LPCOMP_IRQn);
126*150812a8SEvalZero
127*150812a8SEvalZero m_state = NRFX_DRV_STATE_INITIALIZED;
128*150812a8SEvalZero
129*150812a8SEvalZero err_code = NRFX_SUCCESS;
130*150812a8SEvalZero NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
131*150812a8SEvalZero return err_code;
132*150812a8SEvalZero }
133*150812a8SEvalZero
nrfx_lpcomp_uninit(void)134*150812a8SEvalZero void nrfx_lpcomp_uninit(void)
135*150812a8SEvalZero {
136*150812a8SEvalZero NRFX_ASSERT(m_state != NRFX_DRV_STATE_UNINITIALIZED);
137*150812a8SEvalZero NRFX_IRQ_DISABLE(LPCOMP_IRQn);
138*150812a8SEvalZero nrfx_lpcomp_disable();
139*150812a8SEvalZero #if NRFX_CHECK(NRFX_PRS_ENABLED)
140*150812a8SEvalZero nrfx_prs_release(NRF_LPCOMP);
141*150812a8SEvalZero #endif
142*150812a8SEvalZero m_state = NRFX_DRV_STATE_UNINITIALIZED;
143*150812a8SEvalZero m_lpcomp_event_handler = NULL;
144*150812a8SEvalZero NRFX_LOG_INFO("Uninitialized.");
145*150812a8SEvalZero }
146*150812a8SEvalZero
nrfx_lpcomp_enable(void)147*150812a8SEvalZero void nrfx_lpcomp_enable(void)
148*150812a8SEvalZero {
149*150812a8SEvalZero NRFX_ASSERT(m_state == NRFX_DRV_STATE_INITIALIZED);
150*150812a8SEvalZero nrf_lpcomp_enable();
151*150812a8SEvalZero nrf_lpcomp_task_trigger(NRF_LPCOMP_TASK_START);
152*150812a8SEvalZero m_state = NRFX_DRV_STATE_POWERED_ON;
153*150812a8SEvalZero NRFX_LOG_INFO("Enabled.");
154*150812a8SEvalZero }
155*150812a8SEvalZero
nrfx_lpcomp_disable(void)156*150812a8SEvalZero void nrfx_lpcomp_disable(void)
157*150812a8SEvalZero {
158*150812a8SEvalZero NRFX_ASSERT(m_state == NRFX_DRV_STATE_POWERED_ON);
159*150812a8SEvalZero nrf_lpcomp_disable();
160*150812a8SEvalZero nrf_lpcomp_task_trigger(NRF_LPCOMP_TASK_STOP);
161*150812a8SEvalZero m_state = NRFX_DRV_STATE_INITIALIZED;
162*150812a8SEvalZero NRFX_LOG_INFO("Disabled.");
163*150812a8SEvalZero }
164*150812a8SEvalZero
165*150812a8SEvalZero #endif // NRFX_CHECK(NRFX_LPCOMP_ENABLED)
166