|
| 1 | +/* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved. |
| 2 | + * |
| 3 | + * The information contained herein is property of Nordic Semiconductor ASA. |
| 4 | + * Terms and conditions of usage are described in detail in NORDIC |
| 5 | + * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. |
| 6 | + * |
| 7 | + * Licensees are granted free, non-transferable use of the information. NO |
| 8 | + * WARRANTY of ANY KIND is provided. This heading must NOT be removed from |
| 9 | + * the file. |
| 10 | + * |
| 11 | + */ |
| 12 | + |
| 13 | +#include "nrf_drv_adc.h" |
| 14 | +#include "nrf_drv_common.h" |
| 15 | +#include "nrf_assert.h" |
| 16 | +#include "app_util_platform.h" |
| 17 | +#include "app_util.h" |
| 18 | + |
| 19 | +typedef struct |
| 20 | +{ |
| 21 | + nrf_drv_adc_event_handler_t event_handler; |
| 22 | + nrf_drv_adc_channel_t * p_head; |
| 23 | + nrf_drv_adc_channel_t * p_current_conv; |
| 24 | + nrf_adc_value_t * p_buffer; |
| 25 | + uint8_t size; |
| 26 | + uint8_t idx; |
| 27 | + nrf_drv_state_t state; |
| 28 | +} adc_cb_t; |
| 29 | + |
| 30 | +static adc_cb_t m_cb; |
| 31 | +static const nrf_drv_adc_config_t m_default_config = NRF_DRV_ADC_DEFAULT_CONFIG; |
| 32 | + |
| 33 | +ret_code_t nrf_drv_adc_init(nrf_drv_adc_config_t const * p_config, |
| 34 | + nrf_drv_adc_event_handler_t event_handler) |
| 35 | +{ |
| 36 | + if (m_cb.state != NRF_DRV_STATE_UNINITIALIZED) |
| 37 | + { |
| 38 | + return NRF_ERROR_INVALID_STATE; |
| 39 | + } |
| 40 | + |
| 41 | + nrf_adc_event_clear(NRF_ADC_EVENT_END); |
| 42 | + if (event_handler) |
| 43 | + { |
| 44 | + if (!p_config) |
| 45 | + { |
| 46 | + p_config = (nrf_drv_adc_config_t *)&m_default_config; |
| 47 | + } |
| 48 | + nrf_drv_common_irq_enable(ADC_IRQn, p_config->interrupt_priority); |
| 49 | + } |
| 50 | + m_cb.event_handler = event_handler; |
| 51 | + m_cb.state = NRF_DRV_STATE_INITIALIZED; |
| 52 | + |
| 53 | + return NRF_SUCCESS; |
| 54 | +} |
| 55 | + |
| 56 | +void nrf_drv_adc_uninit(void) |
| 57 | +{ |
| 58 | + m_cb.p_head = NULL; |
| 59 | + nrf_drv_common_irq_disable(ADC_IRQn); |
| 60 | + nrf_adc_int_disable(NRF_ADC_INT_END_MASK); |
| 61 | + nrf_adc_task_trigger(NRF_ADC_TASK_STOP); |
| 62 | + |
| 63 | + m_cb.state = NRF_DRV_STATE_UNINITIALIZED; |
| 64 | +} |
| 65 | + |
| 66 | +void nrf_drv_adc_channel_enable(nrf_drv_adc_channel_t * const p_channel) |
| 67 | +{ |
| 68 | + ASSERT(mp_state == NRF_DRV_STATE_INITIALIZED); |
| 69 | + ASSERT(!is_address_from_stack(p_channel)); |
| 70 | + |
| 71 | + p_channel->p_next = NULL; |
| 72 | + if (m_cb.p_head == NULL) |
| 73 | + { |
| 74 | + m_cb.p_head = p_channel; |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + nrf_drv_adc_channel_t * p_curr_channel = m_cb.p_head; |
| 79 | + while (p_curr_channel->p_next != NULL) |
| 80 | + { |
| 81 | + ASSERT(p_channel != p_curr_channel); |
| 82 | + p_curr_channel = p_curr_channel->p_next; |
| 83 | + } |
| 84 | + p_curr_channel->p_next = p_channel; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void nrf_drv_adc_channel_disable(nrf_drv_adc_channel_t * const p_channel) |
| 89 | +{ |
| 90 | + ASSERT(mp_state == NRF_DRV_STATE_INITIALIZED); |
| 91 | + ASSERT(m_cb.p_head); |
| 92 | + |
| 93 | + nrf_drv_adc_channel_t * p_curr_channel = m_cb.p_head; |
| 94 | + nrf_drv_adc_channel_t * p_prev_channel = NULL; |
| 95 | + while(p_curr_channel != p_channel) |
| 96 | + { |
| 97 | + p_prev_channel = p_curr_channel; |
| 98 | + p_curr_channel = p_curr_channel->p_next; |
| 99 | + ASSERT(p_curr_channel == NULL); |
| 100 | + } |
| 101 | + if (p_prev_channel) |
| 102 | + { |
| 103 | + p_prev_channel->p_next = p_curr_channel->p_next; |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + m_cb.p_head = p_curr_channel->p_next; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +void nrf_drv_adc_sample(void) |
| 112 | +{ |
| 113 | + ASSERT(mp_state != NRF_DRV_STATE_UNINITIALIZED); |
| 114 | + ASSERT(!nrf_adc_is_busy()); |
| 115 | + nrf_adc_start(); |
| 116 | +} |
| 117 | + |
| 118 | +ret_code_t nrf_drv_adc_sample_convert(nrf_drv_adc_channel_t const * const p_channel, |
| 119 | + nrf_adc_value_t * p_value) |
| 120 | +{ |
| 121 | + ASSERT(mp_state != NRF_DRV_STATE_UNINITIALIZED); |
| 122 | + if(m_cb.state == NRF_DRV_STATE_POWERED_ON) |
| 123 | + { |
| 124 | + return NRF_ERROR_BUSY; |
| 125 | + } |
| 126 | + else |
| 127 | + { |
| 128 | + m_cb.state = NRF_DRV_STATE_POWERED_ON; |
| 129 | + |
| 130 | + nrf_adc_config_set(p_channel->config.data); |
| 131 | + nrf_adc_enable(); |
| 132 | + nrf_adc_int_disable(NRF_ADC_INT_END_MASK); |
| 133 | + nrf_adc_start(); |
| 134 | + if (p_value) |
| 135 | + { |
| 136 | + while(!nrf_adc_event_check(NRF_ADC_EVENT_END)) {} |
| 137 | + nrf_adc_event_clear(NRF_ADC_EVENT_END); |
| 138 | + *p_value = (nrf_adc_value_t)nrf_adc_result_get(); |
| 139 | + nrf_adc_disable(); |
| 140 | + |
| 141 | + m_cb.state = NRF_DRV_STATE_INITIALIZED; |
| 142 | + } |
| 143 | + else |
| 144 | + { |
| 145 | + ASSERT(m_cb.event_handler); |
| 146 | + m_cb.p_buffer = NULL; |
| 147 | + nrf_adc_int_enable(NRF_ADC_INT_END_MASK); |
| 148 | + } |
| 149 | + return NRF_SUCCESS; |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +static bool adc_sample_process() |
| 154 | +{ |
| 155 | + nrf_adc_event_clear(NRF_ADC_EVENT_END); |
| 156 | + nrf_adc_disable(); |
| 157 | + m_cb.p_buffer[m_cb.idx] = (nrf_adc_value_t)nrf_adc_result_get(); |
| 158 | + m_cb.idx++; |
| 159 | + if (m_cb.idx < m_cb.size) |
| 160 | + { |
| 161 | + bool task_trigger = false; |
| 162 | + if (m_cb.p_current_conv->p_next == NULL) |
| 163 | + { |
| 164 | + m_cb.p_current_conv = m_cb.p_head; |
| 165 | + } |
| 166 | + else |
| 167 | + { |
| 168 | + m_cb.p_current_conv = m_cb.p_current_conv->p_next; |
| 169 | + task_trigger = true; |
| 170 | + } |
| 171 | + nrf_adc_config_set(m_cb.p_current_conv->config.data); |
| 172 | + nrf_adc_enable(); |
| 173 | + if (task_trigger) |
| 174 | + { |
| 175 | + //nrf_adc_start(); |
| 176 | + nrf_adc_task_trigger(NRF_ADC_TASK_START); |
| 177 | + } |
| 178 | + return false; |
| 179 | + } |
| 180 | + else |
| 181 | + { |
| 182 | + return true; |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +ret_code_t nrf_drv_adc_buffer_convert(nrf_adc_value_t * buffer, uint16_t size) |
| 187 | +{ |
| 188 | + ASSERT(mp_state != NRF_DRV_STATE_UNINITIALIZED); |
| 189 | + if(m_cb.state == NRF_DRV_STATE_POWERED_ON) |
| 190 | + { |
| 191 | + return NRF_ERROR_BUSY; |
| 192 | + } |
| 193 | + else |
| 194 | + { |
| 195 | + m_cb.state = NRF_DRV_STATE_POWERED_ON; |
| 196 | + m_cb.p_current_conv = m_cb.p_head; |
| 197 | + m_cb.size = size; |
| 198 | + m_cb.idx = 0; |
| 199 | + m_cb.p_buffer = buffer; |
| 200 | + nrf_adc_config_set(m_cb.p_current_conv->config.data); |
| 201 | + nrf_adc_event_clear(NRF_ADC_EVENT_END); |
| 202 | + nrf_adc_enable(); |
| 203 | + if (m_cb.event_handler) |
| 204 | + { |
| 205 | + nrf_adc_int_enable(NRF_ADC_INT_END_MASK); |
| 206 | + } |
| 207 | + else |
| 208 | + { |
| 209 | + while(1) |
| 210 | + { |
| 211 | + while(!nrf_adc_event_check(NRF_ADC_EVENT_END)){} |
| 212 | + |
| 213 | + if (adc_sample_process()) |
| 214 | + { |
| 215 | + m_cb.state = NRF_DRV_STATE_INITIALIZED; |
| 216 | + break; |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + return NRF_SUCCESS; |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +bool nrf_drv_adc_is_busy(void) |
| 225 | +{ |
| 226 | + ASSERT(mp_state != NRF_DRV_STATE_UNINITIALIZED); |
| 227 | + return (m_cb.state == NRF_DRV_STATE_POWERED_ON) ? true : false; |
| 228 | +} |
| 229 | + |
| 230 | +void ADC_IRQHandler(void) |
| 231 | +{ |
| 232 | + if (m_cb.p_buffer == NULL) |
| 233 | + { |
| 234 | + nrf_adc_event_clear(NRF_ADC_EVENT_END); |
| 235 | + nrf_adc_int_disable(NRF_ADC_INT_END_MASK); |
| 236 | + nrf_adc_disable(); |
| 237 | + nrf_drv_adc_evt_t evt; |
| 238 | + evt.type = NRF_DRV_ADC_EVT_SAMPLE; |
| 239 | + evt.data.sample.sample = (nrf_adc_value_t)nrf_adc_result_get(); |
| 240 | + m_cb.state = NRF_DRV_STATE_INITIALIZED; |
| 241 | + m_cb.event_handler(&evt); |
| 242 | + } |
| 243 | + else if (adc_sample_process()) |
| 244 | + { |
| 245 | + nrf_adc_int_disable(NRF_ADC_INT_END_MASK); |
| 246 | + nrf_drv_adc_evt_t evt; |
| 247 | + evt.type = NRF_DRV_ADC_EVT_DONE; |
| 248 | + evt.data.done.p_buffer = m_cb.p_buffer; |
| 249 | + evt.data.done.size = m_cb.size; |
| 250 | + m_cb.state = NRF_DRV_STATE_INITIALIZED; |
| 251 | + m_cb.event_handler(&evt); |
| 252 | + } |
| 253 | +} |
0 commit comments