bsp_adc.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*********************************************************************
  2. * INCLUDES
  3. */
  4. #include "bsp_adc.h"
  5. #include "system.h"
  6. #include "exception.h"
  7. /*********************************************************************
  8. * DEFINITIONS
  9. */
  10. #define PIN_NOT_USED 0xFF
  11. #define CHANNEL_NOT_USED 0xFF
  12. /*********************************************************************
  13. * STRUCTION
  14. */
  15. typedef struct {
  16. uint32_t pin;
  17. uint32_t channel;
  18. }adc_config_t;
  19. /*********************************************************************
  20. * LOCAL VARIABLES
  21. */
  22. static adc_config_t m_adc_config[CHANNEL_MAX] = {{PIN_NOT_USED,CHANNEL_NOT_USED},{PIN_NOT_USED,CHANNEL_NOT_USED},{PIN_NOT_USED,CHANNEL_NOT_USED},{PIN_NOT_USED,CHANNEL_NOT_USED},{PIN_NOT_USED,CHANNEL_NOT_USED},{PIN_NOT_USED,CHANNEL_NOT_USED},{PIN_NOT_USED,CHANNEL_NOT_USED},{PIN_NOT_USED,CHANNEL_NOT_USED}};
  23. static nrf_saadc_channel_config_t channelConfig[CHANNEL_MAX];
  24. static nrf_saadc_value_t s_bufferPool[CHANNEL_MAX] = {0};
  25. //定义SAADC采样数据缓存
  26. //定义SAADC采样缓存数组大小
  27. //只有采样结果存满该缓存之后,才会产生SAADC采样完成事件
  28. static uint32_t sample_in_buffer = 0;
  29. static bsp_adc_sample_notify_cb m_cb = NULL;
  30. /*********************************************************************
  31. * LOCAL FUNCTIONS
  32. */
  33. /**
  34. @brief ADC中断处理回调函数
  35. @param 无
  36. @return 无
  37. */
  38. static void adcCallbackFunc(nrf_drv_saadc_evt_t const *pEvent)
  39. {
  40. ret_code_t err_code;
  41. static int16_t adc_value[CHANNEL_MAX] = {0,0,0,0,0,0,0,0};
  42. uint8_t adc_sort[CHANNEL_MAX] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
  43. uint8_t sort_index = 0;
  44. int i;
  45. if(pEvent->type == NRF_DRV_SAADC_EVT_DONE) // 采样完成,采集时填充顺序为,通道编号小的先填充。
  46. {
  47. //设置好缓存,为下一次采样做准备
  48. err_code=nrf_drv_saadc_buffer_convert(pEvent->data.done.p_buffer,sample_in_buffer);
  49. if(err_code == NRF_SUCCESS)
  50. {
  51. //预先排序,采集时填充顺序为,通道编号小的先填充。
  52. for(i=0;i<CHANNEL_MAX;i++)
  53. {
  54. if(channelConfig[i].pin_p != NRF_SAADC_INPUT_DISABLED){
  55. adc_sort[sort_index] = i;
  56. // DEBUG_LOG("adc_sort[%d] = %d\r\n",sort_index,i);
  57. sort_index++;
  58. }
  59. }
  60. //获取数据
  61. for(i=0;i<sample_in_buffer;i++)
  62. {
  63. adc_value[adc_sort[i]] = s_bufferPool[i];
  64. }
  65. if(m_cb != NULL)m_cb(adc_value);
  66. }
  67. }
  68. }
  69. //static void bsp_adc_init_process(void)
  70. //{
  71. // if(Except_TxError(EXCEPT_ADC_INIT,"bsp_adc_init_error\r\n") == 0)
  72. // {
  73. // Process_Stop(bsp_adc_init_process);
  74. // }
  75. //}
  76. /**
  77. @brief 初始化ADC
  78. @param 无
  79. @return 无
  80. */
  81. static void ADC_Init(void)
  82. {
  83. int ret = 0;
  84. ret_code_t errCode;
  85. nrf_drv_saadc_config_t p_config = NRFX_SAADC_DEFAULT_CONFIG;
  86. p_config.interrupt_priority = ADC_IRQ_PRIORITY;
  87. // ADC初始化
  88. nrf_drv_saadc_init(&p_config, adcCallbackFunc);//优先级设置为3,比定时器中断要高,不然回调会在定时器中断结束后触发。
  89. if(errCode != NRF_SUCCESS)ret = -1;
  90. // ADC通道配置
  91. for(int i=0;i<CHANNEL_MAX;i++)
  92. {
  93. if(m_adc_config[i].pin != PIN_NOT_USED && m_adc_config[i].channel != CHANNEL_NOT_USED)
  94. {
  95. // ADC通道初始化
  96. nrf_drv_saadc_channel_init(m_adc_config[i].channel, &channelConfig[m_adc_config[i].channel]);
  97. //// if(errCode != NRF_SUCCESS)ret = -1;
  98. }
  99. }
  100. if(sample_in_buffer > 0)
  101. {
  102. // 缓冲配置
  103. nrf_drv_saadc_buffer_convert(s_bufferPool, sample_in_buffer);
  104. if(errCode != NRF_SUCCESS)ret = -1;
  105. }
  106. // if(ret == -1)
  107. // {
  108. //// Process_Start(0,"bsp_adc_init_process",bsp_adc_init_process);
  109. // SEGGER_RTT_printf(0,"bsp_adc_init error\r\n");
  110. // }
  111. }
  112. /*********************************************************************
  113. * PUBLIC FUNCTIONS
  114. */
  115. /**
  116. @brief 初始化ADC引脚和通道
  117. @param pin -[in] 需要初始化的adc引脚
  118. @param channel -[in] 需要初始化的adc通道
  119. @return 错误代码
  120. */
  121. uint32_t ADC_SetPinChannel(uint32_t pin, uint32_t channel,nrf_gpio_pin_pull_t pin_pull)
  122. {
  123. ret_code_t errCode;
  124. //清除已存在的引脚和通道
  125. ADC_RemovePinChannel(pin, channel);
  126. for(int i=0;i<CHANNEL_MAX;i++)
  127. {
  128. if(m_adc_config[i].pin == PIN_NOT_USED && m_adc_config[i].channel == CHANNEL_NOT_USED && PIN_NOT_USED != pin)
  129. {
  130. //不关闭总开关是不能关闭通道的
  131. ADC_Disable();
  132. m_adc_config[i].pin = pin;
  133. m_adc_config[i].channel = channel;
  134. //保留之前的引脚配置,一旦就失败复原
  135. nrf_gpio_pin_dir_t dir = nrf_gpio_pin_dir_get(m_adc_config[i].pin);
  136. //设置ADC引脚为浮空
  137. nrf_gpio_cfg_input(m_adc_config[i].pin,pin_pull);
  138. // 单端输入
  139. channelConfig[m_adc_config[i].channel].resistor_p = NRF_SAADC_RESISTOR_DISABLED;
  140. channelConfig[m_adc_config[i].channel].resistor_n = NRF_SAADC_RESISTOR_DISABLED;
  141. channelConfig[m_adc_config[i].channel].gain = NRF_SAADC_GAIN1_6;
  142. channelConfig[m_adc_config[i].channel].reference = NRF_SAADC_REFERENCE_INTERNAL;
  143. channelConfig[m_adc_config[i].channel].acq_time = NRF_SAADC_ACQTIME_10US;
  144. channelConfig[m_adc_config[i].channel].mode = NRF_SAADC_MODE_SINGLE_ENDED;
  145. channelConfig[m_adc_config[i].channel].burst = NRF_SAADC_BURST_DISABLED;
  146. channelConfig[m_adc_config[i].channel].pin_p = (nrf_saadc_input_t)(m_adc_config[i].channel + 1);
  147. channelConfig[m_adc_config[i].channel].pin_n = NRF_SAADC_INPUT_DISABLED;
  148. // ADC通道初始化
  149. errCode = nrf_drv_saadc_channel_init(m_adc_config[i].channel, &channelConfig[m_adc_config[i].channel]);
  150. if(errCode != NRF_SUCCESS)
  151. {
  152. channelConfig[m_adc_config[i].channel].pin_p = NRF_SAADC_INPUT_DISABLED;
  153. m_adc_config[i].pin = PIN_NOT_USED;
  154. m_adc_config[i].channel = CHANNEL_NOT_USED;
  155. nrf_gpio_pin_dir_set(m_adc_config[i].pin, dir);
  156. ADC_Enable();
  157. return ADC_ERR_INIT_CONGESTION;
  158. }
  159. sample_in_buffer++;
  160. ADC_Enable();
  161. return ADC_OP_SUCCESS;
  162. }
  163. }
  164. return ADC_ERR_INIT_CONGESTION;
  165. }
  166. /**
  167. @brief 移除已初始化ADC引脚和通道
  168. @param pin -[in] 需要移除的adc引脚
  169. @param channel -[in] 需要移除的adc通道
  170. @return 错误代码
  171. */
  172. uint32_t ADC_RemovePinChannel(uint32_t pin, uint32_t channel)
  173. {
  174. for(int i=0;i<CHANNEL_MAX;i++)
  175. {
  176. if(m_adc_config[i].pin == pin && m_adc_config[i].channel == channel)
  177. {
  178. nrf_drv_saadc_channel_uninit(m_adc_config[i].channel);
  179. channelConfig[m_adc_config[i].channel].pin_p = NRF_SAADC_INPUT_DISABLED;
  180. m_adc_config[i].pin = PIN_NOT_USED;
  181. m_adc_config[i].channel = CHANNEL_NOT_USED;
  182. if(sample_in_buffer != 0)sample_in_buffer--;
  183. return ADC_OP_SUCCESS;
  184. }
  185. }
  186. return ADC_ERR_REMOVE_PIN_CHANNEL;
  187. }
  188. /**
  189. @brief ADC触发采样
  190. @param cb -[in]通知回调
  191. @return 错误代码
  192. */
  193. uint32_t ADC_Sample(bsp_adc_sample_notify_cb cb)
  194. {
  195. uint32_t errCode;
  196. if(cb != NULL)
  197. {
  198. //没有配置通道返回失败
  199. if(sample_in_buffer == 0)return ADC_ERR_READ_NO_CHANNEL;
  200. ADC_Disable();
  201. ADC_Enable();
  202. errCode = nrf_drv_saadc_sample();
  203. if(errCode != NRF_SUCCESS)return ADC_ERR_TRIGGER_FAIL;
  204. m_cb = cb;
  205. return ADC_OP_SUCCESS;
  206. }
  207. return ADC_ERR_TRIGGER_FAIL;
  208. }
  209. /**
  210. @brief 开启ADC,与初始化没有区别,为了与Disable成对出现
  211. @param 无
  212. @return 无
  213. */
  214. void ADC_Enable(void)
  215. {
  216. ADC_Init();
  217. }
  218. /**
  219. @brief 禁用ADC
  220. @param 无
  221. @return 无
  222. */
  223. void ADC_Disable(void)
  224. {
  225. nrf_drv_saadc_uninit();
  226. }