fml_adc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*Includes ----------------------------------------------*/
  2. #include "tool.h"
  3. #include "nrf_delay.h"
  4. #include "bsp_adc.h"
  5. #include "bsp_time.h"
  6. #include "exception.h"
  7. #include "system.h"
  8. #include "fml_adc.h"
  9. /*Private macro ------------------------------------------------------------------------------------------------------------------------------------*/
  10. /*STRUCTION ------------------------------------------------------------------------------------------------------------------------------------*/
  11. typedef struct _fml_adc
  12. {
  13. int16_t adc_value[CHANNEL_MAX]; //所有通道的adc采样值
  14. uint32_t sample_point; //采样点
  15. uint32_t switch_sample_point; //转换采样点
  16. uint32_t trigger_base_point; //触发基点
  17. fml_adc_sample_update_notify_callback m_notify_cb; //采样更新通知回调
  18. } Fml_Adc_t;
  19. /*Local Variable ------------------------------------------------------------------------------------------------------------------------------------*/
  20. static Fml_Adc_t ob_fml_adc;
  21. /*Local Functions ------------------------------------------------------------------------------------------------------------------------------------*/
  22. static void fml_adc_samplecallback(int16_t *newset_adc_value)
  23. {
  24. Fml_Adc_All_Channel_Adc_Value_t cur_all_adc_value;
  25. cur_all_adc_value.channel_0_adc_value = ob_fml_adc.adc_value[0] = newset_adc_value[0];
  26. cur_all_adc_value.channel_1_adc_value = ob_fml_adc.adc_value[1] = newset_adc_value[1];
  27. cur_all_adc_value.channel_2_adc_value = ob_fml_adc.adc_value[2] = newset_adc_value[2];
  28. cur_all_adc_value.channel_3_adc_value = ob_fml_adc.adc_value[3] = newset_adc_value[3];
  29. cur_all_adc_value.channel_4_adc_value = ob_fml_adc.adc_value[4] = newset_adc_value[4];
  30. cur_all_adc_value.channel_5_adc_value = ob_fml_adc.adc_value[5] = newset_adc_value[5];
  31. cur_all_adc_value.channel_6_adc_value = ob_fml_adc.adc_value[6] = newset_adc_value[6];
  32. cur_all_adc_value.channel_7_adc_value = ob_fml_adc.adc_value[7] = newset_adc_value[7];
  33. if(ob_fml_adc.m_notify_cb != NULL)
  34. {
  35. ob_fml_adc.m_notify_cb(ob_fml_adc.sample_point,cur_all_adc_value);
  36. }
  37. }
  38. static void fml_adc_timecallback(void* t)
  39. {
  40. uint32_t cur_time = NRF_RTC0->COUNTER;
  41. if(cur_time<ob_fml_adc.trigger_base_point) cur_time += 16777216;
  42. if((cur_time - ob_fml_adc.trigger_base_point)/32.768 >= ob_fml_adc.switch_sample_point)
  43. {
  44. //更新触发基点
  45. ob_fml_adc.trigger_base_point = NRF_RTC0->COUNTER;
  46. //更新采样点
  47. ob_fml_adc.sample_point = ob_fml_adc.switch_sample_point;
  48. //触发采样
  49. ADC_Sample(fml_adc_samplecallback);
  50. //设置下次触发采样时间点
  51. ob_fml_adc.switch_sample_point = (ob_fml_adc.switch_sample_point == FML_ADC_SAMPLE_POINT_1)?FML_ADC_SAMPLE_POINT_2:FML_ADC_SAMPLE_POINT_1;
  52. }
  53. }
  54. static void cb_adcWakeup(uint32_t t)
  55. {
  56. ADC_Enable();
  57. //更新触发基点
  58. ob_fml_adc.trigger_base_point = NRF_RTC0->COUNTER;
  59. //唤醒采样一次
  60. ob_fml_adc.sample_point = FML_ADC_SAMPLE_POINT_0;
  61. ADC_Sample(fml_adc_samplecallback);
  62. //设置下次触发采样时间点
  63. ob_fml_adc.switch_sample_point = FML_ADC_SAMPLE_POINT_1;
  64. //注册定时器1毫秒回调函数
  65. TIME_Regist(fml_adc_timecallback);
  66. }
  67. static void cb_adcSleep(uint32_t t)
  68. {
  69. ADC_Disable();
  70. TIME_UnRegist(fml_adc_timecallback);
  71. }
  72. /*API ------------------------------------------------------------------------------------------------------------------------------------*/
  73. /**
  74. @brief 初始化adc功能模块
  75. @param 无
  76. @return 错误代码 - [out] -1失败,0成功
  77. */
  78. int fml_adc_Init(void)
  79. {
  80. //重置结构体
  81. memset(&ob_fml_adc,0,sizeof(ob_fml_adc));
  82. ADC_SetPinChannel(PIN_ADC_CHARGMEASURE, PIN_ADC_CHARGMEASURE_CHANNEL,NRF_GPIO_PIN_NOPULL);
  83. ADC_SetPinChannel(PIN_ADC_BAT_IN, PIN_ADC_BAT_CHANNEL,NRF_GPIO_PIN_NOPULL);
  84. Wakeup_Regist(cb_adcWakeup);
  85. Sleep_Regist(cb_adcSleep);
  86. cb_adcWakeup(1);
  87. return 0;
  88. }
  89. /**
  90. @brief adc采样更新通知回调注册
  91. @param cb - [in] 回调函数注册
  92. @return 错误代码 - [out] -1失败,0成功
  93. */
  94. int fml_adc_sample_update_notify_register(fml_adc_sample_update_notify_callback cb)
  95. {
  96. if(cb != NULL)
  97. {
  98. ob_fml_adc.m_notify_cb = cb;
  99. return 0;
  100. }
  101. return -1;
  102. }
  103. /**
  104. @brief adc读值
  105. @param channel - [in] 需要读取的通道
  106. @param value - [out] 返回通道的值
  107. @return 错误代码 - [out] -1失败,0成功
  108. */
  109. int fml_adc_get_value(uint32_t channel,int16_t *value)
  110. {
  111. if(channel >= CHANNEL_MAX)
  112. {
  113. return -1;
  114. }
  115. else
  116. {
  117. *value = ob_fml_adc.adc_value[channel];
  118. }
  119. return 0;
  120. }
  121. /**
  122. @brief 设置ADC通道
  123. @param pin - [in] 需要设置的引脚
  124. @param channel - [in] 需要设置的通道
  125. @param pin_pull - [in] 需要设置的通道上下拉
  126. @return 错误代码 - [out] -1失败,0成功
  127. */
  128. int fml_adc_set_pin_channel(uint32_t pin, uint32_t channel, nrf_gpio_pin_pull_t pin_pull)
  129. {
  130. uint32_t err;
  131. err = ADC_SetPinChannel(pin, channel, pin_pull);
  132. if(err != ADC_OP_SUCCESS)return -1;
  133. return 0;
  134. }
  135. /**
  136. @brief 移除ADC通道
  137. @param pin - [in] 需要移除的引脚
  138. @param channel - [in] 需要移除的通道
  139. @return 错误代码 - [out] -1失败,0成功
  140. */
  141. int fml_adc_remove_pin_channel(uint32_t pin, uint32_t channel)
  142. {
  143. uint32_t err;
  144. err = ADC_RemovePinChannel(pin, channel);
  145. if(err != ADC_OP_SUCCESS)return -1;
  146. return 0;
  147. }