app_safe.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*********************************************************************
  2. * INCLUDES
  3. */
  4. #include "ble_comm.h"
  5. #include "app_flash.h"
  6. #include "app_safe.h"
  7. #include "hal_ble_client.h"
  8. #include "hal_ble_host.h"
  9. #include "hal_led.h"
  10. static void app_safe_Process(void)
  11. {
  12. static uint8_t cnt =0;
  13. if(mBackup.ErrStartCnt >0){
  14. cnt++;
  15. if(cnt >=60){//一分钟后清空标志位
  16. mBackup.ErrStartCnt =0;
  17. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_Power,"save ErrStartCnt fail");
  18. DEBUG_LOG("clear mBackup.ErrorStartFlag\n");
  19. }
  20. }
  21. //测试使用
  22. static uint8_t temp =0;
  23. static uint8_t flag =0;
  24. if(mBackup.RestartCnt > 1 && flag ==0){flag =1;}
  25. if(1 == flag){
  26. if(temp ==0){temp++;
  27. Process_SetHoldOn(app_safe_Process,1);
  28. LED_Start(LED_RESTART,COLOR_PURPLE);
  29. }
  30. else if(temp <200){
  31. temp++;
  32. if(temp >20){temp =220;
  33. Process_SetHoldOn(app_safe_Process,0);
  34. LED_Stop(LED_RESTART);
  35. flag =2;
  36. }
  37. }
  38. }
  39. if(mBackup.RestartCnt > 1){
  40. char buff[40]={0};
  41. memset(buff,0,sizeof(buff));
  42. if(mFlash.isHost){
  43. sprintf(buff,"left shoes restart:%d",mBackup.RestartCnt);
  44. }else{
  45. sprintf(buff,"right shoes restart:%d",mBackup.RestartCnt);
  46. }
  47. if(0 == Except_TxError(EXCEPT_Power,(const char*)buff)){
  48. mBackup.RestartCnt =0;
  49. }
  50. }
  51. }
  52. //恢复出厂设置
  53. static void cb_BLE_RESTSETTING(void* handle)
  54. {
  55. uint8_t isHost;
  56. uint8_t LR_FLAG;
  57. isHost = mFlash.isHost;
  58. LR_FLAG = mFlash.LR_FLAG;
  59. memset((uint8_t*)(&mFlash),0,sizeof(Flash_t));
  60. memset((uint8_t*)(&mBackup),0,sizeof(FlashBackup_t));
  61. Flash_DeleteAllInfor();
  62. Flash_DeleteAllStep();
  63. Flash_DeleteAllBackup();
  64. mFlash.isHost = isHost;
  65. mFlash.LR_FLAG = LR_FLAG;
  66. Flash_SaveInfomation();
  67. mBackup.isHost= mFlash.isHost;
  68. mBackup.LR_FLAG = mFlash.LR_FLAG;
  69. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_FLASH,"app_detect_LR save backup fail");
  70. BLE_Client_Rx_t* target = handle;
  71. uint8_t cmd = target->pDat[0];
  72. BLE_Host_Tx_Send(0,BLE_RESTSETTING,&cmd,1);
  73. nrf_delay_ms(500);
  74. NVIC_SystemReset();
  75. }
  76. void app_safe_Init(void)
  77. {
  78. mBackup.RestartCnt++;
  79. Process_Start(1000,"app_safe",app_safe_Process);
  80. BLE_Client_Rx_Regist(BLE_RESTSETTING,cb_BLE_RESTSETTING);
  81. }