hal_charge.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #include "app_one_wire.h"
  16. /************************ 函数声明 ***********************************/
  17. /************************ 变量 ***********************************/
  18. static uint8_t hal_charge_state = BLE_CHARGE_PULLOUT;
  19. //static uint8_t pair_state =0;
  20. //static void pair_start_cb(){
  21. // hal_charge_state = BLE_CHARGE_PULLOUT;
  22. // pair_state =1;
  23. //}
  24. //static void pair_done_cb(){
  25. // pair_state =0;
  26. //}
  27. //PAIR_START_REGISTER(pair_start_cb);
  28. //PAIR_DONE_REGISTER(pair_done_cb);
  29. /********************************************************/
  30. uint8_t hal_charge_Getstate(void)
  31. {
  32. return hal_charge_state;
  33. }
  34. static void hal_send_charge(void)
  35. {
  36. uint8_t sbuf[2]={0};
  37. uint8_t L = 0;
  38. if(mFlash.isHost) sbuf[L++] = 0;
  39. else sbuf[L++] = 1;
  40. sbuf[L++] = hal_charge_state;
  41. BLE_Client_Tx_Send(0,BLE_CHARGE,sbuf,L);
  42. }
  43. void cb_BLE_Client_R_CHARGE(void* handle)
  44. {
  45. BLE_Client_Rx_t* target = handle;
  46. DEBUG_LOG("!!!!!!!!cb_BLE_Client_R_CHARGE\n");
  47. hal_send_charge();
  48. BLE_Host_Tx_Send(0,BLE_CHARGE,target->pDat,target->datLen);
  49. }
  50. void cb_BLE_Host_R_CHARGE(void* handle)
  51. {
  52. BLE_Host_Rx_t* target = handle;
  53. BLE_Client_Tx_Send(0,BLE_CHARGE,target->pDat,target->datLen);
  54. // DEBUG_LOG("cb_BLE_Host_R_CHARGE:%d,%d,%d\n",target->pDat[0],target->pDat[1],target->datLen);
  55. }
  56. extern void selfcheck_trigger_callback(char order);
  57. unsigned char recbuf[4];
  58. static char selfcheck_trigger_ok=0;
  59. static unsigned int ms_cnt=0;
  60. __IO char tsfd=0;
  61. void wait_times(void)
  62. {
  63. ms_cnt++;
  64. if(tsfd)
  65. {
  66. tsfd=0;
  67. selfcheck_trigger_callback((char)recbuf[0]);
  68. }
  69. if(ms_cnt<1000)return;
  70. // SEGGER_RTT_printf(0,"wait_times out...\n");
  71. Process_Stop(wait_times);
  72. one_byte_receive_uninit();
  73. ms_cnt=0;selfcheck_trigger_ok=0;
  74. // fml_adc_set_pin_channel(PIN_CHARGING, PIN_CHARGING_CHANNEL, NRF_GPIO_PIN_NOPULL);
  75. }
  76. void Event_self_check(unsigned char *buf,int length)
  77. {
  78. tsfd=1;
  79. // SEGGER_RTT_printf(0,"Event_self_check out... %x\n",buf[0]);
  80. }
  81. void open_one_shel(void)
  82. {
  83. if(selfcheck_trigger_ok)return;
  84. selfcheck_trigger_ok=1;
  85. one_byte_receive_init(recbuf,2,Event_self_check);
  86. // SEGGER_RTT_printf(0,"open_one_shel in...\n");
  87. Process_Start(1, "wait_times", wait_times);
  88. Process_SetHoldOn(wait_times, 1);
  89. }
  90. static void hal_charge_Process(void)
  91. {
  92. #if ONE_WIRE_ENABLE
  93. pair_line_t CHARGE_state = app_chargepin_pairline();
  94. if(CHARGE != CHARGE_state){ //没充电
  95. if(1 == pair_state)return;//配对不检测充电
  96. #else
  97. uint32_t ch = nrf_gpio_pin_read(PIN_CHARGING);
  98. if(!ch){ //没充电
  99. #endif
  100. if(hal_charge_state!=BLE_CHARGE_PULLOUT){ DEBUG_LOG("charge out...\n");
  101. hal_charge_state = BLE_CHARGE_PULLOUT;
  102. Process_SetHoldOn(hal_charge_Process,0);
  103. hal_send_charge();
  104. }
  105. return;
  106. }
  107. open_one_shel();
  108. if(GetBatteryPersent() >=100){
  109. if(hal_charge_state!=BLE_CHARGE_DONE){ DEBUG_LOG("charge done:...\n");
  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. void hal_charge_init(void)
  123. {
  124. nrf_gpio_cfg_input(PIN_CHARGING,NRF_GPIO_PIN_NOPULL);
  125. BLE_Client_Rx_Regist(BLE_CHARGE,cb_BLE_Client_R_CHARGE);
  126. BLE_Host_Rx_Regist(BLE_CHARGE,cb_BLE_Host_R_CHARGE);
  127. Process_Start(1000,"charge",hal_charge_Process);
  128. hal_charge_Process();
  129. // Process_Start(10,"charge_monitor",hal_charge_monitor_process);
  130. }