Browse Source

1.增加支持远程升级功能

zhuzl 3 years ago
parent
commit
e738a4f049

+ 6 - 0
Core/Inc/config.h

@@ -33,8 +33,14 @@
 #define ADDR_SDATEJUDGE_NUM            0x0807F064              				      //阀门开启判断次数,2字节:(默认无)0x0005
 #define ADDR_RKG_ERROR          	     0x0807F070                          //人孔盖异常屏蔽,2字节:0x0004:屏蔽4仓人孔盖,0x0010:屏蔽所有人孔盖(默认无)0x0000
 
+#define ADDR_UPDATE_FLAG               0x0807F078                          //2??    ???
+#define StartMode_Addr                 0x0807F07A                          //4??
 
 
+#define Application_Buffer_Addr       0x08040000
+#define ADD_UPDATE_PROG               0x08020000                          //APP2???    ???
+#define ADD_UPDATE_PROG1              0x08000000
+
 
 #define   BIT1   0x0001
 #define   BIT2   0x0002

+ 39 - 48
Core/Inc/iap.h

@@ -1,63 +1,54 @@
 #ifndef __IAP_H__
 #define __IAP_H__
-#include "sys.h"  
+
+#include "main.h"
+#include "cmsis_os.h"
+#include "FreeRTOS.h"
+#include "task.h"
+#include "timers.h"
+#include "event_groups.h"
+/* Private includes ----------------------------------------------------------*/
+/* USER CODE BEGIN Includes */
+#include "string.h"
+#include "stdio.h"
+#include "usart.h"
+#include "config.h"
+#include "rkg.h"
+#include "cang.h"
+#include "kzq.h"
+#include "level.h"
+#include "tem.h"
+#include "angle.h"
+#include "xyf.h"
+#include "hdf.h"
+#include "bgy.h"
+#include "yqhs.h"
 //////////////////////////////////////////////////////////////////////////////////	 
-//本程序只供学习使用,未经作者许可,不得用于其它任何用途
-//ALIENTEK战舰STM32开发板
-//IAP 代码	   
-//正点原子@ALIENTEK
-//技术论坛:www.openedv.com
-//修改日期:2012/9/24
-//版本:V1.0
-//版权所有,盗版必究。
-//Copyright(C) 广州市星翼电子科技有限公司 2009-2019
+//邵磊明   2022/01/11
 //All rights reserved									  
 //////////////////////////////////////////////////////////////////////////////////	
 typedef  void (*iapfun)(void);				//定义一个函数类型的参数.
 
 #define FLASH_APP1_ADDR		0x08010000  	//第一个应用程序起始地址(存放在FLASH)
 											//保留0X08000000~0X0800FFFF的空间为IAP使用
-
-void iap_load_app(u32 appxaddr);			//执行flash里面的app程序
-void iap_load_appsram(u32 appxaddr);		//执行sram里面的app程序
-void iap_write_appbin(u32 appxaddr,u8 *appbuf,u32 applen);	//在指定地址开始,写入bin
-#endif
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+/* 启动选项 */
+#define Startup_Normal 	0xBBBBBBBB	///< 正常启动   跳转到APP1
+#define Startup_Update 	0xAAAAAAAA	///< 升级再启动 
+#define Startup_Reset  	0x5555AAAA	///< ***恢复出厂 目前没使用***
+#define Startup_APP2  	0x5555BBBB	///< ***跳转到APP2***
+#define StartBytes_IAP   ((uint32_t)0xA55A55AA) //远程升级的起始字节
+#define EndBytes_IAP     ((uint32_t)0x5AA5AA55) //远程升级的结束字节
 
 
 
 
 
+void Process_CMD_IAP_Update(void);
+void Set_Update_Down(void);
+void Res_Update_Down(void);
+void Start_BootLoader(void);
+void iap_load_app(uint32_t appxaddr);			//执行flash里面的app程序
+void iap_load_appsram(uint32_t appxaddr);		//执行sram里面的app程序
+void iap_write_appbin(uint32_t appxaddr,uint8_t *appbuf,uint32_t applen);	//在指定地址开始,写入bin
 
+#endif

+ 2 - 2
Core/Inc/main.h

