一 bootloader 区
1.通过cubumx配置一个串口,一个led,来验证现象
2.新建一个boot_boot的工程, STM32F103C8TX_FLASH.ld文件 修改
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 20K /* 0x5000 */ 留20k用来做bootloader区
3.添加app 地址定义
#define SYSTEM_APP_FLASH_ADDR 0x8005000
4.写跳转app 函数
typedef void (*iap_fun)(void);
iap_fun jumpfun;
void jump_app()
{
jumpfun = *(__IO uint32_t*)(SYSTEM_APP_FLASH_ADDR + 4);//用户代码区第二个字为程序开始地址
__set_MSP(*(__IO uint32_t*) SYSTEM_APP_FLASH_ADDR);//初始化APP堆栈指针
jumpfun();//跳转到APP.
}
5.最后调用jump_app(),下载进芯片。
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 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 "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
#define SYSTEM_APP_FLASH_ADDR 0x8005000
/* 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 */
//串口重定向代码放在main.c中
#ifdef __GNUC__ //串口重定向
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int c