2023-06-25 11:49 PM - edited 2023-06-25 11:56 PM
Hi, I'm facing a Issue in flashing the code from Bank1 to Bank2 and run, MCU is STM32L4A6RGT6.
In the below mentioned code I'm trying to do bootloader flash, When I press a button, it will start flashing the code from 0x08000000 to 0x08080000 address, which is from bank1 to bank2. After flashing the code successfully its not running in the Bank2, MCU getting stuck.
Kindly requesting to help me out to solve this issue.
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdbool.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
typedef enum {
NONE,
PREPARATION,
FLASH_ERASING,
FLASH_ERASE_DONE,
FLASH_WRITE_IN_PROGRESS,
FLASH_WRITE_DONE,
UPDATE_DONE,
} updateState_t;
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define NUM_PAGES 100
//#define FLUSH asm("dsb");
//#define ADDRESS 0x08080000
uint8_t bank;
uint32_t rt = 0 ;
// Number of bytes for firmware
#define NUM_BYTES NUM_PAGES * FLASH_PAGE_SIZE
// Number of double words (64 bits) for firmware
#define NUM_DOUBLEWORDS NUM_BYTES / 8
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart2;
/* USER CODE BEGIN PV */
volatile updateState_t updateState = NONE;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
return ch;
}
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void HAL_GPIO_EXTI_Callback(uint16_t pin) {
if (pin == GPIO_PIN_2) {
printf("\nButton is pressed\n");
if (updateState == NONE) {
updateState = PREPARATION;
printf("\nStatus: Preparation\n");
}
}
}
void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue) {
rt = ReturnValue;
if (updateState == FLASH_ERASING && ReturnValue == 0xffffffff) {
updateState = FLASH_ERASE_DONE;
printf("\nStatus: flash erase done\n");
} else if (updateState == FLASH_WRITE_IN_PROGRESS) {
updateState = FLASH_WRITE_DONE;
printf("\nStatus: flash write done\n");
}
}
void toggleBankAndReset() {
printf("Toggle Bank and Reset function");
FLASH_OBProgramInitTypeDef OBInit;
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
HAL_FLASH_OB_Unlock();
HAL_FLASHEx_OBGetConfig(&OBInit);
OBInit.OptionType = OPTIONBYTE_USER;
OBInit.USERType = OB_USER_BFB2;
if (((OBInit.USERConfig) & (OB_BFB2_ENABLE)) == OB_BFB2_ENABLE) {
OBInit.USERConfig = OB_BFB2_DISABLE;
printf("BFB2 Disable");
} else {
OBInit.USERConfig = OB_BFB2_ENABLE;
printf("BFB2 Enable");
}
if (HAL_FLASHEx_OBProgram(&OBInit) != HAL_OK) {
// uint32_t errorCode = HAL_FLASH_GetError();
while (1) {
printf("1s delay is running::OB Program ");
HAL_Delay(1000);
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_5);
}
}
if (HAL_FLASH_OB_Launch() != HAL_OK) {
//uint32_t errorCode = HAL_FLASH_GetError();
while (1) {
printf("0.1s delay is running::OB launch ");
HAL_Delay(100);
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_5);
}
}
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();
}
uint8_t getActiveBank() {
volatile uint32_t remap = READ_BIT(SYSCFG->MEMRMP, 0x1 << 8);
return remap == 0 ? 1 : 2;
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
SCB->VTOR = 0x08000000;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
uint8_t bank = getActiveBank();
uint32_t last = HAL_GetTick();
uint32_t delay;
if (bank == 1) {
printf("delay: 1000, when bank==1");
delay = 1000;
} else {
printf("delay: 3000, else");
delay = 3000;
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
// printf("Bank: %d", bank);
while (1) {
uint32_t now = HAL_GetTick();
if (now - last > delay) {
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_5);
last = now;
}
switch (updateState) {
case NONE:
printf("\nStatus: none\n");
break;
case PREPARATION: {
FLASH_EraseInitTypeDef erase = { 0 };
erase.TypeErase = FLASH_TYPEERASE_PAGES;
erase.Banks = bank == 1 ? FLASH_BANK_2 : FLASH_BANK_1;
erase.NbPages = ((uint32_t)0x08080000 - (uint32_t)0x08000000)/FLASH_PAGE_SIZE;
erase.Page = 0;
HAL_FLASH_Unlock();
HAL_StatusTypeDef status = HAL_FLASHEx_Erase_IT(&erase);
if (status != HAL_OK) {
// TODO error case
}
updateState = FLASH_ERASING;
printf("\nStatus: Flash Erasing\n");
}
break;
case FLASH_ERASING:
// delay = 300;
// HAL_FLASH_EndOfOperationCallback();
// break;
case FLASH_WRITE_IN_PROGRESS:
delay = 200;
break;
case FLASH_ERASE_DONE:
case FLASH_WRITE_DONE: {
delay = 50;
static size_t index = 0;
uint32_t dest = 0x08080000;
uint8_t *src=(uint8_t*) 0x08000000;
if (index < NUM_DOUBLEWORDS) {
uint64_t doubleword = *(uint64_t*) (src + (index * 8));
updateState = FLASH_WRITE_IN_PROGRESS;
printf("\nStatus: Flash write in progress\n");
HAL_StatusTypeDef progStatus;
progStatus= HAL_FLASH_Program_IT(
FLASH_TYPEPROGRAM_DOUBLEWORD, dest + index * 8, doubleword);
// if (progStatus == HAL_OK)
index++;
} else {
updateState = UPDATE_DONE;
printf("\nStatus: update done\n");
}
}
break;
case UPDATE_DONE:
printf("Before toogle bank and reset");
// delay = 1000;
toggleBankAndReset();
break;
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @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
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.MSICalibrationValue = 0;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
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_MSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief USART2 Initialization Function
* None
* @retval None
*/
static void MX_USART2_UART_Init(void)
{
/* USER CODE BEGIN USART2_Init 0 */
/* USER CODE END USART2_Init 0 */
/* USER CODE BEGIN USART2_Init 1 */
/* USER CODE END USART2_Init 1 */
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;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */
/* USER CODE END USART2_Init 2 */
}
/**
* @brief GPIO Initialization Function
* None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LED5_GPIO_Port, LED5_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin : PD2 */
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/*Configure GPIO pin : LED5_Pin */
GPIO_InitStruct.Pin = LED5_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED5_GPIO_Port, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI2_IRQn);
}
/* 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 */
__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.
* file: pointer to the source file name
* 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 */