diff --git a/.cproject b/.cproject
index faab14c..231b973 100644
--- a/.cproject
+++ b/.cproject
@@ -113,7 +113,7 @@
@@ -203,7 +203,7 @@
-
+
@@ -244,7 +244,7 @@
-
+
diff --git a/APP/peripheral_main.c b/APP/peripheral_main.c
index 0d086b9..8b22993 100644
--- a/APP/peripheral_main.c
+++ b/APP/peripheral_main.c
@@ -58,6 +58,7 @@ void Main_Circulation()
{
TMOS_SystemProcess();
KEY_ProcessLoop();
+ MultiTimerYield();
}
}
@@ -75,7 +76,7 @@ int main(void)
#endif
HSECFG_Capacitance(HSECap_18p);
SetSysClock(CLK_SOURCE_HSE_PLL_62_4MHz);
- SysTick_Config( GetSysClock() / 1000 * SYSTICK_INTERVAL); //设定嘀嗒时间1ms
+ // SysTick_Config( GetSysClock() / 1000 * SYSTICK_INTERVAL); //设定嘀嗒时间1ms
#if(defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
GPIOA_ModeCfg(GPIO_Pin_All, GPIO_ModeIN_PD);
GPIOB_ModeCfg(GPIO_Pin_All, GPIO_ModeIN_PD);
@@ -97,7 +98,7 @@ int main(void)
logDebug("GAP init ok\n");
Peripheral_Init();
logDebug("Peripheral init ok\n");
-
+ MultiTimerInstall((PlatformTicksFunction_t)BSP_Get_Tick);
BSP_KEY_Init(app_task_handler);
BSP_MOTOR_Init();
@@ -107,13 +108,17 @@ int main(void)
BSP_VBAT_Init();
logDebug("VBAT init ok\n");
+
+ logDebug("BSP_Get_Tick1 %d\n",BSP_Get_Tick());
+ DelayMs(2000);
+ logDebug("BSP_Get_Tick2 %d\n",BSP_Get_Tick());
BSP_Ml307r_Init();
logDebug("BSP_M1307r ok\n");
- ShowRestartReason();
- IWDG_Init(5000);
- logDebug("IWDG init ok\n");
+ // ShowRestartReason();
+ // IWDG_Init(5000);
+ // logDebug("IWDG init ok\n");
BSP_PRESS_Init();
logDebug("BSP init ok\n");
diff --git a/IoT_SCV_CH584M1.wvproj b/IoT_SCV_CH584M1.wvproj
index 055391c..027b6de 100644
--- a/IoT_SCV_CH584M1.wvproj
+++ b/IoT_SCV_CH584M1.wvproj
@@ -150,7 +150,7 @@
"do_not_search_system_directories": false,
"preprocess_only": false,
"defined_symbols": [
- "CLK_OSC32K=1",
+ "CLK_OSC32K=0",
"DEBUG=3"
],
"undefined_symbols": []
@@ -262,7 +262,7 @@
"generate_map": "\"${BuildArtifactFileBaseName}.map\"",
"cross_reference": false,
"print_link_map": false,
- "use_newlib_nano": false,
+ "use_newlib_nano": true,
"use_float_with_nano_printf": false,
"use_float_with_nano_scanf": false,
"do_not_use_syscalls": true,
@@ -305,7 +305,7 @@
"generate_map": "\"${BuildArtifactFileBaseName}.map\"",
"cross_reference": false,
"print_link_map": false,
- "use_newlib_nano": false,
+ "use_newlib_nano": true,
"use_float_with_nano_printf": false,
"use_float_with_nano_scanf": false,
"do_not_use_syscalls": true,
diff --git a/README.md b/README.md
index 3537416..d441bc3 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,9 @@
-1.4G妯$粍鍋跺厛杩炴帴澶辫触闂
-2.闇娣诲姞濡傛灉鐭椂闂村唴澶氭鏀瑰彉闃闂ㄧ姸鎬佹椂4G鍙互閮戒笂鎶ョ殑鍔熻兘
+1.浣跨敤鐘舵佹満閲嶆瀯浜4G妯$粍杩炴帴杩囩▼
+2.璋冪敤杩欎釜TMOS_GetSystemClock()鏃堕渶瑕佷娇鐢ㄨ交閲忕骇 C 搴 newlib-nano锛岋紙榛樿绂佺敤娴偣鏀寔锛
+3.鍥犵鐢ㄦ诞鐐硅繍绠楋紝浼樺寲浠g爜ADC閮ㄥ垎娴偣杩愮畻浠g爜
+4.鎵撳紑浜嗗閮ㄤ綆閫熸椂閽
+
+
+鑾峰彇sim淇℃伅(鐢ㄤ簬涓婃姤)
+鏁版嵁涓婃姤锛堜綆鐢靛帇銆侀榾闂ㄧ姸鎬侊級
+婊寸瓟瀹氭椂鍣ㄧ簿搴
\ No newline at end of file
diff --git a/bsp/inc/bsp_ml307r.h b/bsp/inc/bsp_ml307r.h
index 01732a2..bd38464 100644
--- a/bsp/inc/bsp_ml307r.h
+++ b/bsp/inc/bsp_ml307r.h
@@ -14,6 +14,47 @@
#define ML307_UART_TX_PIN GPIO_Pin_13 //PB13
#define ML307_UART_RX_PIN GPIO_Pin_12 //PB12
+
+
+typedef struct __attribute__((packed))
+{
+ uint8_t module_init_flag : 1; //模块初始化
+ uint8_t module_Sleep_flag : 1; //模块睡眠
+ uint8_t Low_Voltage_flag : 1; //低电压
+ uint8_t Regular_report_flag : 1; //定期上报
+
+ uint8_t PowerOn_success_flag : 1; //模块开机成功
+ uint8_t PowerOn_error_flag : 1; //模块开机失败
+ uint8_t PowerOff_success_flag : 1;//模块关机成功
+ uint8_t PowerOff_error_flag : 1; //模块关机失败
+
+ uint8_t restart_success_flag : 1; //重启模块成功
+ uint8_t restart_error_flag : 1; //重启模块失败
+ uint8_t sim_success_flag : 1; //sim成功
+ uint8_t sim_error_flag : 1; //sim失败
+
+ uint8_t Connect_success_flag : 1; //连接平台成功
+ uint8_t Connect_error_flag : 1; //连接平台失败
+ uint8_t Valve_Open_flag : 1; //阀门打开
+ uint8_t Valve_Close_flag : 1; //阀门关闭
+
+ uint8_t send_complete_flag : 1; //发送数据完成标志
+ uint8_t send_error_flag : 1; //发送数据错误标志
+ uint8_t update_time_flag : 1; //更新网络时间标志位
+ uint8_t login_status_flag : 1; //登录到服务器标志位;
+
+ uint8_t Loop_count_flag : 3; //循环次数标志位
+ uint8_t Iot_Retry_flag : 1; //重试标志位;
+
+}IotFlagStruct;
+
+extern IotFlagStruct IotFlag_t;
+
+
+
+static void BSP_Ml307_Power_Off(void);
+
+
void BSP_Ml307r_Init(void);
void Ml307r_Loop(void);
void BSP_Module_Emergency_Send(uint8_t* data, uint8_t len);
@@ -21,7 +62,7 @@ void BSP_ML307_StartTimeoutTimer(void);
void BSP_ML307_SetActive(void);
void BSP_ML307_SendMessage(void);
static void BSP_Read_Module(void);
-static void simcom_init(void);
+static void BSP_simcom_init(void);
static void BSP_Module_Connect_CtWing(void);
#endif //!@__BSP_ML307R_H__
diff --git a/bsp/inc/bsp_tim.h b/bsp/inc/bsp_tim.h
index 23eeb7d..6a59b19 100644
--- a/bsp/inc/bsp_tim.h
+++ b/bsp/inc/bsp_tim.h
@@ -3,26 +3,58 @@
#include "CH58x_common.h"
-
-
-
-
-
-
+#include "CONFIG.h"
uint32_t BSP_Get_Tick(void);
+typedef uint64_t (*PlatformTicksFunction_t)(void);
+typedef struct MultiTimerHandle MultiTimer;
+typedef void (*MultiTimerCallback)(MultiTimer* timer, void* userData);
+struct MultiTimerHandle
+{
+ MultiTimer* next;
+ uint64_t deadline;
+ MultiTimerCallback callback;
+ void* userData;
+};
+/**
+ * @brief Platform ticks function.
+ *
+ * @param ticksFunc ticks function.
+ * @return int 0 on success, -1 on error.
+ */
+int MultiTimerInstall(PlatformTicksFunction_t ticksFunc);
+/**
+ * @brief Start the timer work, add the handle into work list.
+ *
+ * @param timer target handle strcut.
+ * @param timing Set the start time.
+ * @param callback deadline callback.
+ * @param userData user data.
+ * @return int 0: success, -1: fail.
+ */
+int MultiTimerStart(MultiTimer* timer, uint64_t timing, MultiTimerCallback callback, void* userData);
+/**
+ * @brief Stop the timer work, remove the handle off work list.
+ *
+ * @param timer target handle strcut.
+ * @return int 0: success, -1: fail.
+ */
+int MultiTimerStop(MultiTimer* timer);
-
-
-
+/**
+ * @brief Check the timer expried and call callback.
+ *
+ * @return int The next timer expires.
+ */
+int MultiTimerYield(void);
#endif //!@__BSP_TIM_H__
diff --git a/bsp/src/bsp_adc.c b/bsp/src/bsp_adc.c
index 7287dd7..3f6d81e 100644
--- a/bsp/src/bsp_adc.c
+++ b/bsp/src/bsp_adc.c
@@ -43,55 +43,55 @@ void ADC_GPIO_Init(void)
}
-void Send_Low_Battery_Message(float vbat)
-{
- char json_buffer[300];
- char time_str[20] = {0};
- snprintf(json_buffer, sizeof(json_buffer),
- "{"
- "\"device\":{"
- "\"id\":\"SCV001\","
- "\"type\":\"SelfClosingValve\","
- "\"time\":\"%s\""
- "},"
- "\"status\":\"closed\","
- "\"fault\":{"
- "\"lowBattery\":true,"
- "\"commError\":false,"
- "\"code\":1001"
- "},"
- "\"monitor\":{"
- "\"pressure\":0.8,"
- "\"voltage\":%.2f,"
- "\"lowVoltage\":true"
- "},"
- "\"network\":{"
- "\"sinr\":15.2,"
- "\"rsrp\":-95.5,"
- "\"signal\":4"
- "},"
- "\"standards\":{"
- "\"iotId\":\"IoT-SCV-12345\""
- "}"
- "}",
- time_str, vbat);
- BSP_Module_Emergency_Send((uint8_t*)json_buffer, strlen(json_buffer));
- logDebug("Send low battery message: %s\n", json_buffer);
-}
+// void Send_Low_Battery_Message(float vbat)
+// {
+// char json_buffer[300];
+// char time_str[20] = {0};
+// snprintf(json_buffer, sizeof(json_buffer),
+// "{"
+// "\"device\":{"
+// "\"id\":\"SCV001\","
+// "\"type\":\"SelfClosingValve\","
+// "\"time\":\"%s\""
+// "},"
+// "\"status\":\"closed\","
+// "\"fault\":{"
+// "\"lowBattery\":true,"
+// "\"commError\":false,"
+// "\"code\":1001"
+// "},"
+// "\"monitor\":{"
+// "\"pressure\":0.8,"
+// "\"voltage\":%.2f,"
+// "\"lowVoltage\":true"
+// "},"
+// "\"network\":{"
+// "\"sinr\":15.2,"
+// "\"rsrp\":-95.5,"
+// "\"signal\":4"
+// "},"
+// "\"standards\":{"
+// "\"iotId\":\"IoT-SCV-12345\""
+// "}"
+// "}",
+// time_str, vbat);
+// BSP_Module_Emergency_Send((uint8_t*)json_buffer, strlen(json_buffer));
+// logDebug("Send low battery message: %s\n", json_buffer);
+// }
-// 处理电池电压低事件
-void Handle_Low_Battery(float vbat)
-{
- if (!vbat_low_flag) {
- VALVE_CLOSE();
- LED_VALVE_CLOSE;
- Send_Low_Battery_Message(vbat);
- vbat_low_flag = 1;
- logDebug("Voltage is too low (%.2fV),Close valve\n", vbat);
- }
- LED_VBAT_OPEN;
- DelayMs(100);
-}
+// // 处理电池电压低事件
+// void Handle_Low_Battery(float vbat)
+// {
+// if (!vbat_low_flag) {
+// VALVE_CLOSE();
+// LED_VALVE_CLOSE;
+// Send_Low_Battery_Message(vbat);
+// vbat_low_flag = 1;
+// logDebug("Voltage is too low (%.2fV),Close valve\n", vbat);
+// }
+// LED_VBAT_OPEN;
+// DelayMs(100);
+// }
uint16_t VBAT_ProcessEvent(uint8_t task_id, uint16_t events)
{
@@ -109,15 +109,22 @@ uint16_t VBAT_ProcessEvent(uint8_t task_id, uint16_t events)
// tmos_start_task(vbat_task_id, VBAT_EVT_START, MS1_TO_SYSTEM_TIME(1000*60)); //1000*60
uint8_t retry_count = 0;
uint8_t success = 0;
+ int32_t vbat_mV = 0;
+ int32_t IN_VBAT_mV = 0;
while (retry_count < 3 && !success) {
ADC_GPIO_Init();
ADC_ChannelCfg(0);
adc_vbat = ADC_ExcutSingleConver() + RoughCalib_Value;
if (adc_vbat > 100 && adc_vbat < 6000) {
- vbat = (adc_vbat/1024.0-1)*1.05;
- IN_VBAT = vbat * 2;
- logDebug("adc_vbat = %d, IN_VBAT = %.2f V\n", adc_vbat, IN_VBAT);
+ // vbat = (adc_vbat/1024.0-1)*1.05;
+ // IN_VBAT = vbat * 2;
+ // logDebug("adc_vbat = %d, IN_VBAT = %.2f V\n", adc_vbat, IN_VBAT);
+ // 使用整数计算代替浮点计算 (adc_vbat/1024.0-1)*1.05 转换为定点运算
+ // 公式: ((adc_vbat * 1000) / 1024 - 1000) * 105 / 100
+ vbat_mV = ((adc_vbat * 1000) / 1024 - 1000) * 105 / 100;
+ IN_VBAT_mV = vbat_mV * 2;
+ logDebug("adc_vbat = %d, IN_VBAT = %d.%02d V\n", adc_vbat, IN_VBAT_mV/1000, (IN_VBAT_mV%1000)/10);
success = 1;
} else {
retry_count++;
@@ -128,7 +135,8 @@ uint16_t VBAT_ProcessEvent(uint8_t task_id, uint16_t events)
if (success) {
if (IN_VBAT < VBAT_LOW_THRESHOLD) {
- Handle_Low_Battery(IN_VBAT);
+ // Handle_Low_Battery(IN_VBAT);
+ IotFlag_t.Low_Voltage_flag = 1;
} else if (vbat_low_flag) {
vbat_low_flag = 0;
LED_VBAT_CLOSE;
@@ -138,7 +146,8 @@ uint16_t VBAT_ProcessEvent(uint8_t task_id, uint16_t events)
logDebug("Voltage measurement failed\n");
if (!vbat_low_flag) {
IN_VBAT = 1.0f;
- Handle_Low_Battery(IN_VBAT);
+ // Handle_Low_Battery(IN_VBAT);
+ IotFlag_t.Low_Voltage_flag = 1;
}
}
diff --git a/bsp/src/bsp_bmp390.c b/bsp/src/bsp_bmp390.c
index da3a1fe..ce40327 100644
--- a/bsp/src/bsp_bmp390.c
+++ b/bsp/src/bsp_bmp390.c
@@ -409,7 +409,7 @@ void Lower_IO_Deinit(void)
// shell.write = NULL; // 禁用shell输出
// 关闭外部低速晶振
- GPIOA_ModeCfg(GPIO_Pin_10 | GPIO_Pin_11, GPIO_ModeIN_PD);
+ // GPIOA_ModeCfg(GPIO_Pin_10 | GPIO_Pin_11, GPIO_ModeIN_PD);
}
void PRESS_LowPower(void)
@@ -738,6 +738,7 @@ uint16_t Check_ProcessEvent(uint8_t task_id, uint16_t events)
if(P[0] - P[2] >= 80) //8000
{
VALVE_CLOSE();
+ IotFlag_t.Valve_Close_flag = 1;
fault_state = 1;
Status_upload = 1;
tmos_start_task(check_task_id, MOTOR_STOP_EVT, MS1_TO_SYSTEM_TIME(1500)); //1000
@@ -747,6 +748,7 @@ uint16_t Check_ProcessEvent(uint8_t task_id, uint16_t events)
if(P[0] - P[2] <= 80) //800
{
VALVE_CLOSE();
+ IotFlag_t.Valve_Close_flag = 1;
fault_state = 2;
Status_upload = 1;
tmos_start_task(check_task_id, MOTOR_STOP_EVT, MS1_TO_SYSTEM_TIME(1500)); //1000
@@ -756,6 +758,7 @@ uint16_t Check_ProcessEvent(uint8_t task_id, uint16_t events)
if( P[0] - P[1] >= 7) //700
{
VALVE_CLOSE();
+ IotFlag_t.Valve_Close_flag = 1;
fault_state = 3;
Status_upload = 1;
tmos_start_task(check_task_id, MOTOR_STOP_EVT, MS1_TO_SYSTEM_TIME(1500)); //1000
@@ -768,6 +771,7 @@ uint16_t Check_ProcessEvent(uint8_t task_id, uint16_t events)
{
motor_flag = 0;
VALVE_OPEN();
+ IotFlag_t.Valve_Open_flag = 1;
fault_state = 0;
Status_upload = 2;
LED_VALVE_OPEN;
@@ -778,16 +782,17 @@ uint16_t Check_ProcessEvent(uint8_t task_id, uint16_t events)
{
motor_flag = 0;
VALVE_CLOSE();
+ IotFlag_t.Valve_Close_flag = 1;
Status_upload = 1;
LED_VALVE_CLOSE;
logDebug("motor/LED close");
tmos_start_task(check_task_id, MOTOR_STOP_EVT, MS1_TO_SYSTEM_TIME(1000));
}
- if(Status_upload){
- logDebug("BSP_M1307r start_send message!!!\n");
- BSP_Ml307r_Init();
- Status_upload = 0;
- }
+ // if(Status_upload){
+ // logDebug("BSP_M1307r start_send message!!!\n");
+ // BSP_Ml307r_Init();
+ // Status_upload = 0;
+ // }
tmos_start_task(check_task_id, CHECK_EVT_START, MS1_TO_SYSTEM_TIME(200)); //100
return (events ^ CHECK_EVT_START);
}
diff --git a/bsp/src/bsp_ml307r.c b/bsp/src/bsp_ml307r.c
index bf73472..ed8155a 100644
--- a/bsp/src/bsp_ml307r.c
+++ b/bsp/src/bsp_ml307r.c
@@ -8,14 +8,24 @@
#include "SLEEP.h"
#include "bsp_iwdg.h"
#include "bsp_bmp390.h"
+#include "bsp_tim.h"
#undef LOG_ENABLE
#define LOG_ENABLE 1
+IotFlagStruct IotFlag_t;
+
#define ML307_PERIODIC_EVT (0x0001 << 0)
-#define ML307_TIMEOUT_EVT (0x0001 << 1)
-#define ML307_POWER_ON_EVT (0x0001 << 2)
-#define ML307_POWER_OFF_EVT (0x0001 << 3)
+
+
+MultiTimer Multi_Power_ON_t; //开机
+MultiTimer Multi_Power_OFF_t; //关机
+MultiTimer Multi_Restart_t; //重启
+MultiTimer Multi_Cpin_t; //cpin
+MultiTimer Multi_Connect_t; //Connect
+MultiTimer Multi_Send_t; //Send
+MultiTimer Multi_StopTask_t; //StopTask
+MultiTimer Multi_Loop_t; //Loop
// 电源状态
typedef enum {
@@ -28,12 +38,27 @@ typedef enum {
static ml307_power_state_t ml307_power_state = POWER_STATE_INIT;
static tmosTaskID ml307_task_id = INVALID_TASK_ID;
-static tmosTaskID ml307_timeout_task_id = INVALID_TASK_ID;
-static tmosTaskID ml307_power_task_id = INVALID_TASK_ID;
at_obj_t *ml307r_obj;
uint8_t databuf[5] = {0x01,0xaa,0x12,0xde,0x39};
+
+int Ml307r_Gpio_Init(void)
+{
+ GPIOB_SetBits(ENABLE_3_8_V);
+ GPIOB_ModeCfg(ENABLE_3_8_V, GPIO_ModeOut_PP_5mA);
+
+ GPIOB_ResetBits(ML307_RST_PIN);
+ GPIOB_ModeCfg(ML307_RST_PIN, GPIO_ModeOut_PP_5mA);
+
+ GPIOB_ModeCfg(USIM_DECT_PIN, GPIO_ModeIN_Floating);
+
+ GPIOB_ModeCfg(ML307_PWR_PIN, GPIO_ModeOut_PP_5mA);
+
+
+ return 0;
+}
+
void At_Debug(const char *fmt, ...)
{
va_list args;
@@ -62,10 +87,78 @@ static const at_adapter_t at_adapter =
.urc_bufsize = 512
};
+static uint8_t state = 0;
+/*
+ * 时间间隔,单位:ms,默认:10s
+ */
+#define POWERON_RETRY_INTERVAL_TIME (10*1000UL)
+#define RESTART_RETRY_INTERVAL_TIME (40*1000UL)
+#define CPIN_RETRY_INTERVAL_TIME (20*1000UL)
+#define CONNECT_RETRY_INTERVAL_TIME (20*1000UL)
+#define SEND_RETRY_INTERVAL_TIME (30*1000UL)
+#define STOP_TASK_INTERVAL_TIME (20*1000UL)
+#define LOOP_INTERVAL_TIME (30*1000UL)
+
+//开机回调
+void Power_ON_t_CallBack(MultiTimer* timer, void* userData)
+{
+ IotFlag_t.PowerOn_error_flag = 1;
+}
+
+//重启超时回调
+void Restart_CallBack(MultiTimer* timer, void* userData)
+{
+ IotFlag_t.restart_error_flag = 1;
+}
+
+//Cpin超时回调
+void Cpin_CallBack(MultiTimer* timer, void* userData)
+{
+ IotFlag_t.sim_error_flag = 1;
+}
+
+//Connect超时回调
+void Connect_CallBack(MultiTimer* timer, void* userData)
+{
+ IotFlag_t.Connect_error_flag = 1;
+}
+
+//Send超时回调
+void Send_CallBack(MultiTimer* timer, void* userData)
+{
+ IotFlag_t.send_error_flag = 1;
+}
+
+//Loop回调
+void Loop_CallBack(MultiTimer* timer, void* userData)
+{
+ state = 0;
+ IotFlag_t.Loop_count_flag++;
+ IotFlag_t.module_init_flag = 1;
+ IotFlag_t.Iot_Retry_flag = 0;
+}
+
+//StopTask超时回调
+void StopTask_CallBack(MultiTimer* timer, void* userData)
+{
+ logDebug("BSP_Ml307_Power_Off\r\n");
+ IotFlag_t.Loop_count_flag = 0;
+ if(IotFlag_t.PowerOn_error_flag == 1)
+ {
+ IotFlag_t.PowerOn_error_flag = 0;
+ tmos_stop_task(ml307_task_id, ML307_PERIODIC_EVT);
+ BSP_RequestSleep();
+ }else{
+ BSP_Ml307_Power_Off(); //关机
+ tmos_stop_task(ml307_task_id, ML307_PERIODIC_EVT);
+ BSP_RequestSleep();
+ }
+}
+
//模块准备就绪
static int URC_Module_Ready_Cb(at_urc_info_t *info)
{
- logDebug("module ready complete\r\n");
+ logDebug("module CPIN READY ! \r\n");
return 0;
}
@@ -79,230 +172,6 @@ static const urc_item_t urc_table[] =
{.prefix = "+CPIN: READY", .endmark = '\n', .handler = URC_Module_Ready_Cb},//模块准备就绪
};
-// static void Ml307_Power_On(void)
-// {
-// #define ML307_POWER_ON_TIME 3
-
-// GPIOB_SetBits(ML307_PWR_PIN);
-// Ml307_Delay_Ms(ML307_POWER_ON_TIME * 1000);
-// GPIOB_ResetBits(ML307_PWR_PIN);
-// }
-
-// static void Ml307_Power_Off(void)
-// {
-// #define M307_POWER_OFF_TIME 4
-// GPIOB_SetBits(ML307_PWR_PIN);
-// Ml307_Delay_Ms(M307_POWER_OFF_TIME * 1000);
-// GPIOB_ResetBits(ML307_PWR_PIN);
-// Ml307_Delay_Ms(100);
-// }
-
-
-// 电源任务处理函数
-static uint16_t Ml307_Power_Handle(uint8_t task_id, uint16_t events)
-{
- if (events & ML307_POWER_ON_EVT)
- {
- switch(ml307_power_state)
- {
- case POWER_STATE_INIT:
- // 开始上电过程
- logDebug("ML307 power on start\n");
- GPIOB_SetBits(ML307_PWR_PIN);
- ml307_power_state = POWER_STATE_ON_START;
- // 安排3秒后的事件
- #define ML307_POWER_ON_TIME 5
- tmos_start_task(ml307_power_task_id, ML307_POWER_ON_EVT, MS1_TO_SYSTEM_TIME(ML307_POWER_ON_TIME * 1000));
- break;
-
- case POWER_STATE_ON_START:
- // 完成上电过程
- logDebug("ML307 power on complete\n");
- GPIOB_ResetBits(ML307_PWR_PIN);
- ml307_power_state = POWER_STATE_ON_COMPLETE;
- // 继续初始化过程
- if (ml307r_obj != NULL) {
- BSP_ML307_SetActive();
- // 触发模块初始化流程
- tmos_set_event(ml307_task_id, ML307_PERIODIC_EVT);
- // 执行后续初始化步骤
- logDebug("BSP_Read_Module\n");
- BSP_Read_Module();
- logDebug("simcom_init\n");
- simcom_init();
- BSP_ML307_SetActive();
- logDebug("BSP_Module_Connect_CtWing\n");
- BSP_Module_Connect_CtWing();
- logDebug("BSP_ML307_SendMessage\n");
- BSP_ML307_SetActive();
- // BSP_ML307_SendMessage();
- }
- break;
-
- default:
- break;
- }
- return (events ^ ML307_POWER_ON_EVT);
- }
-
- if (events & ML307_POWER_OFF_EVT)
- {
- switch(ml307_power_state)
- {
- case POWER_STATE_INIT:
- // 开始断电过程
- logDebug("ML307 power off start\n");
- GPIOB_SetBits(ML307_PWR_PIN);
- ml307_power_state = POWER_STATE_OFF_START;
- // 安排4秒后的事件
- #define M307_POWER_OFF_TIME 6
- tmos_start_task(ml307_power_task_id, ML307_POWER_OFF_EVT, MS1_TO_SYSTEM_TIME(M307_POWER_OFF_TIME * 1000));
- break;
-
- case POWER_STATE_OFF_START:
- // 完成断电过程
- logDebug("ML307 power off step 1\n");
- GPIOB_ResetBits(ML307_PWR_PIN);
- ml307_power_state = POWER_STATE_OFF_COMPLETE;
- // 安排100毫秒后的事件
- tmos_start_task(ml307_power_task_id, ML307_POWER_OFF_EVT, MS1_TO_SYSTEM_TIME(100));
- break;
-
- case POWER_STATE_OFF_COMPLETE:
- // 断电流程完全结束
- logDebug("ML307 power off complete\n");
- ml307_power_state = POWER_STATE_INIT;
- // 请求系统进入睡眠状态
- BSP_RequestSleep();
- break;
-
- default:
- break;
- }
- return (events ^ ML307_POWER_OFF_EVT);
- }
-
- return 0;
-}
-
-// 初始化电源控制任务
-static void Ml307_Power_Task_Init(void)
-{
- if (ml307_power_task_id == INVALID_TASK_ID)
- {
- ml307_power_task_id = TMOS_ProcessEventRegister(Ml307_Power_Handle);
- }
-}
-
-// 新的非阻塞式电源控制函数
-static void Ml307_Power_On_NonBlocking(void)
-{
- Ml307_Power_Task_Init();
- ml307_power_state = POWER_STATE_INIT;
- tmos_set_event(ml307_power_task_id, ML307_POWER_ON_EVT);
-}
-
-static void Ml307_Power_Off_NonBlocking(void)
-{
- Ml307_Power_Task_Init();
- ml307_power_state = POWER_STATE_INIT;
- tmos_set_event(ml307_power_task_id, ML307_POWER_OFF_EVT);
-}
-
-
-int Ml307r_Gpio_Init(void)
-{
- GPIOB_SetBits(ENABLE_3_8_V);
- GPIOB_ModeCfg(ENABLE_3_8_V, GPIO_ModeOut_PP_5mA);
-
- GPIOB_ResetBits(ML307_RST_PIN);
- GPIOB_ModeCfg(ML307_RST_PIN, GPIO_ModeOut_PP_5mA);
-
- GPIOB_ModeCfg(USIM_DECT_PIN, GPIO_ModeIN_Floating);
-
- GPIOB_ModeCfg(ML307_PWR_PIN, GPIO_ModeOut_PP_5mA);
-
-
- return 0;
-}
-
-
-static int Module_Read_State(at_env_t *e)
-{
- switch (e->state)
- {
- case 0:
- e->obj->adap->debug("Check if the Module is ready\r\n");
- e->println(e, "AT");
- e->reset_timer(e);
- e->state++;
- break;
- case 1:
- if (e->contains(e, "OK"))
- {
- e->recvclr(e);
- e->finish(e, AT_RESP_OK);
- e->obj->adap->debug("Module is ready\r\n");
- // BSP_Stop_ML307_Task();
- }
- if (e->is_timeout(e, 5000))
- {
- e->state--;
- if (++e->i > 3)
- {
- e->obj->adap->debug("Module error\r\n");
- e->finish(e, AT_RESP_ERROR);
- // BSP_Stop_ML307_Task();
- }
- }
- break;
- }
- return 0;
-}
-
-
-static void BSP_Read_Module(void)
-{
- at_do_work(ml307r_obj, NULL, Module_Read_State); // 重启后生效
-}
-
-
-/**
-* @brief 命令响应处理程序
-*/
-static void simcom_init_callback(at_response_t *r)
-{
- printf("SIM800C Init %s!\r\n",r->code == AT_RESP_OK ? "ok" : "error");
-}
-/*
-* @brief 模块初始化
-*/
-static void simcom_init(void)
-{
- at_attr_t attr;
- static const char *cmds[] = {
- // "AT+GSN=1",
- // "AT+CIMI",
- // "AT+MCCID",
- // "AT+CPIN?",
- // "AT+CEREG?",
- // "AT+CSQ",
- "AT+CPIN?", // 首先检查SIM卡状态
- "AT+CEREG?", // 然后检查网络注册状态
- "AT+CSQ", // 信号质量
- "AT+GSN=1", // 获取IMEI
- "AT+CIMI", // 获取IMSI (需要SIM卡就绪)
- "AT+MCCID", // 获取ICCID (需要SIM卡就绪)
- NULL,
- };
- at_attr_deinit(&attr);
- attr.cb = simcom_init_callback; //设置命令回调
- // 设置命令间延迟,给模块更多响应时间
- attr.timeout = 2000; // 增加超时时间到2秒
- attr.retry = 3; // 设置重试次数
- at_send_multiline(ml307r_obj, &attr, cmds);
-}
-
/*
* @brief 创建设备
* @return
@@ -313,19 +182,16 @@ static int BSP_Module_Connect_CtWing_Handle(at_env_t *e)
{
case 0:
logDebug("create device...\r\n");
- e->println(e, "AT+MIPOPEN=0,\"TCP\",\"112.125.89.8\",46763,,1"); // 8.135.10.183,32994
+ e->println(e, "AT+MIPOPEN=0,\"TCP\",\"112.125.89.8\",47602,,1"); // 8.135.10.183,32994
e->reset_timer(e);
e->state++;
break;
case 1:
if (e->contains(e, "CONNECT"))
{
+ IotFlag_t.Connect_success_flag = 1;
logDebug("create device complete\r\n");
e->finish(e, AT_RESP_OK);
- BSP_ML307_SetActive();
- BSP_ML307_SendMessage();
- // BSP_ML307_SetActive();
- // BSP_ML307_SendMessage();
}
else if (e->is_timeout(e, 5000))
{
@@ -333,6 +199,7 @@ static int BSP_Module_Connect_CtWing_Handle(at_env_t *e)
if (++e->i > 3)
{
logDebug("create device error\r\n");
+ IotFlag_t.Connect_error_flag = 1;
e->finish(e, AT_RESP_ERROR);
}
}
@@ -347,84 +214,6 @@ static void BSP_Module_Connect_CtWing(void)
{
at_do_work(ml307r_obj, NULL, BSP_Module_Connect_CtWing_Handle);//创建设备
}
-static int Module_Restart_Work(at_env_t *e)
-{
- switch (e->state)
- {
- case 0:
- logDebug("restart module...\r\n");
- e->println(e, "AT+CFUN=1,1");//重启模块
- e->reset_timer(e);
- e->state++;
- break;
- case 1:
- if (e->contains(e, "OK"))
- {
- logDebug("restart module complete\r\n");
- e->reset_timer(e);
- e->state++;
- }
- if (e->is_timeout(e, 5000))
- {
- e->state--;
- if (++e->i > 3)
- {
- logDebug("Module restart error\r\n");
- e->finish(e, AT_RESP_ERROR);
- }
- }
- break;
- case 2:
- logDebug("Check if the Module is ready\r\n");
- e->println(e, "AT");
- e->reset_timer(e);
- e->state++;
- break;
- case 3:
- if (e->contains(e, "OK"))
- {
- e->recvclr(e);
- e->finish(e, AT_RESP_OK);
- logDebug("Module is ready\r\n");
- }
- if (e->is_timeout(e, 5000))
- {
- e->state--;
- if (++e->i > 3)
- {
- logDebug("Module error\r\n");
- e->finish(e, AT_RESP_ERROR);
- }
- }
- break;
- }
- return 0;
-}
-//重启模块
-static void BSP_Restart_Module(void)
-{
- at_do_work(ml307r_obj, NULL, Module_Restart_Work);//重启后生效
-}
-
-void Ml307r_Loop(void)
-{
- at_obj_process(ml307r_obj);
-}
-
-__HIGH_CODE
-__attribute__((noinline))
-uint16_t Ml307r_Handle(uint8_t task_id, uint16_t events)
-{
- if (events & ML307_PERIODIC_EVT)
- {
- FEED_IWDG();
- Ml307r_Loop();
- tmos_start_task(ml307_task_id, ML307_PERIODIC_EVT, MS1_TO_SYSTEM_TIME(5)); //5
- return (events ^ ML307_PERIODIC_EVT);
- }
- return 0;
-}
-
//向服务器发送数据
@@ -464,115 +253,391 @@ void BSP_ML307_SendMessage(void)
BSP_Module_Send_Data((uint8_t*)json_buffer, strlen(json_buffer));
// logDebug("fault_state: %d\n", fault_state);
+ IotFlag_t.send_complete_flag = 1;
logDebug("BSP_ML307_SendMessage: %s\n", json_buffer);
}
}
-// 断电保护数据上报
-void BSP_Module_Emergency_Send(uint8_t* data, uint8_t len)
+static int Ml307_Power_On(at_env_t *e)
{
- if (ml307_task_id != INVALID_TASK_ID) {
- GPIOB_SetBits(ENABLE_3_8_V); // 确保3.8V供电正常
- BSP_Read_Module(); // 确保模块在工作状态
- at_attr_t attr;
- at_send_data(ml307r_obj, &attr, data, len);
- // Ml307_Power_Off();
- Ml307_Power_Off_NonBlocking(); // 使用非阻塞式关闭
- logDebug("Power failure protection data has been sent\n");
- } else {
- BSP_Ml307r_Init(); // 如果模块未初始化,先初始化
- logDebug("4G initialization\n");
- at_attr_t attr;
- at_send_data(ml307r_obj, &attr, data, len);
- // Ml307_Power_Off();
- Ml307_Power_Off_NonBlocking(); // 使用非阻塞式关闭
- logDebug("Power failure protection data has been sent\n");
- }
-}
-
-void BSP_Ml307r_Init(void)
-{
- if(ml307_power_state == POWER_STATE_INIT)
+ switch (e->state)
{
- Ml307r_Gpio_Init();
-
- // Ml307_Power_On();
-
- BSP_UART1_Init();
-
- ml307r_obj = at_obj_create(&at_adapter);//Create AT communication object
- // at_obj_set_urc(ml307r_obj, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
- ml307_task_id = TMOS_ProcessEventRegister(Ml307r_Handle);
- // tmos_set_event(ml307_task_id, ML307_PERIODIC_EVT);
-
- // 初始化超时任务
- BSP_ML307_SetActive();
-
- // 启动非阻塞式电源开启,后续初始化会在电源开启完成后自动进行
- Ml307_Power_On_NonBlocking();
-
-
-
-
- // logDebug("BSP_Read_Module\n");
- // BSP_Read_Module();
- // logDebug("simcom_init\n");
- // simcom_init();
- // logDebug("BSP_Module_Connect_CtWing\n");
- // BSP_Module_Connect_CtWing();
- // logDebug("BSP_Module_Send_Data\n");
- // // BSP_Module_Send_Data(databuf, 5);
- // BSP_ML307_SendMessage();
- }
-
-}
-
-// 停止4G通信,停止轮询任务
-void BSP_Stop_ML307_Task(void)
-{
- if (ml307_task_id != INVALID_TASK_ID) {
- tmos_stop_task(ml307_task_id, ML307_PERIODIC_EVT);
- // Ml307_Power_Off();
- Ml307_Power_Off_NonBlocking();
- logDebug("4G module polling stopped\n");
- // BSP_RequestSleep();
- }
-}
-
-/**
- * @brief ML307超时任务处理函数
- */
-static uint16_t BSP_ML307_TimeoutTask(uint8_t task_id, uint16_t events)
-{
- if (events & ML307_TIMEOUT_EVT)
- {
- logDebug("ML307 timeout, stopping task\n");
- BSP_Stop_ML307_Task();
- return (events ^ ML307_TIMEOUT_EVT);
+ case 0:
+ logDebug("Ml307_Power_On start!\r\n");
+ GPIOB_SetBits(ML307_PWR_PIN);
+ e->reset_timer(e);
+ e->state++;
+ break;
+ case 1:
+ #define ML307_POWER_ON_TIME 3
+ if (e->is_timeout(e, ML307_POWER_ON_TIME *1000))
+ {
+ logDebug("Ml307_Power_On end!\r\n");
+ GPIOB_ResetBits(ML307_PWR_PIN);
+ IotFlag_t.PowerOn_success_flag = 1;
+ e->finish(e, AT_RESP_OK);
+ }
+ break;
}
return 0;
}
-/**
- * @brief 启动ML307活动超时定时器
- */
-void BSP_ML307_StartTimeoutTimer(void)
+static void BSP_Ml307_Power_On(void)
{
- if (ml307_timeout_task_id == INVALID_TASK_ID)
+ at_do_work(ml307r_obj, NULL, Ml307_Power_On);
+}
+
+
+static int Ml307_Power_Off(at_env_t *e)
+{
+ switch (e->state)
{
- ml307_timeout_task_id = TMOS_ProcessEventRegister(BSP_ML307_TimeoutTask);
+ case 0:
+ logDebug("Ml307_Power_Off start!\r\n");
+ GPIOB_SetBits(ML307_PWR_PIN);
+ e->reset_timer(e);
+ e->state++;
+ break;
+ case 1:
+ #define M307_POWER_OFF_TIME 4
+ if (e->is_timeout(e, M307_POWER_OFF_TIME * 1000))
+ {
+ GPIOB_ResetBits(ML307_PWR_PIN);
+ logDebug("Ml307_Power_Off end!\r\n");
+ e->reset_timer(e);
+ e->state++;
+ }
+ break;
+ case 2:
+ if (e->is_timeout(e, 1000))
+ {
+ e->finish(e, AT_RESP_OK);
+ }
+ break;
}
- tmos_stop_task(ml307_timeout_task_id, ML307_TIMEOUT_EVT);
- tmos_start_task(ml307_timeout_task_id, ML307_TIMEOUT_EVT, MS1_TO_SYSTEM_TIME(10000)); // 5秒超时
+ return 0;
}
-/**
- * @brief 将ML307设置为活动状态,阻止系统睡眠
- */
-void BSP_ML307_SetActive(void)
+static void BSP_Ml307_Power_Off(void)
{
- BSP_BlockSleep();
- BSP_ML307_StartTimeoutTimer();
+ at_do_work(ml307r_obj, NULL, Ml307_Power_Off);
}
+
+static int Module_Restart_Work(at_env_t *e)
+{
+ switch (e->state)
+ {
+ case 0:
+ logDebug("restart module...\r\n");
+ e->println(e, "AT+CFUN=1,1");//重启模块
+ e->reset_timer(e);
+ e->state++;
+ break;
+ case 1:
+ if (e->contains(e, "OK"))
+ {
+ logDebug("restart module complete\r\n");
+ e->reset_timer(e);
+ e->state++;
+ }
+ if (e->is_timeout(e, 5000))
+ {
+ e->state--;
+ if (++e->i > 3)
+ {
+ IotFlag_t.restart_error_flag = 1;
+ logDebug("Module restart error\r\n");
+ e->finish(e, AT_RESP_ERROR);
+ }
+ }
+ break;
+ case 2:
+ logDebug("Check if the Module is ready\r\n");
+ e->println(e, "AT");
+ e->reset_timer(e);
+ e->state++;
+ break;
+ case 3:
+ if (e->contains(e, "OK"))
+ {
+ IotFlag_t.restart_success_flag = 1;
+ e->recvclr(e);
+ e->finish(e, AT_RESP_OK);
+ logDebug("Module is ready\r\n");
+ }
+ if (e->is_timeout(e, 5000))
+ {
+ e->state--;
+ if (++e->i > 3)
+ {
+ IotFlag_t.restart_error_flag = 1;
+ logDebug("Module error\r\n");
+ e->finish(e, AT_RESP_ERROR);
+ }
+ }
+ break;
+ }
+ return 0;
+}
+//重启模块
+static void BSP_Restart_Module(void)
+{
+ at_do_work(ml307r_obj, NULL, Module_Restart_Work);//重启后生效
+}
+
+
+
+static int simcom_init(at_env_t *e)
+{
+ switch (e->state)
+ {
+ case 0:
+ logDebug("simcom_cpin!\r\n");
+ e->println(e, "AT+CPIN?");
+ e->reset_timer(e);
+ e->state++;
+ break;
+ case 1:
+ if (e->contains(e, "+CPIN: READY"))
+ {
+ IotFlag_t.sim_success_flag = 1;
+ logDebug("simcom CPIN READY!\r\n");
+ e->finish(e, AT_RESP_OK);
+ }
+ if (e->is_timeout(e, 5000))
+ {
+ e->state--;
+ if (++e->i > 3)
+ {
+ IotFlag_t.sim_error_flag = 1;
+ logDebug("simcom CPIN NO READY!\r\n");
+ e->finish(e, AT_RESP_ERROR);
+ }
+ }
+ break;
+ }
+ return 0;
+}
+//CPIN
+static void BSP_simcom_init(void)
+{
+ at_do_work(ml307r_obj, NULL, simcom_init);
+}
+
+
+static void BSP_Iot_Loop(void)
+{
+ switch (state)
+ {
+ case 0: //开机
+ if(IotFlag_t.module_init_flag) //初始化正常
+ {
+ logDebug("IotFlag_t.module_init_flag %d\r\n",IotFlag_t.module_init_flag);
+ IotFlag_t.module_init_flag = 0;
+ MultiTimerStart(&Multi_Power_ON_t,POWERON_RETRY_INTERVAL_TIME, Power_ON_t_CallBack, NULL);
+ logDebug("BSP_Ml307_Power_On\r\n");
+ BSP_Ml307_Power_On();
+ }
+ if (IotFlag_t.PowerOn_success_flag) //开机成功
+ {
+ logDebug("BSP_Ml307_Power_On!\r\n");
+ MultiTimerStop(&Multi_Power_ON_t);
+ state++;
+ }
+ if (IotFlag_t.PowerOn_error_flag)
+ {
+ logDebug("PowerOn_error_flag!\r\n");
+ state = 5;
+ }
+ break;
+ case 1: //重启
+ if(IotFlag_t.PowerOn_success_flag) //开机正常
+ {
+ IotFlag_t.PowerOn_success_flag = 0;
+ MultiTimerStart(&Multi_Restart_t, RESTART_RETRY_INTERVAL_TIME, Restart_CallBack, NULL);
+ BSP_Restart_Module();
+ }
+ if (IotFlag_t.restart_success_flag) //重启成功
+ {
+ logDebug("restart_success_flag!\r\n");
+ MultiTimerStop(&Multi_Restart_t);
+ state++;
+ }
+ if(IotFlag_t.restart_error_flag == 1)
+ {
+ MultiTimerStop(&Multi_Restart_t);
+ IotFlag_t.restart_error_flag = 0;
+ state = 5;
+ }
+ break;
+ case 2: //sim状态
+ if(IotFlag_t.restart_success_flag) //重启成功
+ {
+ IotFlag_t.restart_success_flag = 0;
+ MultiTimerStart(&Multi_Cpin_t, CPIN_RETRY_INTERVAL_TIME, Cpin_CallBack, NULL);
+ BSP_simcom_init();
+ }
+ if (IotFlag_t.sim_success_flag) //sim成功
+ {
+ logDebug("BSP_Get_Module_Info!\r\n");
+ // BSP_Get_Module_Info(); //获取sim信息
+ MultiTimerStop(&Multi_Cpin_t);
+ state++;
+ }
+ if(IotFlag_t.sim_error_flag == 1)
+ {
+ logDebug("BSP_simcom_init Error !\r\n");
+ MultiTimerStop(&Multi_Cpin_t);
+ IotFlag_t.sim_error_flag = 0;
+ state = 5;
+ }
+ break;
+ case 3: //连接
+ if(IotFlag_t.sim_success_flag) //sim正常
+ {
+ IotFlag_t.sim_success_flag = 0;
+ MultiTimerStart(&Multi_Connect_t, CONNECT_RETRY_INTERVAL_TIME, Connect_CallBack, NULL);
+ BSP_Module_Connect_CtWing();
+ }
+ if (IotFlag_t.Connect_success_flag) //连接成功
+ {
+ logDebug("Connect_success_flag!\r\n");
+ MultiTimerStop(&Multi_Connect_t);
+ state++;
+ }
+ if(IotFlag_t.Connect_error_flag == 1)
+ {
+ logDebug("Connect_error_flag!\r\n");
+ IotFlag_t.Connect_error_flag = 0;
+ MultiTimerStop(&Multi_Connect_t);
+ state = 5;
+ }
+ break;
+ case 4: //发送数据
+ if(IotFlag_t.Connect_success_flag) //连接正常
+ {
+ IotFlag_t.Connect_success_flag = 0;
+ MultiTimerStart(&Multi_Send_t, SEND_RETRY_INTERVAL_TIME, Send_CallBack, NULL);
+ BSP_ML307_SendMessage();
+ }
+ if (IotFlag_t.send_complete_flag) //发送成功
+ {
+ logDebug("send_complete_flag!\r\n");
+ MultiTimerStop(&Multi_Send_t);
+ state++;
+ }
+ if(IotFlag_t.send_error_flag == 1)
+ {
+ logDebug("send_error_flag!\r\n");
+ MultiTimerStop(&Multi_Send_t);
+ IotFlag_t.send_error_flag = 0;
+ state = 5;
+ }
+ break;
+ case 5: //处理
+ if(IotFlag_t.send_complete_flag == 1)
+ {
+ logDebug("send_complete_into_stop\r\n");
+ IotFlag_t.send_complete_flag = 0;
+ state = 0;
+ MultiTimerStart(&Multi_StopTask_t, STOP_TASK_INTERVAL_TIME, StopTask_CallBack, NULL); //关闭
+ }else
+ {
+ if (IotFlag_t.Loop_count_flag > 2)
+ {
+ logDebug("Loop_count_flag_end %d\r\n",IotFlag_t.Loop_count_flag);
+ state = 0;
+ IotFlag_t.Iot_Retry_flag = 0;
+ IotFlag_t.module_init_flag = 0;
+ MultiTimerStart(&Multi_StopTask_t, STOP_TASK_INTERVAL_TIME, StopTask_CallBack, NULL);
+ }
+ if((IotFlag_t.Iot_Retry_flag == 0)&&(IotFlag_t.Loop_count_flag < 3))
+ {
+ logDebug("error_into_loop\r\n");
+ logDebug("Loop_count_flag %d\r\n",IotFlag_t.Loop_count_flag);
+ BSP_Ml307_Power_Off(); //关机
+ MultiTimerStart(&Multi_Loop_t, LOOP_INTERVAL_TIME, Loop_CallBack, NULL);
+ IotFlag_t.Iot_Retry_flag = 1;
+ }
+ }
+ break;
+ }
+}
+
+static int Module_Read_State(at_env_t *e)
+{
+ switch (e->state)
+ {
+ case 0:
+ e->obj->adap->debug("Check if the Module is ready\r\n");
+ e->println(e, "AT");
+ e->reset_timer(e);
+ e->state++;
+ break;
+ case 1:
+ if (e->contains(e, "OK"))
+ {
+ e->recvclr(e);
+ e->finish(e, AT_RESP_OK);
+ e->obj->adap->debug("Module is ready\r\n");
+ // BSP_Stop_ML307_Task();
+ }
+ if (e->is_timeout(e, 5000))
+ {
+ e->state--;
+ if (++e->i > 3)
+ {
+ e->obj->adap->debug("Module error\r\n");
+ e->finish(e, AT_RESP_ERROR);
+ // BSP_Stop_ML307_Task();
+ }
+ }
+ break;
+ }
+ return 0;
+}
+static void BSP_Read_Module(void)
+{
+ at_do_work(ml307r_obj, NULL, Module_Read_State); // 重启后生效
+}
+
+
+void Ml307r_Loop(void)
+{
+ FEED_IWDG();
+ at_obj_process(ml307r_obj);
+ BSP_Iot_Loop();
+}
+
+__HIGH_CODE
+__attribute__((noinline))
+uint16_t Ml307r_Handle(uint8_t task_id, uint16_t events)
+{
+ if (events & ML307_PERIODIC_EVT)
+ {
+ Ml307r_Loop();
+ tmos_start_task(ml307_task_id, ML307_PERIODIC_EVT, MS1_TO_SYSTEM_TIME(5)); //5
+ return (events ^ ML307_PERIODIC_EVT);
+ }
+ return 0;
+}
+
+void BSP_Ml307r_Init(void)
+{
+ // if(ml307_power_state == POWER_STATE_INIT)
+ // {
+ Ml307r_Gpio_Init();
+ BSP_UART1_Init();
+ ml307r_obj = at_obj_create(&at_adapter);//Create AT communication object
+ if(ml307r_obj != NULL)
+ {
+ at_obj_set_urc(ml307r_obj, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
+ IotFlag_t.module_init_flag = 1;
+ BSP_BlockSleep();
+ }
+ ml307_task_id = TMOS_ProcessEventRegister(Ml307r_Handle);
+ tmos_set_event(ml307_task_id, ML307_PERIODIC_EVT);
+ // }
+}
diff --git a/bsp/src/bsp_tim.c b/bsp/src/bsp_tim.c
index ba2db63..5035166 100644
--- a/bsp/src/bsp_tim.c
+++ b/bsp/src/bsp_tim.c
@@ -1,5 +1,5 @@
#include "bsp_tim.h"
-
+#include
// tick_1ms_cnt在SysTick_Handler()中1ms +1
volatile uint32_t tick_1ms_cnt = 0;
@@ -11,7 +11,9 @@ volatile uint32_t tick_1ms_cnt = 0;
uint32_t BSP_Get_Tick(void)
{
/* Platform implementation */
- return tick_1ms_cnt;
+ // return tick_1ms_cnt;
+ // return (uint32_t)((double)TMOS_GetSystemClock() / 1.6);
+ return (uint32_t)((double)TMOS_GetSystemClock() / 2.3); // 定时不准,临时修改
}
@@ -24,7 +26,98 @@ void SysTick_Handler()
SysTick->SR = 0; // 清除中断标志
tick_1ms_cnt++;
-
+
cnt_ms++;
}
+
+
+/* Timer handle list head. */
+static MultiTimer* timerList = NULL;
+
+/* Timer tick */
+static PlatformTicksFunction_t platformTicksFunction = NULL;
+
+int MultiTimerInstall(PlatformTicksFunction_t ticksFunc)
+{
+ platformTicksFunction = ticksFunc;
+ return 0;
+}
+
+int MultiTimerStart(MultiTimer* timer, uint64_t timing, MultiTimerCallback callback, void* userData)
+{
+ if (!timer || !callback )
+ {
+ return -1;
+ }
+ MultiTimer** nextTimer = &timerList;
+ /* Remove the existing target timer. */
+ for (; *nextTimer; nextTimer = &(*nextTimer)->next)
+ {
+ if (timer == *nextTimer)
+ {
+ *nextTimer = timer->next; /* remove from list */
+ break;
+ }
+ }
+
+ /* Init timer. */
+ timer->deadline = platformTicksFunction() + timing;
+ timer->callback = callback;
+ timer->userData = userData;
+
+ /* Insert timer. */
+ for (nextTimer = &timerList;; nextTimer = &(*nextTimer)->next)
+ {
+ if (!*nextTimer)
+ {
+ timer->next = NULL;
+ *nextTimer = timer;
+ break;
+ }
+ if (timer->deadline < (*nextTimer)->deadline)
+ {
+ timer->next = *nextTimer;
+ *nextTimer = timer;
+ break;
+ }
+ }
+ return 0;
+}
+
+int MultiTimerStop(MultiTimer* timer)
+{
+ MultiTimer** nextTimer = &timerList;
+ /* Find and remove timer. */
+ for (; *nextTimer; nextTimer = &(*nextTimer)->next)
+ {
+ MultiTimer* entry = *nextTimer;
+ if (entry == timer) {
+ *nextTimer = timer->next;
+ break;
+ }
+ }
+ return 0;
+}
+
+int MultiTimerYield(void)
+{
+ MultiTimer* entry = timerList;
+ for (; entry; entry = entry->next)
+ {
+ /* Sorted list, just process with the front part. */
+ if (platformTicksFunction() < entry->deadline)
+ {
+ return (int)(entry->deadline - platformTicksFunction());
+ }
+ /* remove expired timer from list */
+ timerList = entry->next;
+
+ /* call callback */
+ if (entry->callback)
+ {
+ entry->callback(entry, entry->userData);
+ }
+ }
+ return 0;
+}
diff --git a/common/AT-Command-master/src/at_port.c b/common/AT-Command-master/src/at_port.c
index 83abb7f..282242e 100644
--- a/common/AT-Command-master/src/at_port.c
+++ b/common/AT-Command-master/src/at_port.c
@@ -15,7 +15,8 @@
*/
void *at_malloc(unsigned int nbytes)
{
- return malloc(nbytes);
+ // return malloc(nbytes);
+ return tmos_msg_allocate(nbytes);
}
/**
@@ -23,7 +24,8 @@ void *at_malloc(unsigned int nbytes)
*/
void at_free(void *ptr)
{
- free(ptr);
+ // free(ptr);
+ tmos_msg_deallocate(ptr);
}
/**