2 Commits 0e8eddc022 ... fd055e7b43

Auteur SHA1 Bericht Datum
  linsy fd055e7b43 初版 3 weken geleden
  linsy 0e8eddc022 初版 3 weken geleden

File diff suppressed because it is too large
+ 2 - 2
API_431CCT6.ioc


+ 0 - 1
Core/OBJ/FLASH.h

@@ -51,7 +51,6 @@
 #define SUCCESS               0
 #define ERROR                 1
 
-#define CURRENT_FIRMWARE_VERSION 0x0007B900 // 固件版本号
 /**************************************************************/
 
 /**************************************************************/

+ 26 - 23
Core/OBJ/iap_upgrade.c

@@ -56,7 +56,8 @@ void send_upgrade_start_response(uint8_t func_code, uint16_t reg_addr, uint8_t s
 //    DEBUG_PRINTF("\r\n");
 		
 //    HAL_GPIO_WritePin(GPIOB, RS485_RE_Pin, GPIO_PIN_SET);
-    HAL_UART_Transmit_DMA(&huart1, tx_buf, len);
+//    HAL_UART_Transmit_DMA(&huart1, tx_buf, len);
+		uart485send(tx_buf, len);
 		if(Systemmode != releasemode)
 		taskprinttx(tx_buf,len);
 }
@@ -96,7 +97,8 @@ void send_upgrade_data_response(uint8_t func_code, uint16_t reg_addr, uint8_t st
 //    DEBUG_PRINTF("\r\n");
 		
 //    HAL_GPIO_WritePin(GPIOB, RS485_RE_Pin, GPIO_PIN_SET);
-    HAL_UART_Transmit_DMA(&huart1, tx_buf, len);
+//    HAL_UART_Transmit_DMA(&huart1, tx_buf, len);
+		uart485send(tx_buf, len);
 		if(Systemmode != releasemode)
 		taskprinttx(tx_buf,len);
 }
@@ -136,7 +138,8 @@ void send_upgrade_end_response(uint8_t func_code, uint16_t reg_addr, uint8_t sta
 //    DEBUG_PRINTF("\r\n");
 		
 //    HAL_GPIO_WritePin(GPIOB, RS485_RE_Pin, GPIO_PIN_SET);
-    HAL_UART_Transmit_DMA(&huart1, tx_buf, len);
+//    HAL_UART_Transmit_DMA(&huart1, tx_buf, len);
+		uart485send(tx_buf, len);
 		if(Systemmode != releasemode)
 		taskprinttx(tx_buf,len);
 }
@@ -176,7 +179,7 @@ void handle_upgrade_start_frame(uint8_t *data)
             send_upgrade_start_response(0x42, 0xAABB, 0x02,0x0000); // 文件过大
             return;
         }
-//				erase_APPdata_buffer = true; // 擦除APP2缓存区
+				erase_APPdata_buffer = true; // 擦除APP2缓存区
 
         upgrade_offset = 0;
         current_packet_index = 0;
@@ -234,32 +237,25 @@ void handle_upgrade_data_frame(uint8_t *data)
     if ((data[4] << 8 | data[5]) != 0x4001) return;
     
 		data4001_last_data_time = HAL_GetTick();  // 每次收到数据更新时间戳
-		
     uint16_t index = data[6] << 8 | data[7];
     if (index != current_packet_index) {
         send_upgrade_data_response(0x42, 0xAABB, 0x04, 0x0000 );
         reset_upgrade_state();
         return;
     }
-
     Flash_Write64(IAPDataBuffer_ADDR + upgrade_offset, (uint64_t*)&data[8], PACKET_SIZE / 8);
-
     upgrade_offset += PACKET_SIZE;
     current_packet_index++;
-
     send_upgrade_data_response(0x42, 0xAABB, 0x00, current_packet_index);
