cancel
Showing results for 
Search instead for 
Did you mean: 

hi guys, I've been trying to write to internal flash for the last two weeks, and unfortunately, I didn't manage to do so. I worked with the function I have from the library and read in the manual.

Gbasi.1
Associate II

tst= 0x121212121212

HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,0x8000000,tst);

the address i took from the manual as well.

0693W000001cO3YQAU.png

i cant understand whats going on, please help me.

thanks a lot

18 REPLIES 18
Gbasi.1
Associate II

it is important to mention that the micro is stm32l412kb, which cant work with most of the solutions i found online

berendi
Principal

I can't guess the part number of your microcontroller. OK now we have it.

If you had actually read the reference manual, you would already know that the flash should be unlocked and erased before writing, and that rewriting the start vectors of your program is rarely a good idea.

Gbasi.1
Associate II

it is important to mention that the micro is stm32l412kb, which cant work with most of the solutions i found online

EraseInitStruct.TypeErase=FLASH_TYPEERASE_PAGES;

EraseInitStruct.Banks=FLASH_BANK_1;

EraseInitStruct.Page=1;

EraseInitStruct.NbPages=1;

HAL_FLASH_Unlock();

HAL_FLASHEx_Erase(&EraseInitStruct, &pageError);

tst= 0x121212121212

HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,0x8000000,tst);

HAL_FLASH_Lock();

berendi
Principal

HAL functions are notoriously difficult to work with. Unless you use them exactly as they are used in the example projects, there is no guarantee that they would do what you think they should do, and if they don't, the documentation is not really helpful in troubleshooting. You'd get results more reliably by using the register inerface directly, which is documented in great detail in the reference manual.

1. Check whether the flash is locked. Examine the FLASH_CR_LOCK bit in the FLASH->CR register.

2. If the flash is locked, unlock it by writing the appropriate values in the FLASH->KEYR register, as described in chapter 3.3.5 of the reference manual.

3. Check the FLASH->CR register again to see whether the unlock operation was succesful.

4. Check the FLASH->SR register before and after each following step for possible errors. Clear the status register by writing back its value & 0xFFFF after you have identified and remedied the cause.

5. Check the memory map of your program to find a flash page which is not already occupied by code or data.

6. Find the address range corresponding to the page number using the table above.

7. Erase the flash page as described in 3.3.6 Flash main memory erase sequences. Check the status register.

8. Write data to the flash as descibed in 3.3.7 Flash main memory programming sequences. You may have to flush data by issuing a __DMB(); instruction after each write. Check the status.

9. Lock the flash.

If you are stuck, show the code (using the </> symbol below), explain what it should do, what do you get instead, and post the values of the flash controller registers in order to get a meaningful answer.

TDK
Guru

You're erasing page 1, which isn't at 0x08000000. Page 0 is.

If you feel a post has answered your question, please click "Accept as Solution".
Gbasi.1
Associate II

hi guys thanks a lot for the help,

@TDK​ I did it on page 0 as well' didnt work out.

@berendi​ i did what u told me to, it is exactly what "HAL_FLASH_Program" is doing step by step.

so i cameback to using that.

Address=addr; //=0x8000000

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

FLASH_EraseInitTypeDef EraseInitStruct;

uint64_t data;

uint32_t pageError;

uint8_t buff[8];

uint64_t tst;

EraseInitStruct.TypeErase=FLASH_TYPEERASE_PAGES;

EraseInitStruct.Banks=FLASH_BANK_1;

EraseInitStruct.Page=0;

EraseInitStruct.NbPages=1;

HAL_FLASH_Unlock();

HAL_FLASHEx_Erase(&EraseInitStruct, &pageError);

tst= 0x1212121212112;

__HAL_SYSCFG_REMAPMEMORY_SYSTEMFLASH();

HAL_UART_Transmit(&huart1, buff, 8,1000);

HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,addr,tst);

HAL_FLASH_Lock();

Gbasi.1
Associate II

@berendi​ im trying to write values and to read them later.

Although this is not usually the case for flash, as you said in a previous post.

As most people I asked online explained, the flash on this card is as accessible as RAM, and should be read as follows:

HAL_FLASH_unLock();

data=*(uint64_t *)addr;

for(int i=0;i<8;i++)

{

buff[7-i]=data&0xff;

data=data>>8;

}

Yes, reads like any other memory within the address space.

Stop using the 0x08000000 address, use something at the other end of the memory space, and not occupied by your executing code.

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

i tried that as well and it didnt work out.

@Community member​