From 8703533f5f996a52a86be59eb8b0289391c80a6d Mon Sep 17 00:00:00 2001 From: zhangsheng Date: Fri, 6 Mar 2026 19:06:05 +0800 Subject: [PATCH] add codes --- threadfunc/thread_func.cpp | 24 ++++++------ uart/uart.cpp | 19 +++++++++- uart/uart.hpp | 3 ++ uart/uart_cmd.cpp | 67 +++++++++++++++++++++++++--------- uart/uart_parameter_config.cpp | 50 +++++++------------------ 5 files changed, 96 insertions(+), 67 deletions(-) diff --git a/threadfunc/thread_func.cpp b/threadfunc/thread_func.cpp index 292857e..b278e5f 100644 --- a/threadfunc/thread_func.cpp +++ b/threadfunc/thread_func.cpp @@ -172,20 +172,22 @@ dial5G: void UartStart() { zlog_info(zbt, "zigAckrep = %d,zigAckreset = %d,zigReset = %d,errorLed = %d,power = %d", GlobalConfig::GPIO_G.zigAckrep, GlobalConfig::GPIO_G.zigAckreset, GlobalConfig::GPIO_G.zigReset, GlobalConfig::GPIO_G.errorLed, GlobalConfig::GPIO_G.power); - InitGpio(GlobalConfig::GPIO_G.zigAckrep, 0); // ACK - InitGpio(GlobalConfig::GPIO_G.zigAckreset, 1); // ACK reset - InitGpio(GlobalConfig::GPIO_G.zigReset, 1); // Zigbee reset - gpio_set(GlobalConfig::GPIO_G.zigAckreset, 1); - gpio_set(GlobalConfig::GPIO_G.zigReset, 1); + // InitGpio(GlobalConfig::GPIO_G.zigAckrep, 0); // ACK + // InitGpio(GlobalConfig::GPIO_G.zigAckreset, 1); // ACK reset + // InitGpio(GlobalConfig::GPIO_G.zigReset, 1); // Zigbee reset + // gpio_set(GlobalConfig::GPIO_G.zigAckreset, 1); + // gpio_set(GlobalConfig::GPIO_G.zigReset, 1); zlog_info(zbt, "GPIO Init"); - InitGpio(GlobalConfig::GPIO_G.runLed, 1); - InitGpio(GlobalConfig::GPIO_G.errorLed, 1); - gpio_set(GlobalConfig::GPIO_G.runLed, 1); - gpio_set(GlobalConfig::GPIO_G.errorLed, 0); - InitGpio(GlobalConfig::GPIO_G.netResetNet0, 1); - gpio_set(GlobalConfig::GPIO_G.netResetNet0, 0); + // InitGpio(GlobalConfig::GPIO_G.runLed, 1); + // InitGpio(GlobalConfig::GPIO_G.errorLed, 1); + // gpio_set(GlobalConfig::GPIO_G.runLed, 1); + // gpio_set(GlobalConfig::GPIO_G.errorLed, 0); + // InitGpio(GlobalConfig::GPIO_G.netResetNet0, 1); + // gpio_set(GlobalConfig::GPIO_G.netResetNet0, 0); + uart_inst::instance().resetZigbee(); + sleep(1); GlobalConfig::Zigbee_G.Serial_Rate = 0x07; GlobalConfig::Zigbee_G.Serial_DataB = 0x08; GlobalConfig::Zigbee_G.Serial_StopB = 0x01; diff --git a/uart/uart.cpp b/uart/uart.cpp index 3beeef3..b94afe3 100644 --- a/uart/uart.cpp +++ b/uart/uart.cpp @@ -38,7 +38,7 @@ int Uart::UartRecv(int fd, char srcshow, char *buffer) { int timeoutflag = 0; char head[] = {0xAA, 0x55, 0xAA}; char szbuffer[BUF_LENGTH] = {0x00}; - while (1) { + while (1 && !bUpdate) { if (now_task == WAVE_CMD) { memset(buff, 0, sizeof(buff)); ret = read_data(fd, buff, BUF_LENGTH, 10); @@ -281,6 +281,23 @@ int Uart::ReadFromUart() { return len; } +int Uart::ReadFromAlarm(){ + char buffer[100] = {0x00}; + int len = read_data(fd, (char *)buffer, 100, 10); + for (int i = 0; i < len; i++) printf( "%02X ", buffer[i] & 0xFF); + zlog_info(zct, "==========ReadFromAlarm========len = %d", len); + if (len > 0) + { + if(buffer[5] == 0x02 && buffer[6] == 0x00){ + return 0 ; + }else{ + return 1 ; + } + }else { + return 1 ; + } +} + void Uart::Run() { mIoSev.run(); } void Uart::Stop() { diff --git a/uart/uart.hpp b/uart/uart.hpp index 518d238..4089cda 100644 --- a/uart/uart.hpp +++ b/uart/uart.hpp @@ -164,6 +164,7 @@ public: void setCallBack(onReceiveUart _callback); void Run(); void Stop(); + int ReadFromAlarm(); void UpdateZigbeeInfo(const char* pData); int DealAskTaskOld(uint16_t ushortAdd); @@ -215,6 +216,8 @@ public: void WriteShortAddr_DistAddr2Zigbee(unsigned short pad, unsigned char* pDestShortAddr); bool CheckCrc(char* pCheckBuff, int No); void UpdateZigbeeInfoCtrl(); + void getGPIOStatus(); + void resetZigbee(); // tranducer parameter config void UpdateWirelessNode(uint16_t ushortAdd); diff --git a/uart/uart_cmd.cpp b/uart/uart_cmd.cpp index 3f07198..2d68c52 100644 --- a/uart/uart_cmd.cpp +++ b/uart/uart_cmd.cpp @@ -136,9 +136,7 @@ void Uart::WriteChanl2Zigbee(unsigned char pad) { GlobalConfig::Zigbee_G.Chan = pad1; modify_info(tmp, (char *)&GlobalConfig::Zigbee_G); - gpio_set(GlobalConfig::GPIO_G.zigReset, 0); - mssleep(10000); - gpio_set(GlobalConfig::GPIO_G.zigReset, 1); + //resetZigbee(); } void Uart::WritePanId2Zigbee(unsigned short pad) { @@ -149,9 +147,7 @@ void Uart::WritePanId2Zigbee(unsigned short pad) { GlobalConfig::Zigbee_G.PanID = pad1; modify_info(tmp, (char *)&GlobalConfig::Zigbee_G); - gpio_set(GlobalConfig::GPIO_G.zigReset, 0); - mssleep(10000); - gpio_set(GlobalConfig::GPIO_G.zigReset, 1); + //resetZigbee(); } void Uart::WriteSpeed2Zigbee() { GlobalConfig::Zigbee_G.Serial_Rate = 0x07; @@ -160,9 +156,7 @@ void Uart::WriteSpeed2Zigbee() { unsigned short tmp; tmp = GlobalConfig::Zigbee_G.MyAddr; modify_info(tmp, (char *)&GlobalConfig::Zigbee_G); - gpio_set(GlobalConfig::GPIO_G.zigReset, 0); - mssleep(10000); - gpio_set(GlobalConfig::GPIO_G.zigReset, 1); + //resetZigbee(); } void Uart::WriteTranTimeout2Zigbee(unsigned char Time) { @@ -174,9 +168,7 @@ void Uart::WriteTranTimeout2Zigbee(unsigned char Time) { GlobalConfig::Zigbee_G.TranTimeout = Time; modify_info(tmp, (char *)&GlobalConfig::Zigbee_G); - gpio_set(GlobalConfig::GPIO_G.zigReset, 0); - mssleep(10000); - gpio_set(GlobalConfig::GPIO_G.zigReset, 1); + //resetZigbee(); } void Uart::WriteShortAddr2Zigbee(unsigned short pad) { @@ -187,9 +179,7 @@ void Uart::WriteShortAddr2Zigbee(unsigned short pad) { GlobalConfig::Zigbee_G.MyAddr = pad1; modify_info(tmp, (char *)&GlobalConfig::Zigbee_G); - gpio_set(GlobalConfig::GPIO_G.zigReset, 0); - mssleep(10000); - gpio_set(GlobalConfig::GPIO_G.zigReset, 1); + //resetZigbee(); sleep(1); } @@ -208,7 +198,48 @@ void Uart::WriteShortAddr_DistAddr2Zigbee(unsigned short pad, unsigned char *pDe GlobalConfig::ZigbeeInfo_G.MyAddr = "9999"; modify_info(tmp, (char *)&GlobalConfig::Zigbee_G); - gpio_set(GlobalConfig::GPIO_G.zigReset, 0); - mssleep(10000); - gpio_set(GlobalConfig::GPIO_G.zigReset, 1); + //resetZigbee(); } + +void Uart::getGPIOStatus() +{ + //A5 5A A5 02 02 02 02 00 00 00 + char command[12] = {0x00}; + command[0] = 0xA5; + command[1] = 0x5A; + command[2] = 0xA5; + command[3] = 0x02; + command[4] = 0x02; + command[5] = 0x02; + command[6] = 0x02; + command[7] = 0x00; + command[8] = 0x00; + command[9] = 0x00; + unsigned char tmp = 0x00; + for (int k = 0; k < 10; k++) { + tmp += command[k]; + } + command[10] = tmp; + WriteToUart(command, 11); +} +void Uart::resetZigbee() +{ + //A5 5A A5 02 02 02 02 00 00 00 + char command[12] = {0x00}; + command[0] = 0xA5; + command[1] = 0x5A; + command[2] = 0xA5; + command[3] = 0x02; + command[4] = 0x02; + command[5] = 0x01; + command[6] = 0x00;// 低电平 + command[7] = 0x00; + command[8] = 0x00; + command[9] = 0x00; + unsigned char tmp = 0x00; + for (int k = 0; k < 10; k++) { + tmp += command[k]; + } + command[10] = tmp; + WriteToUart(command, 11); +} \ No newline at end of file diff --git a/uart/uart_parameter_config.cpp b/uart/uart_parameter_config.cpp index 0f98bfc..03da7a6 100644 --- a/uart/uart_parameter_config.cpp +++ b/uart/uart_parameter_config.cpp @@ -183,9 +183,6 @@ void Uart::UpdateWirelessNode(uint16_t shortAdd) { //帧头[3byte] 节点地址[2byte] 数据类型[1byte] 序号[1byte] 数据包[92byte] CRC校验[1byte] zlog_warn(zbt, "Start Upgrade!!! file Size = %d,fileName = %s,Count = %d,lastSize = %d,shortAddr = %02x%02x", (int)thisSize,DataNodeUpdateFile.c_str(),Count,lastSize,UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd)); tmp = 0x00; - gpio_set(GlobalConfig::GPIO_G.zigAckreset, 0); - mssleep(1000); - gpio_set(GlobalConfig::GPIO_G.zigAckreset, 1); for(int j = 0; j < Count;j++){ int time ,value; UpdateData[0]=0xAA; @@ -202,30 +199,20 @@ void Uart::UpdateWirelessNode(uint16_t shortAdd) { } UpdateData[99] = tmp; WriteToUart((const char*)UpdateData,100); - if(gpio_read(GlobalConfig::GPIO_G.zigAckrep) == 48) - gpio_set(GlobalConfig::GPIO_G.zigAckreset,1); - time = 0; - do{ - value = gpio_read(GlobalConfig::GPIO_G.zigAckrep); - if(value == 49) - break; - mssleep(10000); - time += 1; - }while(time < 150); - if(time >= 150){ - zlog_warn(zct, "gpio_read failed shortAdd %02x %02x,index = %d", UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd),j); + mssleep(1000); + if (ReadFromAlarm() == 0) { + zlog_info(zct, "Packge %d ACK send success,shortAddr = %02x%02x", j, UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd)); + } else { + zlog_warn(zct, "Packge %d ACK send failed,shortAddr = %02x%02x", j, UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd)); + zlog_warn(zct, "gpio_read failed shortAdd %02x %02x,index = %d", UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd),j); zlog_warn(zct, "gpio_read failed \n"); bUpdate = false; upgrade_status = 3; goto endUpdate; } - gpio_set(GlobalConfig::GPIO_G.zigAckreset,0); - mssleep(2000); memset(UpdateData,0x00,sizeof(UpdateData)); mssleep(5000); } - if(gpio_read(GlobalConfig::GPIO_G.zigAckrep) == 48) - gpio_set(GlobalConfig::GPIO_G.zigAckreset,1); if (lastSize > 0) { UpdateData[0] = 0xAA; @@ -243,25 +230,14 @@ void Uart::UpdateWirelessNode(uint16_t shortAdd) { UpdateData[99] = tmp; WriteToUart((const char*)UpdateData, 100); mssleep(1000); - int time = 0; - do{ - int value = gpio_read(GlobalConfig::GPIO_G.zigAckrep); - if(value == 49){ - gpio_set(GlobalConfig::GPIO_G.zigAckreset,0); - mssleep(10000); - if(gpio_read(GlobalConfig::GPIO_G.zigAckrep) == 48) - gpio_set(GlobalConfig::GPIO_G.zigAckreset,1); - break; - } - mssleep(10000); - time += 1; - }while(time < 150); - if (time >= 150) { - zlog_warn(zct, "gpio_read failed shortAdd %02x %02x", UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd)); - zlog_warn(zct, "gpio_read failed \n"); - bUpdate = false; + if (ReadFromAlarm() == 0) { + zlog_info(zct, "ACK send success,shortAddr = %02x%02x", UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd)); + } else { + zlog_warn(zct, "ACK send failed,shortAddr = %02x%02x", UINT16_HIGH(shortAdd), UINT16_LOW(shortAdd)); + zlog_warn(zct, "gpio_read failed \n"); + bUpdate = false; upgrade_status = 3; - goto endUpdate; + goto endUpdate; } memset(UpdateData, 0x00, sizeof(UpdateData)); }