stm32 hal库 bootloader代码例程
时间: 2023-08-31 16:05:10 浏览: 203
以下是一个基于 STM32 HAL 库的 Bootloader 代码例程:
```c
#include "main.h"
#include "stm32f4xx_hal.h"
#define FLASH_SECTOR_0 ((uint32_t)0x08000000) /* 16 Kbytes */
#define FLASH_SECTOR_1 ((uint32_t)0x08004000) /* 16 Kbytes */
#define FLASH_SECTOR_2 ((uint32_t)0x08008000) /* 16 Kbytes */
#define FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* 16 Kbytes */
#define FLASH_SECTOR_4 ((uint32_t)0x08010000) /* 64 Kbytes */
#define FLASH_SECTOR_5 ((uint32_t)0x08020000) /* 128 Kbytes */
#define FLASH_SECTOR_6 ((uint32_t)0x08040000) /* 128 Kbytes */
#define FLASH_SECTOR_7 ((uint32_t)0x08060000) /* 128 Kbytes */
#define FLASH_SECTOR_8 ((uint32_t)0x08080000) /* 128 Kbytes */
#define FLASH_SECTOR_9 ((uint32_t)0x080A0000) /* 128 Kbytes */
#define FLASH_SECTOR_10 ((uint32_t)0x080C0000) /* 128 Kbytes */
#define FLASH_SECTOR_11 ((uint32_t)0x080E0000) /* 128 Kbytes */
#define APP_ADDRESS FLASH_SECTOR_2
#define LED_GPIO_PORT GPIOA
#define LED_GPIO_PIN GPIO_PIN_5
#define RX_BUFFER_SIZE 256
UART_HandleTypeDef huart2;
uint8_t rx_buffer[RX_BUFFER_SIZE];
uint8_t command_received = 0;
uint32_t flash_address = APP_ADDRESS;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
HAL_UART_Transmit(&huart2, (uint8_t*)"Bootloader started!\r\n", 21, HAL_MAX_DELAY);
while (1)
{
if (command_received)
{
command_received = 0;
if (rx_buffer[0] == 'E' && rx_buffer[1] == 'R' && rx_buffer[2] == 'A' && rx_buffer[3] == 'S' && rx_buffer[4] == 'E')
{
HAL_UART_Transmit(&huart2, (uint8_t*)"Erasing flash...\r\n", 18, HAL_MAX_DELAY);
HAL_FLASH_Unlock();
FLASH_Erase_Sector(FLASH_SECTOR_2, FLASH_VOLTAGE_RANGE_3);
HAL_FLASH_Lock();
HAL_UART_Transmit(&huart2, (uint8_t*)"Flash erased!\r\n", 15, HAL_MAX_DELAY);
flash_address = APP_ADDRESS;
}
else if (rx_buffer[0] == 'W' && rx_buffer[1] == 'R' && rx_buffer[2] == 'I' && rx_buffer[3] == 'T' && rx_buffer[4] == 'E')
{
HAL_UART_Transmit(&huart2, (uint8_t*)"Writing to flash...\r\n", 22, HAL_MAX_DELAY);
HAL_FLASH_Unlock();
for (int i = 0; i < RX_BUFFER_SIZE; i += 4)
{
uint32_t word = *(uint32_t*)&rx_buffer[i];
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, flash_address, word);
flash_address += 4;
}
HAL_FLASH_Lock();
HAL_UART_Transmit(&huart2, (uint8_t*)"Data written to flash!\r\n", 24, HAL_MAX_DELAY);
}
else if (rx_buffer[0] == 'J' && rx_buffer[1] == 'U' && rx_buffer[2] == 'M' && rx_buffer[3] == 'P')
{
HAL_UART_Transmit(&huart2, (uint8_t*)"Jumping to application...\r\n", 28, HAL_MAX_DELAY);
HAL_Delay(1000);
HAL_DeInit();
__set_MSP(*(__IO uint32_t*)APP_ADDRESS);
uint32_t jump_address = *(uint32_t*)(APP_ADDRESS + 4);
void (*app_entry)(void) = (void (*)(void))jump_address;
app_entry();
}
else
{
HAL_UART_Transmit(&huart2, (uint8_t*)"Invalid command!\r\n", 18, HAL_MAX_DELAY);
}
}
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
static uint16_t rx_index = 0;
if (huart == &huart2)
{
if (rx_buffer[rx_index] == '\r' || rx_index == RX_BUFFER_SIZE - 1)
{
rx_buffer[rx_index] = '\0';
rx_index = 0;
command_received = 1;
}
else
{
rx_index++;
}
HAL_UART_Receive_IT(&huart2, &rx_buffer[rx_index], 1);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
if (HAL_PWREx_EnableOverDrive() != HAL_OK)
{
Error_Handler();
}
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_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
HAL_UART_Receive_IT(&huart2, rx_buffer, 1);
}
static void MX_GPIO_Init(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = LED_GPIO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
}
void Error_Handler(void)
{
while (1)
{
}
}
```
这个 Bootloader 通过 UART 接收来自 PC 的命令,支持三个命令:ERASE 用于擦除 Flash,WRITE 用于写入数据到 Flash,JUMP 用于跳转到应用程序。在写入数据时,Bootloader 每次写入 4 个字节,因此输入的数据必须是 4 的倍数。在跳转到应用程序时,Bootloader 会将堆栈指针设置为应用程序的地址,并跳转到应用程序的入口处。
阅读全文