hal_charge.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "app_chargerpin_conn_detect.h"
  2. #include "usr_config.h"
  3. #include "bsp_time.h"
  4. #include "system.h"
  5. #include "hal_led.h"
  6. #include "nrf_gpio.h"
  7. #include "hal_ble_client.h"
  8. #include "hal_ble_host.h"
  9. #include "app_flash.h"
  10. #include "hal_battery.h"
  11. #include "ble_comm.h"
  12. #include "app_pair_chargerpin.h"
  13. #include "hal_charge.h"
  14. #include "fml_adc.h"
  15. /************************ 函数声明 ***********************************/
  16. /************************ 变量 ***********************************/
  17. static uint8_t hal_charge_state = BLE_CHARGE_PULLOUT;
  18. static uint8_t pair_state =0;
  19. static void pair_start_cb(){
  20. hal_charge_state = BLE_CHARGE_PULLOUT;
  21. pair_state =1;
  22. }
  23. static void pair_done_cb(){
  24. pair_state =0;
  25. }
  26. PAIR_START_REGISTER(pair_start_cb);
  27. PAIR_DONE_REGISTER(pair_done_cb);
  28. /********************************************************/
  29. uint8_t hal_charge_Getstate(void)
  30. {
  31. return hal_charge_state;
  32. }
  33. static void hal_send_charge(void)
  34. {
  35. uint8_t sbuf[2]={0};
  36. uint8_t L = 0;
  37. if(mFlash.isHost) sbuf[L++] = 0;
  38. else sbuf[L++] = 1;
  39. sbuf[L++] = hal_charge_state;
  40. BLE_Client_Tx_Send(0,BLE_CHARGE,sbuf,L);
  41. }
  42. void cb_BLE_Client_R_CHARGE(void* handle)
  43. {
  44. BLE_Client_Rx_t* target = handle;
  45. DEBUG_LOG("!!!!!!!!cb_BLE_Client_R_CHARGE\n");
  46. hal_send_charge();
  47. BLE_Host_Tx_Send(0,BLE_CHARGE,target->pDat,target->datLen);
  48. }
  49. void cb_BLE_Host_R_CHARGE(void* handle)
  50. {
  51. BLE_Host_Rx_t* target = handle;
  52. BLE_Client_Tx_Send(0,BLE_CHARGE,target->pDat,target->datLen);
  53. // DEBUG_LOG("cb_BLE_Host_R_CHARGE:%d,%d,%d\n",target->pDat[0],target->pDat[1],target->datLen);
  54. }
  55. static void hal_charge_Process(void)
  56. {
  57. pair_line_t CHARGE_state = app_chargepin_pairline();
  58. if(1 == pair_state)return;//配对不检测充电
  59. if(CHARGE != CHARGE_state){ //没充电
  60. if(hal_charge_state!=BLE_CHARGE_PULLOUT){ DEBUG_LOG("charge out...\n");
  61. hal_charge_state = BLE_CHARGE_PULLOUT;
  62. Process_SetHoldOn(hal_charge_Process,0);
  63. hal_send_charge();
  64. }
  65. return;
  66. }
  67. if(GetBatteryPersent() >=100){
  68. if(hal_charge_state!=BLE_CHARGE_DONE){ DEBUG_LOG("charge done:...\n");
  69. hal_charge_state = BLE_CHARGE_DONE;
  70. Process_SetHoldOn(hal_charge_Process,1);
  71. hal_send_charge();
  72. }
  73. }else{ //正在充电
  74. if(hal_charge_state!=BLE_CHARGE_INSERT){ DEBUG_LOG("charge in...\n");
  75. hal_charge_state = BLE_CHARGE_INSERT;
  76. Process_SetHoldOn(hal_charge_Process,1);
  77. hal_send_charge();
  78. }
  79. }
  80. }
  81. static void hal_charge_monitor_process(void)
  82. {
  83. #define BATTERY_VOL_THRESHOLD_MIN 2500 //2.5V
  84. #define BATTERY_VOL_THRESHOLD_MAX 4000 //4V
  85. #define CHARGE_VOL_THRESHOLD 80 //充电前和充电期间的充电电压变化值,单位mv
  86. //监测电池和充电数据
  87. int16_t charge_vol = 0;
  88. // int16_t charge_threshold = 0;
  89. static uint32_t charge_cycle = 50;
  90. static int16_t before_charge_vol = 0; //充电前的电压值
  91. static int16_t charge_vol_max = 0; //充电期间最大的电压值
  92. int16_t adcval =0;
  93. if(BLE_CHARGE_PULLOUT == hal_charge_state)//没充电
  94. {
  95. if(-1 ==fml_adc_get_value(PIN_ADC_CHARGMEASURE_CHANNEL,&adcval))return;
  96. charge_vol = ADC_RESULT_IN_MILLI_VOLTS(adcval);
  97. before_charge_vol = before_charge_vol > charge_vol ? charge_vol : before_charge_vol;
  98. charge_vol_max = 0;
  99. charge_cycle = 50;
  100. }else //充电
  101. {
  102. /* 过筛50轮 */
  103. if(charge_cycle != 0){
  104. if(-1 ==fml_adc_get_value(PIN_ADC_CHARGMEASURE_CHANNEL,&adcval))return;
  105. charge_vol = ADC_RESULT_IN_MILLI_VOLTS(adcval);
  106. charge_vol_max = charge_vol_max < charge_vol ? charge_vol : charge_vol_max;
  107. charge_cycle--;
  108. return;
  109. }
  110. if(-1 == fml_adc_get_value(PIN_ADC_CHARGMEASURE_CHANNEL,&adcval))return;
  111. charge_vol = ADC_RESULT_IN_MILLI_VOLTS(adcval);
  112. charge_vol_max = charge_vol_max < charge_vol ? charge_vol : charge_vol_max;
  113. // bat_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_BAT_CHANNEL))*5/3;
  114. //当电池电量没满,充电(经测试,电量没满的充电电压跟电池电压有关,最小充电电压100+mv)
  115. // charge_threshold = charge_vol_max - before_charge_vol; //充电前和充电期间的充电电压变化值
  116. }
  117. }
  118. void hal_charge_init(void)
  119. {
  120. BLE_Client_Rx_Regist(BLE_CHARGE,cb_BLE_Client_R_CHARGE);
  121. BLE_Host_Rx_Regist(BLE_CHARGE,cb_BLE_Host_R_CHARGE);
  122. Process_Start(1000,"charge",hal_charge_Process);
  123. hal_charge_Process();
  124. Process_Start(10,"charge_monitor",hal_charge_monitor_process);
  125. }