工作原理
通过修改FLASH 选项寄存器 (FLASH_OPTR)中nBOOT_SEL、nBOOT0、nBOOT1进行启动区域选择
STM32G0B1xx 在System Bootloader下可以使用UART、SPI、IIC的方式进行升级,详见<AN2602_STM32microcontroller_system_boot_mode.pdf>
工作流程
测试示例
正常运行时LED以200ms的周期闪烁
按键按下后进入system bootloadr
修改代码将LED频率闪烁周期改为500ms,打开STM32CubeProgrammer,选择要使用的串口方式(此处选用uart,开发板使用的USART2)->Connect-》加载待升级用户代码文件->升级
修改Option bytes->User Configuration->nBOOT_SEL=1;nBOOT1=1;nBOOT0=1。
然后等待设置完成断开连接即可
可看到LED闪烁周期已变为500ms
测试代码
#include "main.h"
#include "usart.h"
#include "gpio.h"
#include "user_debug.h"
void SystemClock_Config(void);
void bootloaderEnter( void );
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
while (1)
{
HAL_GPIO_TogglePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin);
HAL_Delay(200);
/* wait USER_KEY down to system bootloder */
volatile int timeout = 50;
while((HAL_GPIO_ReadPin( USER_KEY_GPIO_Port , USER_KEY_Pin ) == GPIO_PIN_RESET) && (timeout-- > 0));
if(timeout > 0)
continue;
else
{
PRINTF("go to bootloadr");
bootloaderEnter();
}
}
}
/**
* @brief 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_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1;
RCC_OscInitStruct.PLL.PLLN = 8;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
void bootloaderEnter( void )
{
FLASH_OBProgramInitTypeDef opdata = {0};
HAL_FLASH_Unlock();
HAL_FLASH_OB_Unlock();
opdata.OptionType = OPTIONBYTE_USER;
opdata.USERConfig = OB_USER_nBOOT_SEL | OB_USER_nBOOT1;
opdata.USERType = FLASH_OPTR_nBOOT_SEL | FLASH_OPTR_nBOOT1 | FLASH_OPTR_nBOOT0;
HAL_FLASHEx_OBProgram(&opdata);
HAL_FLASH_OB_Launch();
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();
HAL_NVIC_SystemReset();
while(1)
;
}
/**
* @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 */
__disable_irq();
while (1)
{
}
/* 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)
{
}
#endif /* USE_FULL_ASSERT */