From d1bb2807b45f096bac5cf34bb07480e5170f5534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E7=8E=89=E7=90=9B?= Date: Wed, 30 Nov 2022 20:08:14 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E7=9B=B4=E9=A9=B1?= =?UTF-8?q?=E6=B3=95CRC16-XModem=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BootLoader.uvoptx | 124 ++++++++++++++++++++++++++++----------------- BootLoader.uvprojx | 42 ++++----------- driverCrc16.c | 50 ++++++++++++++++++ driverCrc16.h | 21 ++++++++ main.c | 8 +-- menu.c | 4 +- ymodem.c | 97 ++++------------------------------- 7 files changed, 176 insertions(+), 170 deletions(-) create mode 100644 driverCrc16.c create mode 100644 driverCrc16.h diff --git a/BootLoader.uvoptx b/BootLoader.uvoptx index b73e744..8b23944 100644 --- a/BootLoader.uvoptx +++ b/BootLoader.uvoptx @@ -75,7 +75,7 @@ 1 0 - 1 + 0 255 @@ -123,7 +123,24 @@ UL2CM3(-S0 -C0 -P0 -FD20000000 -FC1000 -FN1 -FF0GD32F4xx_3MB -FS08000000 -FL0300000 -FP0($$Device:GD32F450ZK$Flash\GD32F4xx_3MB.FLM)) - + + + 0 + 0 + 154 + 1 +
0
+ 0 + 0 + 0 + 0 + 0 + 0 + .\ymodem.c + + +
+
0 @@ -223,7 +240,7 @@ 1 0 - 0 + 1 255 @@ -296,15 +313,52 @@ UL2CM3(-S0 -C0 -P0 -FD20000000 -FC1000 -FN1 -FF0GD32F4xx_3MB -FS08000000 -FL0300000 -FP0($$Device:GD32F450ZK$Flash\GD32F4xx_3MB.FLM)) - - - + + + 0 + 0 + 123 + 1 +
134219818
+ 0 + 0 + 0 + 0 + 0 + 1 + .\ymodem.c + + \\BootLoader\ymodem.c\123 +
+ + 1 + 0 + 44 + 1 +
134218332
+ 0 + 0 + 0 + 0 + 0 + 1 + .\driverCrc16.c + + \\BootLoader\driverCrc16.c\44 +
+
+ + + 0 1 - 0 - 0x8008000 - 0 -
-
+ 0x200004ce + + + 1 + 1 + Crc_Table[256] + + 2 @@ -389,18 +443,6 @@ 1 2 - 5 - 0 - 0 - 0 - .\bsp_uart.h - bsp_uart.h - 0 - 0 - - - 1 - 3 1 0 0 @@ -412,19 +454,7 @@ 1 - 4 - 5 - 0 - 0 - 0 - .\bsp_ocflash.h - bsp_ocflash.h - 0 - 0 - - - 1 - 5 + 3 1 0 0 @@ -436,7 +466,7 @@ 1 - 6 + 4 1 0 0 @@ -448,25 +478,25 @@ 1 - 7 - 5 + 5 + 1 0 0 0 - .\ymodem.h - ymodem.h + .\bsp_gpio.c + bsp_gpio.c 0 0 1 - 8 + 6 1 - 0 + 1 0 0 - .\bsp_gpio.c - bsp_gpio.c + .\driverCrc16.c + driverCrc16.c 0 0 @@ -480,7 +510,7 @@ 0 2 - 9 + 7 1 0 0 @@ -492,7 +522,7 @@ 2 - 10 + 8 1 0 0 diff --git a/BootLoader.uvprojx b/BootLoader.uvprojx index 308be06..a310d68 100644 --- a/BootLoader.uvprojx +++ b/BootLoader.uvprojx @@ -10,7 +10,7 @@ BootLoader 0x4 ARM-ADS - 6180000::V6.18::ARMCLANG + 5060960::V5.06 update 7 (build 960)::ARMCC 0 @@ -389,21 +389,11 @@ 1 .\bsp_uart.c - - bsp_uart.h - 5 - .\bsp_uart.h - systick.c 1 .\systick.c - - bsp_ocflash.h - 5 - .\bsp_ocflash.h - bsp_ocflash.c 1 @@ -414,16 +404,16 @@ 1 .\ymodem.c - - ymodem.h - 5 - .\ymodem.h - bsp_gpio.c 1 .\bsp_gpio.c + + driverCrc16.c + 1 + .\driverCrc16.c + @@ -832,21 +822,11 @@ 1 .\bsp_uart.c - - bsp_uart.h - 5 - .\bsp_uart.h - systick.c 1 .\systick.c - - bsp_ocflash.h - 5 - .\bsp_ocflash.h - bsp_ocflash.c 1 @@ -857,16 +837,16 @@ 1 .\ymodem.c - - ymodem.h - 5 - .\ymodem.h - bsp_gpio.c 1 .\bsp_gpio.c + + driverCrc16.c + 1 + .\driverCrc16.c + diff --git a/driverCrc16.c b/driverCrc16.c new file mode 100644 index 0000000..94f2f37 --- /dev/null +++ b/driverCrc16.c @@ -0,0 +1,50 @@ +/* + * @Author: Ma Yuchen + * @Date: 2022-11-30 19:01:06 + * @LastEditors: Ma YuChen + * @LastEditTime: 2022-11-30 19:33:29 + * @Description: file content + * @FilePath: \BootLoader\driverCrc16.c + */ +#include "driverCrc16.h" + +static uint16_t Crc_Table[256] = {0}; + +void InitCRC(void) +{ + int index = 0; + int bit_cnt = 0; + uint32_t temp = 0; + + for (index = 0; index < 256; index++) + { + temp = 0; + for (bit_cnt = 8; bit_cnt > 0; bit_cnt--) + { + if ((index >> (bit_cnt - 1) ^ temp >> (CRC_WIDTH - 1)) & 0x1) + { + temp = (temp << 1) ^ CRC_PLOY; + } + else + { + temp <<= 1; + } + } + + Crc_Table[index] = (uint16_t)temp; + } +} + +uint16_t CalcuCRC(char *buff, int len) +{ + char *tempPtr = buff; + int reg = CRC_INIT; + int i = 0; + + for (i = 0; i < len; i++) + { + reg = (reg << 8) ^ Crc_Table[((reg >> (CRC_WIDTH - 8)) & 0xFF) ^ *tempPtr]; + tempPtr++; + } + return (uint16_t)(reg & 0xFFFF); +} diff --git a/driverCrc16.h b/driverCrc16.h new file mode 100644 index 0000000..8fb1d75 --- /dev/null +++ b/driverCrc16.h @@ -0,0 +1,21 @@ +/* + * @Author: Ma Yuchen + * @Date: 2022-11-30 19:01:15 + * @LastEditors: Ma YuChen + * @LastEditTime: 2022-11-30 19:32:24 + * @Description: file content + * @FilePath: \BootLoader\driverCrc16.h + */ +#ifndef Driver_CRC16_H +#define Driver_CRC16_H + +#include + +#define CRC_WIDTH 16 +#define CRC_PLOY 0x1021 +#define CRC_INIT 0x0000 + +void InitCRC(void); +uint16_t CalcuCRC(char* buff, int len); + +#endif //Driver_CRC16_H diff --git a/main.c b/main.c index df9d58f..7185d96 100644 --- a/main.c +++ b/main.c @@ -2,7 +2,7 @@ * @Author: Ma Yuchen * @Date: 2022-11-22 21:03:02 * @LastEditors: Ma YuChen - * @LastEditTime: 2022-11-26 15:00:32 + * @LastEditTime: 2022-11-30 19:34:44 * @Description: file content * @FilePath: \BootLoader\main.c */ @@ -11,6 +11,7 @@ #include "bsp_gpio.h" #include "bsp_uart.h" #include "bsp_ocflash.h" +#include "driverCrc16.h" #include "systick.h" extern pFunction Jump_To_Application; @@ -24,6 +25,7 @@ int main(void) { //初始化Core时钟 systick_config(); + InitCRC(); #ifndef BOARD_EVAL //初始化GPIO并打开V3.3外设供电 InitGpio(); @@ -35,8 +37,8 @@ int main(void) SerialPutString("\r\nThanks for use this bootLoader\r\n"); - if(GetIAPIntper() == 1) - //if(1) + //if(GetIAPIntper() == 1) + if(1) //如果获取到IAP请求中断则打印菜单进行IAP操作 { Flash_IF_Init(); diff --git a/menu.c b/menu.c index 9a67862..dd71500 100644 --- a/menu.c +++ b/menu.c @@ -2,7 +2,7 @@ * @Author: Ma Yuchen * @Date: 2022-11-24 10:03:28 * @LastEditors: Ma YuChen - * @LastEditTime: 2022-11-25 15:19:15 + * @LastEditTime: 2022-11-30 19:34:36 * @Description: file content * @FilePath: \BootLoader\menu.c */ @@ -31,7 +31,7 @@ void PrintMenu(void) SerialPutString("=========================================================\r\n"); SerialPutString("= =\r\n"); SerialPutString("= BMC GD32F4XX In-Application Programming Apllication =\r\n"); - SerialPutString("= V 0.0.1 =\r\n"); + SerialPutString("= V 0.0.3 =\r\n"); SerialPutString("=========================================================\r\n"); while (1) diff --git a/ymodem.c b/ymodem.c index dbf2fea..cd439b9 100644 --- a/ymodem.c +++ b/ymodem.c @@ -2,13 +2,14 @@ * @Author: Ma Yuchen * @Date: 2022-11-23 23:29:58 * @LastEditors: Ma YuChen - * @LastEditTime: 2022-11-26 18:26:40 + * @LastEditTime: 2022-11-30 19:35:28 * @Description: file content * @FilePath: \BootLoader\ymodem.c */ #include "ymodem.h" #include "bsp_ocflash.h" #include "bsp_uart.h" +#include "driverCrc16.h" #include "systick.h" typedef struct _YModemInfo @@ -59,71 +60,6 @@ static int Send_Byte(uint8_t c) return 0; } -static uint16_t UpdateCrc16(uint16_t crcIn, uint8_t d) -{ - /* - uint8_t count = 8; - uint16_t newCrc = crc; - uint16_t data = d; - - while ((count--) > 0) - { - if (newCrc & 0x8000) - { - newCrc <<= 1; - newCrc += (((data <<= 1) & 0x0400) != 0); - newCrc ^= 0x1021; - } - else - { - newCrc <<= 1; - newCrc += (((data <<= 1) & 0x0400) != 0); - } - } - - return newCrc; - */ - uint32_t crc = crcIn; - uint32_t in = d|0x100; - - do - { - crc <<= 1; - in <<= 1; - - if(in&0x100) - { - ++crc; - } - - if(crc&0x10000) - { - crc ^= 0x1021; - } - } while(!(in&0x10000)); - - return (crc&0xffffu); -} - -static uint16_t ClcCrc16(const uint8_t *datas, int size) -{ - uint16_t crc = 0; - - const uint8_t *endofDatas = datas + size; - - while (datas < endofDatas) - { - crc = UpdateCrc16(crc, *datas); - datas++; - } - crc = UpdateCrc16(crc, 0); - crc = UpdateCrc16(crc, 0); - - return (crc & (uint16_t)0xffff); -} - -uint8_t debugData=0xff; - /** * @brief * @@ -142,16 +78,16 @@ static int Receive_Packet(uint8_t *buffer, int32_t *length, uint32_t timeout) uint16_t i, packet_size, computedcrc; uint8_t data; *length = 0; - int result=0; - - usart_start_receive_block((uint32_t)buffer, PACKET_OVERHEAD+PACKET_DATA_LENGTH); - result = Receive_Byte(timeout); + int result=0; + // receive packet + usart_start_receive_block((uint32_t)buffer, PACKET_OVERHEAD+PACKET_DATA_LENGTH); + result = Receive_Byte(timeout); if (result == 0) { return -1; } //delay_1ms(50); - data=*buffer; + data=*buffer; //SEGGER_RTT_printf(0,"%d:get Head %X result=%d\r\n",debugLoop++ , data, result); switch (data) { @@ -175,35 +111,22 @@ static int Receive_Packet(uint8_t *buffer, int32_t *length, uint32_t timeout) return -1; } default: - debugData=data; return -3; } - //*buffer = data; // buffer store header in buffer[0]; - - // receive packet - /* - for (i = 1; i < packet_size; i++) - { - if (Receive_Byte(timeout) != 0) - { - return -4; - } - } - */ // check packet no if (buffer[PACKET_NO_INDEX] != ((buffer[PACKET_NO_N_INDEX] ^ 0xff) & 0xff)) { return -2; } -/* - computedcrc = ClcCrc16(buffer[PACKET_HEADER], (int)packet_size); + + computedcrc = CalcuCRC(&(buffer[PACKET_HEADER]), (int)packet_size); if (computedcrc != (((uint16_t)buffer[packet_size + 3] << 8) | buffer[packet_size + 4])) { return -3; } -*/ + *length = packet_size; return 0; -- Gitee From bbe2479b5b2fe4c2a2ac464e06b9291f1acaaba0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E7=8E=89=E7=90=9B?= Date: Wed, 30 Nov 2022 20:46:45 +0800 Subject: [PATCH 2/4] disable all break pointers --- BootLoader.uvoptx | 35 +---------------------------------- 1 file changed, 1 insertion(+), 34 deletions(-) diff --git a/BootLoader.uvoptx b/BootLoader.uvoptx index 8b23944..a0e345d 100644 --- a/BootLoader.uvoptx +++ b/BootLoader.uvoptx @@ -313,40 +313,7 @@ UL2CM3(-S0 -C0 -P0 -FD20000000 -FC1000 -FN1 -FF0GD32F4xx_3MB -FS08000000 -FL0300000 -FP0($$Device:GD32F450ZK$Flash\GD32F4xx_3MB.FLM)) - - - 0 - 0 - 123 - 1 -
134219818
- 0 - 0 - 0 - 0 - 0 - 1 - .\ymodem.c - - \\BootLoader\ymodem.c\123 -
- - 1 - 0 - 44 - 1 -
134218332
- 0 - 0 - 0 - 0 - 0 - 1 - .\driverCrc16.c - - \\BootLoader\driverCrc16.c\44 -
-
+ 0 -- Gitee From 2af9a80dab5e82b6f871cd906d4c3051590b42da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E7=8E=89=E7=90=9B?= Date: Wed, 30 Nov 2022 20:48:58 +0800 Subject: [PATCH 3/4] update software version to v1.0.0 --- BootLoader.uvoptx | 4 ++-- menu.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/BootLoader.uvoptx b/BootLoader.uvoptx index a0e345d..f7df7da 100644 --- a/BootLoader.uvoptx +++ b/BootLoader.uvoptx @@ -75,7 +75,7 @@ 1 0 - 0 + 1 255 @@ -240,7 +240,7 @@ 1 0 - 1 + 0 255 diff --git a/menu.c b/menu.c index dd71500..e647fbc 100644 --- a/menu.c +++ b/menu.c @@ -2,7 +2,7 @@ * @Author: Ma Yuchen * @Date: 2022-11-24 10:03:28 * @LastEditors: Ma YuChen - * @LastEditTime: 2022-11-30 19:34:36 + * @LastEditTime: 2022-11-30 20:47:31 * @Description: file content * @FilePath: \BootLoader\menu.c */ @@ -31,7 +31,7 @@ void PrintMenu(void) SerialPutString("=========================================================\r\n"); SerialPutString("= =\r\n"); SerialPutString("= BMC GD32F4XX In-Application Programming Apllication =\r\n"); - SerialPutString("= V 0.0.3 =\r\n"); + SerialPutString("= V 1.0.0 =\r\n"); SerialPutString("=========================================================\r\n"); while (1) -- Gitee From 306dc1409eda6e3ba40771f58f5d8cdc6885f7dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E7=8E=89=E7=90=9B?= Date: Wed, 30 Nov 2022 20:58:45 +0800 Subject: [PATCH 4/4] disable auto load app debug --- main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index 7185d96..c48662c 100644 --- a/main.c +++ b/main.c @@ -2,7 +2,7 @@ * @Author: Ma Yuchen * @Date: 2022-11-22 21:03:02 * @LastEditors: Ma YuChen - * @LastEditTime: 2022-11-30 19:34:44 + * @LastEditTime: 2022-11-30 20:58:26 * @Description: file content * @FilePath: \BootLoader\main.c */ @@ -37,8 +37,8 @@ int main(void) SerialPutString("\r\nThanks for use this bootLoader\r\n"); - //if(GetIAPIntper() == 1) - if(1) + if(GetIAPIntper() == 1) + // if(1) //如果获取到IAP请求中断则打印菜单进行IAP操作 { Flash_IF_Init(); -- Gitee