1.配置外部高速晶振
2.勾选USB功能
3.将USB模式配置Virtual Port Com
4.将系统主频配置为72M,USB频率配置为48M.
5.配置好项目名称,开发环境,最后获取代码。
6.修改Flash大小和勾选Use Micro LIB
7.修改main.c代码
#include "main.h"
#include "usart.h"
#include "usb_device.h"
#include "gpio.h"
#include "Update.h"
void SystemClock_Config(void);
typedef void (*pFunction)(void);
pFunction Jump_To_Application;
uint32_t JumpAddress;
void Jump_To_App(uint32_t address)
{
if (((*(__IO uint32_t*)address) & 0x2FFE0000) == 0x20000000)
{
JumpAddress = *(__IO uint32_t*) (address + 4);
Jump_To_Application = (pFunction) JumpAddress;
__set_MSP(*(__IO uint32_t*) address);
Jump_To_Application();
}
}
int main(void)
{
uint8_t R_Buff[1] = {0};
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USB_DEVICE_Init();
printf("STM32F103 Boot Code Start\r\n");
Read_Flash_Data(R_Buff, 1, FLASH_Updata_Flag_ADDR);
printf("Bootloader R_Buff = 0x%x\r\n", R_Buff[0]);
while (1)
{
if(R_Buff[0] == 0x55)
{
printf("Jump_To_App = %x\r\n", FLASH_APP_ADDR);
Jump_To_App(FLASH_APP_ADDR);
}
else
{
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 uint8_t Check;
static uint8_t Send_Data[10];
static uint16_t Update_Pack_Num;
static uint16_t Update_Pack_Num_Temp;
static uint32_t Update_Data_Len;
static uint32_t Write_Date_Len;
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++;
Receive_Data_Len = 0;
Receive_Data_Count = 0;
Cmd = Data;
if(Cmd == 0x00)
{
printf("Cmd == 0x00\r\n");
}else if(Cmd == 0x01)
{
Update_State = UPDATE_START;
Update_Pack_Num = 0;
Write_Date_Len = 0;
printf("Update_State UPDATE_START\r\n");
}
else if(Cmd == 0x02)
{
Update_State = UPDATE_STARTING;
printf("Update_State UPDATE_STARTING\r\n");
}
else if(Cmd == 0x03)
{
Update_State = UPDATE_END;
printf("Update_State UPDATE_END\r\n");
}
}
else if(Receiver_State == RECEIVER_LEN_H)
{
Receiver_State++;
Receive_Data_Len |= Data << 8;
}
else if(Receiver_State == RECEIVER_LEN_L)
{
Receiver_State++;
Receive_Data_Len |= Data;
if(Receive_Data_Len == 0)
{
Receiver_State = RECEIVER_CHECK;
}
}
else if(Receiver_State == RECEIVER_DATA)
{
Update_Data[Receive_Data_Count++] = Data;
if(Cmd == 0x01)
{
if(Receive_Data_Count == 4)
{
Update_Data_Len = 0;
Update_Data_Len |= Update_Data[0] << 24;
Update_Data_Len |= Update_Data[1] << 16;
Update_Data_Len |= Update_Data[2] << 8;
Update_Data_Len |= Update_Data[3];
}
}
else if(Cmd == 0x02)
{
if(Receive_Data_Count == 2)
{
Update_Pack_Num_Temp = 0;
Update_Pack_Num_Temp |= Update_Data[0] << 8;
Update_Pack_Num_Temp |= Update_Data[1];
}
}
if(Receive_Data_Count == Receive_Data_Len)
{
Receiver_State++;
}
}
else if(Receiver_State == RECEIVER_CHECK)
{
Receiver_State = RECEIVER_HEAD_H;
Check = Data;
Send_Data[0] = 0x55;
Send_Data[1] = 0x55;
Send_Data[2] = Cmd;
Send_Data[3] = 0x00;
if(Update_State == UPDATE_START)
{
Erase_APP_Flash();
printf("Erase_APP_Flash\r\n");
printf("Update_Data_Len = %d\r\n", Update_Data_Len);
Send_Data[4] = 0x00;
Send_Data[5] = 0x00;
CDC_Transmit_FS(Send_Data, 6);
}
else if(Update_State == UPDATE_STARTING)
{
if(Update_Pack_Num_Temp == Update_Pack_Num)
{
Check = Check_Sum(Update_Data, Receive_Data_Len);
if(Check == Data)
{
Update_Pack_Num++;
Write_Flash(&Update_Data[2], Receive_Data_Len - 2, Write_Date_Len);
Write_Date_Len += Receive_Data_Len - 2;
printf("Receive Update_Pack_Num = %d\r\n", Update_Pack_Num_Temp);
printf("Write_Date_Len = %d, Update_Data_Len = %d\r\n", Write_Date_Len, Update_Data_Len);
}
else
{
printf("Check Error Check = %d, ReceCheck = %d \r\n", Check, Data);
}
}
if(Write_Date_Len == Update_Data_Len)
{
Send_Data[2] = 0x03;
Send_Data[3] = 0x00;
Send_Data[4] = 0x00;
Send_Data[5] = 0x00;
CDC_Transmit_FS(Send_Data, 6);
Erase_Updata_Flag_Flash();
Write_Updata_Flag_Flash();
printf("Upgrade successfully \r\n");
NVIC_SystemReset();
}
else
{
Send_Data[4] = 0x02;
Send_Data[5] = Update_Pack_Num >> 8;
Send_Data[6] = Update_Pack_Num;
Send_Data[7] = Check_Sum(&Send_Data[5], 2);
CDC_Transmit_FS(Send_Data, 8);
}
}
else if(Update_State == UPDATE_END)
{
Send_Data[4] = 0x00;
Send_Data[5] = 0x00;
CDC_Transmit_FS(Send_Data, 6);
}
}
}
}
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
源码链接:跳转链接
上位机升级工具:升级工具
STM32F103 USB OTA升级APP (二):跳转链接