fml_adc.c 5.4 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. // ADC_SetPinChannel(PIN_CHARGING, PIN_CHARGING_CHANNEL,NRF_GPIO_PIN_NOPULL);
  85. Wakeup_Regist(cb_adcWakeup);
  86. Sleep_Regist(cb_adcSleep);
  87. cb_adcWakeup(1);
  88. return 0;
  89. }
  90. /**
  91. @brief adc采样更新通知回调注册
  92. @param cb - [in] 回调函数注册
  93. @return 错误代码 - [out] -1失败,0成功
  94. */
  95. int fml_adc_sample_update_notify_register(fml_adc_sample_update_notify_callback cb)
  96. {
  97. if(cb != NULL)
  98. {
  99. ob_fml_adc.m_notify_cb = cb;
  100. return 0;
  101. }
  102. return -1;
  103. }
  104. /**
  105. @brief adc读值
  106. @param channel - [in] 需要读取的通道
  107. @param value - [out] 返回通道的值
  108. @return 错误代码 - [out] -1失败,0成功
  109. */
  110. int fml_adc_get_value(uint32_t channel,int16_t *value)
  111. {
  112. if(channel >= CHANNEL_MAX)
  113. {
  114. return -1;
  115. }
  116. else
  117. {
  118. *value = ob_fml_adc.adc_value[channel];
  119. }
  120. return 0;
  121. }
  122. /**
  123. @brief 设置ADC通道
  124. @param pin - [in] 需要设置的引脚
  125. @param channel - [in] 需要设置的通道
  126. @param pin_pull - [in] 需要设置的通道上下拉
  127. @return 错误代码 - [out] -1失败,0成功
  128. */
  129. int fml_adc_set_pin_channel(uint32_t pin, uint32_t channel, nrf_gpio_pin_pull_t pin_pull)
  130. {
  131. uint32_t err;
  132. err = ADC_SetPinChannel(pin, channel, pin_pull);
  133. if(err != ADC_OP_SUCCESS)return -1;
  134. return 0;
  135. }
  136. /**
  137. @brief 移除ADC通道
  138. @param pin - [in] 需要移除的引脚
  139. @param channel - [in] 需要移除的通道
  140. @return 错误代码 - [out] -1失败,0成功
  141. */
  142. int fml_adc_remove_pin_channel(uint32_t pin, uint32_t channel)
  143. {
  144. uint32_t err;
  145. err = ADC_RemovePinChannel(pin, channel);
  146. if(err != ADC_OP_SUCCESS)return -1;
  147. return 0;
  148. }