diff --git a/.vscode/settings.json b/.vscode/settings.json index a8d666d..025ba1c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -81,6 +81,7 @@ "stdarg.h": "c", "math.h": "c", "ctime": "c", - "bsp_wdg.h": "c" + "bsp_wdg.h": "c", + "rtdbg.h": "c" } } diff --git a/applications/main.c b/applications/main.c index 52cc865..63412d5 100644 --- a/applications/main.c +++ b/applications/main.c @@ -2,7 +2,7 @@ * @Author: mbw * @Date: 2024-10-23 17:14:16 * @LastEditors: mbw && 1600520629@qq.com - * @LastEditTime: 2025-06-17 08:43:06 + * @LastEditTime: 2025-06-21 10:00:44 * @FilePath: \ble_bjq_ch303rct6_ml307\applications\main.c * @Descrt_thread_ * @@ -489,7 +489,7 @@ int main(void) SysControl.status = kAlarmEvent; LED_R_ALARM; BEEP_ALARM; - + alarm_flag = 1; LOG_I("报警机械手动作"); EMV_FORWARD_ON(50); rt_thread_mdelay(10); @@ -522,7 +522,7 @@ int main(void) SysControl.last_status = SysControl.status; SysControl.status = kAlarmRcyEvent; Flash_Write_Record(kRecordAlarmRcy); - + alarm_flag = 0; BEEP_STOP; #if (IOT_MODULE_SWITCH == 1) @@ -537,7 +537,7 @@ int main(void) SysControl.last_status = SysControl.status; SysControl.status = kFaultEvent; - + fault_flag = 1; Flash_Write_Record(kRecordFault); LED_Y_FAULT; @@ -552,7 +552,7 @@ int main(void) SysControl.last_status = SysControl.status; SysControl.status = kFaultRcyEvent; - + fault_flag = 0; Flash_Write_Record(kRecordFaultRcy); BEEP_STOP; diff --git a/bsp/inc/bsp_mq.h b/bsp/inc/bsp_mq.h index 54ec4b9..b540bb0 100644 --- a/bsp/inc/bsp_mq.h +++ b/bsp/inc/bsp_mq.h @@ -21,6 +21,7 @@ #ifndef __BSP_MQ_H__ #define __BSP_MQ_H__ +#include "rtthread.h" #include "board.h" // MQ采样间隔 @@ -92,6 +93,8 @@ typedef enum kSensorAbnormal, } TeH308Status; +extern rt_uint8_t alarm_flag; +extern rt_uint8_t fault_flag; int BSP_MQ_Init(void); uint16_t Get_Gas_VoltageInt1000x(void); diff --git a/bsp/src/at_device_ml307.c b/bsp/src/at_device_ml307.c index 6491f57..64740d1 100644 --- a/bsp/src/at_device_ml307.c +++ b/bsp/src/at_device_ml307.c @@ -2,7 +2,7 @@ * @Author: mbw * @Date: 2024-11-30 15:46:21 * @LastEditors: mbw && 1600520629@qq.com - * @LastEditTime: 2025-06-05 15:28:05 + * @LastEditTime: 2025-06-21 10:37:54 * @FilePath: \ble_bjq_ch303rct6_ml307\bsp\src\at_device_ml307.c * @Description: * @@ -328,12 +328,12 @@ static int at_device_ml307_connect_tcp (struct at_device *device) { if (at_resp_get_line_by_kw (resp, "OK") != RT_NULL) { - if (rt_sem_take (ml307_connect_flag_sem, 5000) == RT_EOK) + rt_thread_mdelay (1000); + if (rt_sem_take (ml307_connect_flag_sem, 8000) == RT_EOK) { if (ml307_conncet_tcp_flag) { LOG_D ("ml307 connect to tcp server success"); - ml307_conncet_tcp_flag = 1; at_delete_resp (resp); return RT_EOK; } @@ -395,60 +395,57 @@ int at_send_data (struct at_device *device, const char *data, rt_size_t send_len return -RT_ERROR; } -static int ml307_check_link_status (struct at_device *device) +static int ml307_check_link_status(struct at_device *device) { at_response_t resp = RT_NULL; +// struct at_device_ml307 *ml307 = RT_NULL; int result = -RT_ERROR; int link_stat = 0; - RT_ASSERT (device); + RT_ASSERT(device); - resp = at_create_resp (96, 0, rt_tick_from_millisecond (300)); +// ml307 = (struct at_device_ml307 *)device->user_data; + + resp = at_create_resp(96, 0, rt_tick_from_millisecond(300)); if (resp == RT_NULL) { - LOG_E ("no memory for resp create."); + LOG_E("no memory for resp create."); return -RT_ERROR; } - // if (at_obj_exec_cmd(device->client, resp, "AT+MIPCALL?") < 0) - // { - // result = -RT_ERROR; - // goto __exit; - // } - // //// 查询拨号状态 - // if (at_resp_parse_line_args_by_kw(resp, "+MIPCALL:", "+MIPCALL: %d,%*d,%*s", &link_stat) > 0) - // { - // if (link_stat == 1) - // { - // result = RT_EOK; - // } - // } - // else - // { - // LOG_E("AT+MIPCALL error."); - // result = -RT_ERROR; - // goto __exit; - // } + if(at_obj_exec_cmd(device->client, resp, "AT+MIPCALL?") < 0) + { + result = -RT_ERROR; + goto __exit; + } - if (at_obj_exec_cmd (device->client, resp, "AT+CGACT?") < 0) + if (at_resp_parse_line_args_by_kw(resp, "+MIPCALL:", "+MIPCALL: %d,%*d,%*s", &link_stat) > 0) + { + if (link_stat == 1) + { + result = RT_EOK; + } + } + + if(at_obj_exec_cmd(device->client, resp, "AT+CGACT?") < 0) { result = -RT_ERROR; goto __exit; } //+CGACT: 1,1 - if (at_resp_parse_line_args_by_kw (resp, "+CGACT:", "+CGACT: %d,%*d", &link_stat) > 0) + if (at_resp_parse_line_args_by_kw(resp, "+CGACT:", "+CGACT: %d,%*d", &link_stat) > 0) { result = link_stat; } __exit: - if (resp) + if(resp) { - at_delete_resp (resp); + at_delete_resp(resp); } - return (result); + return(result); } /* ============================= ml307 network interface operations ============================= */ @@ -528,38 +525,46 @@ __exit: * * @param netdev: ml307 network interface device */ -static void ml307_check_link_status_entry (void *parameter) +static void ml307_check_link_status_entry(void *parameter) { -#define ML307_LINK_DELAY_TIME (60 * RT_TICK_PER_SECOND) - -#define ML307_LINK_STATUS_OK 1 +#define ML307_LINK_STATUS_OK 1 +#define ML307_LINK_RESP_SIZE 128 +#define ML307_LINK_RESP_TIMO (3 * RT_TICK_PER_SECOND) +#define ML307_LINK_DELAY_TIME (3000 * RT_TICK_PER_SECOND) + at_response_t resp = RT_NULL; int link_status; struct at_device *device = RT_NULL; struct netdev *netdev = (struct netdev *)parameter; - device = at_device_get_by_name (AT_DEVICE_NAMETYPE_NETDEV, netdev->name); + device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name); if (device == RT_NULL) { - LOG_E ("get device(%s) failed.", netdev->name); + LOG_E("get device(%s) failed.", netdev->name); return; } - at_response_t resp = at_create_resp (ML307_IMEI_RESP_SIZE, 0, ML307_INFO_RESP_TIMO); + + resp = at_create_resp(ML307_LINK_RESP_SIZE, 0, ML307_LINK_RESP_TIMO); if (resp == RT_NULL) { - LOG_E ("no memory for resp create."); + LOG_E("no memory for response create."); return; } - while (1) + while(1) { - link_status = ml307_check_link_status (device); - if (link_status < 0) + link_status = ml307_check_link_status(device);//固定时间检查连接状态 + if(link_status < 0) { - rt_thread_mdelay (ML307_LINK_DELAY_TIME); - // at_exec_cmd(resp, "AT+MIPCALL=1,1"); + rt_thread_mdelay(ML307_LINK_DELAY_TIME); continue; } - rt_thread_delay (ML307_LINK_DELAY_TIME); + /* check the network interface device link status */ + if ((ML307_LINK_STATUS_OK == link_status) != netdev_is_link_up(netdev)) + { + netdev_low_level_set_link_status(netdev, (ML307_LINK_STATUS_OK == link_status)); + } + + rt_thread_mdelay(ML307_LINK_DELAY_TIME); } } @@ -1229,6 +1234,21 @@ __exit: } } +//当网络状态更改为断网时,释放断连信号量,启动重连机制 +static void ml307_netdev_status_changed_callback(struct netdev *netdev, enum netdev_cb_type type) +{ + if (type == NETDEV_CB_STATUS_LINK_DOWN) + { + LOG_I("Network link down detected, triggering reconnect..."); + + // 释放断连信号量,用于触发重连线程开始工作 + if (ml307_disconnect_sem != NULL) + { + rt_sem_release(ml307_disconnect_sem); + } + } +} + /* ml307 device network initialize */ static int ml307_net_init (struct at_device *device) { @@ -1236,7 +1256,7 @@ static int ml307_net_init (struct at_device *device) rt_thread_t tid; /* 初始化完成量对象 */ rt_completion_init (&ml307_init_complate); - + netdev_set_status_callback(device->netdev, ml307_netdev_status_changed_callback); tid = rt_thread_create ("ml307_net", ml307_init_thread_entry, (void *)device, ML307_THREAD_STACK_SIZE, ML307_THREAD_PRIORITY, 20); if (tid) diff --git a/bsp/src/bsp_ml307.c b/bsp/src/bsp_ml307.c index 9470acc..a4f5a21 100644 --- a/bsp/src/bsp_ml307.c +++ b/bsp/src/bsp_ml307.c @@ -2,7 +2,7 @@ * @Author: mbw * @Date: 2024-11-30 15:46:21 * @LastEditors: mbw && 1600520629@qq.com - * @LastEditTime: 2025-06-06 14:02:27 + * @LastEditTime: 2025-06-21 10:41:27 * @FilePath: \ble_bjq_ch303rct6_ml307\bsp\src\bsp_ml307.c * @Description: * @@ -70,6 +70,8 @@ Ml307Event ml307_latest_evnet = kMl307PowerOnEvent; // 最新传入的事件 rt_uint8_t power_on_send_flag = 0; rt_mutex_t ml307_mutex; +static rt_uint32_t retry_delay = 8000; + extern valve_data valve[MAX_VALVE_NUM]; int BSP_Ml307_Init (struct Ml307_Ops *ops, rt_uint8_t version); @@ -1013,6 +1015,7 @@ int Ml307_Process_Events (Ml307Event ml307_recv_event, struct at_device *device, } else { + retry_delay = 8000;//如果有事件进来,重置这个延时 ret = RT_ERROR; break; } @@ -1951,7 +1954,7 @@ static void Ml307_Life_Thread_Entry (void *parameter) rt_err_t result = RT_ETIMEOUT; rt_uint32_t delay_n = 0; at_response_t resp = RT_NULL; - rt_uint32_t retry_delay = 8000; + LOG_D ("ml307_life_thread entry\n"); while (1) @@ -2078,21 +2081,21 @@ static void Ml307_Life_Thread_Entry (void *parameter) result = -RT_ERROR; goto __exit; } - for (i = 0; i < 5; i++) - { - if (at_obj_exec_cmd (device->client, resp, "AT+CGDCONT=1,\"IPV4V6\",\"cmnet\"") == RT_EOK) - { - LOG_D ("%s device Define PDP Context Success.", device->name); - break; - } - rt_thread_mdelay (1000); - } - if (i == 5) - { - LOG_E ("%s device Define PDP Context failed.", device->name); - result = -RT_ERROR; - goto __exit; - } + // for (i = 0; i < 5; i++) + // { + // if (at_obj_exec_cmd (device->client, resp, "AT+CGDCONT=1,\"IPV4V6\",\"cmnet\"") == RT_EOK) + // { + // LOG_D ("%s device Define PDP Context Success.", device->name); + // break; + // } + // rt_thread_mdelay (1000); + // } + // if (i == 5) + // { + // LOG_E ("%s device Define PDP Context failed.", device->name); + // result = -RT_ERROR; + // goto __exit; + // } if (at_obj_exec_cmd (device->client, resp, "AT+MIPCALL=1,1") != RT_EOK) { result = -RT_ERROR; diff --git a/bsp/src/bsp_mq.c b/bsp/src/bsp_mq.c index 2bbb437..b80907b 100644 --- a/bsp/src/bsp_mq.c +++ b/bsp/src/bsp_mq.c @@ -34,12 +34,13 @@ ALIGN(RT_ALIGN_SIZE) static char sensor_thread_stack[GAS_SNESOR_THREAD_STACK_SIZE]; static struct rt_thread Sensor_Thread; -rt_uint8_t alarm_flag = 0, fault_flag = 0; + TsSensor_t Sensor_device; +rt_uint8_t alarm_flag = 0, fault_flag = 0; + uint16_t Get_Gas_VoltageInt1000x(void) { - uint16_t voltage = Get_ADC_Voltage(kGasAdc) * MQ_VOLTAGE_RATIO * 1000; LOG_D("Get_Gas_VoltageInt1000x = %04d", voltage); return voltage; @@ -47,8 +48,8 @@ uint16_t Get_Gas_VoltageInt1000x(void) uint16_t Get_Gas_VoltageAdcInt1000x(void) { - rt_uint16_t voltage_adc = (Get_ADC_Average(kGasAdc) * 3.3 / 4096) * 1000; - // LOG_D("Get_Gas_VoltageAdcInt1000x = %04d", voltage_adc); + rt_uint16_t voltage_adc = Get_ADC_Voltage(kGasAdc) * 1000; + LOG_D("Get_Gas_VoltageAdcInt1000x = %04d", voltage_adc); return voltage_adc; }