什么是OTA
百度百科解释 个人理解:就是不通过烧录的方式,通过串口、网口、无线对主板运行的程序进行升级。 减少后期的一个维护迭代程序的一个成本。
STM32的OTA升级模块的一个设计
程序启动的一个框架流程图(大致流程)
FLASH的一个划分框图
BootLoader程序
Flash配置头文件(app_flash_conf.h)
# define SIZE_8B ( 8 )
# define SIZE_256B ( 256 )
# define SIZE_512B ( 512 )
# define SIZE_1K ( 1024 )
# define SIZE_2K ( 2048 )
# define SIZE_3K ( 3072 )
# define SIZE_4K ( 4092 )
# define SYS_CONVERT ( type, val) ( ( type) ( val) )
# define BOOT_AREA_SIZE ( SIZE_1K* 10 )
# define BOOT_START_ADDR FLASH_BASE
# define APP_AREA_SIZE ( SIZE_1K* 150 )
# define APP_START_ADDR SYS_CONVERT ( uint32_t , BOOT_START_ADDR+ BOOT_AREA_SIZE)
# define APP_UPGRADE_AREA_SIZE ( SIZE_1K* 150 )
# define APP_UPGRADE_START_ADDR SYS_CONVERT ( uint32_t , APP_START_ADDR+ APP_AREA_SIZE)
# define CONF_AREA_SIZE ( SIZE_1K* 50 )
# define CONF_START_ADDR SYS_CONVERT ( uint32_t , APP_UPGRADE_START_ADDR+ APP_UPGRADE_AREA_SIZE)
# define DATA_AREA_SIZE ( SIZE_1K* 130 )
# define DATA_START_ADDR SYS_CONVERT ( uint32_t , CONF_START_ADDR+ CONF_AREA_SIZE)
# define BOOT_INF_ADDR SYS_CONVERT ( uint32_t , DATA_START_ADDR+ DATA_AREA_SIZE)
OTA配置
头文件(ota_boot_info.h)
# define OTA_UPGRADE_FLAG ( 0x1389 )
typedef struct {
uint8_t app_version[ 6 ] ;
uint32_t app_new_version_flag;
uint32_t app_upgrade_data_size;
} APP_Info_t;
void Load_App_Info ( APP_Info_t * info) ;
void Update_App_Info ( APP_Info_t * info) ;
void Jump_To_App ( uint32_t run_addr) ;
void Jump_To_BootLoader ( void ) ;
源文件
# include "ota_boot_info.h"
# include "bsp_flash.h"
# include "app_flash_conf.h"
typedef void ( * pFunction) ( void ) ;
void Load_App_Info ( APP_Info_t * info) {
STMFLASH_Read_Base ( BOOT_INF_ADDR, info, sizeof ( APP_Info_t) ) ;
}
void Update_App_Info ( APP_Info_t * info) {
STMFLASH_Write_Base ( BOOT_INF_ADDR, info, sizeof ( APP_Info_t) ) ;
}
void Jump_To_BootLoader ( void ) {
__disable_irq ( ) ;
uint32_t JumpAddress;
pFunction Jump_To_Application;
JumpAddress = * ( volatile uint32_t * ) ( 0x00000004 ) ;
Jump_To_Application = ( pFunction) JumpAddress;
__set_MSP ( * ( volatile uint32_t * ) JumpAddress) ;
__set_PSP ( * ( volatile uint32_t * ) JumpAddress) ;
Jump_To_Application ( ) ;
}
void Jump_To_App ( uint32_t run_addr) {
__disable_irq ( ) ;
pFunction jump_app;
jump_app = ( pFunction) ( * ( volatile uint32_t * ) ( run_addr + 0x04 ) ) ;
__set_MSP ( * ( volatile uint32_t * ) run_addr) ;
jump_app ( ) ;
}
待续