STM32F103基于HAL工程挂载FatFS驱动SD卡实现IAP功能
-
🎬基于SD卡IAP升级演示:
-
📍相关篇《STM32F103基于HAL工程挂载FatFS驱动SD卡》
-
📌《使用STM32F103的串口实现IAP程序升级功能》
-
👉🏻ST相关文档:使用SD卡时的应用内编程,STM32Cube的软件扩展(AN4854)AN4854文件,例程:
https://www.st.com/zh/embedded-software/x-cube-iap-sd.html
📑工程说明
🖍本工程为个人移植项目,并不是采用上面的ST官方例程基础上修改而来。折腾这个有几天时间才成功。IAP升级文件所使用的工程也是原工程,本工程是基于上面相关篇工程(STM32F103基于HAL工程挂载FatFS驱动SD卡)基础上新增的IAP功能。
-
🌿STM32CubeMX有关配置SD工程可以参考上面的相关内容。
-
🌿编译IAP升级文件时,需要设置IAP偏移地址:
这个地址不是固定的,是根据实际工程项目而定,具体大小根据工程编译生成的
.MAP
文件中的描述进行设定。(必须大于Size: 0x00004d1c
,而且是4的整数倍)
- 🔨有关BIN文件生成
-
- 📋ASF转BIN文件转换命令:
C:\Keil_v5\ARM\ARMCC\bin\fromelf.exe --bin -o ./STM32F1-SD-FatFS/iap.bin ./STM32F1-SD-FatFS/STM32F1-SD-FatFS.axf
- 🔰参考:
- 🏳🌈将生成的IAP.BIN文件拷贝到SD卡根目录下。名字一定要和程序中对应读取的名字要一致。
📓IAP核心驱动文件(bootloader
)
- bootloader.h
/*
* bootloader.h
*
*/
#ifndef BOOTLOADER_H_
#define BOOTLOADER_H_
typedef void (*pFunction)(void);
void bootloader(void);
#endif /* BOOTLOADER_H_ */
- bootloader.c
/*
* bootloader.c
*
*/
#include "bootloader.h"
#include "string.h"
#include "stm32f1xx_hal.h"
#include "ff.h"
#include "main.h"
#include <stdio.h>
#define FIRSTPAGE 10 //0x5000
#define LASTPAGE 248
#define ApplicationAddress 0x8005000
typedef enum
{
NO_SDCARD,
PROGRAMMED,
JUST_BOOTED,
} sequence_t;
pFunction Jump_To_Application;
static void boot(void);
static void erasePage(uint32_t pagenr);
static void programPage(uint32_t pagenr, uint8_t* data, uint32_t size);
//__ASM void startApplication(uint32_t stackPointer, uint32_t startupAddress);
static FATFS fatFs;
static FIL file;
static uint8_t pagedat[2048]; //must be 4 byte aligned
void bootloader()
{
volatile FRESULT res;
res = f_mount(&fatFs, "0:", 0);
myprintf("res= %i\r\n",res);
if(res != FR_OK)
{
myprintf("f_mount error (%i)\r\n", res);
boot();
}
res = f_open(&file, "iap.bin", FA_READ);//升级文件名
if(res != FR_OK)
{
myprintf("f_open error (%i)\r\n", res);//打开升级文件失败
boot();//调整到程序入口
}
else
{
myprintf("Find the upgrade file and prepare the IAP upgrade \r\n");
uint32_t page = FIRSTPAGE;
uint32_t programmed = 0;
while(page < LASTPAGE)
{
myprintf("Write to flash\r\n");
memset(pagedat, 0xFF, PAGESIZE); //clear the buffer
UINT len;
f_read(&file, pagedat, PAGESIZE, &len); //read from file
uint8_t* flashpointer = (uint8_t*)(FLASH_BASE + PAGESIZE * page);
if(memcmp(flashpointer, pagedat, len) != 0) //check if there is a difference
{
//program page
if(!programmed)
{
HAL_FLASH_Unlock();
programmed = 1;
}
erasePage(page);
programPage(page, (uint8_t*)pagedat, 2048);
}
if(len < PAGESIZE)
{
myprintf("Write programPage complete!\r\n");
break; //file is ending
}
page++;
}
if(programmed)
{
myprintf("PROGRAMMED\r\n");
}
else
{
myprintf("JUST_BOOTED\r\n");
}
boot();
}
}
static void erasePage(uint32_t pagenr)
{
FLASH_EraseInitTypeDef def;
def.Banks = FLASH_BANK_1;
def.NbPages = 1;
def.PageAddress = FLASH_BASE + PAGESIZE * pagenr;
def.TypeErase = FLASH_TYPEERASE_PAGES;
uint32_t err;
HAL_FLASHEx_Erase(&def, &err);
}
static void programPage(uint32_t pagenr, uint8_t* data, uint32_t size)
{
uint32_t targetaddr = FLASH_BASE + PAGESIZE * pagenr;
uint32_t* data32 = (uint32_t*)data;
size = size / 4;
while(size)
{
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, targetaddr, *data32);
targetaddr += 4;
data32++;
size--;
}
}
/**
* starts an application (sets the main stack pointer to stackPointer and jumps to startupAddress)
*/
__ASM void startApplication(uint32_t stackPointer, uint32_t startupAddress)
{
msr msp, r0 //set stack pointer to application stack pointer
bx r1 //branch to application startup code r1
}
/*
void boot() {
uint32_t JumpAddress;
uint32_t* app_start = (uint32_t*)(FLASH_BASE + PAGESIZE * FIRSTPAGE);
// __disable_irq();
// SCB->VTOR = (uint32_t)app_start;
JumpAddress = *(__IO uint32_t*)(app_start + 4);
Jump_To_Application = (pFunction) JumpAddress;
startApplication(app_start[0], app_start[1]);
__set_MSP(*(__IO uint32_t*) app_start);
Jump_To_Application();//跳转到APP.
}
*/
void boot()
{
uint32_t JumpAddress;
// uint32_t* app_start = (uint32_t*)(FLASH_BASE + PAGESIZE * FIRSTPAGE);
// __disable_irq();
// SCB->VTOR = (uint32_t)app_start;
if(((*(__IO uint32_t*)ApplicationAddress) & 0x2FFE0000) == 0x20000000)//检查栈顶地址是否合法.
{
myprintf("Address OK \r\n");
JumpAddress = *(__IO uint32_t*)(ApplicationAddress + 4);
Jump_To_Application = (pFunction) JumpAddress;
// startApplication(app_start[0], app_start[1]);
__set_MSP(*(__IO uint32_t*) ApplicationAddress);
Jump_To_Application();//跳转到APP.
}
}
//__ASM void __set_MSP(uint32_t mainStackPointer)
//{
// msr msp, r0
// bx lr
//}
📝main主程序
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "fatfs.h"
#include "spi.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "bootloader.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h> //for va_list var arg functions
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void myprintf(const char* fmt, ...);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void myprintf(const char* fmt, ...)
{
static char buffer[256];
va_list args;
va_start(args, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, args);
va_end(args);
int len = strlen(buffer);
HAL_UART_Transmit(&huart1, (uint8_t*)buffer, len, 10000);
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SPI1_Init();
MX_USART1_UART_Init();
MX_FATFS_Init();
/* USER CODE BEGIN 2 */
myprintf("\r\n SD card demo \r\n");
bootloader();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while(1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
myprintf("IAP main task\r\n");
// myprintf("New main task\r\n");
HAL_Delay(1000);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while(1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
📚工程源码
链接:https://pan.baidu.com/s/1Ut357bllVGCkTMlPyEdBZA
提取码:iahu