hal_charge.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include "usr_config.h"
  2. #include "bsp_time.h"
  3. #include "system.h"
  4. #include "hal_led.h"
  5. #include "nrf_gpio.h"
  6. #include "hal_ble_client.h"
  7. #include "hal_ble_host.h"
  8. #include "app_flash.h"
  9. #include "hal_battery.h"
  10. #include "ble_comm.h"
  11. #include "hal_ble_uart0.h"
  12. #include "app_pair_chargerpin.h"
  13. #include "hal_charge.h"
  14. /************************ 函数声明 ***********************************/
  15. /************************ 变量 ***********************************/
  16. static uint8_t hal_charge_state = BLE_CHARGE_PULLOUT;
  17. static uint8_t pair_state =0;
  18. static void pair_start_cb(){
  19. hal_charge_state = BLE_CHARGE_PULLOUT;
  20. pair_state =1;
  21. }
  22. static void pair_done_cb(){
  23. pair_state =0;
  24. }
  25. PAIR_START_REGISTER(pair_start_cb);
  26. PAIR_DONE_REGISTER(pair_done_cb);
  27. /********************************************************/
  28. uint8_t hal_charge_Getstate(void)
  29. {
  30. return hal_charge_state;
  31. }
  32. static void hal_send_charge(void)
  33. {
  34. uint8_t sbuf[2]={0};
  35. uint8_t L = 0;
  36. if(mFlash.isHost) sbuf[L++] = 0;
  37. else sbuf[L++] = 1;
  38. sbuf[L++] = hal_charge_state;
  39. BLE_Client_Tx_Send(0,BLE_CHARGE,sbuf,L);
  40. }
  41. void cb_BLE_Client_R_CHARGE(void* handle)
  42. {
  43. BLE_Client_Rx_t* target = handle;
  44. DEBUG_LOG("!!!!!!!!cb_BLE_Client_R_CHARGE\n");
  45. hal_send_charge();
  46. BLE_Host_Tx_Send(0,BLE_CHARGE,target->pDat,target->datLen);
  47. }
  48. void cb_BLE_Host_R_CHARGE(void* handle)
  49. {
  50. BLE_Host_Rx_t* target = handle;
  51. BLE_Client_Tx_Send(0,BLE_CHARGE,target->pDat,target->datLen);
  52. // DEBUG_LOG("cb_BLE_Host_R_CHARGE:%d,%d,%d\n",target->pDat[0],target->pDat[1],target->datLen);
  53. }
  54. static void hal_charge_Vol_Charge_Send(void)
  55. {
  56. uint8_t buf[10]={0};
  57. uint8_t L = 0;
  58. buf[L++] = GetBatteryPersent();
  59. buf[L++] = hal_charge_state;
  60. if(mFlash.isHost)BLE_Host_Tx_Send(0,BLE_CHAR_VOL,buf,L);
  61. else BLE_Client_Tx_Send(0,BLE_CHAR_VOL,buf,L);
  62. }
  63. static uint8_t OtherShoesVol = 0;
  64. static uint8_t otherShoes_charge = BLE_CHARGE_PULLOUT;
  65. static void cb_BLE_R_CHAR_VOL(void* handle)
  66. {
  67. UART0_Rx_t* target = handle;
  68. OtherShoesVol = target->pDat[0];
  69. otherShoes_charge = target->pDat[1];
  70. if(!mFlash.isHost)hal_charge_Vol_Charge_Send();
  71. // DEBUG_LOG("OtherShoesVol=%d\n",OtherShoesVol);
  72. }
  73. static void cb_BLE_Host_R_CHAR_VOL(void* handle)
  74. {
  75. UART0_Rx_t* target = handle;
  76. OtherShoesVol = target->pDat[0];
  77. otherShoes_charge = target->pDat[1];
  78. // DEBUG_LOG("OtherShoesVol=%d\n",OtherShoesVol);
  79. }
  80. static void hal_charge_Process(void)
  81. {
  82. static uint8_t client_connect =0;
  83. if(slave_isconnect() && !client_connect){
  84. hal_send_charge();
  85. uint8_t data =0;
  86. BLE_Host_Tx_Send(0,BLE_CHARGE,&data,1);
  87. }
  88. else if(!slave_isconnect() && client_connect && (!mFlash.isHost)){
  89. OtherShoesVol = 0;
  90. otherShoes_charge =0;
  91. }
  92. if(client_connect != slave_isconnect())client_connect = slave_isconnect();
  93. int16_t ADC_value =0;
  94. ADC_Read(PIN_CHARGING_CHANNEL,&ADC_value);
  95. if(1 == pair_state || ADC_value < CHARGE_ADC){ //没充电
  96. if(hal_charge_state!=BLE_CHARGE_PULLOUT){ DEBUG_LOG("charge out...\n");
  97. hal_charge_state = BLE_CHARGE_PULLOUT;
  98. Process_SetHoldOn(hal_charge_Process,0);
  99. hal_send_charge();
  100. OtherShoesVol = 0;
  101. otherShoes_charge =0;
  102. }
  103. return;
  104. }
  105. if(mFlash.isHost)hal_charge_Vol_Charge_Send(); //共享充电电量和充电状态
  106. if((otherShoes_charge == BLE_CHARGE_INSERT || otherShoes_charge == BLE_CHARGE_DONE) &&
  107. GetBatteryPersent() >=100 &&
  108. OtherShoesVol >=100 ){ //左右鞋都充满电的情况下
  109. if(hal_charge_state!=BLE_CHARGE_DONE){ DEBUG_LOG("charge done:%d...\n",OtherShoesVol);
  110. hal_charge_state = BLE_CHARGE_DONE;
  111. Process_SetHoldOn(hal_charge_Process,1);
  112. hal_send_charge();
  113. }
  114. }else{ //正在充电
  115. if(hal_charge_state!=BLE_CHARGE_INSERT){ DEBUG_LOG("charge in...\n");
  116. hal_charge_state = BLE_CHARGE_INSERT;
  117. Process_SetHoldOn(hal_charge_Process,1);
  118. hal_send_charge();
  119. }
  120. }
  121. }
  122. static void hal_charge_monitor_process(void)
  123. {
  124. #define BATTERY_VOL_THRESHOLD_MIN 2500 //2.5V
  125. #define BATTERY_VOL_THRESHOLD_MAX 4000 //4V
  126. #define CHARGE_VOL_THRESHOLD 80 //充电前和充电期间的充电电压变化值,单位mv
  127. //监测电池和充电数据
  128. int16_t charge_vol = 0;
  129. // int16_t charge_threshold = 0;
  130. static uint32_t charge_cycle = 50;
  131. static int16_t before_charge_vol = 0; //充电前的电压值
  132. static int16_t charge_vol_max = 0; //充电期间最大的电压值
  133. if(BLE_CHARGE_PULLOUT == hal_charge_state)//没充电
  134. {
  135. charge_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL));
  136. before_charge_vol = before_charge_vol > charge_vol ? charge_vol : before_charge_vol;
  137. charge_vol_max = 0;
  138. charge_cycle = 50;
  139. }else //充电
  140. {
  141. /* 过筛50轮 */
  142. if(charge_cycle != 0){
  143. charge_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL));
  144. charge_vol_max = charge_vol_max < charge_vol ? charge_vol : charge_vol_max;
  145. charge_cycle--;
  146. return;
  147. }
  148. charge_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL));
  149. charge_vol_max = charge_vol_max < charge_vol ? charge_vol : charge_vol_max;
  150. // bat_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_BAT_CHANNEL))*5/3;
  151. //当电池电量没满,充电(经测试,电量没满的充电电压跟电池电压有关,最小充电电压100+mv)
  152. // charge_threshold = charge_vol_max - before_charge_vol; //充电前和充电期间的充电电压变化值
  153. }
  154. }
  155. void hal_charge_init(void)
  156. {
  157. BLE_Client_Rx_Regist(BLE_CHAR_VOL,cb_BLE_R_CHAR_VOL);
  158. BLE_Host_Rx_Regist(BLE_CHAR_VOL,cb_BLE_Host_R_CHAR_VOL);
  159. BLE_Client_Rx_Regist(BLE_CHARGE,cb_BLE_Client_R_CHARGE);
  160. BLE_Host_Rx_Regist(BLE_CHARGE,cb_BLE_Host_R_CHARGE);
  161. Process_Start(1000,"app_charge",hal_charge_Process);
  162. hal_charge_Process();
  163. Process_Start(10,"hal_charge_monitor_process",hal_charge_monitor_process);
  164. }