hal_charge.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #if ONE_WIRE_ENABLE
  58. pair_line_t CHARGE_state = app_chargepin_pairline();
  59. if(CHARGE != CHARGE_state){ //没充电
  60. if(1 == pair_state)return;//配对不检测充电
  61. #else
  62. int16_t charge_vol = 0;
  63. if(-1 ==fml_adc_get_value(PIN_CHARGING_CHANNEL,&charge_vol))return;
  64. if(charge_vol < 1200){ //没充电
  65. #endif
  66. if(hal_charge_state!=BLE_CHARGE_PULLOUT){ DEBUG_LOG("charge out...\n");
  67. hal_charge_state = BLE_CHARGE_PULLOUT;
  68. Process_SetHoldOn(hal_charge_Process,0);
  69. hal_send_charge();
  70. }
  71. return;
  72. }
  73. if(GetBatteryPersent() >=100){
  74. if(hal_charge_state!=BLE_CHARGE_DONE){ DEBUG_LOG("charge done:...\n");
  75. hal_charge_state = BLE_CHARGE_DONE;
  76. Process_SetHoldOn(hal_charge_Process,1);
  77. hal_send_charge();
  78. }
  79. }else{ //正在充电
  80. if(hal_charge_state!=BLE_CHARGE_INSERT){ DEBUG_LOG("charge in...\n");
  81. hal_charge_state = BLE_CHARGE_INSERT;
  82. Process_SetHoldOn(hal_charge_Process,1);
  83. hal_send_charge();
  84. }
  85. }
  86. }
  87. //static void hal_charge_monitor_process(void)
  88. //{
  89. // #define BATTERY_VOL_THRESHOLD_MIN 2500 //2.5V
  90. // #define BATTERY_VOL_THRESHOLD_MAX 4000 //4V
  91. // #define CHARGE_VOL_THRESHOLD 80 //充电前和充电期间的充电电压变化值,单位mv
  92. //
  93. // //监测电池和充电数据
  94. // int16_t charge_vol = 0;
  95. //// int16_t charge_threshold = 0;
  96. // static uint32_t charge_cycle = 50;
  97. // static int16_t before_charge_vol = 0; //充电前的电压值
  98. // static int16_t charge_vol_max = 0; //充电期间最大的电压值
  99. // int16_t adcval =0;
  100. // if(BLE_CHARGE_PULLOUT == hal_charge_state)//没充电
  101. // {
  102. // if(-1 ==fml_adc_get_value(PIN_ADC_CHARGMEASURE_CHANNEL,&adcval))return;
  103. // charge_vol = ADC_RESULT_IN_MILLI_VOLTS(adcval);
  104. // before_charge_vol = before_charge_vol > charge_vol ? charge_vol : before_charge_vol;
  105. // charge_vol_max = 0;
  106. // charge_cycle = 50;
  107. // }else //充电
  108. // {
  109. // /* 过筛50轮 */
  110. // if(charge_cycle != 0){
  111. // if(-1 ==fml_adc_get_value(PIN_ADC_CHARGMEASURE_CHANNEL,&adcval))return;
  112. // charge_vol = ADC_RESULT_IN_MILLI_VOLTS(adcval);
  113. // charge_vol_max = charge_vol_max < charge_vol ? charge_vol : charge_vol_max;
  114. // charge_cycle--;
  115. // return;
  116. // }
  117. // if(-1 == fml_adc_get_value(PIN_ADC_CHARGMEASURE_CHANNEL,&adcval))return;
  118. // charge_vol = ADC_RESULT_IN_MILLI_VOLTS(adcval);
  119. // charge_vol_max = charge_vol_max < charge_vol ? charge_vol : charge_vol_max;
  120. //// bat_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_BAT_CHANNEL))*5/3;
  121. //
  122. // //当电池电量没满,充电(经测试,电量没满的充电电压跟电池电压有关,最小充电电压100+mv)
  123. //// charge_threshold = charge_vol_max - before_charge_vol; //充电前和充电期间的充电电压变化值
  124. // }
  125. //}
  126. void hal_charge_init(void)
  127. {
  128. BLE_Client_Rx_Regist(BLE_CHARGE,cb_BLE_Client_R_CHARGE);
  129. BLE_Host_Rx_Regist(BLE_CHARGE,cb_BLE_Host_R_CHARGE);
  130. Process_Start(1000,"charge",hal_charge_Process);
  131. hal_charge_Process();
  132. // Process_Start(10,"charge_monitor",hal_charge_monitor_process);
  133. }