| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- #include "CONFIG.h"
- #if EC800M
- #include "stm32f10x.h"
- #include <stdio.h>
- #include <stdarg.h>
- #include <string.h>
- #include <stdlib.h>
- #include "Initialize.h"
- #include "ec800m.h"
- #include "PumpBusiness.h"
- #include "Log_Module.h"
- // 流程
- enum PowerStep{
- STEP_START, // 开始
- STEP_EXIT_SLEEP, // 退出睡眠
- STEP_ENTER_SLEEP, // 进入睡眠
- STEP_SET_SLEEP ,// 设置休眠模式
- STEP_SET_CFUN_0, // 设置最小功能模式
- STEP_SET_CFUN_1, // 设置全功能模式
- STEP_WAIT_SET_CFUN_1, // 等待设置全功能模式结果
- STEP_WAIT, // 等待
- STEP_SET_CGREG_2, // 设置ps域允许上报网络注册和位置信息
- STEP_QUERY_CGREG, // 查询网络注册状态
- STEP_SUCCESS, // 成功
- STEP_FAILURE, // 失败
- STEP_FINISH,
- };
- // 当前
- static enum PowerStep powerstep = STEP_START;
- // 下一步
- static enum PowerStep next_step = STEP_START;
- static enum Initialize_Result InitializeResult;
- static struct TIMER_Struct timer;
- static struct TIMER_Struct timer2;
- // 计时相关的变量
- static uint8_t Restart_flag = 0;//4分钟内重启次数计数
- static uint32_t Power_wait_time = 10;
- static uint8_t set_mincfun_flag=1;//0代表正在初始化,1代表空闲
- static uint8_t cgreg_n;
- static uint8_t cgreg_stat;
- static uint16_t cgreg_lac;
- static uint32_t cgreg_ci;
- static uint8_t query_cgreg_times = 0; // 查询网络状态的次数
- //// 声明函数。步骤跳转
- void pownext_step(enum PowerStep ns);
- // 等待
- static void powerwait(void)
- {
- if(time_get_delay(&timer) > Power_wait_time)
- {
- pownext_step(next_step); // 进入下一步
- time_clear(&timer);
- }
- }
- // 失败
- static void power_failure(char * info)
- {
- Log_Printf_Debug("STEP: 数据发送失败,%s\r\n", info);
- networkTest_Flag=1;
- pownext_step(STEP_SET_CFUN_0);
- }
- // 成功
- static void power_success(char * info)
- {
- Log_Printf_Debug("STEP: 数据发送成功,%s\r\n", info);
- networkTest_Flag=2;
- pownext_step(STEP_SET_CFUN_0);
- }
- //直接跳转到下一步
- static void pownext_step(enum PowerStep ns)
- {
- // 重置ec800m状态
- ec800m.reset();
- powerstep = ns;
- }
- // 先等待再跳转到下一步
- static void pownext_wait_step(enum PowerStep ns, uint32_t t)
- {
- pownext_step(STEP_WAIT); // 等待
- Power_wait_time = t;
- next_step = ns; // 等待之后跳转
- }
- // 发送数据的逻辑
- static enum Result result = Result_None;
- //获取初始化状态
- enum Initialize_Result get_initialize_status(void){
-
- if(set_mincfun_flag==0)
- {
- InitializeResult=Initialize_Result_Busy;
- }
- else
- {
- InitializeResult=Initialize_Result_free;
- }
- return InitializeResult;
- }
- void Initialize_Handle(void)
- {
- result =ec800m.ready();
- if(result==Result_Success)
- {
- set_mincfun_flag=0;
- pownext_step(STEP_START);
- Restart_flag++;
- Log_Printf_Debug("完成准备%d\r\n",Restart_flag);
- }
- if(module_switch==1)networkTest_Flag=1;
- if(time_get_delay(&timer2) >= 240000)
- {
- Restart_flag=0;
- time_clear(&timer2);
- }
- else if(Restart_flag >= 4 && module_switch == 0)//4分钟内重启4次
- {
- module_switch=1;
- pownext_step(STEP_SET_CFUN_0);//模块不断重启,直接进入最小功能模式
- Log_Printf_Debug("模块关闭中...%d\r\n",module_switch);
- }
- if(set_mincfun_flag == 1)
- {
- return ;
- }
- // 流程
- switch(powerstep)
- {
- case STEP_START: // 开始
- pownext_step(STEP_EXIT_SLEEP);
- query_cgreg_times = 0; // 查询网络状态的次数
- break;
- case STEP_EXIT_SLEEP: // 退出休眠
- ec800m.exit_sleep();
- pownext_wait_step(STEP_SET_SLEEP, 10);
- break;
-
- case STEP_SET_SLEEP: // 设置休眠模式
- result = ec800m.set_sleep(1);
- if(result == Result_Success)
- {
- Log_Printf_Debug("设置休眠模式成功\r\n");
- pownext_wait_step(STEP_SET_CFUN_1,3);
- }
- else if(result == Result_Failed)
- {
- Log_Printf_Debug("设置休眠模式失败\r\n");
- pownext_step(STEP_FAILURE);
- }
- break;
- case STEP_SET_CFUN_1:
- result = ec800m.set_cfun(1);
-
- if(result == Result_Success)
- {
- pownext_step(STEP_SET_CGREG_2);
- }
- else if(result == Result_Failed)
- {
- power_failure("设置全功能模式失败");
- }
- break;
- case STEP_SET_CGREG_2: // 设置ps域
- result = ec800m.set_cgreg(2);
- if(result == Result_Success)
- {
- pownext_step(STEP_QUERY_CGREG);
- }
- else if(result == Result_Failed)
- {
- power_failure("设置ps域失败");
- }
- break;
- case STEP_QUERY_CGREG: // 查询ps域
- result = ec800m.query_cgreg(&cgreg_n, &cgreg_stat, &cgreg_lac, &cgreg_ci);
- if(result == Result_Success)
- {
- if(cgreg_stat == 1)
- {
- // 下一步
- power_success("查询PS域成功");
- }
- else if(query_cgreg_times > 20) // 最多查询20次
- {
- power_failure("查询ps域次数过多");
- }
- else
- {
- pownext_wait_step(STEP_QUERY_CGREG,400);
- query_cgreg_times++;
- }
- }
- else if(result == Result_Failed)
- {
- power_failure("查询ps域失败");
- }
- break;
- case STEP_WAIT: // 等待
- powerwait();
- break;
- case STEP_SUCCESS: // 成功
- pownext_step(STEP_SET_CFUN_0);
- Log_Printf_Debug("初始化成功\r\n");
- break;
- case STEP_FAILURE: // 失败
- pownext_step(STEP_SET_CFUN_0);
- Log_Printf_Debug("初始化失败\r\n");
- break;
- case STEP_SET_CFUN_0: // 设置最小功能模式
- result = ec800m.set_cfun(0);
- if(result == Result_Success)
- {
- Log_Printf_Debug("设置最小功能模式成功\r\n");
- pownext_step(STEP_ENTER_SLEEP);
- }
- else if(result == Result_Failed)
- {
- Log_Printf_Debug("设置最小功能模式失败\r\n");
- pownext_step(STEP_ENTER_SLEEP);
- }
- break;
- case STEP_ENTER_SLEEP: // 进入睡眠
- ec800m.enter_sleep();
- pownext_step(STEP_FINISH);
- break;
- case STEP_FINISH: // 结束流程
- set_mincfun_flag=1;
- break;
- default:
- break;
- }
-
- }
- #endif
|