@@ -34,8 +34,8 @@ extern "C" {
 /* USER CODE BEGIN Includes */
 
 //0----关闭看门狗SP706   1----打开看门狗SP706--------------必须配置的参数1/3
-//#define WatchDogOn   1   //是否打开看门狗 
-#define WatchDogOn   0   //是否打开看门狗 
+#define WatchDogOn   1   //是否打开看门狗 
+//#define WatchDogOn   0   //是否打开看门狗 
 
 /* USER CODE END Includes */
 

+ 27 - 0
Core/Inc/md5c.h

@@ -0,0 +1,27 @@
+#ifndef __MD5C_H__
+#define __MD5C_H__
+/* POINTER defines a generic pointer type */ 
+typedef unsigned char * POINTER; 
+ 
+/* UINT2 defines a two byte word */ 
+//typedef unsigned short int UINT2; 
+ 
+/* UINT4 defines a four byte word */ 
+typedef unsigned long int UINT4; 
+ 
+ 
+/* MD5 context. */ 
+typedef struct { 
+ UINT4 state[4];         /* state (ABCD) */ 
+ UINT4 count[2];  /* number of bits, modulo 2^64 (lsb first) */ 
+ unsigned char buffer[64];       /* input buffer */ 
+} MD5_CTX; 
+ 
+void MD5Init (MD5_CTX *context); 
+void MD5Update (MD5_CTX *context, unsigned char *input, unsigned int inputLen); 
+void MD5UpdaterString(MD5_CTX *context,const char *string); 
+int MD5FileUpdateFile (MD5_CTX *context,char *filename); 
+void MD5Final (unsigned char digest[16], MD5_CTX *context); 
+void MDString (char *string,unsigned char digest[16]); 
+int MD5File (char *filename,unsigned char digest[16]); 
+#endif

+ 78 - 0
Core/Inc/stmflash.h

@@ -0,0 +1,78 @@
+#ifndef __STMFLASH_H__
+#define __STMFLASH_H__
+#include "main.h"
+#include "cmsis_os.h"
+#include "FreeRTOS.h"
+#include "task.h"
+#include "timers.h"
+#include "event_groups.h"
+/* Private includes ----------------------------------------------------------*/
+/* USER CODE BEGIN Includes */
+#include "string.h"
+#include "stdio.h"
+#include "usart.h"
+#include "config.h"
+#include "rkg.h"
+#include "cang.h"
+#include "kzq.h"
+#include "level.h"
+#include "tem.h"
+#include "angle.h"
+#include "xyf.h"
+#include "hdf.h"
+#include "bgy.h"
+#include "yqhs.h"
+//////////////////////////////////////////////////////////////////////////////////	 
+//本程序只供学习使用,未经作者许可,不得用于其它任何用途
+//ALIENTEK战舰STM32开发板V3
+//STM32 FLASH 驱动代码	   
+//正点原子@ALIENTEK
+//技术论坛:www.openedv.com
+//创建日期:2017/6/1
+//版本:V1.1
+//版权所有,盗版必究。
+//Copyright(C) 广州市星翼电子科技有限公司 2009-2019
+//All rights reserved									   
+//////////////////////////////////////////////////////////////////////////////////
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////
+//用户根据自己的需要设置
+extern void  FLASH_PageErase(uint32_t PageAddress);
+
+#define STM32_FLASH_SIZE 	512 	 		//所选STM32的FLASH容量大小(单位为K)
+#define STM32_FLASH_WREN 	1              	//使能FLASH写入(0,不是能;1,使能)
+#define FLASH_WAITETIME  	50000          	//FLASH等待超时时间
+
+//FLASH起始地址
+#define STM32_FLASH_BASE 0x08000000 		//STM32 FLASH的起始地址
+
+uint8_t STMFLASH_GetStatus(void);				  //获得状态
+uint8_t STMFLASH_WaitDone(uint16_t time);				  //等待操作结束
+uint8_t STMFLASH_ErasePage(uint32_t paddr);			  //擦除页
+uint8_t STMFLASH_WriteHalfWord(uint32_t faddr, uint16_t dat);//写入半字
+uint16_t STMFLASH_ReadHalfWord(uint32_t faddr);		  //读出半字  
+void STMFLASH_WriteLenByte(uint32_t WriteAddr,uint32_t DataToWrite,uint16_t Len);	//指定地址开始写入指定长度的数据
+uint32_t STMFLASH_ReadLenByte(uint32_t ReadAddr,uint16_t Len);						//指定地址开始读取指定长度数据
+void STMFLASH_Write(uint32_t WriteAddr,uint16_t *pBuffer,uint16_t NumToWrite);		//从指定地址开始写入指定长度的数据
+void STMFLASH_Read(uint32_t ReadAddr,uint16_t *pBuffer,uint16_t NumToRead);   		//从指定地址开始读出指定长度的数据
+
+//测试写入
+void Test_Write(uint32_t WriteAddr,uint16_t WriteData);								   
+#endif
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+ 1 - 1
Core/Inc/usart.h

@@ -68,7 +68,7 @@ extern UART_HandleTypeDef huart3;
 /* USER CODE BEGIN Private defines */
 #define BUF_SIZE 500
 #define Uart1_BUF_SIZE  256
-#define Uart2_BUF_SIZE  256      /*为UART2、3专门开一个DMA接收存储区 2021-4-12 by Daiyf*/
+#define Uart2_BUF_SIZE  1024      /*为UART2、3专门开一个DMA接收存储区 2021-4-12 by Daiyf*/
 #define Uart3_BUF_SIZE  256
 	 
 extern uint8_t usart1_rx_flag;

+ 397 - 51
Core/Src/iap.c

@@ -1,71 +1,417 @@
-#include "sys.h"
-#include "delay.h"
+
+#include "main.h"
+#include "cmsis_os.h"
+#include "FreeRTOS.h"
+#include "task.h"
+#include "timers.h"
+#include "event_groups.h"
+/* Private includes ----------------------------------------------------------*/
+/* USER CODE BEGIN Includes */
+#include "string.h"
+#include "stdio.h"
 #include "usart.h"
-#include "stmflash.h"
+#include "config.h"
+#include "rkg.h"
+#include "cang.h"
+#include "kzq.h"
+#include "level.h"
+#include "tem.h"
+#include "angle.h"
+#include "xyf.h"
+#include "hdf.h"
+#include "bgy.h"
+#include "yqhs.h"
 #include "iap.h"
-//////////////////////////////////////////////////////////////////////////////////	 
-//本程序只供学习使用,未经作者许可,不得用于其它任何用途
-//ALIENTEK战舰STM32开发板
-//IAP 代码	   
-//正点原子@ALIENTEK
-//技术论坛:www.openedv.com
-//修改日期:2012/9/24
-//版本:V1.0
-//版权所有,盗版必究。
-//Copyright(C) 广州市星翼电子科技有限公司 2009-2019
-//All rights reserved									  
-//////////////////////////////////////////////////////////////////////////////////	
+#include "stmflash.h"
+#include "md5c.h"
+//////////////////////////////////////////////////////////////////////////////////
+extern void __set_FAULTMASK(uint32_t faultMask);
+//////////////////////////////////////////////////////////////////////////////////
 
-iapfun jump2app; 
-u16 iapbuf[1024];   
-//appxaddr:应用程序的起始地址
-//appbuf:应用程序CODE.
-//appsize:应用程序大小(字节).
-void iap_write_appbin(u32 appxaddr,u8 *appbuf,u32 appsize)
+iapfun jump2app;
+uint16_t iapbuf[1024];
+
+__asm void MSR_MSP(uint32_t addr) 
+{
+    MSR MSP, r0 			//set Main Stack value
+    BX r14
+}
+
+int App2_MD5_Check(uint32_t addr,unsigned int all_len)
+{
+	unsigned char digest[16];
+//	unsigned char *md5_ptr=(unsigned char *)(StartMode_Addr+72);
+	unsigned int i,update_len;
+	MD5_CTX md5c;
+	all_len -= 16;
+	MD5Init(&md5c);
+	for(i=0;i<all_len;)
+	{
+		if(all_len>(i+512))
+			update_len = 512;
+		else
+			update_len = all_len-i;
+		memcpy(iapbuf,(const void *)(addr+i),update_len);
+		MD5Update (&md5c, (char *)iapbuf, update_len);
+		i += update_len;
+		HAL_GPIO_TogglePin(WDI_sp706_kanmemgou_GPIO_Port, WDI_sp706_kanmemgou_Pin);
+	}
+	MD5Final(digest,&md5c);
+	//memcpy(iapbuf,(const void *)(addr+all_len),16);
+	for(i=0;i<16;++i)
+	{
+		if(digest[i]!=*(unsigned char *)(addr+all_len+i))
+			break;
+	}
+	if(i>=16)
+		return 1;
+	else
+		return 0;
+}
+// appxaddr:应用程序的起始地址
+// appbuf:应用程序CODE.
+// appsize:应用程序大小(字节).
+void iap_write_appbin(uint32_t appxaddr, uint8_t *appbuf, uint32_t appsize)
 {
-	u16 t;
-	u16 i=0;
-	u16 temp;
-	u32 fwaddr=appxaddr;//当前写入的地址
-	u8 *dfu=appbuf;
-	for(t=0;t<appsize;t+=2)
-	{						    
-		temp=(u16)dfu[1]<<8;
-		temp+=(u16)dfu[0];	  
-		dfu+=2;//偏移2个字节
-		iapbuf[i++]=temp;	    
-		if(i==1024)
+	uint32_t t;
+	uint16_t i = 0;
+	uint16_t temp;
+	uint32_t fwaddr = appxaddr; //当前写入的地址
+	uint8_t *dfu = appbuf;
+	for (t = 0; t < appsize; t += 2)
+	{
+		temp = (uint16_t)dfu[1] << 8;
+		temp += (uint16_t)dfu[0];
+		dfu += 2; //偏移2个字节
+		iapbuf[i++] = temp;
+		if (i == 1024)
 		{
-			i=0;
-			STMFLASH_Write(fwaddr,iapbuf,1024);	
-			fwaddr+=2048;//偏移2048  16=2*8.所以要乘以2.
+			i = 0;
+			HAL_GPIO_TogglePin(WDI_sp706_kanmemgou_GPIO_Port, WDI_sp706_kanmemgou_Pin);
+			STMFLASH_Write(fwaddr, iapbuf, 1024);
+			fwaddr += 2048; //偏移2048  16=2*8.所以要乘以2.
 		}
+		
 	}
-	if(i)STMFLASH_Write(fwaddr,iapbuf,i);//将最后的一些内容字节写进去.  
+	if (i)
+		STMFLASH_Write(fwaddr, iapbuf, i); //将最后的一些内容字节写进去.
 }
 
 //跳转到应用程序段
-//appxaddr:用户代码起始地址.
-void iap_load_app(u32 appxaddr)
+// appxaddr:用户代码起始地址.
+void iap_load_app(uint32_t appxaddr)
 {
-	if(((*(vu32*)appxaddr)&0x2FFE0000)==0x20000000)	//检查栈顶地址是否合法.
-	{ 
-		jump2app=(iapfun)*(vu32*)(appxaddr+4);		//用户代码区第二个字为程序开始地址(复位地址)		
-		MSR_MSP(*(vu32*)appxaddr);					      //初始化APP堆栈指针(用户代码区的第一个字用于存放栈顶地址)
-		jump2app();									              //跳转到APP.
+	if (((*(uint32_t *)appxaddr) & 0x2FFE0000) == 0x20000000) //检查栈顶地址是否合法.
+	{
+		jump2app = (iapfun) * (uint32_t *)(appxaddr + 4); //用户代码区第二个字为程序开始地址(复位地址)
+		MSR_MSP(*(uint32_t *)appxaddr);					  //初始化APP堆栈指针(用户代码区的第一个字用于存放栈顶地址)
+		jump2app();										  //跳转到APP.
 	}
-}		 
-
-
-
-
-
-
+}
 
+/**
+ * @bieaf 进行BootLoader的启动
+ *
+ * @param none
+ * @return none
+ */
+void Start_BootLoader(void)
+{
+	unsigned int update_flag = 0xAAAAAAAA; ///< 对应bootloader的启动步骤
+	unsigned int ModeStart;
+	unsigned int i,all_len;
+	uint16_t ModeStart1[2];
+	HAL_GPIO_TogglePin(WDI_sp706_kanmemgou_GPIO_Port, WDI_sp706_kanmemgou_Pin);
+	Flash_ReadBytes(ModeStart1, StartMode_Addr, 2);
+	ModeStart = ModeStart1[0];
+	ModeStart <<= 16;
+	ModeStart |= ModeStart1[1];
+	switch (ModeStart) ///< 读取是否启动应用程序 */
+	{
+	case Startup_Normal: ///< 正常启动 */   //在APP2中可以设定此标志位   使得下次重启之后进入APP1
+	{
+		; // printf("> Normal start......\r\n");
+		break;
+	}
+	case Startup_Update: /*启动最新的程序 */
+		HAL_GPIO_TogglePin(WDI_sp706_kanmemgou_GPIO_Port, WDI_sp706_kanmemgou_Pin);
+		for (i = 0, all_len = 0; i < 4; ++i)
+		{
+				all_len = all_len << 8;
+				all_len |= *(unsigned char *)(StartMode_Addr + 68 + i);
+		}
+		if (App2_MD5_Check(Application_Buffer_Addr,all_len))
+			{
+				iap_write_appbin(ADD_UPDATE_PROG,(uint8_t *)Application_Buffer_Addr,all_len);
+				HAL_GPIO_TogglePin(WDI_sp706_kanmemgou_GPIO_Port, WDI_sp706_kanmemgou_Pin);
+				Flash_WriteBytes((uint16_t*)(StartMode_Addr + 68), StartMode_Addr+132, 2);
+			}
+			Set_App2_Flag();
+	case Startup_APP2: /*启动最新的程序 */
+		for (i = 0, all_len = 0; i < 4; ++i)
+			{
+				all_len = all_len << 8;
+				all_len |= *(unsigned char *)(StartMode_Addr + 132 + i);
+			}
+			if ((all_len<0x20000) && App2_MD5_Check(ADD_UPDATE_PROG,all_len))
+			{
+				__set_FAULTMASK(0);			   //先关闭全局中断
+				iap_load_app(ADD_UPDATE_PROG); //执行FLASH APP2代码
+			}
+		break;
+	default: ///< 启动失败
+	{
+		return;
+	}
+	}
+}
 
+// extern uint8_t ACK_Arr[16];//={'A','C','K'};
+// extern uint8_t ERR_Arr[8];//={'E','R','R'};
+// extern void Set_Update_Down(void);
 
+uint8_t YmodemID;
+uint16_t packIndex, packTotalNum, packIndexbak; //当前包号,总包数
+uint16_t crcIAP, crcTerminal, crcIAP1;
+uint32_t FileLength_IAP = 0;  //文件长度
+uint16_t FileBuffArray[512];  // MD5校验
+uint16_t FileBuffArray1[512]; // MD5校验
+uint32_t AddrToWrite = Application_Buffer_Addr;
+uint32_t DataReadFlash, DataReadFlash1, update_flag;
+void Set_Update_Down(void)
+{
+	uint16_t update_flag1[2];
+	update_flag = Startup_Update; ///< 对应bootloader的启动步骤
+	update_flag1[0] = (uint16_t)update_flag;
+	update_flag1[1] = (uint16_t)(update_flag >> 16);
+	Flash_WriteBytes(update_flag1, StartMode_Addr, 2);
+	__set_FAULTMASK(1);
+	NVIC_SystemReset();
+}
 
+void Res_Update_Down(void)
+{
+	uint16_t update_flag1[2];
+	update_flag = Startup_Normal; ///< 对应bootloader的启动步骤
+	update_flag1[0] = (uint16_t)update_flag;
+	update_flag1[1] = (uint16_t)(update_flag >> 16);
+	Flash_WriteBytes(update_flag1, StartMode_Addr, 2);
+	__set_FAULTMASK(1);
+	NVIC_SystemReset();
+}
+void Set_App2_Flag(void)
+{
+	uint16_t update_flag1[2];
+	update_flag = Startup_APP2; ///< 对应bootloader的启动步骤
+	update_flag1[1] = (uint16_t)update_flag;
+	update_flag1[0] = (uint16_t)(update_flag >> 16);
+	Flash_WriteBytes(update_flag1, StartMode_Addr, 2);
+	//__set_FAULTMASK(1);
+	//NVIC_SystemReset();
+}
+void Process_CMD_IAP_Update(void)
+{
 
+	int lenRx1, lenRx2;
+	int i;
+	uint16_t CP_CNT = 0, j = 0;
+	static uint16_t update_frame_num = 0;
+	for (i = 0; i < 512; i++)
+	{
+		FileBuffArray[i] = 0;
+		FileBuffArray1[i] = 0;
+	}
+	packIndex = (USART2_RX_BUF002[5] << 8) + USART2_RX_BUF002[6];
+	packTotalNum = (USART2_RX_BUF002[7] << 8) + USART2_RX_BUF002[8];
+	lenRx1 = data_lengthU2;
+	YmodemID = USART2_RX_BUF002[4]; //只能是01,02
+	data_lengthU2 = 0;
+	uint8_t ERR_Arr[8] = "error\r\n";
+	uint8_t ACK_Arr[16] = "acknowlege";
 
+	HAL_GPIO_TogglePin(WDI_sp706_kanmemgou_GPIO_Port, WDI_sp706_kanmemgou_Pin);
+	switch (YmodemID)
+	{
+	case 0x01: //起始帧与结束帧解析  根据包序号判别该帧为起始帧还是结束帧
+		if (packIndex == 1)
+		{ //起始帧处理
+			FileLength_IAP = (USART2_RX_BUF002[73] << 24) + (USART2_RX_BUF002[74] << 16) + (USART2_RX_BUF002[75] << 8) + (USART2_RX_BUF002[76]);
+			crcIAP = LIB_CRC_MODBUS(&USART2_RX_BUF002[4], 133); //
+			crcTerminal = (USART2_RX_BUF002[lenRx1 - 5] << 8) + USART2_RX_BUF002[lenRx1 - 6];
+			if (crcIAP == crcTerminal)
+			{
+				for (i = 0; i < 88; ++i)
+				{
+					if (*(unsigned char *)(StartMode_Addr + i + 4) != USART2_RX_BUF002[9 + i])
+						break;
+				}
+				if (i < 88)
+				{
+					for (i = 0; i < 44; i++)
+					{
+						FileBuffArray[i] = (uint16_t)(USART2_RX_BUF002[i * 2 + 10] << 8 | USART2_RX_BUF002[i * 2 + 9]);
+					}
+					HAL_GPIO_TogglePin(WDI_sp706_kanmemgou_GPIO_Port, WDI_sp706_kanmemgou_Pin);
+					Flash_WriteBytes(FileBuffArray, StartMode_Addr + 4, 44);
+					HAL_GPIO_TogglePin(WDI_sp706_kanmemgou_GPIO_Port, WDI_sp706_kanmemgou_Pin);
+					delay_sys_us(80);
+					packIndexbak = 0;
+					AddrToWrite = Application_Buffer_Addr;
+					HAL_GPIO_TogglePin(WDI_sp706_kanmemgou_GPIO_Port, WDI_sp706_kanmemgou_Pin);
+				}
+				else
+				{
+					if(packIndexbak>1)
+						packIndexbak-=1;
+				}
 
+				{
+					static uint16_t txLen;
+					for (i = 0; i < 1024; i++)
+					{
+						USART2_RX_BUF002[i] = 0;
+					}
+					txLen = sprintf((char *)FileBuffArray, "file=%d\r\n", packIndexbak);
+					HAL_GPIO_WritePin(GPIOA, con03_uart2_kongzhiqi_Pin, GPIO_PIN_RESET); //
+					delay_sys_us(80);
+					HAL_UART_Transmit_IT(&huart2,(char *)FileBuffArray,txLen);		
+					while (huart2.gState == HAL_UART_STATE_BUSY_TX)
+					{
+						osDelay(1);
+					}
+					delay_sys_us(80);
+					delay_sys_us(80);
+					HAL_GPIO_WritePin(GPIOA, con03_uart2_kongzhiqi_Pin, GPIO_PIN_SET); //
+				}
+			}
+			else
+			{
+				for (i = 0; i < 1024; i++)
+				{
+					USART2_RX_BUF002[i] = 0;
+				}
+				HAL_GPIO_WritePin(GPIOA, con03_uart2_kongzhiqi_Pin, GPIO_PIN_RESET); //
+				delay_sys_us(80);
+				HAL_UART_Transmit(&huart2, ERR_Arr, strlen((char *)ERR_Arr), 100);
+				delay_sys_us(80);
+				HAL_GPIO_WritePin(GPIOA, con03_uart2_kongzhiqi_Pin, GPIO_PIN_SET); //
+				__NOP();
+			}
+			__NOP();
+		}
+		else
+		{
+			__NOP();
+		}
+		break;
+	case 0x02:														//有效载荷帧数据处理
+		crcIAP = LIB_CRC_MODBUS(&USART2_RX_BUF002[4], lenRx1 - 10); //
+		crcTerminal = (USART2_RX_BUF002[lenRx1 - 5] << 8) + USART2_RX_BUF002[lenRx1 - 6];
+		if ((crcIAP == crcTerminal) && (((packIndexbak + 1) == packIndex) || ((packIndexbak) == packIndex)))
+		{
+			if ((packIndexbak + 1) == packIndex)
+			{
+				lenRx2 = (lenRx1 - 15) % 2;
+				if (!lenRx2)
+				{
+					for (i = 0; i < ((lenRx1 - 15) / 2); i++)
+					{
+						FileBuffArray[i] = (uint16_t)(USART2_RX_BUF002[i * 2 + 10] << 8 | USART2_RX_BUF002[i * 2 + 9]);
+						FileBuffArray1[i] = (uint16_t)(USART2_RX_BUF002[i * 2 + 10] << 8 | USART2_RX_BUF002[i * 2 + 9]);
+					}
+					Flash_WriteBytes(FileBuffArray, AddrToWrite, ((lenRx1 - 15) / 2));
+					HAL_GPIO_TogglePin(WDI_sp706_kanmemgou_GPIO_Port, WDI_sp706_kanmemgou_Pin);
+					Flash_ReadBytes(FileBuffArray1, AddrToWrite, ((lenRx1 - 15) / 2));
+					HAL_GPIO_TogglePin(WDI_sp706_kanmemgou_GPIO_Port, WDI_sp706_kanmemgou_Pin);
+				}
+				else
+				{
+					for (i = 0; i < ((lenRx1 - 16) / 2); i++)
+					{
+						FileBuffArray[i] = (uint16_t)(USART2_RX_BUF002[i * 2 + 10] << 8 | USART2_RX_BUF002[i * 2 + 9]);
+						FileBuffArray1[i] = (uint16_t)(USART2_RX_BUF002[i * 2 + 10] << 8 | USART2_RX_BUF002[i * 2 + 9]);
+					}
+					FileBuffArray[(lenRx1 - 16) / 2] = USART2_RX_BUF002[(lenRx1 - 16 + 10)] << 8;
+					FileBuffArray1[(lenRx1 - 16) / 2] = USART2_RX_BUF002[(lenRx1 - 16 + 10)] << 8;
+					HAL_GPIO_TogglePin(WDI_sp706_kanmemgou_GPIO_Port, WDI_sp706_kanmemgou_Pin);
+					Flash_WriteBytes(FileBuffArray, AddrToWrite, ((lenRx1 - 15 + 1) / 2));
+					HAL_GPIO_TogglePin(WDI_sp706_kanmemgou_GPIO_Port, WDI_sp706_kanmemgou_Pin);
+					Flash_ReadBytes(FileBuffArray1, AddrToWrite, ((lenRx1 - 15 + 1) / 2));
+					HAL_GPIO_TogglePin(WDI_sp706_kanmemgou_GPIO_Port, WDI_sp706_kanmemgou_Pin);
+				}
+				AddrToWrite += lenRx1 - 15;
+				DataReadFlash = *((uint32_t *)(AddrToWrite));
+				DataReadFlash1 = (*(__IO uint32_t *)AddrToWrite);
+				HAL_GPIO_TogglePin(WDI_sp706_kanmemgou_GPIO_Port, WDI_sp706_kanmemgou_Pin);
+			}
+			if (DataReadFlash == DataReadFlash1)
+			{
+				__NOP();
+				HAL_GPIO_TogglePin(WDI_sp706_kanmemgou_GPIO_Port, WDI_sp706_kanmemgou_Pin);
+				packIndexbak = packIndex;
+			}
 
+			{
+				int txLen;
+				for (i = 0; i < 1024; i++)
+				{
+					USART2_RX_BUF002[i] = 0;
+				}
+				txLen = sprintf((char *)USART2_RX_BUF002, "%s=%d\r\n", ACK_Arr, packIndex);
+				HAL_GPIO_WritePin(GPIOA, con03_uart2_kongzhiqi_Pin, GPIO_PIN_RESET); //
+				delay_sys_us(80);
+				HAL_UART_Transmit_IT(&huart2,USART2_RX_BUF002,txLen);		
+				while (huart2.gState == HAL_UART_STATE_BUSY_TX)
+				{
+					osDelay(1);
+				}
+				HAL_GPIO_WritePin(GPIOA, con03_uart2_kongzhiqi_Pin, GPIO_PIN_SET); //
+			}
+			__NOP();
+		}
+		else
+		{
+			HAL_GPIO_WritePin(GPIOA, con03_uart2_kongzhiqi_Pin, GPIO_PIN_RESET); //
+			delay_sys_us(80);
+			HAL_UART_Transmit(&huart2, ERR_Arr, strlen((char *)ERR_Arr), 100);
+			delay_sys_us(80);
+			HAL_GPIO_WritePin(GPIOA, con03_uart2_kongzhiqi_Pin, GPIO_PIN_SET); //
+			__NOP();
+		}
+		break;
+	case 3:
+		//	if(packIndex>2)
+		{														 //结束帧处理
+			crcIAP1 = LIB_CRC_MODBUS(&USART2_RX_BUF002[4], 133); //
+			crcTerminal = (USART2_RX_BUF002[lenRx1 - 5] << 8) + USART2_RX_BUF002[lenRx1 - 6];
+			if (crcIAP1 == crcTerminal)
+			{
+				int txLen;
+				HAL_GPIO_WritePin(GPIOA, con03_uart2_kongzhiqi_Pin, GPIO_PIN_RESET); //
+				delay_sys_us(80);
+				txLen = sprintf((char *)USART2_RX_BUF002, "%s=%d\r\n", ACK_Arr, packIndex);
+				HAL_UART_Transmit_IT(&huart2,USART2_RX_BUF002,txLen);		
+				while (huart2.gState == HAL_UART_STATE_BUSY_TX)
+				{
+					osDelay(1);
+				}
+				HAL_GPIO_WritePin(GPIOA, con03_uart2_kongzhiqi_Pin, GPIO_PIN_SET); //
+				__NOP();
+				Set_Update_Down(); //设定标志位  下次启动时进行程序拷贝
+			}
+			else
+			{
+				HAL_GPIO_WritePin(GPIOA, con03_uart2_kongzhiqi_Pin, GPIO_PIN_RESET); //
+				delay_sys_us(80);
+				HAL_UART_Transmit(&huart2, ERR_Arr, strlen((char *)ERR_Arr), 100);
+				delay_sys_us(80);
+				HAL_GPIO_WritePin(GPIOA, con03_uart2_kongzhiqi_Pin, GPIO_PIN_SET); //
+				__NOP();
+			}
+			__NOP();
+		}
+		break;
+	default:
+		break;
+	}
+}

+ 43 - 51
Core/Src/iap.h

@@ -1,63 +1,55 @@
 #ifndef __IAP_H__
 #define __IAP_H__
-#include "sys.h"  
+
+#include "main.h"
+#include "cmsis_os.h"
+#include "FreeRTOS.h"
+#include "task.h"
+#include "timers.h"
+#include "event_groups.h"
+/* Private includes ----------------------------------------------------------*/
+/* USER CODE BEGIN Includes */
+#include "string.h"
+#include "stdio.h"
+#include "usart.h"
+#include "config.h"
+#include "rkg.h"
+#include "cang.h"
+#include "kzq.h"
+#include "level.h"
+#include "tem.h"
+#include "angle.h"
+#include "xyf.h"
+#include "hdf.h"
+#include "bgy.h"
+#include "yqhs.h"
 //////////////////////////////////////////////////////////////////////////////////	 
-//本程序只供学习使用,未经作者许可,不得用于其它任何用途
-//ALIENTEK战舰STM32开发板
-//IAP 代码	   
-//正点原子@ALIENTEK
-//技术论坛:www.openedv.com
-//修改日期:2012/9/24
-//版本:V1.0
-//版权所有,盗版必究。
-//Copyright(C) 广州市星翼电子科技有限公司 2009-2019
+//???   2022/01/11
 //All rights reserved									  
 //////////////////////////////////////////////////////////////////////////////////	
-typedef  void (*iapfun)(void);				//定义一个函数类型的参数.
-
-#define FLASH_APP1_ADDR		0x08010000  	//第一个应用程序起始地址(存放在FLASH)
-											//保留0X08000000~0X0800FFFF的空间为IAP使用
-
-void iap_load_app(u32 appxaddr);			//执行flash里面的app程序
-void iap_load_appsram(u32 appxaddr);		//执行sram里面的app程序
-void iap_write_appbin(u32 appxaddr,u8 *appbuf,u32 applen);	//在指定地址开始,写入bin
-#endif
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+typedef  void (*iapfun)(void);				//???????????.
 
+#define FLASH_APP1_ADDR		0x08010000  	//???????????(???FLASH)
+											//??0X08000000~0X0800FFFF????IAP??
+/* ???? */
+#define Startup_Normal 	0xBBBBBBBB	///< ????   ???APP1
+#define Startup_Update 	0xAAAAAAAA	///< ????? 
+#define Startup_Reset  	0x5555AAAA	///< ***???? ?????***
+#define Startup_APP2  	0x5555BBBB	///< ***???APP2***
+#define StartBytes_IAP   ((uint32_t)0xA55A55AA) //?????????
+#define EndBytes_IAP     ((uint32_t)0x5AA5AA55) //?????????
 
 
 
 
 
+void Process_CMD_IAP_Update(void);
+void Set_Update_Down(void);
+void Res_Update_Down(void);
+void Set_App2_Flag(void);
+void Start_BootLoader(void);
+void iap_load_app(uint32_t appxaddr);			//??flash???app??
+void iap_load_appsram(uint32_t appxaddr);		//??sram???app??
+void iap_write_appbin(uint32_t appxaddr,uint8_t *appbuf,uint32_t applen);	//???????,??bin
 
+#endif

+ 1 - 1
Core/Src/kzq.c

@@ -3882,7 +3882,7 @@ uint16_t Tem_SetType(uint8_t* pTx)
 	else
 		return 0;
 }
-uint32_t Version_Soft[8] = {2, 1, 3, 9, 1, 1, 0, 20220414};
+uint32_t Version_Soft[8] = {2, 2, 3, 9, 1, 1, 0, 20220424};
 uint16_t Read_SoftVersion(uint8_t *pTx)
 {
 	int i;

+ 71 - 50
Core/Src/main.c

@@ -40,7 +40,13 @@
 #include "hdf.h"
 #include "bgy.h"
 #include "yqhs.h"
+#include "iap.h"
 /* USER CODE END Includes */
+#if 1
+#define IR_ROM1   0x08000000
+#else
+#define IR_ROM1   0x08020000
+#endif
 
 /* Private typedef -----------------------------------------------------------*/
 /* USER CODE BEGIN PTD */
@@ -818,7 +824,12 @@ int main(void)
 
 	int spr000;
   /* USER CODE END 1 */
-
+#if	IR_ROM1 == 0x08000000	
+	Start_BootLoader();
+#else	
+	SCB->VTOR = 0x08020000;
+	__set_PRIMASK(0);
+#endif	
   /* MCU Configuration--------------------------------------------------------*/
 
   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
@@ -1660,7 +1671,7 @@ uni_float tprture001,tprture002,tprture003,Yewei001,tprtureAver;
 void StartTask02(void *argument)				 															//卸油阀总线传感器命令轮询  UART3
 {
   /* USER CODE BEGIN StartTask02 */
-	uint8_t i001=0,i002 = 0,rx_len = 0;
+	uint8_t i001=0,i002 = 0;
 	uint16_t ModbusCRC = 0,xyfaddr_max = 0,hdfaddr_max;                     				 //xyfaddr_max:卸油阀最高地址,根据每仓卸油阀之和计算得出
 	static uint16_t i = 0,j = 0,receive_error = 0;
 	HDF_Inf* phdf = hdf_inf;
@@ -2372,6 +2383,7 @@ void StartTask03(void *argument)															//
 		{
 			if(i002 == 3)
 			{
+				extern uint16_t Uart_len_TouChuan;
 				HAL_GPIO_WritePin(GPIOB,Con01_uart1_rankonggai_Pin,GPIO_PIN_RESET);//
 				delay_sys_us(80);
 #if 0				
@@ -2830,6 +2842,7 @@ void StartTask04(void *argument)                                    //
 	int i000;
 	uint8_t* send_ptr;
 	uint16_t send_len;
+	uint32_t tmpU32;
 	
 	KZQ_Inf* pkzq = &kzq_inf;
 
@@ -2843,61 +2856,69 @@ void StartTask04(void *argument)                                    //
     osDelay(50);    																								  //以ms为单位
 	//	HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_0);
 
-		HAL_GPIO_TogglePin(WDI_sp706_kanmemgou_GPIO_Port, WDI_sp706_kanmemgou_Pin);		
-		if(flagU2Rx==1)
+	HAL_GPIO_TogglePin(WDI_sp706_kanmemgou_GPIO_Port, WDI_sp706_kanmemgou_Pin);
+	if (flagU2Rx == 1)
+	{
+		flagU2Rx = 0;
+		if ((rx1_len > 300) || ((USART2_RX_BUF002[0] == 0xa5) && (USART2_RX_BUF002[1] == 0x5a)))
 		{
-			flagU2Rx=0;
-			//KZQ_RTerror = 0;
-			//ASC转换为16进制,收到数据为:3901开头的数据总长度131字节
-			if((USART2_RX_BUF002[0] == 0x3A) && (USART2_RX_BUF002[1] == 0x33)&& (USART2_RX_BUF002[2] == 0x39)&& (USART2_RX_BUF002[3] == 0x30))   //判断帧头
+			tmpU32 = (USART2_RX_BUF002[0] << 24) | (USART2_RX_BUF002[1] << 16) | (USART2_RX_BUF002[2] << 8) | (USART2_RX_BUF002[3]);
+			if (StartBytes_IAP == tmpU32)
 			{
-				if(data_lengthU2!=131)
-				{
-					++KZQ_RTerror;
-					continue;
-				}
-				for(i000=0;i000<(data_lengthU2-3)/2;i000++)
-				{
-					T2C_RemoteCaliDat001.PayLoadData[i000]=MODBUS_ASCII_AsciiToHex(USART2_RX_BUF002+1+0+i000*2);
-					
-					USART2_RX_BUF003[i000]=MODBUS_ASCII_AsciiToHex(USART2_RX_BUF002+1+0+i000*2);
-
-				}	
-				//解析后的数据拷贝过来
-				memcpy(USART2_RX_BUF002,USART2_RX_BUF003,(data_lengthU2-3)/2);
-				data_lengthU2=(data_lengthU2-3)/2;
-			}
-			if(data_lengthU2<5)
+				Process_CMD_IAP_Update();
 				continue;
-			ModbusCRC = USART2_RX_BUF002[data_lengthU2-1]<<8;
-			ModbusCRC |= USART2_RX_BUF002[data_lengthU2-2];
-			ModbusCRC1 = LIB_CRC_MODBUS(USART2_RX_BUF002,data_lengthU2-2);
-			
-			if((USART2_RX_BUF002[0] != 0x39) && (USART2_RX_BUF002[1] <= 0x01)&& (USART2_RX_BUF002[2] <= 0x95)&& (USART2_RX_BUF002[3] <= 0x50))   //判断帧头
-			{
-				pkzq->KZQ_Error++;
-				KZQ_RTerror = 1;
 			}
-			else if(USART2_RX_BUF002[5]!=0x01)            					  //校验地址
+		}
+		// KZQ_RTerror = 0;
+		// ASC转换为16进制,收到数据为:3901开头的数据总长度131字节
+		if ((USART2_RX_BUF002[0] == 0x3A) && (USART2_RX_BUF002[1] == 0x33) && (USART2_RX_BUF002[2] == 0x39) && (USART2_RX_BUF002[3] == 0x30)) //判断帧头
+		{
+			if (data_lengthU2 != 131)
 			{
-				pkzq->KZQ_Error++;
-				KZQ_RTerror = 1;
+				++KZQ_RTerror;
+				continue;
 			}
-			else if((USART2_RX_BUF002[7] != 0x03)&&(USART2_RX_BUF002[7] != 0x06))			//校验数据长度
+			for (i000 = 0; i000 < (data_lengthU2 - 3) / 2; i000++)
 			{
-				pkzq->KZQ_Error++;
-				KZQ_RTerror = 1;
+				T2C_RemoteCaliDat001.PayLoadData[i000] = MODBUS_ASCII_AsciiToHex(USART2_RX_BUF002 + 1 + 0 + i000 * 2);
+
+				USART2_RX_BUF003[i000] = MODBUS_ASCII_AsciiToHex(USART2_RX_BUF002 + 1 + 0 + i000 * 2);
 			}
-			/*else if(ModbusCRC != ModbusCRC1)		//校验CRC
-			{	
-				pkzq->KZQ_Error++;
-				KZQ_RTerror = 1;
-			}*/
-			else
-			{
-				pkzq->KZQ_Error = 0;
-				KZQ_RTerror=0;
-				for(i = 0;i < 64;i++)
+			//解析后的数据拷贝过来
+			memcpy(USART2_RX_BUF002, USART2_RX_BUF003, (data_lengthU2 - 3) / 2);
+			data_lengthU2 = (data_lengthU2 - 3) / 2;
+		}
+		if (data_lengthU2 < 5)
+			continue;
+		ModbusCRC = USART2_RX_BUF002[data_lengthU2 - 1] << 8;
+		ModbusCRC |= USART2_RX_BUF002[data_lengthU2 - 2];
+		ModbusCRC1 = LIB_CRC_MODBUS(USART2_RX_BUF002, data_lengthU2 - 2);
+
+		if ((USART2_RX_BUF002[0] != 0x39) && (USART2_RX_BUF002[1] <= 0x01) && (USART2_RX_BUF002[2] <= 0x95) && (USART2_RX_BUF002[3] <= 0x50)) //判断帧头
+		{
+			pkzq->KZQ_Error++;
+			KZQ_RTerror = 1;
+		}
+		else if (USART2_RX_BUF002[5] != 0x01) //校验地址
+		{
+			pkzq->KZQ_Error++;
+			KZQ_RTerror = 1;
+		}
+		else if ((USART2_RX_BUF002[7] != 0x03) && (USART2_RX_BUF002[7] != 0x06)) //校验数据长度
+		{
+			pkzq->KZQ_Error++;
+			KZQ_RTerror = 1;
+		}
+		/*else if(ModbusCRC != ModbusCRC1)		//校验CRC
+		{
+			pkzq->KZQ_Error++;
+			KZQ_RTerror = 1;
+		}*/
+		else
+		{
+			pkzq->KZQ_Error = 0;
+			KZQ_RTerror = 0;
+			for (i = 0; i < 64; i++)
 				pkzq->data_buf[i] = USART2_RX_BUF002[i];
 				for(i = 0;i < 16;i++)
 				ptx[i] = USART2_RX_BUF002[i];
@@ -3133,7 +3154,7 @@ void StartTask04(void *argument)                                    //
 				}	
 			}
 			flagU2Rx = 0;
-		}
+	}
   }
   /* USER CODE END StartTask04 */
 }

+ 364 - 0
Core/Src/md5c.c

@@ -0,0 +1,364 @@
+/* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm 
+ */ 
+ 
+/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All 
+rights reserved. 
+ 
+License to copy and use this software is granted provided that it 
+is identified as the "RSA Data Security, Inc. MD5 Message-Digest 
+Algorithm" in all material mentioning or referencing this software 
+or this function. 
+ 
+License is also granted to make and use derivative works provided 
+that such works are identified as "derived from the RSA Data 
+Security, Inc. MD5 Message-Digest Algorithm" in all material 
+mentioning or referencing the derived work. 
+ 
+RSA Data Security, Inc. makes no representations concerning either 
+the merchantability of this software or the suitability of this 
+software for any particular purpose. It is provided "as is" 
+without express or implied warranty of any kind. 
+ 
+These notices must be retained in any copies of any part of this 
+documentation and/or software. 
+ */ 
+#include "md5c.h" 
+#include <string.h> 
+#include <stdio.h> 
+ 
+/* Constants for MD5Transform routine. 
+*/ 
+ 
+ 
+#define S11 7 
+#define S12 12 
+#define S13 17 
+#define S14 22 
+#define S21 5 
+#define S22 9 
+#define S23 14 
+#define S24 20 
+#define S31 4 
+#define S32 11 
+#define S33 16 
+#define S34 23 
+#define S41 6 
+#define S42 10 
+#define S43 15 
+#define S44 21 
+ 
+static void MD5_memcpy (POINTER output, POINTER input, unsigned int len); 
+static void MD5Transform (UINT4 state[4], unsigned char block[64]); 
+static void Encode (unsigned char *output, UINT4 *input, unsigned int len); 
+static void MD5_memset (POINTER output, int value, unsigned int len); 
+static void Decode (UINT4 *output, unsigned char *input, unsigned int len); 
+ 
+static unsigned char PADDING[64] = { 
+ 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 
+}; 
+ 
+/* F, G, H and I are basic MD5 functions. 
+*/ 
+#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) 
+#define G(x, y, z) (((x) & (z)) | ((y) & (~z))) 
+#define H(x, y, z) ((x) ^ (y) ^ (z)) 
+#define I(x, y, z) ((y) ^ ((x) | (~z))) 
+ 
+/* ROTATE_LEFT rotates x left n bits. 
+*/ 
+#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) 
+ 
+/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. 
+Rotation is separate from addition to prevent recomputation. 
+*/ 
+#define FF(a, b, c, d, x, s, ac) { \
+	(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+ } 
+#define GG(a, b, c, d, x, s, ac) { \
+ (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+ } 
+#define HH(a, b, c, d, x, s, ac) { \
+ (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+ } 
+#define II(a, b, c, d, x, s, ac) { \
+ (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+ } 
+ 
+/* MD5 initialization. Begins an MD5 operation, writing a new context. 
+ */ 
+void MD5Init (MD5_CTX *context)          /* context */ 
+{ 
+ context->count[0] = context->count[1] = 0; 
+ /* Load magic initialization constants. 
+ */ 
+ context->state[0] = 0x67452301; 
+ context->state[1] = 0xefcdab89; 
+ context->state[2] = 0x98badcfe; 
+ context->state[3] = 0x10325476; 
+} 
+ 
+/* MD5 block update operation. Continues an MD5 message-digest 
+ operation, processing another message block, and updating the 
+ context. 
+ */ 
+void MD5Update (MD5_CTX *context, unsigned char *input, unsigned int inputLen) 
+ 
+{ 
+ unsigned int i, index, partLen; 
+ 
+ /* Compute number of bytes mod 64 */ 
+ index = (unsigned int)((context->count[0] >> 3) & 0x3F); 
+ 
+ /* Update number of bits */ 
+ if ((context->count[0] += ((UINT4)inputLen << 3)) 
+  < ((UINT4)inputLen << 3)) 
+  context->count[1]++; 
+ context->count[1] += ((UINT4)inputLen >> 29); 
+ 
+ partLen = 64 - index; 
+ 
+ /* Transform as many times as possible. 
+ */ 
+ if (inputLen >= partLen) { 
+  MD5_memcpy((POINTER)&context->buffer[index], (POINTER)input, partLen); 
+  MD5Transform (context->state, context->buffer); 
+ 
+  for (i = partLen; i + 63 < inputLen; i += 64) 
+   MD5Transform (context->state, &input[i]); 
+ 
+  index = 0; 
+ } 
+ else 
+  i = 0; 
+ 
+ /* Buffer remaining input */ 
+ MD5_memcpy((POINTER)&context->buffer[index], (POINTER)&input[i],inputLen-i); 
+} 
+ 
+/* MD5 finalization. Ends an MD5 message-digest operation, writing the 
+ the message digest and zeroizing the context. 
+ */ 
+void MD5Final (unsigned char digest[16], MD5_CTX *context)         
+{ 
+ unsigned char bits[8]; 
+ unsigned int index, padLen; 
+ 
+ /* Save number of bits */ 
+ Encode (bits, context->count, 8); 
+ 
+ /* Pad out to 56 mod 64. 
+ */ 
+ index = (unsigned int)((context->count[0] >> 3) & 0x3f); 
+ padLen = (index < 56) ? (56 - index) : (120 - index); 
+ MD5Update (context, PADDING, padLen); 
+ 
+ /* Append length (before padding) */ 
+ MD5Update (context, bits, 8); 
+ 
+ /* Store state in digest */ 
+ Encode (digest, context->state, 16); 
+ 
+ /* Zeroize sensitive information. 
+ */ 
+ MD5_memset ((POINTER)context, 0, sizeof (*context)); 
+} 
+ 
+/* MD5 basic transformation. Transforms state based on block. 
+ */ 
+static void MD5Transform (UINT4 state[4], unsigned char block[64]) 
+{ 
+ UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; 
+ 
+ Decode (x, block, 64); 
+ 
+ /* Round 1 */ 
+ FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */ 
+ FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */ 
+ FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */ 
+ FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */ 
+ FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */ 
+ FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */ 
+ FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */ 
+ FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */ 
+ FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */ 
+ FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */ 
+ FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ 
+ FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ 
+ FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ 
+ FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ 
+ FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ 
+ FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ 
+ 
+ /* Round 2 */ 
+ GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */ 
+ GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */ 
+ GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ 
+ GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */ 
+ GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */ 
+ GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */ 
+ GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ 
+ GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */ 
+ GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */ 
+ GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ 
+ GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */ 
+ GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ 
+ GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ 
+ GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ 
+ GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ 
+ GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ 
+ 
+ /* Round 3 */ 
+ HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */ 
+ HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */ 
+ HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ 
+ HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ 
+ HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */ 
+ HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */ 
+ HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */ 
+ HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ 
+ HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ 
+ HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */ 
+ HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */ 
+ HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */ 
+ HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */ 
+ HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ 
+ HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ 
+ HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */ 
+ 
+ /* Round 4 */ 
+ II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */ 
+ II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */ 
+ II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ 
+ II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */ 
+ II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ 
+ II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */ 
+ II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ 
+ II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */ 
+ II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */ 
+ II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ 
+ II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */ 
+ II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ 
+ II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */ 
+ II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ 
+ II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */ 
+ II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ 
+ 
+ state[0] += a; 
+ state[1] += b; 
+ state[2] += c; 
+ state[3] += d; 
+ 
+ /* Zeroize sensitive information. 
+ */ 
+ MD5_memset ((POINTER)x, 0, sizeof (x)); 
+} 
+ 
+/* Encodes input (UINT4) into output (unsigned char). Assumes len is 
+ a multiple of 4. 
+ */ 
+static void Encode (unsigned char *output, UINT4 *input, unsigned int len) 
+{ 
+ unsigned int i, j; 
+ 
+ for (i = 0, j = 0; j < len; i++, j += 4) { 
+  output[j] = (unsigned char)(input[i] & 0xff); 
+  output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); 
+  output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); 
+  output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); 
+ } 
+} 
+ 
+/* Decodes input (unsigned char) into output (UINT4). Assumes len is 
+ a multiple of 4. 
+ */ 
+static void Decode (UINT4 *output, unsigned char *input, unsigned int len) 
+{ 
+ unsigned int i, j; 
+ 
+ for (i = 0, j = 0; j < len; i++, j += 4) 
+  output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | 
+  (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24); 
+} 
+ 
+/* Note: Replace "for loop" with standard memcpy if possible. 
+ */ 
+ 
+static void MD5_memcpy (POINTER output, POINTER input, unsigned int len) 
+{ 
+ unsigned int i; 
+ 
+ for (i = 0; i < len; i++) 
+  output[i] = input[i]; 
+} 
+ 
+/* Note: Replace "for loop" with standard memset if possible. 
+ */ 
+static void MD5_memset (POINTER output, int value, unsigned int len) 
+{ 
+ unsigned int i; 
+ 
+ for (i = 0; i < len; i++) 
+  ((char *)output)[i] = (char)value; 
+} 
+/* Digests a string and prints the result. 
+ */ 
+void MDString (char *string,unsigned char digest[16]) 
+{ 
+ MD5_CTX context; 
+ unsigned int len = strlen (string); 
+ 
+ MD5Init (&context); 
+ MD5Update (&context, (unsigned char *)string, len); 
+ MD5Final (digest, &context); 
+} 
+/* Digests a file and prints the result. 
+ */ 
+int MD5File (char *filename,unsigned char digest[16]) 
+{ 
+ FILE *file; 
+ MD5_CTX context; 
+ int len; 
+ unsigned char buffer[1024]; 
+ 
+ if ((file = fopen (filename, "rb")) == NULL) 
+  return -1; 
+ else { 
+  MD5Init (&context); 
+  while (len = fread (buffer, 1, 1024, file)) 
+   MD5Update (&context, buffer, len); 
+  MD5Final (digest, &context); 
+ 
+  fclose (file); 
+ } 
+ return 0; 
+} 
+void MD5UpdaterString(MD5_CTX *context,const char *string) 
+{ 
+ unsigned int len = strlen (string); 
+ MD5Update (context, (unsigned char *)string, len); 
+} 
+int MD5FileUpdateFile (MD5_CTX *context,char *filename) 
+{ 
+ FILE *file; 
+ int len; 
+ unsigned char buffer[1024]; 
+ 
+ if ((file = fopen (filename, "rb")) == NULL) 
+  return -1; 
+ else { 
+  while (len = fread (buffer, 1, 1024, file)) 
+   MD5Update (context, buffer, len); 
+  fclose (file); 
+ } 
+ return 0; 
+} 

