cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f0 help with flash, to read and write + hal libraries

angeliran
Senior
Posted on September 22, 2015 at 22:29

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 BEFORE

HAL_FLASH_Unlock ();

HAL_FLASHEx_Erase (& erase_pages, error);

HAL_FLASH_Lock ();

// WRITE ONLY AFTER AN ERASE PAGE

HAL_FLASH_Unlock ();

  HAL_FLASH_Program (TYPEPROGRAM_HALFWORD, 0x08000000, Data_config [0]);

HAL_FLASH_Lock ();

// READ

Data_config [0] = * ((uint16_t *) 0x08000000); // Read flash memory at address 0x08000000

From 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.
12 REPLIES 12
Posted on September 22, 2015 at 22:36

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
angeliran
Senior
Posted on September 23, 2015 at 01:32

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.

Posted on September 23, 2015 at 02:21

 0x0800FC00 - assuming each block is 0x400

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
angeliran
Senior
Posted on September 24, 2015 at 18:36

Yes, 0x0800Fc00, but same result, i've continue check it...

angeliran
Senior
Posted on September 25, 2015 at 20:47

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 change

FLASH / AR

with 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);

Posted on September 26, 2015 at 19:46

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();

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
angeliran
Senior
Posted on September 27, 2015 at 02:08

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.

Posted on September 27, 2015 at 03:22

I'm not familiar with the MACRO's you're using

But I'm pretty sure that

WRITE_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).

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
angeliran
Senior
Posted on September 28, 2015 at 01:50

Ok Clive, i´ve try your advices, and post results, thx!