diff --git a/applications/_main.c b/applications/_main.c index 5a29a85..4f6abb1 100644 --- a/applications/_main.c +++ b/applications/_main.c @@ -202,31 +202,57 @@ void chkAndSendFile() //tcp连接保活 //实际场景不存在中间断掉的可能 -void iniCnt() +void initTT_thread_entry() { - rt_hw_stm32_eth_init(); //激活网口 - rt_thread_mdelay(5000); - - while (1 && !isTCPok()) //判断TCP连接是否正常,异常自动重连 + while (1) { -// while (!isTCPok()) - { - LOG_W("TT server is not ready."); - tcpInit(); - LOG_D("s=%d",isTCPok()); -// rt_thread_mdelay(5000); + if (!isTTon()) { + break; } + if (!isEthUP()) + { //只初始化一次 + LOG_D("init eth..."); + if (rt_hw_stm32_eth_init() == RT_EOK) + { + LOG_D("eth inited DONE."); + }; //激活网口 + } + else if (!isTCPok()) //判断TCP连接是否正常,异常自动重连 { - LOG_D("TT server is ready."); - tcpRecMQ(); - recTT(); - rt_thread_mdelay(1000); +// LOG_W("TT server is not ready,--%d",isTCPok()); + tcpInit(); +// rt_thread_mdelay(1000); + if (isTCPok()) + { + LOG_D("TT server is ready."); + tcpRecMQ(); //开启tcp接收线程 + recTT(); + } } + rt_thread_mdelay(3000); //chk with 3 second interval +// LOG_D("rechk-%d",isTCPok()); } } -void deiniCnt() +void initTT() { + /* 创建 serial 线程 */ + rt_thread_t thread = rt_thread_create("initTT", initTT_thread_entry, RT_NULL, 1024 * 1.5, 24, 10); + /* 创建成功则启动线程 */ + if (thread != RT_NULL) + { + rt_thread_startup(thread); + } + else + { + LOG_E("thread 'initTT' create failure."); + return; + } +} + +void deInitTT() +{ + tcpClose(); rt_hw_stm32_eth_deinit(); //qu激活网口 } @@ -234,6 +260,6 @@ void deiniCnt() #ifdef FUNC_DEMO //测试时导出命令到控制台 MSH_CMD_EXPORT(chkAndSendFile, chkAndSendFile); MSH_CMD_EXPORT(upSendFile, upSendFile); -MSH_CMD_EXPORT(iniCnt,初始化tcp连接); -MSH_CMD_EXPORT(deiniCnt,初始化tcp连接); +MSH_CMD_EXPORT(initTT,初始化tcp连接); +MSH_CMD_EXPORT(deInitTT,初始化tcp连接); #endif diff --git a/applications/func/func.c b/applications/func/func.c index 97cbc14..dedfb3d 100644 --- a/applications/func/func.c +++ b/applications/func/func.c @@ -786,7 +786,10 @@ void parseTTData(uint8_t *din, size_t len) //按帧头分割 int cnt=(i+11) { + LOG_HEX("frame",16,ndin,cnt); + } + //判断是否为ACK if ((ndin[10]<<8) | ndin[11] == 0x03) {//数据长度只有3 @@ -994,11 +997,34 @@ void parseRS232(uint8_t *din, size_t len) } +//#include +//#include +#include +#include +/** + * check eth + * @return 1-UP,0-DOWN + */ +int isEthUP() +{ + //static void netdev_list_if(void) in netdev.c +// struct netdev *netdev = RT_NULL; +// netdev = netdev_get_by_name(argv[1]); +// if (netdev == RT_NULL) +// { +// LOG_D("none"); +// } +// else { +// LOG_D("--%d",netdev->flags); +// } + extern struct netdev *netdev_default; + int rst = (netdev_default->flags & 0x01U) ? 1:0; +// LOG_D("eth is %s.",rst?"up":"down"); - - + return rst; +} @@ -1013,6 +1039,7 @@ MSH_CMD_EXPORT(d_packLocMsg,dpackLocMsg); MSH_CMD_EXPORT(packAndSendLoc,packAndSendLoc); MSH_CMD_EXPORT(d_cacheData,d_cacheData); MSH_CMD_EXPORT(d_getFileSize,d_getFileSize); +MSH_CMD_EXPORT(isEthUP,isEthUP); #endif diff --git a/applications/main.c b/applications/main.c index a378f44..01bb0a7 100644 --- a/applications/main.c +++ b/applications/main.c @@ -18,6 +18,11 @@ //#include +///* 中断回调函数 */ +//void pwTT_irq_callback(void *args) +//{ +// LOG_D("key irq callback"); +//} @@ -34,14 +39,22 @@ int main(void) rt_pin_mode(ETH_RESET_PIN, PIN_MODE_OUTPUT); rt_pin_mode(TT_EN, PIN_MODE_OUTPUT); + rt_pin_write(TT_EN, PIN_HIGH); +// rt_pin_attach_irq(TT_EN, PIN_IRQ_MODE_FALLING, pwTT_irq_callback, RT_NULL); // 下降沿触发 +// rt_pin_irq_enable(TT_EN, PIN_IRQ_ENABLE); // 使能中断 rt_pin_mode(TR485_RE, PIN_MODE_OUTPUT); rt_pin_write(TR485_RE, PIN_LOW); // rt_pin_write(ETH_RESET_PIN, PIN_LOW);//关闭ETH // rt_pin_write(TT_EN, PIN_HIGH);//关闭TT + + + //sysSemInit + sysSemInit(); if (isInWindowZone()) {//开机检查是否在开窗区间内,是则给TT开机 pwTT_thread_entry("1"); +// initTT(); } @@ -60,16 +73,6 @@ int main(void) rt_pin_write(LED_HEART_DEBUG, PIN_LOW); rt_thread_mdelay(1000); - -// rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL); - /* 从消息队列中读取消息*/ -// rt_err_t result = rt_mq_recv(&update_cfg, &msg, sizeof(msg), RT_WAITING_NO); -// if (result == RT_EOK) -// { -// LOG_I("updatecfg:%10s -->%s", msg.key, msg.value); -// } -// rt_thread_mdelay(1000); - } return RT_EOK; @@ -77,6 +80,21 @@ int main(void) //fastlz_test -c demo.bin f.bin +// +//RT_WEAK void initTT() +//{ +// +//} +// +//RT_WEAK void deInitTT() +//{ +// +//} + + + + + INIT_APP_EXPORT(main); extern int rt_hw_stm32_eth_init(void); MSH_CMD_EXPORT(rt_hw_stm32_eth_init, 初始化网络。); @@ -87,7 +105,7 @@ void show_version(void) uint8_t t[10]; size_t len=time2Byte(t); bytes2str(t, len, 10, "", str); - rt_kprintf("SW Version: %s, build-%s\n","1.45",bytes2str(t, len, 10, "", str)); + rt_kprintf("SW Version: %s, build-%s\n","1.46",bytes2str(t, len, 10, "", str)); } MSH_CMD_EXPORT(show_version,显示版本号); diff --git a/applications/tools.c b/applications/tools.c index f554517..7d96eb5 100644 --- a/applications/tools.c +++ b/applications/tools.c @@ -417,14 +417,6 @@ void sDemo() MSH_CMD_EXPORT(sDemo,喂文件数据); -/** - * 关闭网口,去初始化,释放内存 - */ -RT_WEAK void rt_hw_stm32_eth_deinit() -{ - -} -MSH_CMD_EXPORT(rt_hw_stm32_eth_deinit, 去初始化网络。); @@ -432,16 +424,13 @@ void TTisReady(void) { rt_sem_release(TTReady); } -void sysInit() - { - -// pwTT_thread_entry("1");//开机 - TTReady = rt_sem_create("TTisReady", 0, RT_IPC_FLAG_PRIO); - cfgUpdate = rt_sem_create("cfgUpdate", 0, RT_IPC_FLAG_PRIO); - rt_sem_release(cfgUpdate);//上电更新值 -// rt_hw_stm32_eth_init();//激活网口 - LOG_D("sysInit"); - } -MSH_CMD_EXPORT(sysInit,semInit); -INIT_APP_EXPORT(sysInit); +void sysSemInit() +{ + TTReady = rt_sem_create("TTisReady", 0, RT_IPC_FLAG_PRIO); + cfgUpdate = rt_sem_create("cfgUpdate", 0, RT_IPC_FLAG_PRIO); + rt_sem_release(cfgUpdate); //上电更新值 + LOG_D("sysSemInit DONE."); +} +MSH_CMD_EXPORT(sysSemInit,semInit); +//INIT_APP_EXPORT(sysInit); INIT_APP_EXPORT(updatecfg); diff --git a/applications/ttTR.c b/applications/ttTR.c index 6080c9e..532865e 100644 --- a/applications/ttTR.c +++ b/applications/ttTR.c @@ -33,11 +33,14 @@ void tcpClose(void) /* 关闭这个连接 */ if (sock) { closesocket(sock); + } + flag=0; } -int isTCPok(void) + int isTCPok(void) { +// return sock; return flag; } @@ -155,6 +158,9 @@ void tcpRecMQ_thread_entry(void) while (1) { + if (!isTCPok()) { + break; + } rt_memset(&msg, 0, sizeof(msg)); /* 从sock连接中接收最大BUFSZ字节数据 */ msg.size = recv(sock, msg.data, BUFSZ, 0); @@ -184,6 +190,7 @@ void tcpRecMQ_thread_entry(void) msg.size=0;//收到数据长度为0表示tcp断开 rt_mq_send(&TTrx_mq, &msg, sizeof(msg)); LOG_E("tcp error, close."); + tcpClose(); break; } } diff --git a/applications/ttmsg/ttmsg.c b/applications/ttmsg/ttmsg.c index 0406730..5eda9bd 100644 --- a/applications/ttmsg/ttmsg.c +++ b/applications/ttmsg/ttmsg.c @@ -469,26 +469,36 @@ MSH_CMD_EXPORT(pmsg, 打包文件。); void pwTT_thread_entry(void *parameter) { // LOG_I("--%s--",parameter); + int f = rt_pin_read(TT_EN); + int flag = f; if (strcmp(parameter,"NULL")==0)//无参数,状态翻转 { // LOG_I("null"); - int f = rt_pin_read(TT_EN); - rt_pin_write(TT_EN, !f); +// int f = rt_pin_read(TT_EN); +// rt_pin_write(TT_EN, !f); + flag = !f; } else { - rt_bool_t flag = !(rt_bool_t) atoi(parameter); - if ((rt_bool_t)rt_pin_read(TT_EN) != flag) {//目标状态与当前状态一致才响应 - rt_pin_write(TT_EN, flag);// - } - + flag = !(rt_bool_t) atoi(parameter); } +// if ((rt_bool_t)rt_pin_read(TT_EN) != flag) + //目标状态与当前状态一致才响应 +// LOG_D("new sta"); + rt_pin_write(TT_EN, flag);// + + if (!flag) { + initTT(); + } + else { + deInitTT(); + } + + if (rt_pin_read(TT_EN))//检查设置后状态 { -//#ifdef NO_RELAY LOG_I("set TT %s", "OFF");//高电平关断 -//#endif add_val("swCnt");//更新统计值 } else @@ -537,6 +547,9 @@ void recTT_thread_entry() static TTRx_MSG msg; while (1) { + if (!isTCPok()) { + break; + } rt_memset(&msg, 0, sizeof(msg)); if (rt_mq_recv(&TTrx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER) == RT_EOK) @@ -548,17 +561,9 @@ void recTT_thread_entry() } LOG_I("%d Bytes received from TT",msg.size); LOG_HEX("TTrec", 16, msg.data, msg.size); - //此处处理接收到数据 -// rt_uint8_t rec_good[] = { 0x88, 0xAA, 0xBB, 0x88, 0x41,0x43,0x4B }; //前四字节=帧头、后三字节=ACK -// if (rt_memcmp(msg.data, rec_good, 4) == 0 && rt_memcmp(msg.data+msg.size-3, rec_good+4, 3) == 0) -// { -// LOG_I("ack is good."); -// } -// else - { - LOG_D("try to parse data."); - parseTTData(msg.data,msg.size); - } + //此处调用处理函数 +// LOG_D("try to parse data."); + parseTTData(msg.data,msg.size); } } } diff --git a/drivers/drv_eth.c b/drivers/drv_eth.c index a239698..95a8f98 100644 --- a/drivers/drv_eth.c +++ b/drivers/drv_eth.c @@ -693,7 +693,7 @@ void rt_hw_stm32_eth_deinit() rt_free(DMATxDscrTab); } } -//MSH_CMD_EXPORT(rt_hw_stm32_eth_deinit, 去初始化网络。); +MSH_CMD_EXPORT(rt_hw_stm32_eth_deinit, 去初始化网络。); //INIT_DEVICE_EXPORT(rt_hw_stm32_eth_init);