cancel
Showing results for 
Search instead for 
Did you mean: 

I'm using STM32L4A6RGT6 in this bootloader got corrupted, Address 0x00000000 its neither erasing nor writing, and some of the addresses like 0x20000000, 0x10000000 cant be erased, Is there any way to sort this?

HA.4
Associate II
 
5 REPLIES 5

> I'm using STM32L4A6RGT6 in this bootloader

Are you talking about the default, built-in bootloader?

> Address 0x00000000 its neither erasing nor writing

Why do you expect that address to be erasable/writable? If you are using the built-in bootloader, the System memory is mapped at this address, and it's not erasable/writable.

You probably want to erase/write the FLASH, and that's at 0x0800'0000.

JW

Nobody has any idea what you're actually doing​.

E​xplain more fully in the body of the question rather than the title/summary.

A​ paragraph saying something doesn't work provides no context.

RAM addresses don't contain FLASH

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
HA.4
Associate II

When I flash this code into stm32L4A6RGT6, and pressed the button, stm32 is getting stuck in line 56 inside the function HAL_FLASH_OB_Launch() . And once again try to flash to same board, its not flashing and saying that "Break at address "0x1fff06f6" with no debug information available, or outside of program code". 

And if I flash any other firmware to the same board, intially it works, when we press restart button its getting stuck.

#include "main.h"
#include "stdbool.h"
typedef enum {
	NONE,
	PREPARATION,
	FLASH_ERASING,
	FLASH_ERASE_DONE,
	FLASH_WRITE_IN_PROGRESS,
	FLASH_WRITE_DONE,
	UPDATE_DONE,
} updateState_t;
#define NUM_PAGES 100
#define NUM_BYTES       NUM_PAGES * FLASH_PAGE_SIZE
#define NUM_DOUBLEWORDS NUM_BYTES / 8
volatile updateState_t updateState = NONE;
 
void HAL_GPIO_EXTI_Callback(uint16_t pin) {
	if (pin == GPIO_PIN_2) {
		printf("Button is pressed");
		if (updateState == NONE) {
			updateState = PREPARATION;
		}
	}
 
void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue) {
	if (updateState == FLASH_ERASING && ReturnValue==0xffffffff) {
	updateState = FLASH_ERASE_DONE;
	} else if (updateState == FLASH_WRITE_IN_PROGRESS) {
		updateState = FLASH_WRITE_DONE;
	}
}
 
void toggleBankAndReset() {
	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;
	} else {
		OBInit.USERConfig = OB_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;
}
}
 
int main(void)
{
	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: 500, else");
		delay = 500;
	}
while (1) {
		uint32_t now = HAL_GetTick();
		if (now - last > delay) {
			HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_5);
			last = now;
		}
		switch (updateState) {
		case NONE:
			break;
		case PREPARATION: {
			FLASH_EraseInitTypeDef erase = { 0 };
			erase.TypeErase = FLASH_TYPEERASE_PAGES;
			erase.Banks = bank == 1 ? FLASH_BANK_2 : FLASH_BANK_1;
			erase.NbPages = NUM_PAGES;
			erase.Page = 0;
 
			HAL_FLASH_Unlock();
			HAL_StatusTypeDef status = HAL_FLASHEx_Erase_IT(&erase);
			if (status != HAL_OK) {
				// TODO error case
			}
 
			updateState = FLASH_ERASING;
		}
			break;
 
		case FLASH_ERASING:
		case FLASH_WRITE_IN_PROGRESS:
			delay = 200;
//			updateState = FLASH_ERASE_DONE;
			break;
 
		case FLASH_ERASE_DONE:
		case FLASH_WRITE_DONE: {
			delay = 50;
			static size_t index = 0;
 
			uint32_t dest = 0x08010000;
			uint8_t *src = (uint8_t*) 0x08000000;
 
			if (index < NUM_DOUBLEWORDS) {
			     uint64_t doubleword = *(uint64_t*) (src + (index * 8));
 
				updateState = FLASH_WRITE_IN_PROGRESS;
				HAL_StatusTypeDef progStatus;
				progStatus= HAL_FLASH_Program_IT(
				FLASH_TYPEPROGRAM_DOUBLEWORD, dest + index * 8, doubleword);
				if (progStatus == HAL_OK)
					index++;
			} else {
				updateState = UPDATE_DONE;
			}
		}
			break;
 
		case UPDATE_DONE:
			toggleBankAndReset();
			break;
		}
       }
}
HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
{
  /* Set the bit to force the option byte reloading */
  SET_BIT(FLASH->CR, FLASH_CR_OBL_LAUNCH);
 
  /* Wait for last operation to be completed */
  return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE));
}

> "Break at address "0x1fff06f6"

That's in the system memory = built-in bootloader area.

If you set BFB2, system memory remains mapped at 0x0000'0000, so the bootloader's interrupt table is at 0x0000'0000, where VTOR points to by default.

0693W00000aI8TBQA0.png 

Make sure to set VTOR to 0x0800'0000 before enabling any interrupt.

JW

HA.4
Associate II

Thank you, so much for you time.