drv_adc.c 8.3 KB

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