cancel
Showing results for 
Search instead for 
Did you mean: 

I'm trying to write and read flash

Gbasi.1
Associate II

hi guys, im working on stm32l412kb mcu using keil v5 uvision and i ve found it difficult to write and read flash memory. all the explanation online are not relevant for my mcu. that the way i'm trying to confirm my actions, if anyone understand the problem it would be extremely helpful.

{EraseInitStruct.TypeErase=FLASH_TYPEERASE_PAGES;

EraseInitStruct.Banks=FLASH_BANK_1;

EraseInitStruct.Page=255;

EraseInitStruct.NbPages=1;

HAL_FLASH_Unlock();

HAL_FLASHEx_Erase(&EraseInitStruct, &pageError);

HAL_UART_Transmit(&huart1, (uint8_t*)"tried2\n\r", 13,100);

data=*(uint64_t *)0x08000004;

HAL_UART_Transmit(&huart1,(uint8_t*)data, 13,100);

HAL_UART_Transmit(&huart1, (uint8_t*)"\n\rtried3\n\r", 13,100);

HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,0x08000004,0x01u);

data=*(uint64_t *)0x08000004;

HAL_UART_Transmit(&huart1,(uint8_t*)value, 13,100);

if (value==0x01u)

HAL_UART_Transmit(&huart1, (uint8_t*)"we made it\n\r", 20,100);

else

HAL_UART_Transmit(&huart1, (uint8_t*)"\n\r we are losers\n\r", 20,100);

HAL_FLASH_Lock();

HAL_Delay(100);}

10 REPLIES 10

I have already pointed out the problems with your code.

Data should be aligned to doubleword addresses, i.e. ones that are evenly divisible by 8. The address above is not.

Moreover, it does not point to the address range that have been erased, but attempting to overwrite the reset address of the program itself. 

There is nothing specific to the stm32l412 in it.

Read chapter 3 of the reference manual. I can't read it for you. If there are terms that you don't understand, like alignment, look them up.