bsp_adc.c 6.0 KB

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