app_power.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "app_power.h"
  2. #include "nrf_gpio.h"
  3. #include "usr_config.h"
  4. #include "bsp_time.h"
  5. #include "system.h"
  6. #include "hal_mt.h"
  7. #include "hal_battery.h"
  8. #include "hal_charge.h"
  9. #include "hal_ble_client.h"
  10. #include "nrf_delay.h"
  11. #include "app_flash.h"
  12. #include "bsp_wdt.h"
  13. #include "app_timer.h"
  14. #include "system.h"
  15. #include "bsp_pwm.h"
  16. #include "app_client.h"
  17. #include "hal_ble_uart0.h"
  18. #include "hal_led.h"
  19. #include "lsm6ds3tr_c.h"
  20. #include "app_math.h"
  21. #include "bll_imu.h"
  22. #include "drv_qma7981.h"
  23. /********************** 变量区 *************************/
  24. /********************** 函数声明区 *************************/
  25. void PWR_Off(void)
  26. {
  27. // UART0_unInit(PIN_TXD_BLE,PIN_RXD_BLE);
  28. LED_Close_Enforce();
  29. bll_imu_close();
  30. drv_qma_power_off();
  31. nrf_gpio_pin_write(PIN_MT_EN,0);
  32. nrf_delay_ms(200);
  33. ADC_Disable();
  34. ADC_RemovePinChannel(PIN_CHARGING,PIN_CHARGING_CHANNEL);
  35. nrf_gpio_cfg_sense_input(PIN_CHARGING, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_SENSE_HIGH);//IO
  36. uint32_t err_code = sd_power_system_off();
  37. DEBUG_LOG("power off...BAT:%d,ERR:%d\n",GetBatteryPersent(),err_code);
  38. //APP_ERROR_CHECK(err_code);
  39. }
  40. static void PWR_Process(void)
  41. {
  42. if((0 == GetBatteryPersent() && hal_charge_Getstate()==BLE_CHARGE_PULLOUT)){
  43. DEBUG_LOG("BatteryPersent:%d\n",GetBatteryPersent());
  44. DEBUG_LOG("power off...\n");
  45. //保存数据到flash
  46. if(Flash_SaveStep() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_Power,"save step fail");
  47. extern battercb_t battery_record;
  48. extern void printbatter_cb(battercb_t *c,unsigned char *buff);
  49. memcpy(&mFlash.mbattercb_t,&battery_record,sizeof(battercb_t));
  50. mBackup.RestartCnt =0;
  51. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_Power,"pwr save backup fail");
  52. if(Flash_SaveInfomation() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_Power,"pwr save information fail");
  53. MT_Run(1000);
  54. feed_watchdog();
  55. for(uint8_t temp = 0;temp < 10; temp++){
  56. app_client_DataUpdate_Send();
  57. nrf_delay_ms(100);
  58. }
  59. PWR_Off();
  60. }
  61. }
  62. void PWR_Init(void)
  63. {
  64. Process_Start(10000,"PWR_Process",PWR_Process);
  65. }