nrf_drv_adc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. #include "nrf_drv_adc.h"
  13. #include "nrf_drv_common.h"
  14. #include "nrf_assert.h"
  15. #include "app_util_platform.h"
  16. #include "app_util.h"
  17. typedef struct
  18. {
  19. nrf_drv_adc_event_handler_t event_handler;
  20. nrf_drv_adc_channel_t * p_head;
  21. nrf_drv_adc_channel_t * p_current_conv;
  22. nrf_adc_value_t * p_buffer;
  23. uint8_t size;
  24. uint8_t idx;
  25. nrf_drv_state_t state;
  26. } adc_cb_t;
  27. static adc_cb_t m_cb;
  28. static const nrf_drv_adc_config_t m_default_config = NRF_DRV_ADC_DEFAULT_CONFIG;
  29. ret_code_t nrf_drv_adc_init(nrf_drv_adc_config_t const * p_config,
  30. nrf_drv_adc_event_handler_t event_handler)
  31. {
  32. if (m_cb.state != NRF_DRV_STATE_UNINITIALIZED)
  33. {
  34. return NRF_ERROR_INVALID_STATE;
  35. }
  36. nrf_adc_event_clear(NRF_ADC_EVENT_END);
  37. if (event_handler)
  38. {
  39. if (!p_config)
  40. {
  41. p_config = (nrf_drv_adc_config_t *)&m_default_config;
  42. }
  43. nrf_drv_common_irq_enable(ADC_IRQn, p_config->interrupt_priority);
  44. }
  45. m_cb.event_handler = event_handler;
  46. m_cb.state = NRF_DRV_STATE_INITIALIZED;
  47. return NRF_SUCCESS;
  48. }
  49. void nrf_drv_adc_uninit(void)
  50. {
  51. m_cb.p_head = NULL;
  52. nrf_drv_common_irq_disable(ADC_IRQn);
  53. nrf_adc_int_disable(NRF_ADC_INT_END_MASK);
  54. nrf_adc_task_trigger(NRF_ADC_TASK_STOP);
  55. m_cb.state = NRF_DRV_STATE_UNINITIALIZED;
  56. }
  57. void nrf_drv_adc_channel_enable(nrf_drv_adc_channel_t * const p_channel)
  58. {
  59. ASSERT(mp_state == NRF_DRV_STATE_INITIALIZED);
  60. ASSERT(!is_address_from_stack(p_channel));
  61. p_channel->p_next = NULL;
  62. if (m_cb.p_head == NULL)
  63. {
  64. m_cb.p_head = p_channel;
  65. }
  66. else
  67. {
  68. nrf_drv_adc_channel_t * p_curr_channel = m_cb.p_head;
  69. while (p_curr_channel->p_next != NULL)
  70. {
  71. ASSERT(p_channel != p_curr_channel);
  72. p_curr_channel = p_curr_channel->p_next;
  73. }
  74. p_curr_channel->p_next = p_channel;
  75. }
  76. }
  77. void nrf_drv_adc_channel_disable(nrf_drv_adc_channel_t * const p_channel)
  78. {
  79. ASSERT(mp_state == NRF_DRV_STATE_INITIALIZED);
  80. ASSERT(m_cb.p_head);
  81. nrf_drv_adc_channel_t * p_curr_channel = m_cb.p_head;
  82. nrf_drv_adc_channel_t * p_prev_channel = NULL;
  83. while(p_curr_channel != p_channel)
  84. {
  85. p_prev_channel = p_curr_channel;
  86. p_curr_channel = p_curr_channel->p_next;
  87. ASSERT(p_curr_channel == NULL);
  88. }
  89. if (p_prev_channel)
  90. {
  91. p_prev_channel->p_next = p_curr_channel->p_next;
  92. }
  93. else
  94. {
  95. m_cb.p_head = p_curr_channel->p_next;
  96. }
  97. }
  98. void nrf_drv_adc_sample(void)
  99. {
  100. ASSERT(mp_state != NRF_DRV_STATE_UNINITIALIZED);
  101. ASSERT(!nrf_adc_is_busy());
  102. nrf_adc_start();
  103. }
  104. ret_code_t nrf_drv_adc_sample_convert(nrf_drv_adc_channel_t const * const p_channel,
  105. nrf_adc_value_t * p_value)
  106. {
  107. ASSERT(mp_state != NRF_DRV_STATE_UNINITIALIZED);
  108. if(m_cb.state == NRF_DRV_STATE_POWERED_ON)
  109. {
  110. return NRF_ERROR_BUSY;
  111. }
  112. else
  113. {
  114. m_cb.state = NRF_DRV_STATE_POWERED_ON;
  115. nrf_adc_config_set(p_channel->config.data);
  116. nrf_adc_enable();
  117. nrf_adc_int_disable(NRF_ADC_INT_END_MASK);
  118. nrf_adc_start();
  119. if (p_value)
  120. {
  121. while(!nrf_adc_event_check(NRF_ADC_EVENT_END)) {}
  122. nrf_adc_event_clear(NRF_ADC_EVENT_END);
  123. *p_value = (nrf_adc_value_t)nrf_adc_result_get();
  124. nrf_adc_disable();
  125. m_cb.state = NRF_DRV_STATE_INITIALIZED;
  126. }
  127. else
  128. {
  129. ASSERT(m_cb.event_handler);
  130. m_cb.p_buffer = NULL;
  131. nrf_adc_int_enable(NRF_ADC_INT_END_MASK);
  132. }
  133. return NRF_SUCCESS;
  134. }
  135. }
  136. static bool adc_sample_process()
  137. {
  138. nrf_adc_event_clear(NRF_ADC_EVENT_END);
  139. nrf_adc_disable();
  140. m_cb.p_buffer[m_cb.idx] = (nrf_adc_value_t)nrf_adc_result_get();
  141. m_cb.idx++;
  142. if (m_cb.idx < m_cb.size)
  143. {
  144. bool task_trigger = false;
  145. if (m_cb.p_current_conv->p_next == NULL)
  146. {
  147. m_cb.p_current_conv = m_cb.p_head;
  148. }
  149. else
  150. {
  151. m_cb.p_current_conv = m_cb.p_current_conv->p_next;
  152. task_trigger = true;
  153. }
  154. nrf_adc_config_set(m_cb.p_current_conv->config.data);
  155. nrf_adc_enable();
  156. if (task_trigger)
  157. {
  158. //nrf_adc_start();
  159. nrf_adc_task_trigger(NRF_ADC_TASK_START);
  160. }
  161. return false;
  162. }
  163. else
  164. {
  165. return true;
  166. }
  167. }
  168. ret_code_t nrf_drv_adc_buffer_convert(nrf_adc_value_t * buffer, uint16_t size)
  169. {
  170. ASSERT(mp_state != NRF_DRV_STATE_UNINITIALIZED);
  171. if(m_cb.state == NRF_DRV_STATE_POWERED_ON)
  172. {
  173. return NRF_ERROR_BUSY;
  174. }
  175. else
  176. {
  177. m_cb.state = NRF_DRV_STATE_POWERED_ON;
  178. m_cb.p_current_conv = m_cb.p_head;
  179. m_cb.size = size;
  180. m_cb.idx = 0;
  181. m_cb.p_buffer = buffer;
  182. nrf_adc_config_set(m_cb.p_current_conv->config.data);
  183. nrf_adc_event_clear(NRF_ADC_EVENT_END);
  184. nrf_adc_enable();
  185. if (m_cb.event_handler)
  186. {
  187. nrf_adc_int_enable(NRF_ADC_INT_END_MASK);
  188. }
  189. else
  190. {
  191. while(1)
  192. {
  193. while(!nrf_adc_event_check(NRF_ADC_EVENT_END)){}
  194. if (adc_sample_process())
  195. {
  196. m_cb.state = NRF_DRV_STATE_INITIALIZED;
  197. break;
  198. }
  199. }
  200. }
  201. return NRF_SUCCESS;
  202. }
  203. }
  204. bool nrf_drv_adc_is_busy(void)
  205. {
  206. ASSERT(mp_state != NRF_DRV_STATE_UNINITIALIZED);
  207. return (m_cb.state == NRF_DRV_STATE_POWERED_ON) ? true : false;
  208. }
  209. void ADC_IRQHandler(void)
  210. {
  211. if (m_cb.p_buffer == NULL)
  212. {
  213. nrf_adc_event_clear(NRF_ADC_EVENT_END);
  214. nrf_adc_int_disable(NRF_ADC_INT_END_MASK);
  215. nrf_adc_disable();
  216. nrf_drv_adc_evt_t evt;
  217. evt.type = NRF_DRV_ADC_EVT_SAMPLE;
  218. evt.data.sample.sample = (nrf_adc_value_t)nrf_adc_result_get();
  219. m_cb.state = NRF_DRV_STATE_INITIALIZED;
  220. m_cb.event_handler(&evt);
  221. }
  222. else if (adc_sample_process())
  223. {
  224. nrf_adc_int_disable(NRF_ADC_INT_END_MASK);
  225. nrf_drv_adc_evt_t evt;
  226. evt.type = NRF_DRV_ADC_EVT_DONE;
  227. evt.data.done.p_buffer = m_cb.p_buffer;
  228. evt.data.done.size = m_cb.size;
  229. m_cb.state = NRF_DRV_STATE_INITIALIZED;
  230. m_cb.event_handler(&evt);
  231. }
  232. }