2015-09-22 01:29 PM
Hi all, I wonder if anyone knows how to read and write to the flash stm32f0, using hal libraries ?. What I hold, it is to store some values in real time, and when you restart the device, are present (such as EEPROM of the pics, and I know what are the differences between flash and EEPROM). Basically I read that in order to write, you have to unlock with:
HAL_FLASH_Unlock (void);Then, you have to delete, either by page, or if all with:FLASH_EraseInitTypeDef erase_pages;erase_pages.PageAddress = 0x08000000;erase_pages.NbPages = 1;erase_pages.TypeErase = TYPEERASE_PAGES;uint32_t * Error;// To write this done BEFOREHAL_FLASH_Unlock ();HAL_FLASHEx_Erase (& erase_pages, error);HAL_FLASH_Lock ();// WRITE ONLY AFTER AN ERASE PAGEHAL_FLASH_Unlock (); HAL_FLASH_Program (TYPEPROGRAM_HALFWORD, 0x08000000, Data_config [0]);HAL_FLASH_Lock ();// READData_config [0] = * ((uint16_t *) 0x08000000); // Read flash memory at address 0x08000000From here, it stays stuck in:HAL_FLASHEx_Erase (& erase_pages, error);I know the flash memory starts at: 0x08000000, and only occupy one page to try to record a value, switch off the device, read, and verify that indeed the recorded data is left.I guess I'm pounding of the code, for that reason, don´t advance, i tell to you that I do not use bootloader.Greetings, and if someone could give me a guide, they are grateful enough.2015-09-22 01:36 PM
And where is this code residing? 0x08000000 doesn't sound like a good place to start testing.
Pick some where that's not sitting on top of your code, and step through it with the debugger, or instrument it with USART output. You shouldn't need to Lock/Unlock between operations. Look for HAL examples under the Cube installation. How big is the FLASH in the part you're using? Start at the back end and move downward.2015-09-22 04:32 PM
Hi clive1, thx for answering.
I have tried in page 60, according to RM, the direction: 0x0800F00c, with the same result. I´ve use stmstudio, to check the variables, but, it hang when go to:HAL_FLASHEx_Erase(&erase_pages, errores);The board is stm32f0 discovery, with stm32f051r8 ic, it has 64 kb of flash.
2015-09-22 05:21 PM
0x0800FC00 - assuming each block is 0x400
2015-09-24 09:36 AM
Yes, 0x0800Fc00, but same result, i've continue check it...
2015-09-25 11:47 AM
There are some case, that it write it, but when i´ll try to isolate, it don't write... i´ll try to do only that part, not in my main program...
UPDATE:No luck...UPDATE2: I tried to upload some pictures, but the server has problems. Basically, I am using the Keil debugger, but does not show me a registry changeFLASH / ARwith the instruction:WRITE_REG (FLASH-> AR, 0x08007000);UPDATE3:in desperation, I wrote directly to the memory address register AR with:WRITE_REG ((* (uint32_t *) 0x40022014), 0x08007000);And again, no luck ... WRITE_REG((*(uint32_t *)0x40022014), 0x08007000);2015-09-26 10:46 AM
Ok, got some fundamental pointer issues there
&FLASH->AR /* The address OF the register, not the content */ 0x40022014 /* The ADDRESS, not the content at the address */ You should perhaps pay attention to errors returned by the routines, and ensure the flash status is cleared of errors before you start. The HAL/F0 really aren't things I'm working with, I've posted FLASH code for SPL/F0 It would look something like/* Unlock the Flash to enable the flash control register access *************/
FLASH_Unlock();
/* Erase the user Flash area */
/* Clear pending flags (if any) */
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
if (FLASH_ErasePage(0x08007000)!= FLASH_COMPLETE)
{
/* Error occurred while sector erase.
User can add here some code to deal with this error */
while (1)
{
}
}
/* Program the user Flash area word by word */
Address = 0x08007000;
while (Address < 0x08007014)
{
if (FLASH_ProgramWord(Address, DATA_32) == FLASH_COMPLETE)
{
Address = Address + 4;
}
else
{
/* Error occurred while writing data in Flash memory.
User can add here some code to deal with this error */
while (1)
{
}
}
}
/* Lock the Flash to disable the flash control register access (recommended
to protect the FLASH memory against possible unwanted operation) *********/
FLASH_Lock();
2015-09-26 05:08 PM
Thanks for answering clive, the FLASH-> AR register, contains the begin memory address, that we want to erase?
So, in keil, in debug mode, i saw, the address of FLASH-> AR register, was: 0x40022014, so, i want to put in that address, 0x08007000, that represent, where i want to erase one page, with enabling bit: SET_BIT(FLASH->CR, FLASH_CR_STRT); or i´m wrong?Sorry for your headaches, jeje.
I´ll check your sugestion, but yes or yes, i need to modify the iROM section?I´ll try to adapt to HAL libraries.2015-09-26 06:22 PM
I'm not familiar with the MACRO's you're using
But I'm pretty sure thatWRITE_REG (FLASH-> AR, 0x08007000);Is NOT the equivalent of FLASH->AR = 0x08007000;Because you're randomly mixing what's a pointer (an address) and what's the value (content at an address).2015-09-27 04:50 PM
Ok Clive, i´ve try your advices, and post results, thx!