+ 128 - 0
Core/Src/stmflash.c

@@ -0,0 +1,128 @@
+#include "stmflash.h"
+#include "usart.h"
+//////////////////////////////////////////////////////////////////////////////////	 
+//////////////////////////////////////////////////////////////////////////////////
+ 
+//读取指定地址的半字(16位数据) 
+//faddr:读地址 
+//返回值:对应数据.
+uint16_t STMFLASH_ReadHalfWord(uint32_t faddr)
+{
+	return *(uint16_t*)faddr; 
+}
+#if STM32_FLASH_WREN	//如果使能了写   
+//不检查的写入
+//WriteAddr:起始地址
+//pBuffer:数据指针
+//NumToWrite:半字(16位)数   
+void STMFLASH_Write_NoCheck(uint32_t WriteAddr,uint16_t *pBuffer,uint16_t NumToWrite)   
+{ 			 		 
+	uint16_t i;
+	for(i=0;i<NumToWrite;i++)
+	{
+		HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,WriteAddr,pBuffer[i]);
+	  WriteAddr+=2;//地址增加2.
+	}  
+} 
+//从指定地址开始写入指定长度的数据
+//WriteAddr:起始地址(此地址必须为2的倍数!!)
+//pBuffer:数据指针
+//NumToWrite:半字(16位)数(就是要写入的16位数据的个数.)
+#if STM32_FLASH_SIZE<256
+#define STM_SECTOR_SIZE 1024 //字节
+#else 
+#define STM_SECTOR_SIZE	2048
+#endif		 
+uint16_t STMFLASH_BUF[STM_SECTOR_SIZE/2];//最多是2K字节
+void STMFLASH_Write(uint32_t WriteAddr,uint16_t *pBuffer,uint16_t NumToWrite)	
+{
+	uint32_t secpos;	   //扇区地址
+	uint16_t secoff;	   //扇区内偏移地址(16位字计算)
+	uint16_t secremain; //扇区内剩余地址(16位字计算)	   
+ 	uint16_t i;    
+	uint32_t offaddr;   //去掉0X08000000后的地址
+	
+	if(WriteAddr<STM32_FLASH_BASE||(WriteAddr>=(STM32_FLASH_BASE+1024*STM32_FLASH_SIZE)))return;//非法地址
+	
+	HAL_FLASH_Unlock();					//解锁
+	offaddr=WriteAddr-STM32_FLASH_BASE;		//实际偏移地址.
+	secpos=offaddr/STM_SECTOR_SIZE;			//扇区地址  0~127 for STM32F103RBT6
+	secoff=(offaddr%STM_SECTOR_SIZE)/2;		//在扇区内的偏移(2个字节为基本单位.)
+	secremain=STM_SECTOR_SIZE/2-secoff;		//扇区剩余空间大小   
+	if(NumToWrite<=secremain)secremain=NumToWrite;//不大于该扇区范围
+	while(1) 
+	{	
+		STMFLASH_Read(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE,STMFLASH_BUF,STM_SECTOR_SIZE/2);//读出整个扇区的内容
+		for(i=0;i<secremain;i++)	//校验数据
+		{
+			if(STMFLASH_BUF[secoff+i]!=0XFFFF)break;//需要擦除  	  
+		}
+		if(i<secremain)				//需要擦除
+		{
+			FLASH_PageErase(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE);	//擦除这个扇区
+			FLASH_WaitForLastOperation(FLASH_WAITETIME);            	//等待上次操作完成
+			CLEAR_BIT(FLASH->CR, FLASH_CR_PER);							//清除CR寄存器的PER位,此操作应该在FLASH_PageErase()中完成!
+																		//但是HAL库里面并没有做,应该是HAL库bug!
+			for(i=0;i<secremain;i++)//复制
+			{
+				STMFLASH_BUF[i+secoff]=pBuffer[i];	  
+			}
+			STMFLASH_Write_NoCheck(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE,STMFLASH_BUF,STM_SECTOR_SIZE/2);//写入整个扇区  
+		}else 
+		{
+			FLASH_WaitForLastOperation(FLASH_WAITETIME);       	//等待上次操作完成
+			STMFLASH_Write_NoCheck(WriteAddr,pBuffer,secremain);//写已经擦除了的,直接写入扇区剩余区间. 
+		}
+		if(NumToWrite==secremain)break;//写入结束了
+		else//写入未结束
+		{
+			secpos++;				//扇区地址增1
+			secoff=0;				//偏移位置为0 	 
+		   	pBuffer+=secremain;  	//指针偏移
+			WriteAddr+=secremain*2;	//写地址偏移(16位数据地址,需要*2)	   
+		   	NumToWrite-=secremain;	//字节(16位)数递减
+			if(NumToWrite>(STM_SECTOR_SIZE/2))secremain=STM_SECTOR_SIZE/2;//下一个扇区还是写不完
+			else secremain=NumToWrite;//下一个扇区可以写完了
+		}	 
+	};	
+	HAL_FLASH_Lock();		//上锁
+}
+#endif
+
+//从指定地址开始读出指定长度的数据
+//ReadAddr:起始地址
+//pBuffer:数据指针
+//NumToWrite:半字(16位)数
+void STMFLASH_Read(uint32_t ReadAddr,uint16_t *pBuffer,uint16_t NumToRead)   	
+{
+	uint16_t i;
+	for(i=0;i<NumToRead;i++)
+	{
+		pBuffer[i]=STMFLASH_ReadHalfWord(ReadAddr);//读取2个字节.
+		ReadAddr+=2;//偏移2个字节.	
+	}
+}
+
+//////////////////////////////////////////测试用///////////////////////////////////////////
+//WriteAddr:起始地址
+//WriteData:要写入的数据
+void Test_Write(uint32_t WriteAddr,uint16_t WriteData)   	
+{
+	STMFLASH_Write(WriteAddr,&WriteData,1);//写入一个字 
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+ 93 - 199
MDK-ARM/caijiqiV001.uvoptx

@@ -135,7 +135,7 @@
         <SetRegEntry>
           <Number>0</Number>
           <Key>DLGTARM</Key>
-          <Name>(1010=-1,-1,-1,-1,0)(1007=-1,-1,-1,-1,0)(1008=-1,-1,-1,-1,0)(1009=-1,-1,-1,-1,0)</Name>
+          <Name>(1010=778,129,1228,686,0)(1007=-1,-1,-1,-1,0)(1008=-1,-1,-1,-1,0)(1009=-1,-1,-1,-1,0)</Name>
         </SetRegEntry>
         <SetRegEntry>
           <Number>0</Number>
@@ -155,181 +155,33 @@
         <SetRegEntry>
           <Number>0</Number>
           <Key>ST-LINKIII-KEIL_SWO</Key>
-          <Name>-U32FF6F064247363333581557 -O8399 -SF1800 -C0 -A0 -I0 -HNlocalhost -HP7184 -P1 -N00("ARM CoreSight SW-DP") -D00(1BA01477) -L00(0) -TO131090 -TC10000000 -TT10000000 -TP21 -TDS8004 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO31 -FD20000000 -FC800 -FN1 -FF0STM32F10x_512.FLM -FS08000000 -FL080000 -FP0($$Device:STM32F103RE$Flash\STM32F10x_512.FLM)</Name>
+          <Name>-U36FF6E064E55373453232143 -O8399 -SF1800 -C0 -A0 -I0 -HNlocalhost -HP7184 -P1 -N00("ARM CoreSight SW-DP") -D00(1BA01477) -L00(0) -TO131090 -TC10000000 -TT10000000 -TP21 -TDS8004 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO15 -FD20000000 -FC800 -FN1 -FF0STM32F10x_512.FLM -FS08000000 -FL080000 -FP0($$Device:STM32F103RE$Flash\STM32F10x_512.FLM)</Name>
         </SetRegEntry>
       </TargetDriverDllRegistry>
-      <Breakpoint/>
+      <Breakpoint>
+        <Bp>
+          <Number>0</Number>
+          <Type>0</Type>
+          <LineNumber>828</LineNumber>
+          <EnabledFlag>1</EnabledFlag>
+          <Address>134276368</Address>
+          <ByteObject>0</ByteObject>
+          <HtxType>0</HtxType>
+          <ManyObjects>0</ManyObjects>
+          <SizeOfObject>0</SizeOfObject>
+          <BreakByAccess>0</BreakByAccess>
+          <BreakIfRCount>1</BreakIfRCount>
+          <Filename>D:\zhuzl鐢佃剳澶囦唤\zhuzl_work\Collect\Collect\Core\Src\main.c</Filename>
+          <ExecCommand></ExecCommand>
+          <Expression>\\DZQF_CJB09A0_20220209_Temp1\../Core/Src/main.c\828</Expression>
+        </Bp>
+      </Breakpoint>
       <WatchWindow1>
         <Ww>
           <count>0</count>
           <WinNumber>1</WinNumber>
-          <ItemText>xyf_inf</ItemText>
-        </Ww>
-        <Ww>
-          <count>1</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>rkg_inf</ItemText>
-        </Ww>
-        <Ww>
-          <count>2</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>gs_AngleData</ItemText>
-        </Ww>
-        <Ww>
-          <count>3</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>cang_inf</ItemText>
-        </Ww>
-        <Ww>
-          <count>4</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>xyf_inf</ItemText>
-        </Ww>
-        <Ww>
-          <count>5</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>USART1_RX_BUF002</ItemText>
-        </Ww>
-        <Ww>
-          <count>6</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>USART3_RX_BUF002</ItemText>
-        </Ww>
-        <Ww>
-          <count>7</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>hdf_inf</ItemText>
-        </Ww>
-        <Ww>
-          <count>8</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>Flash_buff</ItemText>
-        </Ww>
-        <Ww>
-          <count>9</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>psAD</ItemText>
-        </Ww>
-        <Ww>
-          <count>10</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>gs_AngleData</ItemText>
-        </Ww>
-        <Ww>
-          <count>11</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>kzq_inf</ItemText>
-        </Ww>
-        <Ww>
-          <count>12</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>hdf_inf</ItemText>
-        </Ww>
-        <Ww>
-          <count>13</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>CMD_XYF</ItemText>
-        </Ww>
-        <Ww>
-          <count>14</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>level_inf</ItemText>
-        </Ww>
-        <Ww>
-          <count>15</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>Volume_1cang</ItemText>
-        </Ww>
-        <Ww>
-          <count>16</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>\\caijiqiV001\../Core/Src/level.c\Volume_1cang[0]</ItemText>
-        </Ww>
-        <Ww>
-          <count>17</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>Volume_2cang</ItemText>
-        </Ww>
-        <Ww>
-          <count>18</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>ModbusCRC</ItemText>
-        </Ww>
-        <Ww>
-          <count>19</count>
-          <WinNumber>1</WinNumber>
           <ItemText>USART2_RX_BUF002</ItemText>
         </Ww>
-        <Ww>
-          <count>20</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>CMD_KZQ</ItemText>
-        </Ww>
-        <Ww>
-          <count>21</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>angle_inf</ItemText>
-        </Ww>
-        <Ww>
-          <count>22</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>cang_inf</ItemText>
-        </Ww>
-        <Ww>
-          <count>23</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>tem_inf</ItemText>
-        </Ww>
-        <Ww>
-          <count>24</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>hex_to_float</ItemText>
-        </Ww>
-        <Ww>
-          <count>25</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>hex_to_float</ItemText>
-        </Ww>
-        <Ww>
-          <count>26</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>ptxCang01Temp</ItemText>
-        </Ww>
-        <Ww>
-          <count>27</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>hex_to_float2</ItemText>
-        </Ww>
-        <Ww>
-          <count>28</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>hex_to_float1</ItemText>
-        </Ww>
-        <Ww>
-          <count>29</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>prkg</ItemText>
-        </Ww>
-        <Ww>
-          <count>30</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>usage_Tsk12</ItemText>
-        </Ww>
-        <Ww>
-          <count>31</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>cang_inf</ItemText>
-        </Ww>
-        <Ww>
-          <count>32</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>LIB_CRC_MODBUS_HI</ItemText>
-        </Ww>
-        <Ww>
-          <count>33</count>
-          <WinNumber>1</WinNumber>
-          <ItemText>H_1cang</ItemText>
-        </Ww>
       </WatchWindow1>
       <WatchWindow2>
         <Ww>
@@ -387,7 +239,7 @@
         <Mm>
           <WinNumber>1</WinNumber>
           <SubType>0</SubType>
-          <ItemText>0x08010a40</ItemText>
+          <ItemText>0x08040000</ItemText>
           <AccSizeX>0</AccSizeX>
         </Mm>
       </MemoryWindow1>
@@ -405,7 +257,7 @@
       <DebugFlag>
         <trace>0</trace>
         <periodic>1</periodic>
-        <aLwin>1</aLwin>
+        <aLwin>0</aLwin>
         <aCover>0</aCover>
         <aSer1>0</aSer1>
         <aSer2>0</aSer2>
@@ -441,6 +293,12 @@
       <pszMrulep></pszMrulep>
       <pSingCmdsp></pSingCmdsp>
       <pMultCmdsp></pMultCmdsp>
+      <SystemViewers>
+        <Entry>
+          <Name>System Viewer\USART2</Name>
+          <WinId>35905</WinId>
+        </Entry>
+      </SystemViewers>
       <DebugDescription>
         <Enable>0</Enable>
         <EnableFlashSeq>0</EnableFlashSeq>
@@ -493,7 +351,7 @@
       <GroupNumber>2</GroupNumber>
       <FileNumber>3</FileNumber>
       <FileType>1</FileType>
-      <tvExp>0</tvExp>
+      <tvExp>1</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
       <PathWithFileName>../Core/Src/main.c</PathWithFileName>
@@ -669,6 +527,42 @@
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
+    <File>
+      <GroupNumber>2</GroupNumber>
+      <FileNumber>18</FileNumber>
+      <FileType>1</FileType>
+      <tvExp>0</tvExp>
+      <tvExpOptDlg>0</tvExpOptDlg>
+      <bDave2>0</bDave2>
+      <PathWithFileName>..\Core\Src\iap.c</PathWithFileName>
+      <FilenameWithoutPath>iap.c</FilenameWithoutPath>
+      <RteFlg>0</RteFlg>
+      <bShared>0</bShared>
+    </File>
+    <File>
+      <GroupNumber>2</GroupNumber>
+      <FileNumber>19</FileNumber>
+      <FileType>1</FileType>
+      <tvExp>0</tvExp>
+      <tvExpOptDlg>0</tvExpOptDlg>
+      <bDave2>0</bDave2>
+      <PathWithFileName>..\Core\Src\stmflash.c</PathWithFileName>
+      <FilenameWithoutPath>stmflash.c</FilenameWithoutPath>
+      <RteFlg>0</RteFlg>
+      <bShared>0</bShared>
+    </File>
+    <File>
+      <GroupNumber>2</GroupNumber>
+      <FileNumber>20</FileNumber>
+      <FileType>1</FileType>
+      <tvExp>0</tvExp>
+      <tvExpOptDlg>0</tvExpOptDlg>
+      <bDave2>0</bDave2>
+      <PathWithFileName>..\Core\Src\md5c.c</PathWithFileName>
+      <FilenameWithoutPath>md5c.c</FilenameWithoutPath>
+      <RteFlg>0</RteFlg>
+      <bShared>0</bShared>
+    </File>
   </Group>
 
   <Group>
@@ -679,7 +573,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>18</FileNumber>
+      <FileNumber>21</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -699,7 +593,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>4</GroupNumber>
-      <FileNumber>19</FileNumber>
+      <FileNumber>22</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -711,7 +605,7 @@
     </File>
     <File>
       <GroupNumber>4</GroupNumber>
-      <FileNumber>20</FileNumber>
+      <FileNumber>23</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -723,7 +617,7 @@
     </File>
     <File>
       <GroupNumber>4</GroupNumber>
-      <FileNumber>21</FileNumber>
+      <FileNumber>24</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -735,7 +629,7 @@
     </File>
     <File>
       <GroupNumber>4</GroupNumber>
-      <FileNumber>22</FileNumber>
+      <FileNumber>25</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -747,7 +641,7 @@
     </File>
     <File>
       <GroupNumber>4</GroupNumber>
-      <FileNumber>23</FileNumber>
+      <FileNumber>26</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -759,7 +653,7 @@
     </File>
     <File>
       <GroupNumber>4</GroupNumber>
-      <FileNumber>24</FileNumber>
+      <FileNumber>27</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -771,7 +665,7 @@
     </File>
     <File>
       <GroupNumber>4</GroupNumber>
-      <FileNumber>25</FileNumber>
+      <FileNumber>28</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -783,7 +677,7 @@
     </File>
     <File>
       <GroupNumber>4</GroupNumber>
-      <FileNumber>26</FileNumber>
+      <FileNumber>29</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -795,7 +689,7 @@
     </File>
     <File>
       <GroupNumber>4</GroupNumber>
-      <FileNumber>27</FileNumber>
+      <FileNumber>30</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -807,7 +701,7 @@
     </File>
     <File>
       <GroupNumber>4</GroupNumber>
-      <FileNumber>28</FileNumber>
+      <FileNumber>31</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -819,7 +713,7 @@
     </File>
     <File>
       <GroupNumber>4</GroupNumber>
-      <FileNumber>29</FileNumber>
+      <FileNumber>32</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -831,7 +725,7 @@
     </File>
     <File>
       <GroupNumber>4</GroupNumber>
-      <FileNumber>30</FileNumber>
+      <FileNumber>33</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -843,7 +737,7 @@
     </File>
     <File>
       <GroupNumber>4</GroupNumber>
-      <FileNumber>31</FileNumber>
+      <FileNumber>34</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -855,7 +749,7 @@
     </File>
     <File>
       <GroupNumber>4</GroupNumber>
-      <FileNumber>32</FileNumber>
+      <FileNumber>35</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -867,7 +761,7 @@
     </File>
     <File>
       <GroupNumber>4</GroupNumber>
-      <FileNumber>33</FileNumber>
+      <FileNumber>36</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -879,7 +773,7 @@
     </File>
     <File>
       <GroupNumber>4</GroupNumber>
-      <FileNumber>34</FileNumber>
+      <FileNumber>37</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -891,7 +785,7 @@
     </File>
     <File>
       <GroupNumber>4</GroupNumber>
-      <FileNumber>35</FileNumber>
+      <FileNumber>38</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -911,7 +805,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>5</GroupNumber>
-      <FileNumber>36</FileNumber>
+      <FileNumber>39</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -923,7 +817,7 @@
     </File>
     <File>
       <GroupNumber>5</GroupNumber>
-      <FileNumber>37</FileNumber>
+      <FileNumber>40</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -935,7 +829,7 @@
     </File>
     <File>
       <GroupNumber>5</GroupNumber>
-      <FileNumber>38</FileNumber>
+      <FileNumber>41</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -947,7 +841,7 @@
     </File>
     <File>
       <GroupNumber>5</GroupNumber>
-      <FileNumber>39</FileNumber>
+      <FileNumber>42</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -959,7 +853,7 @@
     </File>
     <File>
       <GroupNumber>5</GroupNumber>
-      <FileNumber>40</FileNumber>
+      <FileNumber>43</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -971,7 +865,7 @@
     </File>
     <File>
       <GroupNumber>5</GroupNumber>
-      <FileNumber>41</FileNumber>
+      <FileNumber>44</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -983,7 +877,7 @@
     </File>
     <File>
       <GroupNumber>5</GroupNumber>
-      <FileNumber>42</FileNumber>
+      <FileNumber>45</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -995,7 +889,7 @@
     </File>
     <File>
       <GroupNumber>5</GroupNumber>
-      <FileNumber>43</FileNumber>
+      <FileNumber>46</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1007,7 +901,7 @@
     </File>
     <File>
       <GroupNumber>5</GroupNumber>
-      <FileNumber>44</FileNumber>
+      <FileNumber>47</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -1019,7 +913,7 @@
     </File>
     <File>
       <GroupNumber>5</GroupNumber>
-      <FileNumber>45</FileNumber>
+      <FileNumber>48</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>

+ 20 - 5
MDK-ARM/caijiqiV001.uvprojx

@@ -80,9 +80,9 @@
             <nStopB2X>0</nStopB2X>
           </BeforeMake>
           <AfterMake>
-            <RunUserProg1>0</RunUserProg1>
+            <RunUserProg1>1</RunUserProg1>
             <RunUserProg2>0</RunUserProg2>
-            <UserProg1Name></UserProg1Name>
+            <UserProg1Name>C:\Keil_v5\ARM\ARMCC\bin\fromelf.exe  --bin -o  ..\MDK-ARM\caijiqiV001\DZQF_CJB04_A0_APP888.bin ..\MDK-ARM\caijiqiV001\DZQF-CJB09A0_20220209_Temp1.axf</UserProg1Name>
             <UserProg2Name></UserProg2Name>
             <UserProg1Dos16Mode>0</UserProg1Dos16Mode>
             <UserProg2Dos16Mode>0</UserProg2Dos16Mode>
@@ -275,8 +275,8 @@
               </OCR_RVCT3>
               <OCR_RVCT4>
                 <Type>1</Type>
-                <StartAddress>0x8000000</StartAddress>
-                <Size>0x80000</Size>
+                <StartAddress>0x8020000</StartAddress>
+                <Size>0x20000</Size>
               </OCR_RVCT4>
               <OCR_RVCT5>
                 <Type>1</Type>
@@ -313,7 +313,7 @@
           </ArmAdsMisc>
           <Cads>
             <interw>1</interw>
-            <Optim>1</Optim>
+            <Optim>4</Optim>
             <oTime>0</oTime>
             <SplitLS>0</SplitLS>
             <OneElfS>1</OneElfS>
@@ -473,6 +473,21 @@
               <FileType>1</FileType>
               <FilePath>..\Core\Src\yqhs.c</FilePath>
             </File>
+            <File>
+              <FileName>iap.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\Core\Src\iap.c</FilePath>
+            </File>
+            <File>
+              <FileName>stmflash.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\Core\Src\stmflash.c</FilePath>
+            </File>
+            <File>
+              <FileName>md5c.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\Core\Src\md5c.c</FilePath>
+            </File>
           </Files>
         </Group>
         <Group>

File diff suppressed because it is too large
+ 3322 - 4346
MDK-ARM/caijiqiV001/DZQF-CJB09A0_20220209_Temp1.hex