|
|
@@ -33,8 +33,8 @@
|
|
|
#include "sys_config.h"
|
|
|
#include <easyflash.h>
|
|
|
|
|
|
-#define BL_VERSION "1.1.4"
|
|
|
-#define COMPILE "20230901"
|
|
|
+#define BL_VERSION RT_FOTA_SW_VERSION
|
|
|
+#define COMPILE "20261013"
|
|
|
|
|
|
#define RTU_DTU GET_PIN(B,13)
|
|
|
#define Beep GET_PIN(B, 4)
|
|
|
@@ -157,84 +157,53 @@ void HMI_WRITE(char *fmt, ...)
|
|
|
}
|
|
|
static void HMI_Decode(char *cmd,uint8_t cmd_len)
|
|
|
{
|
|
|
- //擦除APP分区,将df_area分区固件搬运至app分区
|
|
|
+ //擦除APP分区并从df_area备份恢复固件(v1.3.0:校验 magic/CRC 后恢复,保留备份)
|
|
|
if(strstr(rx_buffer,"resetapp")!=NULL)
|
|
|
{
|
|
|
rt_kprintf("固件恢复!!!\n");
|
|
|
HMI_WRITE("固件恢复!!!");
|
|
|
- const char *dst_part_name = "app";
|
|
|
- const char *src_part_name = "df_area";
|
|
|
- const struct fal_partition *dst_part;
|
|
|
- const struct fal_partition *src_part;
|
|
|
- dst_part = fal_partition_find(dst_part_name);
|
|
|
- src_part = fal_partition_find(src_part_name);
|
|
|
- if (dst_part == RT_NULL || src_part == RT_NULL)
|
|
|
+ if (rt_fota_restore_app_from_df(0) != RT_FOTA_NO_ERR)
|
|
|
{
|
|
|
- if (dst_part == RT_NULL)
|
|
|
- rt_kprintf("%s partition is not exist!\n", dst_part_name);
|
|
|
- if (src_part == RT_NULL)
|
|
|
- rt_kprintf("%s partition is not exist!\n", src_part_name);
|
|
|
+ rt_kprintf("resetapp: no valid backup or restore failed!\n");
|
|
|
+ HMI_WRITE("恢复失败:无有效备份!");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name);
|
|
|
- HMI_WRITE("Clone %s partition to %s partition:", src_part_name, dst_part_name);
|
|
|
- if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0)
|
|
|
- {
|
|
|
- int clone_pos = 0;
|
|
|
- int clone_len = 0, clone_tol_len;
|
|
|
- static int clone_num;
|
|
|
- rt_uint8_t *buf = rt_malloc(4096);
|
|
|
-
|
|
|
- if (dst_part->len < src_part->len)
|
|
|
- clone_tol_len = dst_part->len;
|
|
|
- else
|
|
|
- clone_tol_len = src_part->len;
|
|
|
-
|
|
|
- while ((clone_pos < clone_tol_len) && (buf != RT_NULL))
|
|
|
- {
|
|
|
- clone_len = fal_partition_read(src_part, clone_pos, buf, 4096);
|
|
|
- if (clone_len < 0)
|
|
|
- {
|
|
|
- rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name);
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0)
|
|
|
- {
|
|
|
- rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name);
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- clone_pos += clone_len;
|
|
|
- clone_num++;
|
|
|
- HMI_WRITE("debug.update.val=%d",clone_num);
|
|
|
- if(clone_num==99) clone_num = 0;
|
|
|
- }
|
|
|
-
|
|
|
- if (clone_pos >= clone_tol_len)
|
|
|
- {
|
|
|
- HMI_WRITE("Clone partition success, total %d bytes!", clone_tol_len);
|
|
|
- rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len);
|
|
|
- HMI_WRITE("debug.update.val=100");
|
|
|
- HMI_WRITE("debug.update.val=100");
|
|
|
- HMI_WRITE("debug.update.val=100");
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- HMI_WRITE("Clone partition failed!");
|
|
|
- rt_kprintf("Clone partition failed!");
|
|
|
- }
|
|
|
- if (buf)
|
|
|
- rt_free(buf);
|
|
|
- }
|
|
|
+ rt_kprintf("resetapp done.\n");
|
|
|
+ HMI_WRITE("固件恢复完成!");
|
|
|
}
|
|
|
}
|
|
|
- //备份app分区至df_area分区
|
|
|
+ //备份app分区至df_area分区(v1.3.0)
|
|
|
else if(strstr(rx_buffer, "backupapp")!=NULL)
|
|
|
{
|
|
|
-
|
|
|
+ rt_kprintf("backupapp: backup app partition to df_area...\n");
|
|
|
+ HMI_WRITE("备份固件...");
|
|
|
+ if (rt_fota_backup_app_to_df() != RT_FOTA_NO_ERR)
|
|
|
+ {
|
|
|
+ rt_kprintf("backupapp failed!\n");
|
|
|
+ HMI_WRITE("备份失败!");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ rt_kprintf("backupapp done.\n");
|
|
|
+ HMI_WRITE("固件备份完成!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //从df_area备份恢复app分区(v1.3.0 调试命令,保留备份方便重复执行)
|
|
|
+ else if(strstr(rx_buffer, "dbg_restore")!=NULL)
|
|
|
+ {
|
|
|
+ rt_kprintf("dbg_restore: restore app partition from df_area backup...\n");
|
|
|
+ HMI_WRITE("恢复固件...");
|
|
|
+ if (rt_fota_restore_app_from_df(0) != RT_FOTA_NO_ERR)
|
|
|
+ {
|
|
|
+ rt_kprintf("dbg_restore: no valid backup or restore failed!\n");
|
|
|
+ HMI_WRITE("恢复失败:无有效备份!");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ rt_kprintf("dbg_restore done.\n");
|
|
|
+ HMI_WRITE("固件恢复完成!");
|
|
|
+ }
|
|
|
}
|
|
|
//启动程序
|
|
|
else if(strstr(rx_buffer, "startup")!=NULL)
|
|
|
@@ -253,11 +222,20 @@ static void HMI_Decode(char *cmd,uint8_t cmd_len)
|
|
|
}
|
|
|
//擦除app分区
|
|
|
else if (strstr(rx_buffer, "eraseapp")!=NULL) {
|
|
|
- struct fal_partition fal_erase;
|
|
|
- fal_erase.offset=204800;
|
|
|
- rt_sprintf(fal_erase.name, "%s","app");
|
|
|
- rt_sprintf(fal_erase.flash_name, "%s","onchip_flash");
|
|
|
- fal_partition_erase(&fal_erase,0,319488);
|
|
|
+ const struct fal_partition *fal_erase;
|
|
|
+ fal_erase = fal_partition_find("app");
|
|
|
+ if (fal_erase != RT_NULL)
|
|
|
+ {
|
|
|
+ rt_kprintf("eraseapp: erase %s partition(%d bytes)...\n", fal_erase->name, fal_erase->len);
|
|
|
+ if (fal_partition_erase(fal_erase, 0, fal_erase->len) < 0)
|
|
|
+ rt_kprintf("eraseapp failed!\n");
|
|
|
+ else
|
|
|
+ rt_kprintf("eraseapp done.\n");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ rt_kprintf("eraseapp: 'app' partition is not exist!\n");
|
|
|
+ }
|
|
|
}
|
|
|
//擦除easyflash
|
|
|
else if(strstr(rx_buffer, "eraseeasyflash")!=NULL){
|
|
|
@@ -268,11 +246,20 @@ static void HMI_Decode(char *cmd,uint8_t cmd_len)
|
|
|
HMI_WRITE("easyflash擦除成功!");
|
|
|
}
|
|
|
else if(strstr(rx_buffer, "erasedf_area")!=NULL){
|
|
|
- struct fal_partition fal_erase;
|
|
|
- fal_erase.offset=15729152;
|
|
|
- rt_sprintf(fal_erase.name, "%s","df_area");
|
|
|
- rt_sprintf(fal_erase.flash_name, "%s","w25q16m");
|
|
|
- fal_partition_erase(&fal_erase,0,524288);
|
|
|
+ const struct fal_partition *fal_erase;
|
|
|
+ fal_erase = fal_partition_find("df_area");
|
|
|
+ if (fal_erase != RT_NULL)
|
|
|
+ {
|
|
|
+ rt_kprintf("erasedf_area: erase %s partition(%d bytes)...\n", fal_erase->name, fal_erase->len);
|
|
|
+ if (fal_partition_erase(fal_erase, 0, fal_erase->len) < 0)
|
|
|
+ rt_kprintf("erasedf_area failed!\n");
|
|
|
+ else
|
|
|
+ rt_kprintf("erasedf_area done.\n");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ rt_kprintf("erasedf_area: 'df_area' partition is not exist!\n");
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
/*HMI*/
|
|
|
@@ -325,7 +312,17 @@ static void Hmi_thread_entry(void *parameter)
|
|
|
result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), 1000);
|
|
|
if (result == RT_EOK)
|
|
|
{
|
|
|
+ /* 防止越界:msg.size 为环形缓冲区累计未读长度,可能超过 rx_buffer 容量 */
|
|
|
+ if (msg.size > sizeof(rx_buffer) - 1)
|
|
|
+ {
|
|
|
+ LOG_W("HMI rx data truncated: %d > %d", (int)msg.size, (int)(sizeof(rx_buffer) - 1));
|
|
|
+ msg.size = sizeof(rx_buffer) - 1;
|
|
|
+ }
|
|
|
rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
|
|
|
+ if (rx_length > sizeof(rx_buffer) - 1)
|
|
|
+ {
|
|
|
+ rx_length = sizeof(rx_buffer) - 1;
|
|
|
+ }
|
|
|
rx_buffer[rx_length] = '\0';
|
|
|
LOG_I("%s\n",rx_buffer);
|
|
|
HMI_Decode(rx_buffer,rx_length);
|
|
|
@@ -346,43 +343,6 @@ static void Hmi_thread_entry(void *parameter)
|
|
|
//rt_kprintf("workSta.vbat.txt=%c%d.%02d%c\n",hmi_str,voltage/100,voltage%100,hmi_str);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-/* 获取STM32F401的复位原因 */
|
|
|
-static void print_reset_reason(void)
|
|
|
-{
|
|
|
- uint32_t rst_status = RCC->CSR;
|
|
|
- bool is_iwdg = false;
|
|
|
-
|
|
|
- rt_kprintf("\n========== Reset Reason ==========\n");
|
|
|
-
|
|
|
- if (rst_status & RCC_CSR_LPWRRSTF)
|
|
|
- rt_kprintf("❌ Low Power reset\n");
|
|
|
-
|
|
|
- if (rst_status & RCC_CSR_WWDGRSTF)
|
|
|
- rt_kprintf("❌ Window Watchdog reset\n");
|
|
|
-
|
|
|
- if (rst_status & RCC_CSR_IWDGRSTF)
|
|
|
- {
|
|
|
- rt_kprintf("❌ Independent Watchdog reset\n");
|
|
|
- is_iwdg = true;
|
|
|
- }
|
|
|
-
|
|
|
- if (rst_status & RCC_CSR_SFTRSTF)
|
|
|
- rt_kprintf("✅ Software reset\n");
|
|
|
-
|
|
|
- if (rst_status & RCC_CSR_PORRSTF)
|
|
|
- rt_kprintf("✅ Power-on reset\n");
|
|
|
-
|
|
|
- if (rst_status & RCC_CSR_PINRSTF)
|
|
|
- rt_kprintf("✅ NRST pin reset\n");
|
|
|
-
|
|
|
- rt_kprintf("RCC_CSR: 0x%08X\n", rst_status);
|
|
|
- rt_kprintf("==================================\n");
|
|
|
-
|
|
|
- /* 清除复位标志(重要!)*/
|
|
|
- RCC->CSR |= RCC_CSR_RMVF;
|
|
|
-}
|
|
|
-
|
|
|
int main(void)
|
|
|
{
|
|
|
|
|
|
@@ -390,9 +350,8 @@ int main(void)
|
|
|
finsh_set_prompt("rt-fota />");
|
|
|
#endif
|
|
|
extern void rt_fota_init(void);
|
|
|
- rt_fota_print_log();
|
|
|
easyflash_init();
|
|
|
- print_reset_reason();
|
|
|
+ rt_fota_print_log();
|
|
|
rt_fota_init();
|
|
|
HMI_Thread();
|
|
|
Beep_Tip(100);
|