-
 }
 
 // 处理结束帧 (0x4002)
 void handle_upgrade_end_frame(void)
 {
-		DEBUG_PRINTF("0x4002 start\r\n");
-
     uint8_t result_md5[16];
     MD5_CTX context;
 	
-	send_upgrade_end_response(0x42, 0xAABB, 0x00, 0x0000); // 应答表示收到结束帧
+		send_upgrade_end_response(0x42, 0xAABB, 0x00, 0x0000); // 应答表示收到结束帧
 		
 		bool md5_pass = false;
 		
@@ -319,10 +315,17 @@ void handle_upgrade_end_frame(void)
 		{
 			DEBUG_PRINTF(" MD5 check passed \r\n");       
 
-			JsRoot.iapLoadStatus = 300;				
+			JsRoot.iapLoadStatus = 300;					
 			DB_SaveUInt(DB_ID_SNAPSHOT, KV_KEY_IAP_LOAD , JsRoot.iapLoadStatus ); // 存入到SNAPSHOT数据库
 			
-			osDelay(300);
+			// 将每个字节转换为两位16进制字符
+			for(int i = 0; i < 4; i++) {
+					sprintf(&JsRoot.iapMd5[i*2], "%02X", result_md5[i]);
+			}
+			JsRoot.iapMd5[8] = '\0';
+			DB_SaveChar(DB_ID_SNAPSHOT, KV_KEY_IAP_MD5 , JsRoot.iapMd5 ); // 存入MD5到SNAPSHOT数据库
+			
+			osDelay(500);
 			NVIC_SystemReset();
 			
     } else {
@@ -333,7 +336,7 @@ void handle_upgrade_end_frame(void)
     }
 
     reset_upgrade_state(); // 清除相关升级状态标志
-		DEBUG_PRINTF("0x4002 end\r\n");
+// 4.09.06
 }
 // 清除升级状态(在失败或完成后调用)
 void reset_upgrade_state(void)
