app_power.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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_led.h"
  18. #include "lsm6ds3tr_c.h"
  19. #include "app_math.h"
  20. #include "bll_imu.h"
  21. #include "drv_qma7981.h"
  22. /********************** 变量区 *************************/
  23. /********************** 函数声明区 *************************/
  24. void PWR_Off(void)
  25. {
  26. LED_Close_Enforce();
  27. bll_imu_close();
  28. drv_qma_power_off();
  29. nrf_gpio_pin_write(PIN_MT_EN,0);
  30. nrf_delay_ms(200);
  31. ADC_Disable();
  32. ADC_RemovePinChannel(PIN_CHARGING,PIN_CHARGING_CHANNEL);
  33. nrf_gpio_cfg_sense_input(PIN_CHARGING, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_SENSE_HIGH);//IO
  34. uint32_t err_code = sd_power_system_off();
  35. DEBUG_LOG("power off...BAT:%d,ERR:%d\n",GetBatteryPersent(),err_code);
  36. //APP_ERROR_CHECK(err_code);
  37. }
  38. static void PWR_Process(void)
  39. {
  40. if((0 == GetBatteryPersent() && hal_charge_Getstate()==BLE_CHARGE_PULLOUT)){
  41. DEBUG_LOG("BatteryPersent:%d\n",GetBatteryPersent());
  42. DEBUG_LOG("power off...\n");
  43. //保存步数
  44. if(Flash_SaveStep() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_Power,"save step fail");
  45. extern battercb_t battery_record;
  46. memcpy(&mFlash.mbattercb_t,&battery_record,sizeof(battercb_t));
  47. mBackup.RestartCnt =0;
  48. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_Power,"pwr save backup fail");
  49. if(Flash_SaveInfomation() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_Power,"pwr save information fail");
  50. MT_Run(1000);
  51. feed_watchdog();
  52. for(uint8_t temp = 0;temp < 10; temp++){
  53. app_client_DataUpdate_Send();
  54. nrf_delay_ms(100);
  55. }
  56. PWR_Off();
  57. }
  58. }
  59. void PWR_Init(void)
  60. {
  61. Process_Start(30000,"PWR",PWR_Process);
  62. }