drv_adc.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-08-18 guohp1128 the first version
  9. */
  10. #include "drv_adc.h"
  11. #ifdef RT_USING_ADC
  12. struct rt_adc_device nrf5x_adc_device;
  13. drv_nrfx_saadc_result_t results;
  14. nrf_saadc_value_t result_buff_cache[8];
  15. static void nrf5x_saadc_event_hdr(nrfx_saadc_evt_t const * p_event)
  16. {
  17. uint8_t i,j;
  18. if(p_event->type == NRFX_SAADC_EVT_DONE)
  19. {
  20. j = 0;
  21. for(i = 0; i < 8; i++)
  22. {
  23. if(results.channels[i].channel_index == i)
  24. {
  25. results.result_buffer[i] = result_buff_cache[j];
  26. j ++;
  27. }
  28. }
  29. results.done = 1;
  30. }
  31. }
  32. static uint32_t get_channels_mask(void)
  33. {
  34. uint8_t i;
  35. uint32_t mask = 0;
  36. for(i = 0; i < 8; i++)
  37. {
  38. if(results.channels[i].channel_index != 0xff)
  39. {
  40. mask |= (1 << results.channels[i].channel_index);
  41. }
  42. }
  43. return mask;
  44. }
  45. static void set_channels(drv_nrfx_saadc_channel_t * channel)
  46. {
  47. uint8_t i;
  48. if(channel -> mode == NRF_SAADC_MODE_SINGLE_ENDED)
  49. {
  50. results.channels[channel->channel_num] = (nrfx_saadc_channel_t)NRFX_SAADC_DEFAULT_CHANNEL_SE(channel -> pin_p + 1, channel -> channel_num);
  51. }
  52. else if(channel -> mode == NRF_SAADC_MODE_DIFFERENTIAL)
  53. {
  54. results.channels[channel->channel_num] = (nrfx_saadc_channel_t)NRFX_SAADC_DEFAULT_CHANNEL_DIFFERENTIAL(channel -> pin_p + 1, channel -> pin_n + 1, channel -> channel_num);
  55. }
  56. results.channel_count = 0;
  57. for(i = 0; i < 8; i++)
  58. {
  59. if(results.channels[i].channel_index != 0xff)
  60. {
  61. results.channel_count ++;
  62. }
  63. }
  64. }
  65. /* channel: 0-7 */
  66. static rt_err_t nrf5x_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
  67. {
  68. nrfx_err_t err_code = NRFX_SUCCESS;
  69. uint8_t i,j;
  70. if (enabled)
  71. {
  72. RT_ASSERT(device != RT_NULL);
  73. RT_ASSERT(device->parent.user_data != RT_NULL);
  74. drv_nrfx_saadc_channel_t * drv_channel_config = NULL;
  75. drv_channel_config = (drv_nrfx_saadc_channel_t *)device->parent.user_data;
  76. set_channels(drv_channel_config);
  77. nrfx_saadc_channel_t channels_cache[results.channel_count];
  78. j = 0;
  79. for(i = 0; i < 8; i++)
  80. {
  81. if(results.channels[i].channel_index != 0xff)
  82. {
  83. channels_cache[j] = results.channels[i];
  84. j ++;
  85. }
  86. }
  87. err_code = nrfx_saadc_channels_config(channels_cache,results.channel_count);
  88. err_code = nrfx_saadc_simple_mode_set(get_channels_mask(),
  89. NRF_SAADC_RESOLUTION_12BIT,
  90. NRF_SAADC_OVERSAMPLE_DISABLED,
  91. nrf5x_saadc_event_hdr);
  92. err_code = nrfx_saadc_buffer_set(result_buff_cache, results.channel_count);
  93. }
  94. else
  95. {
  96. results.channels[channel].channel_index = 0xff;
  97. results.channel_count = 0;
  98. for(i = 0; i < 8; i++)
  99. {
  100. if(results.channels[i].channel_index != 0xff)
  101. {
  102. results.channel_count ++;
  103. }
  104. }
  105. if(results.channel_count == 0)
  106. {
  107. nrfx_saadc_channel_t channels_cache[1];
  108. err_code = nrfx_saadc_channels_config(channels_cache, 0);
  109. return err_code;
  110. }
  111. else
  112. {
  113. nrfx_saadc_channel_t channels_cache[results.channel_count];
  114. j = 0;
  115. for(i = 0; i < 8; i++)
  116. {
  117. if(results.channels[i].channel_index != 0xff)
  118. {
  119. channels_cache[j] = results.channels[i];
  120. j ++;
  121. }
  122. }
  123. err_code = nrfx_saadc_channels_config(channels_cache,results.channel_count);
  124. err_code = nrfx_saadc_simple_mode_set(get_channels_mask(),
  125. NRF_SAADC_RESOLUTION_12BIT,
  126. NRF_SAADC_OVERSAMPLE_DISABLED,
  127. nrf5x_saadc_event_hdr);
  128. err_code = nrfx_saadc_buffer_set(result_buff_cache, results.channel_count);
  129. }
  130. }
  131. return err_code;
  132. }
  133. static rt_err_t nrf5x_get_adc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
  134. {
  135. nrfx_err_t err_code = NRFX_SUCCESS;
  136. if (results.channels[channel].channel_index != 0xff)
  137. {
  138. results.done = 0;
  139. err_code = nrfx_saadc_mode_trigger();
  140. while(results.done == 0)
  141. {
  142. ;
  143. }
  144. * value = results.result_buffer[channel];
  145. results.done = 0;
  146. }
  147. return err_code;
  148. }
  149. static const struct rt_adc_ops nrf5x_adc_ops =
  150. {
  151. .enabled = nrf5x_adc_enabled,
  152. .convert = nrf5x_get_adc_value,
  153. };
  154. int rt_hw_adc_init(void)
  155. {
  156. int result = RT_EOK;
  157. uint8_t i;
  158. char name_buf[6] = ADC_NAME;
  159. for(i = 0; i < 8; i++)
  160. {
  161. results.channels[i].channel_index = 0xff;
  162. results.result_buffer[i] = 0;
  163. results.channel_count = 0;
  164. results.done = 0;
  165. }
  166. /* initializing SAADC interrupt priority */
  167. if (nrfx_saadc_init(NRFX_SAADC_CONFIG_IRQ_PRIORITY) != NRFX_SUCCESS)
  168. {
  169. rt_kprintf("%s init failed", name_buf);
  170. rt_kprintf("The driver is already initialized.");
  171. result = -RT_ERROR;
  172. }
  173. else
  174. {
  175. /* register ADC device */
  176. if (rt_hw_adc_register(&nrf5x_adc_device, name_buf, &nrf5x_adc_ops, nrf5x_adc_device.parent.user_data) == RT_EOK)
  177. {
  178. rt_kprintf("%s init success", name_buf);
  179. }
  180. else
  181. {
  182. rt_kprintf("%s register failed", name_buf);
  183. result = -RT_ERROR;
  184. }
  185. }
  186. return result;
  187. }
  188. INIT_BOARD_EXPORT(rt_hw_adc_init);
  189. /*test saadc*/
  190. #include <drv_adc.h>
  191. #define SAMPLE_ADC_MODE_SINGLE_ENDED 0 //single-ended mode
  192. #define SAMPLE_ADC_MODE_DIFFERENTIAL 1 //differential mode
  193. #define SAMPLE_ADC_AIN1 1
  194. #define SAMPLE_ADC_AIN2 2
  195. #define SAMPLE_ADC_AIN7 7
  196. #define SAMPLE_ADC_AIN_NC 0 //disable input of AINx
  197. #define SAMPLE_ADC_CHANNEL_0 0
  198. #define SAMPLE_ADC_CHANNEL_1 1
  199. #define SAMPLE_ADC_CHANNEL_5 5
  200. void saadc_sample(void)
  201. {
  202. drv_nrfx_saadc_channel_t channel_config;
  203. rt_uint32_t result;
  204. rt_adc_device_t adc_dev;
  205. adc_dev = (rt_adc_device_t)rt_device_find(ADC_NAME);
  206. adc_dev->parent.user_data = &channel_config;
  207. channel_config = (drv_nrfx_saadc_channel_t){.mode = SAMPLE_ADC_MODE_SINGLE_ENDED,
  208. .pin_p = SAMPLE_ADC_AIN1,
  209. .pin_n = SAMPLE_ADC_AIN_NC,
  210. .channel_num = SAMPLE_ADC_CHANNEL_0};
  211. rt_adc_enable(adc_dev, channel_config.channel_num);
  212. channel_config = (drv_nrfx_saadc_channel_t){.mode = SAMPLE_ADC_MODE_SINGLE_ENDED,
  213. .pin_p = SAMPLE_ADC_AIN2,
  214. .pin_n = SAMPLE_ADC_AIN_NC,
  215. .channel_num = SAMPLE_ADC_CHANNEL_1};
  216. rt_adc_enable(adc_dev, channel_config.channel_num);
  217. channel_config = (drv_nrfx_saadc_channel_t){.mode = SAMPLE_ADC_MODE_SINGLE_ENDED,
  218. .pin_p = SAMPLE_ADC_AIN7,
  219. .pin_n = SAMPLE_ADC_AIN_NC,
  220. .channel_num = SAMPLE_ADC_CHANNEL_5};
  221. rt_adc_enable(adc_dev, channel_config.channel_num);
  222. int count = 1;
  223. while(count++)
  224. {
  225. result = rt_adc_read(adc_dev, 0);
  226. rt_kprintf("saadc channel 0 value = %d, ",result);
  227. result = rt_adc_read(adc_dev, 1);
  228. rt_kprintf("saadc channel 1 value = %d, ",result);
  229. result = rt_adc_read(adc_dev, 5);
  230. rt_kprintf("saadc channel 5 value = %d",result);
  231. rt_kprintf("\r\n");
  232. rt_thread_mdelay(1000);
  233. }
  234. }
  235. MSH_CMD_EXPORT(saadc_sample, saadc sample);
  236. #endif /* RT_USING_ADC */