接上一篇STM32F103 USB OTA升级BootLoader (一):跳转链接
修改程序启动地址和Flash大小
修改main.c代码
#include "main.h"
#include "usart.h"
#include "usb_device.h"
#include "gpio.h"
#include "Update.h"
void SystemClock_Config(void);
int main(void)
{
SCB->VTOR = FLASH_APP_ADDR;
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USB_DEVICE_Init();
printf("IAP Code V1.1\r\n");
while (1)
{
Usart_Data_Handler();
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {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();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != 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 */
/* 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,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Update.c
#include "Update.h"
uint32_t PageError = 0;
void Write_Flash(uint8_t *data, uint16_t DataLen, uint32_t Addr)
{
uint16_t i = 0;
uint64_t Data = 0;
uint64_t temp = 0;
HAL_FLASH_Unlock();
for(i = 0; i < DataLen; i += 8)
{
Data = 0;
for(uint8_t j = 0; j < 8; j++)
{
temp = data[i + j];
Data |= temp << 8 * j;
}
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, FLASH_APP_ADDR + Addr + i, Data) == HAL_OK)
{
}
/* printf("0x%16llx |", Data);
if(i % 16 == 0 && i != 0)
printf("\r\n");*/
}
HAL_FLASH_Lock();
}
void Write_Updata_Flag_Flash(void)
{
HAL_FLASH_Unlock();
HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, FLASH_Updata_Flag_ADDR, 0x55555555);
HAL_FLASH_Lock();
}
void Erase_Updata_Flag_Flash(void)
{
FLASH_EraseInitTypeDef EraseInitStruct;
HAL_FLASH_Unlock();
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.PageAddress = FLASH_Updata_Flag_ADDR;
EraseInitStruct.NbPages = 1;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
{
}
HAL_FLASH_Lock();
}
void Erase_APP_Flash(void)
{
FLASH_EraseInitTypeDef EraseInitStruct;
HAL_FLASH_Unlock();
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.PageAddress = FLASH_APP_ADDR;
EraseInitStruct.NbPages = 20;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
{
}
HAL_FLASH_Lock();
}
void Read_Flash_Data(uint8_t* pBuffer, uint32_t NumToRead, uint32_t ReadAddr)
{
uint32_t i;
for (i = 0; i < NumToRead; i++)
{
*((uint8_t*)pBuffer + i) = *((uint8_t*)ReadAddr + i);
}
}
#define RX_QUEUE_LENGTH 2048
static uint8_t RxQueue[RX_QUEUE_LENGTH];
uint8_t Update_Data[1024 + 10];
static uint16_t RxQueueHead = 0;
static uint16_t RxQueueTail = 0;
typedef enum
{
RECEIVER_IDLE = 0,
RECEIVER_HEAD_H,
RECEIVER_HEAD_L,
RECEIVER_CMD,
RECEIVER_LEN_H,
RECEIVER_LEN_L,
RECEIVER_DATA,
RECEIVER_CHECK,
}RECEIVER_STATE;
typedef enum
{
UPDATE_IDLE = 0,
UPDATE_START,
UPDATE_STARTING,
UPDATE_END,
}UPDATE_STATE;
RECEIVER_STATE Receiver_State = RECEIVER_HEAD_H;
UPDATE_STATE Update_State = UPDATE_IDLE;
void OnDataReceived(uint8_t val)
{
if(((RxQueueTail + 1) % RX_QUEUE_LENGTH) != RxQueueHead) //queue is not full
{
RxQueue[RxQueueTail++] = val; //save data into queue
RxQueueTail %= RX_QUEUE_LENGTH; //queue tail++
}
}
void ClearRxQueue(void)
{
RxQueueHead = 0;
RxQueueTail = 0;
memset(RxQueue, 0, sizeof(RxQueue));
}
static uint16_t GetRxQueueLen(void)
{
return ((RxQueueTail + RX_QUEUE_LENGTH - RxQueueHead) % RX_QUEUE_LENGTH);
}
uint8_t GetRxQueueData(void)
{
uint8_t val;
val = RxQueue[RxQueueHead];
RxQueueHead = ((RxQueueHead + 1) % RX_QUEUE_LENGTH);
return val;
}
uint8_t Check_Sum(uint8_t *str, uint16_t len)
{
uint8_t sum = 0;
for(;len > 0; len--)
{
sum += *str++;
}
return sum;
}
bool Usart_Data_Handler(void)
{
static uint8_t Data;
static uint8_t Cmd;
static uint16_t Receive_Data_Len;
static uint16_t Receive_Data_Count;
while(GetRxQueueLen() > 0)
{
Data = GetRxQueueData();
if(Receiver_State == RECEIVER_HEAD_H)
{
if(Data == 0x55)
{
Receiver_State++;
}
}
else if(Receiver_State == RECEIVER_HEAD_L)
{
if(Data == 0x55)
{
Receiver_State++;
}
}
else if(Receiver_State == RECEIVER_CMD)
{
Receiver_State++;
Cmd = Data;
if(Cmd == 0x00)
{
Erase_Updata_Flag_Flash();
printf("Erase_Updata_Flag_Flash \r\n");
NVIC_SystemReset();
}
}
else if(Receiver_State == RECEIVER_LEN_H)
{
Receiver_State++;
}
else if(Receiver_State == RECEIVER_LEN_L)
{
Receiver_State++;
}
else if(Receiver_State == RECEIVER_DATA)
{
if(Receive_Data_Count == Receive_Data_Len)
{
Receiver_State++;
}
}
else if(Receiver_State == RECEIVER_CHECK)
{
Receiver_State = RECEIVER_HEAD_H;
}
}
}
void Usart_Get_Data_Handler(void)
{
if(!Usart_Data_Handler())
{
return;
}
}
Update.h
#ifndef __UPDATE_H
#define __UPDATE_H
#include "main.h"
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include "usbd_cdc_if.h"
#define FLASH_Page_Size (2048)
#define FLASH_Updata_Flag_ADDR (0x08000000 + 9 * FLASH_Page_Size)
#define FLASH_APP_ADDR (0x08000000 + 10 * FLASH_Page_Size)
void Erase_APP_Flash(void);
void Erase_Updata_Flag_Flash(void);
void Write_Flash(uint8_t *data, uint16_t DataLen, uint32_t Addr);
void Read_Flash_Data(uint8_t* pBuffer, uint32_t NumToRead, uint32_t ReadAddr);
void ClearRxQueue(void);
void OnDataReceived(uint8_t val);
bool Usart_Data_Handler(void);
#endif
源码链接:跳转链接
上位机升级工具:升级工具