app_self_checking - 副本.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*Includes ----------------------------------------------*/
  2. #include "system.h"
  3. #include "exception.h"
  4. #include "app_charge.h"
  5. #include "app_flash.h"
  6. #include "app_self_checking.h"
  7. #include "hal_led.h"
  8. #include "hal_mt.h"
  9. #include "hal_ble_uart0.h"
  10. #include "hal_ble_client.h"
  11. #include "hal_ser_imu_mode_manage.h"
  12. #include "app_flash.h"
  13. #include "app_pair.h"
  14. #include "bsp_time.h"
  15. #include "hal_scan_manage.h"
  16. #include "ble_comm.h"
  17. /*Private macro ------------------------------------------------*/
  18. #define SELF_CHECK_WHEEL_EMPTY_TIMES_MAX MONITOR_SUSPEND_MODE_OVERFLOW_ERR_SUM_MAX //允许最大轮空次数
  19. #define SELF_CHECK_ITEMS 6 //自检项数
  20. #define SELF_CHECK_ITEM_SUCCESS 0 //自检项目成功
  21. #define SELF_CHECK_ITEM_FAIL 1 //自检项目失败
  22. #define SELF_CHECK_ITEM_FRONT_SENSOR_INDEX 0 //自检项目 - 前脚传感器
  23. #define SELF_CHECK_ITEM_BACK_SENSOR_INDEX 1 //自检项目 - 后脚传感器
  24. #define SELF_CHECK_ITEM_BATTERY_INDEX 2 //自检项目 - 电量
  25. #define SELF_CHECK_ITEM_CHARGE_INDEX 3 //自检项目 - 充电电压
  26. #define SELF_CHECK_ITEM_ANT_INDEX 4 //自检项目 - 天线
  27. #define SELF_CHECK_ITEM_SUSPEND_MODE_INDEX 5 //自检项目 - 异常挂起
  28. #define SELF_CHECK_PROCESS_CYCLE 10 //自检线程周期 - ms为单位
  29. #define SELF_CHECK_LED_DISPLAY_TIME 3000 //自检灯显示时长 - ms为单位
  30. #define SELF_CHECK_LED_TOGGLE_TIME 200 //自检灯翻转时长 - ms为单位
  31. #define SELF_CHECK_SCAN_TRIGGER_TIMEOUT 10000 //扫描触发自检超时时间 - ms为单位
  32. #define SELF_CHECK_SET_IMU_TIMEOUT 20000 //等待IMU初始化成功的时间
  33. #define SELF_CHECK_SCAN_TARGET_ADV_NAME "SH_SELF_CHECK" //扫描触发的目标广播名字
  34. /*STRUCTION --------------------------------------------------
  35. --*/
  36. typedef enum {
  37. SELF_CHECK_STAGE_READY, //准备阶段
  38. SELF_CHECK_STAGE_CHECK, //自检阶段
  39. } SELF_CHECK_STAGE_e;
  40. typedef enum{
  41. SELF_CHECK_LED_ON, //自检灯 - 开
  42. SELF_CHECK_LED_OFF, //自检灯 - 关
  43. } SELF_CHECK_LED_SWITCH_e;
  44. typedef struct self_check
  45. {
  46. /*private member*/
  47. SELF_CHECK_STAGE_e stage; //阶段字段
  48. uint32_t ImuSetTime_times; //轮空次数
  49. uint8_t result[SELF_CHECK_ITEMS]; //自检结果
  50. uint8_t item_fail_num; //自检项目失败数量
  51. SELF_CHECK_LED_SWITCH_e led_switch; //自检灯开关
  52. uint32_t led_display_time; //自检灯显示时长
  53. uint32_t led_toggle_time; //自检灯翻转时长
  54. uint32_t scan_trigger_timeout; //扫描触发自检超时时间
  55. bool is_scan_success; //是否找到广播名字
  56. } Self_Check_t;
  57. /*Local Variable -----------------------------------------------*/
  58. static Self_Check_t ob_self_check;
  59. /*Local Functions ----------------------------------------------*/
  60. static void clear_all_except(void)
  61. {
  62. Except_ClearExceptype(EXCEPT_DATA_BACK_MAG);
  63. Except_ClearExceptype(EXCEPT_DATA_FRONT_ACC);
  64. Except_ClearExceptype(EXCEPT_DATA_FRONT_GRY);
  65. Except_ClearExceptype(EXCEPT_DATA_FRONT_MAG);
  66. Except_ClearExceptype(EXCEPT_DATA_CHARGE);
  67. Except_ClearExceptype(EXCEPT_DATA_BATTERY);
  68. Except_ClearExceptype(EXCEPT_IMU_SUSPEND_OVERFLOW);
  69. Except_ClearExceptype(EXCEPT_ANT_DAMAGE);
  70. }
  71. static void selft_scan_cb(void)
  72. {
  73. ob_self_check.is_scan_success = true;
  74. }
  75. static scan_reslt_t scan_reslt_handle ={
  76. .handle = selft_scan_cb,
  77. .scanname =SELF_CHECK_SCAN_TARGET_ADV_NAME,
  78. };
  79. static uint8_t self_uart_check_flag =0;
  80. static void cb_UART0_R_LINE_CHECK_ACK(void* handle)
  81. {
  82. //主机接收到配对应答
  83. if(!mFlash.isHost) return; //从机自发自收
  84. self_uart_check_flag = 1;
  85. }
  86. static void cb_UART0_R_LINE_CHECK_ASK(void* handle)
  87. { //从机接收到配对申请
  88. uint32_t txd,rxd;
  89. if(mFlash.isHost){
  90. return; //防止主机自发自收
  91. }
  92. self_uart_check_flag =1;
  93. UART0_GetPinConfig(&txd, &rxd);
  94. UART0_Initialize(PIN_TXD_BLE,UART0_INVALID_PIN,UART_HZ);
  95. UART0_Config_Send(UART0_T_LINE_CHECK_ACK);//应答主机申请
  96. if(rxd == 0 && txd == 0){txd = UART0_INVALID_PIN;rxd = PIN_RXD_BLE;}
  97. UART0_Initialize(txd,rxd,UART_HZ);
  98. }
  99. static void app_UartLine_Pair_Process(){
  100. static uint8_t state =0;
  101. uint32_t txd,rxd;
  102. switch(state){
  103. case 0:
  104. if(true == ob_self_check.is_scan_success){
  105. state =1;
  106. self_uart_check_flag =0;
  107. }
  108. break;
  109. case 1:
  110. if(mFlash.isHost){
  111. UART0_GetPinConfig(&txd, &rxd);
  112. UART0_Initialize(PIN_TXD_BLE,UART0_INVALID_PIN,UART_HZ);
  113. UART0_Config_Send(UART0_T_LINE_CHECK_ASK);
  114. if(rxd == 0 && txd == 0){txd = UART0_INVALID_PIN;rxd = PIN_RXD_BLE;}
  115. UART0_Initialize(txd,rxd,UART_HZ);
  116. }
  117. if(false == ob_self_check.is_scan_success){
  118. state = 0;
  119. }
  120. break;
  121. default:
  122. break;
  123. }
  124. }
  125. static void app_self_check_getreslut(){
  126. /*获取自检结果前---------------------------------------------------------------------*/
  127. ob_self_check.result[SELF_CHECK_ITEM_FRONT_SENSOR_INDEX] = SELF_CHECK_ITEM_SUCCESS;
  128. ob_self_check.result[SELF_CHECK_ITEM_BACK_SENSOR_INDEX] = SELF_CHECK_ITEM_SUCCESS;
  129. ob_self_check.result[SELF_CHECK_ITEM_BATTERY_INDEX] = SELF_CHECK_ITEM_SUCCESS;
  130. ob_self_check.result[SELF_CHECK_ITEM_CHARGE_INDEX] = SELF_CHECK_ITEM_SUCCESS;
  131. ob_self_check.result[SELF_CHECK_ITEM_ANT_INDEX] = SELF_CHECK_ITEM_SUCCESS;
  132. ob_self_check.result[SELF_CHECK_ITEM_SUSPEND_MODE_INDEX] = SELF_CHECK_ITEM_SUCCESS;
  133. ob_self_check.item_fail_num = 0;
  134. /*获取自检结果中---------------------------------------------------------------------*/
  135. //自检结果 - 异常挂起
  136. if(Except_IsError(EXCEPT_IMU_SUSPEND_OVERFLOW)){ob_self_check.result[SELF_CHECK_ITEM_SUSPEND_MODE_INDEX] = SELF_CHECK_ITEM_FAIL;ob_self_check.item_fail_num = SELF_CHECK_ITEM_SUSPEND_MODE_INDEX + 1;}
  137. //自检结果 - 前脚传感器
  138. if(Except_IsError(EXCEPT_DATA_FRONT_ACC)){ob_self_check.result[SELF_CHECK_ITEM_FRONT_SENSOR_INDEX] = SELF_CHECK_ITEM_FAIL;}
  139. if(Except_IsError(EXCEPT_DATA_FRONT_GRY)){ob_self_check.result[SELF_CHECK_ITEM_FRONT_SENSOR_INDEX] = SELF_CHECK_ITEM_FAIL;}
  140. if(Except_IsError(EXCEPT_DATA_FRONT_MAG)){ob_self_check.result[SELF_CHECK_ITEM_FRONT_SENSOR_INDEX] = SELF_CHECK_ITEM_FAIL;}
  141. if(Except_IsError(EXCEPT_DATA_FRONT_ACC) || Except_IsError(EXCEPT_DATA_FRONT_GRY) || Except_IsError(EXCEPT_DATA_FRONT_MAG)){ob_self_check.item_fail_num = SELF_CHECK_ITEM_FRONT_SENSOR_INDEX + 1;}
  142. //自检结果 - 后脚传感器
  143. if(Except_IsError(EXCEPT_DATA_BACK_MAG)){ob_self_check.result[SELF_CHECK_ITEM_BACK_SENSOR_INDEX] = SELF_CHECK_ITEM_FAIL;ob_self_check.item_fail_num = SELF_CHECK_ITEM_BACK_SENSOR_INDEX + 1;}
  144. //自检结果 - 电量
  145. if(Except_IsError(EXCEPT_DATA_BATTERY)){ob_self_check.result[SELF_CHECK_ITEM_BATTERY_INDEX] = SELF_CHECK_ITEM_FAIL;ob_self_check.item_fail_num = SELF_CHECK_ITEM_BATTERY_INDEX + 1;}
  146. //自检结果 - 充电电压
  147. if(Except_IsError(EXCEPT_DATA_CHARGE)){ob_self_check.result[SELF_CHECK_ITEM_CHARGE_INDEX] = SELF_CHECK_ITEM_FAIL;ob_self_check.item_fail_num = SELF_CHECK_ITEM_CHARGE_INDEX + 1;}
  148. //自检结果 - 天线
  149. if(Except_IsError(EXCEPT_ANT_DAMAGE)){ob_self_check.result[SELF_CHECK_ITEM_ANT_INDEX] = SELF_CHECK_ITEM_FAIL;ob_self_check.item_fail_num = SELF_CHECK_ITEM_ANT_INDEX + 1;}
  150. //设置自检灯显示时长和翻转时长
  151. if(ob_self_check.item_fail_num != 0){
  152. ob_self_check.led_switch = SELF_CHECK_LED_ON;
  153. ob_self_check.led_display_time = TIME_GetTicks();
  154. ob_self_check.led_toggle_time = TIME_GetTicks();
  155. }else{
  156. ob_self_check.led_switch = SELF_CHECK_LED_ON;
  157. if(self_uart_check_flag){
  158. LED_Start(LED_SELF_CHECK,COLOR_BLUE);
  159. }
  160. else{
  161. LED_Start(LED_SELF_CHECK,COLOR_CYAN);
  162. }
  163. }
  164. }
  165. static void ReadyIntoSelfMode(void){
  166. //设置为自检模式,且跳转到下一个流程
  167. hal_ble_scan_result_Regist(&scan_reslt_handle);
  168. ob_self_check.is_scan_success = false;
  169. ob_self_check.scan_trigger_timeout = TIME_GetTicks();
  170. ob_self_check.stage = SELF_CHECK_STAGE_READY;
  171. clear_all_except(); //清除所有异常
  172. LED_Start(LED_SELF_CHECK,COLOR_BLACK);
  173. LED_Stop(LED_SELF_CHECK);
  174. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_SELF_CHECK, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_ON);
  175. memset(ob_self_check.result, SELF_CHECK_ITEM_SUCCESS, SELF_CHECK_ITEMS);
  176. SEGGER_RTT_printf(0,"app_self_checking_Process clear_all_except\r\n");
  177. }
  178. static void app_self_checking_Process(void)
  179. {
  180. static uint8_t state =0;
  181. static uint8_t state_selft =0;
  182. static uint8_t displaycnt =0;
  183. switch(state){
  184. case 0:
  185. if(BLE_Client_T_CHARGE_DONE == app_charge_Getstate() || \
  186. BLE_Client_T_CHARGE_INSERT == app_charge_Getstate()
  187. ){
  188. if(Except_IsErrorExist()) //如果存在错误
  189. {
  190. if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_SELF_CHECK) != -1) //如果当前是自检配置
  191. {
  192. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_SELF_CHECK, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF); //关掉自检配置
  193. }
  194. else{ //如果当前不是自检配置{
  195. ST_scan_start();
  196. ReadyIntoSelfMode();
  197. state =1;
  198. state_selft =0;
  199. }
  200. }
  201. else{
  202. //设置为自检模式,且跳转到下一个流程
  203. ST_scan_start();
  204. ReadyIntoSelfMode();
  205. state =1;
  206. state_selft =0;
  207. }
  208. }
  209. break;
  210. case 1:
  211. if(ob_self_check.is_scan_success == true)
  212. {
  213. ob_self_check.stage = SELF_CHECK_STAGE_CHECK; //进入自检阶段
  214. state =2;
  215. Process_SetHoldOn(app_self_checking_Process,1);
  216. SEGGER_RTT_printf(0,"ob_self_check.is_scan_success\r\n");
  217. }else{
  218. if((TIME_GetTicks() - ob_self_check.scan_trigger_timeout) > SELF_CHECK_SCAN_TRIGGER_TIMEOUT) //超时且没扫描到广播名
  219. {
  220. state = 2;
  221. state_selft =3;
  222. SEGGER_RTT_printf(0,"ob_self_check.scan_trigger_timeout\r\n");
  223. }
  224. }
  225. break;
  226. case 2:
  227. if(BLE_Client_T_CHARGE_PULLOUT == app_charge_Getstate()){
  228. ob_self_check.is_scan_success = false; //重置扫描成功标志位
  229. hal_ble_scan_result_Clear(&scan_reslt_handle); //禁止扫描触发
  230. ST_scan_stop();
  231. state = 0;
  232. }
  233. break;
  234. default:
  235. break;
  236. }
  237. switch(state_selft)
  238. {
  239. case 0:
  240. if(SELF_CHECK_STAGE_CHECK == ob_self_check.stage){//扫描成功
  241. MT_Run(500);
  242. if(-1 == hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_SELF_CHECK)){
  243. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_SELF_CHECK, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_ON);
  244. }
  245. state_selft = 1;
  246. ob_self_check.ImuSetTime_times = TIME_GetTicks();
  247. }
  248. break;
  249. case 1:
  250. if(BLE_Client_T_CHARGE_PULLOUT == app_charge_Getstate()){
  251. state_selft = 3;
  252. }
  253. else {
  254. if(0 == hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_SELF_CHECK)){
  255. state_selft = 2;
  256. displaycnt =0;
  257. app_self_check_getreslut();
  258. }
  259. else if(TIME_GetTicks() - ob_self_check.ImuSetTime_times >= SELF_CHECK_SET_IMU_TIMEOUT){
  260. Except_SetExceptype(EXCEPT_DATA_FRONT_ACC);
  261. Except_SetExceptype(EXCEPT_DATA_FRONT_GRY);
  262. Except_SetExceptype(EXCEPT_DATA_FRONT_MAG);
  263. Except_SetExceptype(EXCEPT_IMU_SUSPEND_OVERFLOW);
  264. state_selft = 2;
  265. displaycnt =0;
  266. }
  267. }
  268. break;
  269. case 2:
  270. if(displaycnt < ob_self_check.item_fail_num){
  271. if(TIME_GetTicks() - ob_self_check.led_toggle_time >= SELF_CHECK_LED_TOGGLE_TIME){ob_self_check.led_toggle_time = TIME_GetTicks();
  272. if(ob_self_check.led_switch == SELF_CHECK_LED_ON){
  273. LED_Start(LED_SELF_CHECK,COLOR_LIGHRED);
  274. ob_self_check.led_switch = SELF_CHECK_LED_OFF;
  275. }
  276. else{
  277. LED_Start(LED_SELF_CHECK,COLOR_BLACK);
  278. ob_self_check.led_switch = SELF_CHECK_LED_ON;
  279. displaycnt++;
  280. }
  281. }
  282. }
  283. else if(TIME_GetTicks() - ob_self_check.led_display_time >= SELF_CHECK_LED_DISPLAY_TIME){//显示时间到了,重现读取自检结果,并显示
  284. ob_self_check.led_display_time = TIME_GetTicks();
  285. displaycnt =0;
  286. app_self_check_getreslut();
  287. }
  288. break;
  289. case 3:
  290. state_selft = 0;
  291. ob_self_check.stage = SELF_CHECK_STAGE_READY;
  292. LED_Start(LED_SELF_CHECK,COLOR_BLACK);
  293. LED_Stop(LED_SELF_CHECK);
  294. ob_self_check.led_switch = SELF_CHECK_LED_OFF; //设置自检灯为关
  295. if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_SELF_CHECK) != -1) //当前是自检模式,退出。
  296. {
  297. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_SELF_CHECK, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
  298. }
  299. break;
  300. default:
  301. break;
  302. }
  303. }
  304. static void app_self_checking_printf_process(void)
  305. {
  306. SEGGER_RTT_printf(0,"self_check: FRONT_SENSOR:%d BACK_SENSOR:%d BATTERY:%d CHARGE:%d ANT:%d suspend_mode:%d scan_trigger_timeout:%d\n", \
  307. Except_IsError(EXCEPT_DATA_FRONT_ACC) || Except_IsError(EXCEPT_DATA_FRONT_GRY) || Except_IsError(EXCEPT_DATA_FRONT_MAG), \
  308. Except_IsError(EXCEPT_DATA_BACK_MAG), \
  309. Except_IsError(EXCEPT_DATA_BATTERY), \
  310. Except_IsError(EXCEPT_DATA_CHARGE), \
  311. Except_IsError(EXCEPT_ANT_DAMAGE), \
  312. Except_IsError(EXCEPT_IMU_SUSPEND_OVERFLOW),ob_self_check.scan_trigger_timeout);
  313. }
  314. /*API ----------------------------------------------------------*/
  315. void app_self_checking_Init(void)
  316. {
  317. ob_self_check.stage = SELF_CHECK_STAGE_READY;
  318. ob_self_check.ImuSetTime_times = 0;
  319. ob_self_check.item_fail_num = 0;
  320. ob_self_check.led_switch = SELF_CHECK_LED_OFF;
  321. ob_self_check.led_display_time = 0;
  322. ob_self_check.led_toggle_time = 0;
  323. ob_self_check.is_scan_success = false;
  324. ob_self_check.scan_trigger_timeout = SELF_CHECK_SCAN_TRIGGER_TIMEOUT;
  325. Process_Start(SELF_CHECK_PROCESS_CYCLE,"app_self_checking_Process",app_self_checking_Process);
  326. Process_Start(300,"app_UartLine_Pair_Process",app_UartLine_Pair_Process);
  327. UART0_Rx_Regist(UART0_R_LINE_CHECK_ASK,cb_UART0_R_LINE_CHECK_ASK);
  328. UART0_Rx_Regist(UART0_R_LINE_CHECK_ACK,cb_UART0_R_LINE_CHECK_ACK);
  329. // Process_Start(1000,"app_self_checking_printf_process",app_self_checking_printf_process);
  330. }
  331. uint32_t app_self_checking_get_scan_trigger_timeout(void)
  332. {
  333. return ob_self_check.scan_trigger_timeout;
  334. }