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