@@ -377,26 +380,26 @@ void app_jump_check_and_jump(void) // 初始化完成之后,检查状态是否
 		}
 
     if (JsRoot.iapLoadStatus == 2) {
-
+				printf ("\r\n valid APP2. Run APP2\r\n");
 				if (!JumpToApp(APP2_ADDR)) {
 
-						printf("\r\n Jump failed. Keep Run FACTORY" );
+						printf("\r\n Jump failed. Run APP1" );
         } 
 					
     } else if(JsRoot.iapLoadStatus == 0){
-      // 运行当前FACTORY程序
-			printf ("\r\n No valid APP2. Keep Run FACTORY\r\n");
+      // 运行当前 APP1 程序
+			printf ("\r\n No valid APP2. Run APP1\r\n");
     }
 }
 
 bool JumpToApp(uint32_t addr) 
 {
-		// FACTORY: sp = 0x200092F8 ; pc = 0x08014281;
-		// APP2: 		sp = 0x200092F8 ; pc = 0x08000281;
-    uint32_t sp = *(volatile uint32_t*)addr;// *(volatile uint32_t*)addr;// 0x200092F8;// 
+		// APP1: sp = 0x100037A8 ; pc = 0x08014281;
+		// APP2: sp = 0x100037A8 ; pc = 0x08000281;
+    uint32_t sp = *(volatile uint32_t*)addr;// *(volatile uint32_t*)addr;// 0x100037A8;// 
     uint32_t pc = *(volatile uint32_t*)(addr + 4);// *(volatile uint32_t*)(addr + 4);// 0x08014281;// 
 				
-		if( sp ==0x200092F8 && pc == 0x08014281) // 跳转到APP1区域
+		if( sp ==0x100037A8 && pc == 0x08014281) // 跳转到APP2区域
 		{
 			DEBUG_PRINTF("\r\n Jumping app_address. SP=0x%08X, PC=0x%08X \r\n", sp, pc);
 			DEBUG_PRINTF("\r\n Jumping app_address success");

+ 11 - 1
Core/OBJ/kv_flash.c

@@ -847,7 +847,10 @@ void DB_InitAllConfigs(void) { // 初始化数据库
         HAL_IWDG_Refresh(&hiwdg);  // 喂狗,防止初始化太久被复位
         osDelay(1);   
     }
-
+		
+		JsRoot.vers_id = CURRENT_FIRMWARE_VERSION;
+		DB_SaveUInt(DB_ID_SYSTEM, KV_KEY_VERSION , JsRoot.vers_id ); // 存入 VERSION 到 SYSTEM 数据库
+		
     printf("DB_InitAllConfigs complete.\n");
 
     osDelay(100); 
@@ -1021,6 +1024,13 @@ void sync_JsRoot_field(const char *key, const char *value) { // 查询更新某
 }
 
 
+void DB__Dump_IAP_LOAD(void){
+	int iapload =0;
+	pickup_int_from_key(2,KV_KEY_IAP_LOAD,&iapload);
+	JsRoot.iapLoadStatus = iapload;
+	printf("iapLoadStatus %d\r\n",JsRoot.iapLoadStatus);
+}
+
 /**@END --- 初始化函数 */
 
 /* USER CODE BEGIN EFP */

+ 1 - 1
Core/OBJ/kv_flash.h

@@ -200,7 +200,7 @@ uint8_t pickup_long_from_cache(uint8_t id, const char *key, long *val_out);
 void DB_Dump_to_JsRoot(void);
 void DB_SyncAllToJsRoot(void);
 void DB_Debug_Print_All(void);
-
+void DB__Dump_IAP_LOAD(void);
 #ifdef __cplusplus
 }
 #endif

+ 7 - 0
Core/OBJ/readme.txt

@@ -66,3 +66,10 @@ Modbus 06 和 10 改结构体数据后要同步到数据库。
 
 */
 
+/*2025/08/20版本
+修复IAP升级的一些小问题
+
+要做的事:
+硬件版本要在开始时强制写入
+*/
+

+ 6 - 4
Core/OBJ/uType.h

@@ -38,15 +38,17 @@ extern "C" {
 
 /* Exported types ------------------------------------------------------------*/
 /* USER CODE BEGIN ET */
-#define MAX_FRAME_SIZE 256  // 485接收回调一帧字节
-#define BUFFER_SIZE485 256  // 485串口接收缓冲区大小
-#define BUFFER_SIZEP 256  // 调试串口接收缓冲区大小
+#define MAX_FRAME_SIZE 270  // 485接收回调一帧字节
+#define BUFFER_SIZE485 270  // 485串口接收缓冲区大小
+#define BUFFER_SIZEP 270  // 调试串口接收缓冲区大小
 #define FILTER_BUFFER_SIZE 30  // 滤波缓冲区大小
 
-#define PRINT_BUF_SIZE 256	// 打印结构体的缓冲区
+#define PRINT_BUF_SIZE 270	// 打印结构体的缓冲区
 	
 #define FILTER_THRESHOLD 600  // 
 
+#define CURRENT_FIRMWARE_VERSION 0x0007C0EC // 固件版本号
+
 extern volatile uint8_t debug_print_enabled;
 extern volatile uint8_t fdc_print_enabled;
 extern volatile uint8_t ads_print_enabled;

+ 30 - 16
Core/Src/freertos.c

@@ -44,7 +44,8 @@ typedef StaticQueue_t osStaticMessageQDef_t;
 
 /* Private macro -------------------------------------------------------------*/
 /* USER CODE BEGIN PM */
-
+bool DB_init_flag = false; // 数据库初始化标志位
+	
 extern uint8_t dataReceive485[BUFFER_SIZE485];	// 485串口接收缓冲区
 extern uint8_t dataReceivep[BUFFER_SIZEP]; // 调试串口接收buffer
 extern uint32_t uartIRQ_rx_len ;	// 485串口接收数据长度
@@ -152,7 +153,7 @@ const osThreadAttr_t FilterTask_attributes = {
 };
 /* Definitions for uart485rxqueue */
 osMessageQueueId_t uart485rxqueueHandle;
-uint8_t uart485queueBuffer[ 32 * sizeof( Uart485Rx_msg ) ];
+uint8_t uart485queueBuffer[ 20 * sizeof( Uart485Rx_msg ) ];
 osStaticMessageQDef_t uart485queueControlBlock;
 const osMessageQueueAttr_t uart485rxqueue_attributes = {
   .name = "uart485rxqueue",
@@ -163,7 +164,7 @@ const osMessageQueueAttr_t uart485rxqueue_attributes = {
 };
 /* Definitions for printqueue */
 osMessageQueueId_t printqueueHandle;
-uint8_t printqueueBuffer[ 40 * sizeof( Print_msg ) ];
+uint8_t printqueueBuffer[ 32 * sizeof( Print_msg ) ];
 osStaticMessageQDef_t printqueueControlBlock;
 const osMessageQueueAttr_t printqueue_attributes = {
   .name = "printqueue",
@@ -272,8 +273,9 @@ void MX_FREERTOS_Init(void) {
 	Restart_UART1_DMA();
 	Restart_UART2_DMA();
 	JsRoot.work = &JsWork;
-	JsRoot.addr = 0xFF;
+//	JsRoot.addr = 0xFF;
 	printf ("\r\n API_MUL app1 2025_08_20 \r\n");
+	
   /* USER CODE END Init */
 
   /* USER CODE BEGIN RTOS_MUTEX */
@@ -290,10 +292,10 @@ void MX_FREERTOS_Init(void) {
 
   /* Create the queue(s) */
   /* creation of uart485rxqueue */
-  uart485rxqueueHandle = osMessageQueueNew (32, sizeof(Uart485Rx_msg), &uart485rxqueue_attributes);
+  uart485rxqueueHandle = osMessageQueueNew (20, sizeof(Uart485Rx_msg), &uart485rxqueue_attributes);
 
   /* creation of printqueue */
-  printqueueHandle = osMessageQueueNew (40, sizeof(Print_msg), &printqueue_attributes);
+  printqueueHandle = osMessageQueueNew (32, sizeof(Print_msg), &printqueue_attributes);
 
   /* creation of consolemsgqueue */
   consolemsgqueueHandle = osMessageQueueNew (32, sizeof(Console_msg), &consolemsgqueue_attributes);
@@ -511,7 +513,7 @@ void DealcallbackTask(void *argument)
 			
 			save_JsRoot_0100_flag = false;
 		}
-		if(JsRoot.iapLoadStatus == 300) // 已完成APP数据缓存区的下载,接下来开始复制到APP2区
+		if(JsRoot.iapLoadStatus == 300 && DB_init_flag == true) // 已完成APP数据缓存区的下载,接下来开始复制到APP2区
 		{
 						
 			Flash_ErasePages(APP2_ADDR, APP_SIZE / 4);  // 擦除整个 APP2 区域(80KB)擦除 40 页(2048字节/页)
@@ -530,14 +532,26 @@ void DealcallbackTask(void *argument)
 			uint8_t cal_app2_md5[16];
 			MD5_CTX context;
 			MD5Init(&context);
-			if( JsRoot.subcode == 0x0100 ){
-				MD5Update(&context, (uint8_t*)APP2_ADDR, JsRoot.iapSize - 16); // 0100 -16
-			}else if( JsRoot.subcode == 0x0200 ){
+			if( JsRoot.subcode == 0x0200 ){
 				MD5Update(&context, (uint8_t*)APP2_ADDR, JsRoot.iapSize); // 0200 -0
+			}else{
+				MD5Update(&context, (uint8_t*)APP2_ADDR, JsRoot.iapSize - 16); // 0100 -16
 			}
 			MD5Final(cal_app2_md5, &context); // 计算APP2区的MD5码
-			printf("\r\n Calculate Md5 Complete");
+			printf("\r\n Calculate Md5 Complete");		
 			
+			printf("APP2_md5_bytes : ");
+			for(int i = 0; i < 4; i++) {
+					printf("%02X ", APP2_md5_bytes[i]);
+			}
+			printf("\n");
+
+			printf("cal_app2_md5 : ");
+			for(int i = 0; i < 4; i++) {
+					printf("%02X ", cal_app2_md5[i]);
+			}
+			printf("\n");
+
 			// 比较md5结果
 			if (memcmp(cal_app2_md5, APP2_md5_bytes, 4) == 0) //验证复制后的APP2的MD5码和APP缓存区存入到数据库的MD5是否一致
 			{
@@ -550,6 +564,7 @@ void DealcallbackTask(void *argument)
 				osDelay(300);
 				NVIC_SystemReset();
 			}else{
+
 				printf("\r\nCopy IAPDataBuffer to APP2 Failed!!! ");
 				JsRoot.ubootback = 401; // 代表APP2区文件完整性校验错误
 				JsRoot.iapLoadStatus = 0; // 代表程序在APP1区运行
@@ -559,10 +574,10 @@ void DealcallbackTask(void *argument)
 			}
 		}
 		// 如果检测到APP2区文件完整性错误,再尝试3次将APP缓存区复制到APP2区
-		if(JsRoot.ubootback == 401)
-		{
-			// 复制flash
-		}
+//		if(JsRoot.ubootback == 401)
+//		{
+//			// 复制flash
+//		}
 		check_upgrade_data4001_timeout();
   }
   /* USER CODE END DealcallbackTask */
@@ -580,7 +595,6 @@ void CommonPrintTask(void *argument)
   /* USER CODE BEGIN CommonPrintTask */
   /* Infinite loop */
 	Print_msg Printmsg;
-	bool DB_init_flag = false;
 
   for(;;)
   {

+ 1 - 1
Core/Src/main.c

@@ -129,7 +129,7 @@ int main(void)
 	/* -- version data 2025_08_10 ---*/
 	HAL_TIM_Base_Start(&htim15);  // 启动定时器
 //	printf ("\r\n API_MUL main FACTORY start \r\n");
-	
+	DB__Dump_IAP_LOAD(); // 从数据库读取 iapLoadStatus IAP装载状态
 	app_jump_check_and_jump();
 	
   /* USER CODE END 2 */

File diff suppressed because it is too large
+ 47 - 38
MDK-ARM/API_431CCT6.uvguix.Administrator


BIN
OUTPUT/API_431CCT6.bin


File diff suppressed because it is too large
+ 3880 - 0
OUTPUT/API_431CCT6.hex