From 312c7e29266f2d7504c7659e22b4d8a3ad44bb31 Mon Sep 17 00:00:00 2001 From: YuanHongbin <975559679@qq.com> Date: Thu, 25 Jun 2026 09:29:12 +0800 Subject: [PATCH] =?UTF-8?q?boot=E8=B0=83=E8=AF=95=E6=9A=82=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auto-skill-submodule-commit-push/SKILL.md | 96 +++++++++++++++ Project/GateWay/MDK/GateWay.uvoptx | 64 ++++++---- Project/GateWay/MDK/GateWay.uvprojx | 16 ++- Project/GateWay/source/Module/GateWay_Debug | 2 +- Project/GateWay/source/User/Inc/Public.h | 3 + Project/GateWay/source/User/Inc/Update.h | 55 +++++++++ Project/GateWay/source/User/Inc/bsp.h | 2 +- Project/GateWay/source/User/Src/LoraTask.c | 105 +++++++++++++++- Project/GateWay/source/User/Src/Public.c | 115 +++++++++++++++++- Project/GateWay/source/User/Src/RS485Task.c | 106 +++++++++++++++- Project/GateWay/source/User/Src/Update.c | 59 +++++++++ Project/GateWay/source/User/Src/main.c | 8 +- 12 files changed, 590 insertions(+), 41 deletions(-) create mode 100644 .qwen/skills/auto-skill-submodule-commit-push/SKILL.md create mode 100644 Project/GateWay/source/User/Inc/Update.h create mode 100644 Project/GateWay/source/User/Src/Update.c diff --git a/.qwen/skills/auto-skill-submodule-commit-push/SKILL.md b/.qwen/skills/auto-skill-submodule-commit-push/SKILL.md new file mode 100644 index 0000000..98008dc --- /dev/null +++ b/.qwen/skills/auto-skill-submodule-commit-push/SKILL.md @@ -0,0 +1,96 @@ +--- +name: submodule-commit-push +description: 提交并推送含子模块的 Git 仓库(先子模块后父仓库,处理 detached HEAD / 落后远程等常见问题) +source: auto-skill +extracted_at: '2026-06-22T03:44:15.544Z' +--- + +# 提交并推送所有仓库(含子模块) + +## 适用场景 +项目包含多个 Git 子模块,需要将所有变更(子模块内文件删除/修改 + 父仓库指针更新)一次性提交并推送到远端。 + +## 核心原则 + +**子模块必须先于父仓库提交并推送**,否则父仓库的指针会指向尚未推送到远端的提交,导致其他人拉取失败。 + +## 操作流程 + +### 1. 检查整体状态 + +```bash +cd <项目根目录> +git status # 查看父仓库变更 +git submodule status # 查看子模块状态(+ 表示新提交,- 表示未初始化) +``` + +### 2. 逐个检查子模块 + +对每个有变更的子模块,进入目录检查: + +```bash +cd <子模块路径> +git status +git diff HEAD --stat +git branch -a # 检查是否在 detached HEAD +git log -n 1 --oneline # 检查是否落后远端 +``` + +### 3. 处理子模块常见问题 + +| 问题 | 表现 | 解决方案 | +|------|------|----------| +| **落后远端** | `Your branch is behind 'origin/xxx' by N commits` | 先 `git stash` → `git pull` → `git stash pop`,再提交 | +| **detached HEAD** | 子模块不在任何分支上 | `git checkout -b develop` 或 `git checkout develop` 切到分支再提交 | +| **仅 modified content** | 父仓库显示 `modified content` 但子模块 `git status` 干净 | 子模块只有未提交的本地修改,需在子模块内 add + commit | + +### 4. 提交并推送每个子模块 + +```bash +cd <子模块路径> +git add -A +git commit -m "chore: 具体变更描述 + +- 文件1: 变更说明 +- 文件2: 变更说明" +git push origin <分支名> # 首次推送新分支用 git push -u origin <分支名> +``` + +> **提交消息格式**:按用户偏好,需列出每个文件的变更说明,而非笼统总结。 + +### 5. 在父仓库更新子模块指针 + +```bash +cd <项目根目录> +git add <子模块路径1> <子模块路径2> ... +git commit -m "chore: 更新子模块指针到最新提交 + +- 子模块1: 变更说明 +- 子模块2: 变更说明" +git push origin <分支名> +``` + +### 6. 验证 + +```bash +cd <项目根目录> +git status # 应显示 nothing to commit, working tree clean +git submodule status # 所有子模块无 + 或 - 前缀 +``` + +## 本项目子模块清单 + +| 子模块 | 路径 | 默认分支 | 远端地址 | +|--------|------|----------|----------| +| FatFS | `Project/GateWay/source/Module/FatFS` | master | 192.168.2.47 | +| GateWay_Debug | `Project/GateWay/source/Module/GateWay_Debug` | develop | 192.168.2.47 | +| SDCard | `Project/GateWay/source/Module/SDCard` | master | 192.168.2.47 | +| sx127x | `Project/GateWay/source/Module/sx127x` | develop | 192.168.2.47 | + +> 父仓库 GateWay 的远端在 8.137.104.4,子模块的远端在 192.168.2.47(内网),注意网络可达性。 + +## 注意事项 + +- 如果子模块只有指针变更(无文件级修改),说明子模块已有新提交但父仓库指针未更新,只需在父仓库 `git add <子模块路径>` 即可 +- FatFS 子模块容易因为 documents/ 目录产生大量 untracked 文件冲突,`git stash` 时加 `--include-untracked` +- sx127x 经常处于 detached HEAD 状态,操作前务必先切到 develop 分支 diff --git a/Project/GateWay/MDK/GateWay.uvoptx b/Project/GateWay/MDK/GateWay.uvoptx index 740478b..afe92e5 100644 --- a/Project/GateWay/MDK/GateWay.uvoptx +++ b/Project/GateWay/MDK/GateWay.uvoptx @@ -729,6 +729,18 @@ 0 0 + + 2 + 14 + 1 + 0 + 0 + 0 + ..\source\User\Src\Update.c + Update.c + 0 + 0 + @@ -739,7 +751,7 @@ 0 3 - 14 + 15 1 0 0 @@ -751,7 +763,7 @@ 3 - 15 + 16 1 0 0 @@ -763,7 +775,7 @@ 3 - 16 + 17 1 0 0 @@ -775,7 +787,7 @@ 3 - 17 + 18 1 0 0 @@ -787,7 +799,7 @@ 3 - 18 + 19 1 0 0 @@ -799,7 +811,7 @@ 3 - 19 + 20 1 0 0 @@ -811,7 +823,7 @@ 3 - 20 + 21 1 0 0 @@ -823,7 +835,7 @@ 3 - 21 + 22 1 0 0 @@ -835,7 +847,7 @@ 3 - 22 + 23 1 0 0 @@ -847,7 +859,7 @@ 3 - 23 + 24 1 0 0 @@ -859,7 +871,7 @@ 3 - 24 + 25 1 0 0 @@ -871,7 +883,7 @@ 3 - 25 + 26 1 0 0 @@ -883,7 +895,7 @@ 3 - 26 + 27 1 0 0 @@ -895,7 +907,7 @@ 3 - 27 + 28 1 0 0 @@ -907,7 +919,7 @@ 3 - 28 + 29 1 0 0 @@ -919,7 +931,7 @@ 3 - 29 + 30 1 0 0 @@ -931,7 +943,7 @@ 3 - 30 + 31 1 0 0 @@ -951,7 +963,7 @@ 0 4 - 31 + 32 1 0 0 @@ -971,7 +983,7 @@ 0 5 - 32 + 33 1 0 0 @@ -983,7 +995,7 @@ 5 - 33 + 34 1 0 0 @@ -1003,7 +1015,7 @@ 0 6 - 34 + 35 1 0 0 @@ -1015,7 +1027,7 @@ 6 - 35 + 36 1 0 0 @@ -1027,7 +1039,7 @@ 6 - 36 + 37 1 0 0 @@ -1039,7 +1051,7 @@ 6 - 37 + 38 1 0 0 @@ -1059,12 +1071,12 @@ 0 7 - 38 + 39 1 0 0 0 - ..\source\Module\SX127x\sx127x.c + ..\..\..\..\Module\SX127x\sx127x.c sx127x.c 0 0 diff --git a/Project/GateWay/MDK/GateWay.uvprojx b/Project/GateWay/MDK/GateWay.uvprojx index e88e94b..6a5c5db 100644 --- a/Project/GateWay/MDK/GateWay.uvprojx +++ b/Project/GateWay/MDK/GateWay.uvprojx @@ -493,6 +493,11 @@ 1 ..\source\User\Src\utils.c + + Update.c + 1 + ..\source\User\Src\Update.c + @@ -779,7 +784,7 @@ sx127x.c 1 - ..\source\Module\SX127x\sx127x.c + ..\..\..\..\Module\SX127x\sx127x.c @@ -1267,7 +1272,7 @@ --diag_suppress=186,66 __DEBUG,HC32F460,USE_DEVICE_DRIVER_LIB,JEUA,BOOT_ENABLE - ..\..\..\driver\inc;..\..\..\mcu\common;..\source\User\Inc;..\source\Module\Debug;..\source\Module\SDCard;..\source\Module\FatFS\source;..\source\Module\sx127x;..\source\Module\GateWay_Debug + ..\..\..\driver\inc;..\..\..\mcu\common;..\source\User\Inc;..\source\User\Src;..\source\Module\Debug;..\source\Module\SDCard;..\source\Module\FatFS\source;..\source\Module\sx127x;..\source\Module\GateWay_Debug @@ -1419,6 +1424,11 @@ 1 ..\source\User\Src\utils.c + + Update.c + 1 + ..\source\User\Src\Update.c + @@ -1705,7 +1715,7 @@ sx127x.c 1 - ..\source\Module\SX127x\sx127x.c + ..\..\..\..\Module\SX127x\sx127x.c diff --git a/Project/GateWay/source/Module/GateWay_Debug b/Project/GateWay/source/Module/GateWay_Debug index a57b9dd..5c41459 160000 --- a/Project/GateWay/source/Module/GateWay_Debug +++ b/Project/GateWay/source/Module/GateWay_Debug @@ -1 +1 @@ -Subproject commit a57b9ddf3176aebc736f0dd32b7fbbb16ef5c368 +Subproject commit 5c414595cc38c49ad3aa0dc09e59192f08ea8492 diff --git a/Project/GateWay/source/User/Inc/Public.h b/Project/GateWay/source/User/Inc/Public.h index 0fb7e2c..1aee4f9 100644 --- a/Project/GateWay/source/User/Inc/Public.h +++ b/Project/GateWay/source/User/Inc/Public.h @@ -5,6 +5,8 @@ #define LORA_LOWPOWER //LORA͹豸ϱʽ +#define CRC16_BASE 0xA001 + #define SENSOR_DATA_LEN_MAX 50 #define SENSOR_NUM_MAX 16 #define COMMUNIT_NUM_MAX 32 @@ -315,6 +317,7 @@ struct GateWay_t{ }; int Cat1EthRevCallBack(GateWayPara GateWay, uint8_t *rData, uint16_t rLen); +void Cat1UpgradeSetEnable(bool enable); uint16_t CRC_Modbus(uint16_t wBase, __IO uint8_t *para, uint16_t length); uint8_t AsciiToHex(char *ASCData, uint8_t *HexData, uint8_t sLen); void HexToAscii(uint8_t *HexData, char *ASCData, uint8_t sLen); diff --git a/Project/GateWay/source/User/Inc/Update.h b/Project/GateWay/source/User/Inc/Update.h new file mode 100644 index 0000000..6cf42f9 --- /dev/null +++ b/Project/GateWay/source/User/Inc/Update.h @@ -0,0 +1,55 @@ +#ifndef __UPDATE_H +#define __UPDATE_H + +#include +#include "bsp.h" + +/*启动标志定义*/ +#define APP_START_FLAG 0x55AA5A5A +#define APP_UPDATE_FLAG 0xA5A5AA55 + +/*主机下发升级请求载荷结构体*/ +typedef struct { + uint16_t PackageNum; + uint32_t AppSize; + uint32_t AppCrc32; +}__attribute__ ((packed))UpDataRequset_t, *UDReq; + +/*帧头结构体 - 与BootLoader协议一致*/ +typedef struct { + uint8_t Header; // 0x7D + uint8_t DevMac[6]; // 设备MAC地址 + uint8_t Cmd; // 命令码 + uint8_t PayloadLen; // 载荷长度 +}__attribute__((packed))UpdateFrameHeader_t, *pUpdateFrameHeader; + +/*升级命令码*/ +typedef enum { + INSIDE_CMD_UPDATE = 0x81, // 升级程序下发请求 +}INSIDE_CMD_M; + +/*内部指令状态机*/ +typedef enum { + IN_STATUS_IDLE, + IN_STATUS_UPDATE, +}InStatus_m; + +/***************************************************************************************** +* 函数名称: Update +* 功能描述: 固件升级 - 解析到升级指令后调用此函数 +* 参 数: PayLoad 协议负载,需去除帧头等信息,只传负载数据 +* 返 回 值: 无 +*****************************************************************************************/ +void Update(uint8_t *PayLoad); + +/***************************************************************************************** +* 函数名称: UpdateInit +* 功能描述: 升级参数初始化 - 需要在程序启动,外设初始化完后调用 +* 参 数: 无 +* 返 回 值: 无 +*****************************************************************************************/ +void UpdateInit(void); + +#endif + + diff --git a/Project/GateWay/source/User/Inc/bsp.h b/Project/GateWay/source/User/Inc/bsp.h index b44ea73..aa3950c 100644 --- a/Project/GateWay/source/User/Inc/bsp.h +++ b/Project/GateWay/source/User/Inc/bsp.h @@ -8,7 +8,7 @@ #include #include "hc32_ddl.h" #include "rtthread.h" -#include "ff.h" +#include "ff.h" #define SOFTWARE_VERSION 10 #define HARDWARE_VERSION 12 diff --git a/Project/GateWay/source/User/Src/LoraTask.c b/Project/GateWay/source/User/Src/LoraTask.c index 134a091..35a3bb4 100644 --- a/Project/GateWay/source/User/Src/LoraTask.c +++ b/Project/GateWay/source/User/Src/LoraTask.c @@ -2,6 +2,7 @@ #include "main.h" #include "CatOneTask.h" #include "DebugCmd.h" +#include "Update.h" #define UC_OFFLINE_TIME_INTERVAL_MAX (2 * 60 * 60)//жʱλ @@ -9,6 +10,14 @@ static rt_sem_t LoraDioIRQ_Sem = RT_NULL; static rt_thread_t LoraIRQ_Thread = RT_NULL; static GateWayPara GateWay; +/*Loraͨر*/ +static InStatus_m s_LoraInStatus = IN_STATUS_IDLE; +static uint8_t s_LoraInsidePay[256]; +static uint8_t s_LoraInsidePayLen = 0; +static bool s_LoraUpgradeEnable = false; +static void LoraInSideCmdHandler(uint8_t *rData, uint16_t rLen); +static void LoraInSideLoopHandler(void); + void LoraSend(uint8_t* pucBuff, uint8_t ucLen) { Sx1276LoRaSendBuffer(pucBuff, ucLen); @@ -98,6 +107,10 @@ void Lora_Thread_Entry(void *parameter) Sx1276LoRaInit(LoraRevCallBack); Debug_Printf("Lora Thread is running\r\n"); + + /*Loraͨ*/ + s_LoraUpgradeEnable = true; + #ifdef LORA_LOWPOWER while(1) { if(GateWay->ConfigPara.Lora.OnOff == false) { @@ -107,9 +120,19 @@ void Lora_Thread_Entry(void *parameter) result = rt_mq_recv(GateWay->LoraRev_MQ, Payload, 256, 1000); if(result == RT_EOK) { - int ret = GateWay->CommUnitRevCallBack(GateWay, &Payload[1], Payload[0], LoRaSendBuffer); - if(ret == 0xff) //עϢ - continue; + /*ڲָ(֡ͷ0x7D)*/ + if(Payload[1] == 0x7D && s_LoraUpgradeEnable) { + LoraInSideCmdHandler(&Payload[1], Payload[0]); + } + else { + /*ڲָ״̬*/ + LoraInSideLoopHandler(); + + /*ͨѶԪݴ*/ + int ret = GateWay->CommUnitRevCallBack(GateWay, &Payload[1], Payload[0], LoRaSendBuffer); + if(ret == 0xff) //עϢ + continue; + } } else { //ж for(int i = 0; i < COMMUNIT_NUM_MAX; i++) { @@ -210,3 +233,79 @@ void LoraInit(void) { Sx1276LoRaInit(LoraRevCallBack); } + +/***************************************************************************************** +* : LoraInSideCmdHandler +* : Loraͨڲָ +* : rData + rLen ݳ +* ֵ: +*****************************************************************************************/ +static void LoraInSideCmdHandler(uint8_t *rData, uint16_t rLen) +{ + uint16_t crc16, Check; + uint8_t *Payload; + pUpdateFrameHeader IFHeader = (pUpdateFrameHeader)rData; + + /*CRCУ*/ + Check = CRC_Modbus(CRC16_BASE, rData, rLen - 2); + crc16 = (rData[rLen - 1] << 8) | rData[rLen - 2]; + if(Check != crc16) { + Debug_Printf("Lora Inside CRC Err...\r\n"); + return; + } + + /*豸MACǷƥ(ֹ֧㲥0xFF)*/ + bool isMatch = false; + for(int i = 0; i < 6; i++) { + if(IFHeader->DevMac[i] != GateWay->ConfigPara.GwMac[i]) { + if(IFHeader->DevMac[i] == 0xFF) { + isMatch = true; + break; + } + break; + } + else { + isMatch = true; + } + } + if(!isMatch) { + Debug_Printf("Lora Inside MAC Err...\r\n"); + return; + } + + Payload = &rData[sizeof(UpdateFrameHeader_t)]; + s_LoraInsidePayLen = IFHeader->PayloadLen; + + switch((INSIDE_CMD_M)IFHeader->Cmd) { + case INSIDE_CMD_UPDATE: { + memcpy(s_LoraInsidePay, Payload, s_LoraInsidePayLen); + s_LoraInStatus = IN_STATUS_UPDATE; + break; + } + default: + break; + } +} + +/***************************************************************************************** +* : LoraInSideLoopHandler +* : Loraͨڲָ״̬ +* : +* ֵ: +*****************************************************************************************/ +static void LoraInSideLoopHandler(void) +{ + switch(s_LoraInStatus) { + case IN_STATUS_IDLE: { + break; + } + + case IN_STATUS_UPDATE: { + Update(s_LoraInsidePay); + memset(s_LoraInsidePay, 0, sizeof(s_LoraInsidePay)); + s_LoraInStatus = IN_STATUS_IDLE; + break; + } + } +} diff --git a/Project/GateWay/source/User/Src/Public.c b/Project/GateWay/source/User/Src/Public.c index b5d5b7f..8212285 100644 --- a/Project/GateWay/source/User/Src/Public.c +++ b/Project/GateWay/source/User/Src/Public.c @@ -4,8 +4,17 @@ #include "LoraTask.h" #include "spiflash.h" #include "CatOneTask.h" +#include "Update.h" #include +/*Cat1/LANͨر*/ +static InStatus_m s_Cat1InStatus = IN_STATUS_IDLE; +static uint8_t s_Cat1InsidePay[256]; +static uint8_t s_Cat1InsidePayLen = 0; +static bool s_Cat1UpgradeEnable = false; +static void Cat1InSideCmdHandler(uint8_t *rData, uint16_t rLen, GateWayPara GateWay); +static void Cat1InSideLoopHandler(void); + //Ӧݳ const uint8_t SensorTypeDataLen[] = { 0, //0x0000- @@ -861,14 +870,26 @@ int Cat1EthRevCallBack(GateWayPara GateWay, uint8_t *rData, uint16_t rLen) uint8_t *Data; uint8_t *MegData; uint8_t sLen; - + uint8_t CommUintMac[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - + NetCommFrameHeader NCHeader = (NetCommFrameHeader)rData; - + uint16_t FrameLen = sizeof(NetCommFrameHeader_t) + NCHeader->PayloadLen; if(FrameLen > CAT_ONE_REV_LEN_MAX) return -1; + + /*Cat1/LANͨ - Э֡ͷ0x7D*/ + if(NCHeader->Header == 0x7D && s_Cat1UpgradeEnable) { + /*ڲָ״̬*/ + Cat1InSideLoopHandler(); + Cat1InSideCmdHandler(rData, rLen, GateWay); + return 0; + } + + /*ڲָ״̬*/ + Cat1InSideLoopHandler(); + if(NCHeader->Header != 0x7A) return -1; if(memcmp(NCHeader->GWMac, GateWay->ConfigPara.GwMac, 6) != 0x00) @@ -1212,3 +1233,91 @@ int Cat1EthRevCallBack(GateWayPara GateWay, uint8_t *rData, uint16_t rLen) } return 0; } + +/***************************************************************************************** +* : Cat1InSideCmdHandler +* : Cat1/LANͨڲָ +* : rData + rLen ݳ + GateWay ز +* ֵ: +*****************************************************************************************/ +static void Cat1InSideCmdHandler(uint8_t *rData, uint16_t rLen, GateWayPara GateWay) +{ + uint16_t crc16, Check; + uint8_t *Payload; + pUpdateFrameHeader IFHeader = (pUpdateFrameHeader)rData; + + /*CRCУ*/ + Check = CRC_Modbus(CRC16_BASE, rData, rLen - 2); + crc16 = (rData[rLen - 1] << 8) | rData[rLen - 2]; + if(Check != crc16) { + Debug_Printf("Cat1 Inside CRC Err...\r\n"); + return; + } + + /*豸MACǷƥ(ֹ֧㲥0xFF)*/ + bool isMatch = false; + for(int i = 0; i < 6; i++) { + if(IFHeader->DevMac[i] != GateWay->ConfigPara.GwMac[i]) { + if(IFHeader->DevMac[i] == 0xFF) { + isMatch = true; + break; + } + break; + } + else { + isMatch = true; + } + } + if(!isMatch) { + Debug_Printf("Cat1 Inside MAC Err...\r\n"); + return; + } + + Payload = &rData[sizeof(UpdateFrameHeader_t)]; + s_Cat1InsidePayLen = IFHeader->PayloadLen; + + switch((INSIDE_CMD_M)IFHeader->Cmd) { + case INSIDE_CMD_UPDATE: { + memcpy(s_Cat1InsidePay, Payload, s_Cat1InsidePayLen); + s_Cat1InStatus = IN_STATUS_UPDATE; + break; + } + default: + break; + } +} + +/***************************************************************************************** +* : Cat1InSideLoopHandler +* : Cat1/LANͨڲָ״̬ +* : +* ֵ: +*****************************************************************************************/ +static void Cat1InSideLoopHandler(void) +{ + switch(s_Cat1InStatus) { + case IN_STATUS_IDLE: { + break; + } + + case IN_STATUS_UPDATE: { + Update(s_Cat1InsidePay); + memset(s_Cat1InsidePay, 0, sizeof(s_Cat1InsidePay)); + s_Cat1InStatus = IN_STATUS_IDLE; + break; + } + } +} + +/***************************************************************************************** +* : Cat1UpgradeSetEnable +* : Cat1/LANͨʹ +* : enable ʹܱ־ +* ֵ: +*****************************************************************************************/ +void Cat1UpgradeSetEnable(bool enable) +{ + s_Cat1UpgradeEnable = enable; +} diff --git a/Project/GateWay/source/User/Src/RS485Task.c b/Project/GateWay/source/User/Src/RS485Task.c index 8107b30..c8a5056 100644 --- a/Project/GateWay/source/User/Src/RS485Task.c +++ b/Project/GateWay/source/User/Src/RS485Task.c @@ -5,8 +5,18 @@ #include "CatOneTask.h" #include "DebugCmd.h" #include "update_protocol.h" +#include "Update.h" #include "bsp.h" +/*ر - ʹUpdate.hжInStatus_m*/ +static InStatus_m s_InStatus = IN_STATUS_IDLE; +static uint8_t s_InsidePay[256]; +static uint8_t s_InsidePayLen = 0; + +/*̬*/ +static void InSideCmdHandler(uint8_t *rData, uint16_t rLen, GateWayPara GateWay, SensorCommPara SCPara); +static void InSideLoopHandler(GateWayPara GateWay, SensorCommPara SCPara); + //#define RS485_DBG_LOG(...) { if(RS485DispEn) Debug_Printf(__VA_ARGS__);} //static uint8_t RS485DispEn = false; @@ -260,12 +270,15 @@ void RS485LoopHandler(GateWayPara GateWay, RS485Para RS485Ch, SensorCommPara SCP rt_err_t result; uint8_t MegData[9]; int ret; - + if(RS485Ch->Enable == false) { rt_thread_delay(10); return; } - + + /*ڲָ״̬()*/ + InSideLoopHandler(GateWay, SCPara); + if(RS485Ch->CommUnitEnable == true) { //ʹͨѶԪ if(RS485Ch->QuerySensorFlag == true) { //Ҵ if(SCPara->SensorCnt < SENSOR_NUM_MAX) { @@ -365,7 +378,12 @@ void RS485LoopHandler(GateWayPara GateWay, RS485Para RS485Ch, SensorCommPara SCP RxLenTemp = SCPara->RS485RxLen; // memset(SCPara->RS485RxBuff, 0x00, RS485_RX_BUFF_LEN_MAX); SCPara->RS485RxLen = 0; - if(RxBuffTemp[0] == 0x7A && RS485Ch->UpgradeEnable == true) { // + + /*ڲָ(֡ͷ0x7D)*/ + if(RxBuffTemp[0] == 0x7D && RS485Ch->UpgradeEnable == true) { + InSideCmdHandler(RxBuffTemp, RxLenTemp, GateWay, SCPara); + } + else if(RxBuffTemp[0] == 0x7A && RS485Ch->UpgradeEnable == true) { //ɰЭ() update_process(RxBuffTemp,RxLenTemp); } else if(RxBuffTemp[0] == 0x7A && RS485Ch->CommUnitEnable == true) { //ͨѶԪݽ @@ -621,3 +639,85 @@ void RS485Ch2RxIrqCallback(uint8_t rData) } } #endif + +/***************************************************************************************** +* : InSideCmdHandler +* : ڲָ() +* : rData + rLen ݳ + GateWay ز + SCPara RS485ͨѶ +* ֵ: +*****************************************************************************************/ +static void InSideCmdHandler(uint8_t *rData, uint16_t rLen, GateWayPara GateWay, SensorCommPara SCPara) +{ + uint16_t crc16, Check; + uint8_t *Payload; + pUpdateFrameHeader IFHeader = (pUpdateFrameHeader)rData; + + /*CRCУ*/ + Check = CRC_Modbus(CRC16_BASE, rData, rLen - 2); + crc16 = (rData[rLen - 1] << 8) | rData[rLen - 2]; + if(Check != crc16) { + Debug_Printf("Inside CRC Err...\r\n"); + return; + } + + /*豸MACǷƥ(ֹ֧㲥0xFF)*/ + bool isMatch = false; + for(int i = 0; i < 6; i++) { + if(IFHeader->DevMac[i] != GateWay->ConfigPara.GwMac[i]) { + if(IFHeader->DevMac[i] == 0xFF) { + isMatch = true; + break; + } + break; + } + else { + isMatch = true; + } + } + if(!isMatch) { + Debug_Printf("Inside MAC Err...\r\n"); + return; + } + + Payload = &rData[sizeof(UpdateFrameHeader_t)]; + s_InsidePayLen = IFHeader->PayloadLen; + + switch((INSIDE_CMD_M)IFHeader->Cmd) { + case INSIDE_CMD_UPDATE: { + /*ݲ״̬*/ + memcpy(s_InsidePay, Payload, s_InsidePayLen); + s_InStatus = IN_STATUS_UPDATE; + break; + } + + default: + break; + } +} + +/***************************************************************************************** +* : InSideLoopHandler +* : ڲָ״̬ +* : GateWay ز + SCPara RS485ͨѶ +* ֵ: +*****************************************************************************************/ +static void InSideLoopHandler(GateWayPara GateWay, SensorCommPara SCPara) +{ + switch(s_InStatus) { + case IN_STATUS_IDLE: { + break; + } + + case IN_STATUS_UPDATE: { + /**/ + Update(s_InsidePay); + memset(s_InsidePay, 0, sizeof(s_InsidePay)); + s_InStatus = IN_STATUS_IDLE; + break; + } + } +} diff --git a/Project/GateWay/source/User/Src/Update.c b/Project/GateWay/source/User/Src/Update.c new file mode 100644 index 0000000..fee9357 --- /dev/null +++ b/Project/GateWay/source/User/Src/Update.c @@ -0,0 +1,59 @@ +#include "Update.h" +#include "main.h" +#include "bsp.h" +#include + +/*Boot参数存储地址定义*/ +#define BOOT_PARA_ADDR FLASH_BINFO_BASE + +/***************************************************************************************** +* 函数名称: Update +* 功能描述: 固件升级 - 此函数在解析到升级指令时调用 +* 参 数: PayLoad 协议负载,需去除帧头等信息,只传负载数据 +* 返 回 值: 无 +*****************************************************************************************/ +void Update(uint8_t *PayLoad) +{ + UpDataRequset_t Req; + boot_para_t cfg = {0}; + + memcpy(&Req, PayLoad, sizeof(UpDataRequset_t)); + cfg.AppFlag = 0; + cfg.UpdateFlag = APP_UPDATE_FLAG; + cfg.Crc32Check = Req.AppCrc32; + cfg.PackageNum = Req.PackageNum; + cfg.AppSize = Req.AppSize; + + Debug_Printf("Receive upgrade command:\r\n"); + Debug_Printf("PackageNum: %d\r\n", cfg.PackageNum); + Debug_Printf("AppSize: %d\r\n", cfg.AppSize); + Debug_Printf("AppCrc32: 0x%08x\r\n", cfg.Crc32Check); + Debug_Printf("System Reset!\r\n"); + + /*保存启动参数到Flash*/ + dev_boot_write_param(cfg); + + /*延时后复位*/ + rt_thread_mdelay(200); + NVIC_SystemReset(); +} + +/***************************************************************************************** +* 函数名称: UpdateInit +* 功能描述: 升级参数初始化 - 需要在程序启动,外设初始化完后调用 +* 参 数: 无 +* 返 回 值: 无 +*****************************************************************************************/ +void UpdateInit(void) +{ + boot_para_t cfg = {0}; + + /*读取启动参数*/ + dev_boot_read_param(&cfg); + + /*如果是首次运行,设置APP启动标志*/ + if(cfg.AppFlag != APP_START_FLAG) { + cfg.AppFlag = APP_START_FLAG; + dev_boot_write_param(cfg); + } +} \ No newline at end of file diff --git a/Project/GateWay/source/User/Src/main.c b/Project/GateWay/source/User/Src/main.c index 7d3571f..013e109 100644 --- a/Project/GateWay/source/User/Src/main.c +++ b/Project/GateWay/source/User/Src/main.c @@ -178,7 +178,7 @@ void GateWayInit(void) GateWay.ConfigPara.Rs485Ch1.Power = false; GateWay.ConfigPara.Rs485Ch2.Enable = true; GateWay.ConfigPara.Rs485Ch2.BaudRate = 500000; - GateWay.ConfigPara.Rs485Ch2.UpgradeEnable = false; + GateWay.ConfigPara.Rs485Ch2.UpgradeEnable = true; // Ch2 GateWay.ConfigPara.Rs485Ch2.CommUnitEnable = false; GateWay.ConfigPara.Rs485Ch2.Power = false; GateWay.ConfigPara.Rs485Ch1.RS485Send = RS485Ch1UartSend; @@ -394,6 +394,9 @@ int main(void) GateWay.CommUnitRevCallBack = CommUnitAnalyze; GateWay.SvrRevCallBack = Cat1EthRevCallBack; + /*Cat1/LANͨ*/ + Cat1UpgradeSetEnable(true); + GateWay.NetSendData_MQ = rt_mq_create("NetSendMQ", 512, 10, RT_IPC_FLAG_FIFO); if(GateWay.NetSendData_MQ == RT_NULL) { rt_kprintf("CatOne MQ Create Failed!\r\n"); @@ -428,6 +431,9 @@ int main(void) rt_thread_startup(Debug_Thread); else return -1; + + /*Debugͨ*/ + DebugUpgradeSetEnable(true); Cat1Eth_Thread = rt_thread_create("Cat1Eth", CatOne_Eth_Thread_Entry, &GateWay, 4096, 3, 20); if (Cat1Eth_Thread != RT_NULL)