nrfx_adc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /**
  2. * Copyright (c) 2015 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include <nrfx.h>
  41. #if NRFX_CHECK(NRFX_ADC_ENABLED)
  42. #include <nrfx_adc.h>
  43. #define NRFX_LOG_MODULE ADC
  44. #include <nrfx_log.h>
  45. #define EVT_TO_STR(event) (event == NRF_ADC_EVENT_END ? "NRF_ADC_EVENT_END" : "UNKNOWN EVENT")
  46. typedef struct
  47. {
  48. nrfx_adc_event_handler_t event_handler;
  49. nrfx_adc_channel_t * p_head;
  50. nrfx_adc_channel_t * p_current_conv;
  51. nrf_adc_value_t * p_buffer;
  52. uint16_t size;
  53. uint16_t idx;
  54. nrfx_drv_state_t state;
  55. } adc_cb_t;
  56. static adc_cb_t m_cb;
  57. nrfx_err_t nrfx_adc_init(nrfx_adc_config_t const * p_config,
  58. nrfx_adc_event_handler_t event_handler)
  59. {
  60. NRFX_ASSERT(p_config);
  61. nrfx_err_t err_code;
  62. if (m_cb.state != NRFX_DRV_STATE_UNINITIALIZED)
  63. {
  64. err_code = NRFX_ERROR_INVALID_STATE;
  65. NRFX_LOG_WARNING("Function: %s, error code: %s.",
  66. __func__,
  67. NRFX_LOG_ERROR_STRING_GET(err_code));
  68. return err_code;
  69. }
  70. nrf_adc_event_clear(NRF_ADC_EVENT_END);
  71. if (event_handler)
  72. {
  73. NRFX_IRQ_PRIORITY_SET(ADC_IRQn, p_config->interrupt_priority);
  74. NRFX_IRQ_ENABLE(ADC_IRQn);
  75. }
  76. m_cb.event_handler = event_handler;
  77. m_cb.state = NRFX_DRV_STATE_INITIALIZED;
  78. err_code = NRFX_SUCCESS;
  79. NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
  80. return err_code;
  81. }
  82. void nrfx_adc_uninit(void)
  83. {
  84. NRFX_IRQ_DISABLE(ADC_IRQn);
  85. nrf_adc_int_disable(NRF_ADC_INT_END_MASK);
  86. nrf_adc_task_trigger(NRF_ADC_TASK_STOP);
  87. // Disable all channels. This must be done after the interrupt is disabled
  88. // because adc_sample_process() dereferences this pointer when it needs to
  89. // switch back to the first channel in the list (when the number of samples
  90. // to read is bigger than the number of enabled channels).
  91. m_cb.p_head = NULL;
  92. m_cb.state = NRFX_DRV_STATE_UNINITIALIZED;
  93. }
  94. void nrfx_adc_channel_enable(nrfx_adc_channel_t * const p_channel)
  95. {
  96. NRFX_ASSERT(!nrfx_adc_is_busy());
  97. p_channel->p_next = NULL;
  98. if (m_cb.p_head == NULL)
  99. {
  100. m_cb.p_head = p_channel;
  101. }
  102. else
  103. {
  104. nrfx_adc_channel_t * p_curr_channel = m_cb.p_head;
  105. while (p_curr_channel->p_next != NULL)
  106. {
  107. NRFX_ASSERT(p_channel != p_curr_channel);
  108. p_curr_channel = p_curr_channel->p_next;
  109. }
  110. p_curr_channel->p_next = p_channel;
  111. }
  112. NRFX_LOG_INFO("Enabled.");
  113. }
  114. void nrfx_adc_channel_disable(nrfx_adc_channel_t * const p_channel)
  115. {
  116. NRFX_ASSERT(m_cb.p_head);
  117. NRFX_ASSERT(!nrfx_adc_is_busy());
  118. nrfx_adc_channel_t * p_curr_channel = m_cb.p_head;
  119. nrfx_adc_channel_t * p_prev_channel = NULL;
  120. while (p_curr_channel != p_channel)
  121. {
  122. p_prev_channel = p_curr_channel;
  123. p_curr_channel = p_curr_channel->p_next;
  124. NRFX_ASSERT(p_curr_channel != NULL);
  125. }
  126. if (p_prev_channel)
  127. {
  128. p_prev_channel->p_next = p_curr_channel->p_next;
  129. }
  130. else
  131. {
  132. m_cb.p_head = p_curr_channel->p_next;
  133. }
  134. NRFX_LOG_INFO("Disabled.");
  135. }
  136. void nrfx_adc_all_channels_disable(void)
  137. {
  138. NRFX_ASSERT(!nrfx_adc_is_busy());
  139. m_cb.p_head = NULL;
  140. }
  141. void nrfx_adc_sample(void)
  142. {
  143. NRFX_ASSERT(m_cb.state != NRFX_DRV_STATE_UNINITIALIZED);
  144. NRFX_ASSERT(!nrf_adc_busy_check());
  145. nrf_adc_task_trigger(NRF_ADC_TASK_START);
  146. }
  147. nrfx_err_t nrfx_adc_sample_convert(nrfx_adc_channel_t const * const p_channel,
  148. nrf_adc_value_t * p_value)
  149. {
  150. nrfx_err_t err_code;
  151. NRFX_ASSERT(m_cb.state != NRFX_DRV_STATE_UNINITIALIZED);
  152. if (m_cb.state == NRFX_DRV_STATE_POWERED_ON)
  153. {
  154. err_code = NRFX_ERROR_BUSY;
  155. NRFX_LOG_WARNING("Function: %s, error code: %s.",
  156. __func__,
  157. NRFX_LOG_ERROR_STRING_GET(err_code));
  158. return err_code;
  159. }
  160. else
  161. {
  162. m_cb.state = NRFX_DRV_STATE_POWERED_ON;
  163. nrf_adc_init(&p_channel->config);
  164. nrf_adc_enable();
  165. nrf_adc_int_disable(NRF_ADC_INT_END_MASK);
  166. nrf_adc_task_trigger(NRF_ADC_TASK_START);
  167. if (p_value)
  168. {
  169. while (!nrf_adc_event_check(NRF_ADC_EVENT_END)) {}
  170. nrf_adc_event_clear(NRF_ADC_EVENT_END);
  171. *p_value = (nrf_adc_value_t)nrf_adc_result_get();
  172. nrf_adc_disable();
  173. m_cb.state = NRFX_DRV_STATE_INITIALIZED;
  174. }
  175. else
  176. {
  177. NRFX_ASSERT(m_cb.event_handler);
  178. m_cb.p_buffer = NULL;
  179. nrf_adc_int_enable(NRF_ADC_INT_END_MASK);
  180. }
  181. err_code = NRFX_SUCCESS;
  182. NRFX_LOG_INFO("Function: %s, error code: %s.",
  183. __func__,
  184. NRFX_LOG_ERROR_STRING_GET(err_code));
  185. return err_code;
  186. }
  187. }
  188. static bool adc_sample_process()
  189. {
  190. nrf_adc_event_clear(NRF_ADC_EVENT_END);
  191. nrf_adc_disable();
  192. m_cb.p_buffer[m_cb.idx] = (nrf_adc_value_t)nrf_adc_result_get();
  193. m_cb.idx++;
  194. if (m_cb.idx < m_cb.size)
  195. {
  196. bool task_trigger = false;
  197. if (m_cb.p_current_conv->p_next == NULL)
  198. {
  199. // Make sure the list of channels has not been somehow removed
  200. // (it is when all channels are disabled).
  201. NRFX_ASSERT(m_cb.p_head);
  202. m_cb.p_current_conv = m_cb.p_head;
  203. }
  204. else
  205. {
  206. m_cb.p_current_conv = m_cb.p_current_conv->p_next;
  207. task_trigger = true;
  208. }
  209. nrf_adc_init(&m_cb.p_current_conv->config);
  210. nrf_adc_enable();
  211. if (task_trigger)
  212. {
  213. nrf_adc_task_trigger(NRF_ADC_TASK_START);
  214. }
  215. return false;
  216. }
  217. else
  218. {
  219. return true;
  220. }
  221. }
  222. nrfx_err_t nrfx_adc_buffer_convert(nrf_adc_value_t * buffer, uint16_t size)
  223. {
  224. NRFX_ASSERT(m_cb.state != NRFX_DRV_STATE_UNINITIALIZED);
  225. nrfx_err_t err_code;
  226. NRFX_LOG_INFO("Number of samples requested to convert: %d.", size);
  227. if (m_cb.state == NRFX_DRV_STATE_POWERED_ON)
  228. {
  229. err_code = NRFX_ERROR_BUSY;
  230. NRFX_LOG_WARNING("Function: %s, error code: %s.",
  231. __func__,
  232. NRFX_LOG_ERROR_STRING_GET(err_code));
  233. return err_code;
  234. }
  235. else
  236. {
  237. m_cb.state = NRFX_DRV_STATE_POWERED_ON;
  238. m_cb.p_current_conv = m_cb.p_head;
  239. m_cb.size = size;
  240. m_cb.idx = 0;
  241. m_cb.p_buffer = buffer;
  242. nrf_adc_init(&m_cb.p_current_conv->config);
  243. nrf_adc_event_clear(NRF_ADC_EVENT_END);
  244. nrf_adc_enable();
  245. if (m_cb.event_handler)
  246. {
  247. nrf_adc_int_enable(NRF_ADC_INT_END_MASK);
  248. }
  249. else
  250. {
  251. while (1)
  252. {
  253. while (!nrf_adc_event_check(NRF_ADC_EVENT_END)){}
  254. if (adc_sample_process())
  255. {
  256. m_cb.state = NRFX_DRV_STATE_INITIALIZED;
  257. break;
  258. }
  259. }
  260. }
  261. err_code = NRFX_SUCCESS;
  262. NRFX_LOG_INFO("Function: %s, error code: %s.",
  263. __func__,
  264. NRFX_LOG_ERROR_STRING_GET(err_code));
  265. return err_code;
  266. }
  267. }
  268. bool nrfx_adc_is_busy(void)
  269. {
  270. NRFX_ASSERT(m_cb.state != NRFX_DRV_STATE_UNINITIALIZED);
  271. return (m_cb.state == NRFX_DRV_STATE_POWERED_ON) ? true : false;
  272. }
  273. void nrfx_adc_irq_handler(void)
  274. {
  275. if (m_cb.p_buffer == NULL)
  276. {
  277. nrf_adc_event_clear(NRF_ADC_EVENT_END);
  278. NRFX_LOG_DEBUG("Event: %s.",NRFX_LOG_ERROR_STRING_GET(NRF_ADC_EVENT_END));
  279. nrf_adc_int_disable(NRF_ADC_INT_END_MASK);
  280. nrf_adc_disable();
  281. nrfx_adc_evt_t evt;
  282. evt.type = NRFX_ADC_EVT_SAMPLE;
  283. evt.data.sample.sample = (nrf_adc_value_t)nrf_adc_result_get();
  284. NRFX_LOG_DEBUG("ADC data:");
  285. NRFX_LOG_HEXDUMP_DEBUG((uint8_t *)(&evt.data.sample.sample), sizeof(nrf_adc_value_t));
  286. m_cb.state = NRFX_DRV_STATE_INITIALIZED;
  287. m_cb.event_handler(&evt);
  288. }
  289. else if (adc_sample_process())
  290. {
  291. NRFX_LOG_DEBUG("Event: %s.", NRFX_LOG_ERROR_STRING_GET(NRF_ADC_EVENT_END));
  292. nrf_adc_int_disable(NRF_ADC_INT_END_MASK);
  293. nrfx_adc_evt_t evt;
  294. evt.type = NRFX_ADC_EVT_DONE;
  295. evt.data.done.p_buffer = m_cb.p_buffer;
  296. evt.data.done.size = m_cb.size;
  297. m_cb.state = NRFX_DRV_STATE_INITIALIZED;
  298. NRFX_LOG_DEBUG("ADC data:");
  299. NRFX_LOG_HEXDUMP_DEBUG((uint8_t *)m_cb.p_buffer, m_cb.size * sizeof(nrf_adc_value_t));
  300. m_cb.event_handler(&evt);
  301. }
  302. }
  303. #endif // NRFX_CHECK(NRFX_ADC_ENABLED)