- #include "main.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();
- }
- }
- void USB_Disconnected(void)
- {
- __HAL_RCC_USB_OTG_FS_FORCE_RESET();
- HAL_Delay(200);
- __HAL_RCC_USB_OTG_FS_RELEASE_RESET();
-
- GPIO_InitTypeDef GPIO_Initure;
- __HAL_RCC_GPIOA_CLK_ENABLE();
- GPIO_Initure.Pin = GPIO_PIN_11 | GPIO_PIN_12;
- GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_Initure.Pull = GPIO_PULLDOWN;
- GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;
- HAL_GPIO_Init(GPIOA, &GPIO_Initure);
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_RESET);
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_RESET);
- HAL_Delay(300);
- }
- int main(void)
- {
- uint8_t R_Buff[1] = {0};
- HAL_Init();
- SystemClock_Config();
- Read_Flash_Data(R_Buff, 1, FLASH_Updata_Flag_ADDR);
-
- if(R_Buff[0] != 0x55)
- {
- USB_Disconnected();
- MX_GPIO_Init();
- MX_USB_DEVICE_Init();
- printf("Boot Code Start\r\n");
- printf("%s\r\n", Device_Version_Info_Str);
- }
- else
- {
- }
-
- while (1)
- {
- if(R_Buff[0] == 0x55)
- {
- Jump_To_App(FLASH_APP_ADDR);
- }
- else
- {
- Data_Handler();
- }
- }
- }
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- /** Configure the main internal regulator output voltage
- */
- __HAL_RCC_PWR_CLK_ENABLE();
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- /** 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.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLM = 4;
- RCC_OscInitStruct.PLL.PLLN = 96;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 4;
- 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_3) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] 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
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] 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 */
增加Update.c文件- #include "Update.h"
- int fputc(int ch, FILE *f)
- {
- while(CDC_Transmit_FS((uint8_t *)&ch, 1) == USBD_BUSY);
- return ch;
- }
- void Set_Device_Info(void)
- {
- uint32_t deviceserial2;
- deviceserial2 = *(uint32_t *) DEVICE_ID3;
-
- sprintf((char *)&Device_SSID_Info, "%d", deviceserial2);
- strcpy((char *)Device_Name_Info, Device_Name_Info_Str);
- strcpy((char *)Device_Version_Info, Device_Version_Info_Str);
- }
- void Write_Flash(uint8_t *data, uint16_t DataLen, uint32_t Addr)
- {
- uint16_t i = 0;
- uint32_t Data = 0;
- uint32_t temp = 0;
-
- HAL_FLASH_Unlock();
- for(i = 0; i < DataLen; i += 4)
- {
- Data = 0;
- for(uint8_t j = 0; j < 4; j++)
- {
- temp = data[i + j];
- Data |= temp << 8 * j;
- }
-
- if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, FLASH_APP_ADDR + Addr + i, Data) == HAL_OK)
- {
-
- }
- }
- HAL_FLASH_Lock();
- }
- void Write_Updata_Flag_Flash(void)
- {
- HAL_FLASH_Unlock();
- HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, FLASH_Updata_Flag_ADDR, 0x55555555);
- HAL_FLASH_Lock();
- }
- void Erase_Updata_Flag_Flash(void)
- {
- FLASH_EraseInitTypeDef EraseInitStruct;
- HAL_FLASH_Unlock();
- EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
- EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
- EraseInitStruct.Sector = UPDATA_FLAG_SECTOR;
- EraseInitStruct.NbSectors = UPDATA_FLAG_SECTOR_NUM;
- 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_SECTORS;
- EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
- EraseInitStruct.Sector = UPDATA_APP_SECTOR;
- EraseInitStruct.NbSectors = UPDATA_APP_SECTOR_NUM;
- 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);
- }
- }
增加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 ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base [url=home.php?mod=space&uid=72445]@[/url] of Sector 0, 16 Kbytes */
- #define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) /* Base [url=home.php?mod=space&uid=72445]@[/url] of Sector 1, 16 Kbytes */
- #define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) /* Base [url=home.php?mod=space&uid=72445]@[/url] of Sector 2, 16 Kbytes */
- #define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* Base [url=home.php?mod=space&uid=72445]@[/url] of Sector 3, 16 Kbytes */
- #define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base [url=home.php?mod=space&uid=72445]@[/url] of Sector 4, 64 Kbytes */
- #define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base [url=home.php?mod=space&uid=72445]@[/url] of Sector 5, 128 Kbytes */
- #define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) /* Base [url=home.php?mod=space&uid=72445]@[/url] of Sector 6, 128 Kbytes */
- #define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) /* Base [url=home.php?mod=space&uid=72445]@[/url] of Sector 7, 128 Kbytes */
- #define FLASH_Updata_Flag_ADDR ADDR_FLASH_SECTOR_2
- #define FLASH_APP_ADDR ADDR_FLASH_SECTOR_3
- #define UPDATA_FLAG_SECTOR FLASH_SECTOR_2
- #define UPDATA_FLAG_SECTOR_NUM 1
- #define UPDATA_APP_SECTOR FLASH_SECTOR_3
- #define UPDATA_APP_SECTOR_NUM 3
- #define DEVICE_ID3 (UID_BASE + 0x8)
- #define Device_Name_Info_Str "APM32F411"
- #define Device_SSID_Info_Str "1234"
- #define Device_Version_Info_Str "Boot V 1.0"
- extern uint8_t Device_Name_Info[12];
- extern uint8_t Device_SSID_Info[12];
- extern uint8_t Device_Version_Info[12];
- extern uint32_t PageError;
- void Erase_APP_Flash(void);
- void Erase_Updata_Flag_Flash(void);
- void Write_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);
- void Data_Handler(void);
- #endif
修改usbd_cdc_if.c文件,修改如下函数即可。
- #include "Update.h"
- static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
- {
- /* USER CODE BEGIN 6 */
- USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
- USBD_CDC_ReceivePacket(&hUsbDeviceFS);
- for(int i = 0; i < *Len; i++)
- {
- OnDataReceived(Buf[i]);
- }
- return (USBD_OK);
- /* USER CODE END 6 */
- }
接下来进行APP代码修改。可以复制Boot代码进行修改。对Flash起始地址和大小进行修改。
修改main.c文件
- #include "main.h"
- #include "usb_device.h"
- #include "gpio.h"
- #include "Update.h"
- void SystemClock_Config(void);
- void USB_Disconnected(void)
- {
- __HAL_RCC_USB_OTG_FS_FORCE_RESET();
- HAL_Delay(200);
- __HAL_RCC_USB_OTG_FS_RELEASE_RESET();
-
- GPIO_InitTypeDef GPIO_Initure;
- __HAL_RCC_GPIOA_CLK_ENABLE();
- GPIO_Initure.Pin = GPIO_PIN_11 | GPIO_PIN_12;
- GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_Initure.Pull = GPIO_PULLDOWN;
- GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;
- HAL_GPIO_Init(GPIOA, &GPIO_Initure);
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_RESET);
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_RESET);
- HAL_Delay(300);
- }
- int main(void)
- {
- SCB->VTOR = FLASH_APP_ADDR;
- HAL_Init();
- SystemClock_Config();
- USB_Disconnected();
- MX_GPIO_Init();
- MX_USB_DEVICE_Init();
- printf("APP Code Start\r\n");
- printf("%s\r\n", Device_Version_Info_Str);
- while (1)
- {
- Data_Handler();
- }
- }
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- /** Configure the main internal regulator output voltage
- */
- __HAL_RCC_PWR_CLK_ENABLE();
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- /** 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.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLM = 4;
- RCC_OscInitStruct.PLL.PLLN = 96;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 4;
- 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_3) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] 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
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] 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 */
以上就是OTA升级的全部配置步骤。Update.h有很多宏定义可以修改。
下面介绍一下升级工具的使用。
1.将Boot程序烧录到板子中。
2.APP工程编译出的bin文件可以通过升级工具烧写到板子中。
3.打开升级工具,下图是只有Boot程序的板子。
4.打开升级文件升级后截图,烧写成功,板子会自动运行APP代码。
升级过程如下
喜欢朋友可以下载下面的升级工具和程序固件进行测试。链接不能直接跳转,需要手动复制粘贴网页地址才能跳转。
APP和Boot固件:
升级固件.zip
(35.66 KB, 下载次数: 30)
APM32F4XX升级工具:CSDN:链接:https://download.csdn.net/download/dailin2012/88288374
百度网盘:链接:https://pan.baidu.com/s/1iYWV7D9ABpPxeaiuQe3DTw
提取码:mwjs
下面有源码下载。回复帖子可见。
APM32F411Boot源码:
APM32F411APP源码:
下载源码后,可在Update.h文件对一些参数的宏定义进行修。如修改设备名称,版本等等信息,Updata_Flag地址和占用空间,APP地址和占用Flash空间等。此协议也可以移植到其他系列的MCU上如APM32F103.只需要将FLASH读写函数修改成APM32F103的接口就可以与上位机通讯进行升级。