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
angeliran
Senior
Posted on September 28, 2015 at 21:08

Ok clive, thx for your help!!!, only work with this 7 steps, with f0/ha1: 

HAL_FLASH_Unlock();

FLASH->CR |= FLASH_CR_PER; /* (1) */

FLASH->AR = 0x0800e400; /* (2) */

FLASH->CR |= FLASH_CR_STRT; /* (3) */

while ((FLASH->SR & FLASH_SR_BSY) != 0) /* (4) */

{/* For robust implementation, add here time-out management */}

if ((FLASH->SR & FLASH_SR_EOP) != 0) /* (5) */

{FLASH->SR |= FLASH_SR_EOP; /* (6)*/}

else

{/* Manage the error cases */}

FLASH->CR &= ~FLASH_CR_PER; /* (7) */

HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, 0x0800e400, 0x5678);

HAL_FLASH_Lock();

uint16_t dato;

  /* Infinite loop */

  /* USER CODE BEGIN WHILE */

  while (1){ 

dato=*((uint16_t *) 0x0800e400);

if (dato==0x5678){

HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_9);}

HAL_Delay(500);}

  /* USER CODE END 3 */

}

I need to do more tests, for example, check if the data continue there, at the moment of excitement, I wanted to share this achievement, hehe...

UPDATE:

It work! doing power cycle, the data is there. The last doubts Clive, did you know why need to do the hole between iROM1 and iROM2? why not only cut at the end with one iROM?

Posted on September 28, 2015 at 22:47

The last doubts Clive, did you know why need to do the hole between iROM1 and iROM2? why not only cut at the end with one iROM?

Indeed why not? As I explained in the STM32F4[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flash%20as%20EEPROM%20%20FW%20Code%20Image%20Collision%20%28avoiding%29&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/AllItems.aspx&currentviews=60]thread you inserted yourself into, the reason you'd want a hole in the F2/F4 parts is because of the non-uniform sector arrangement, and the large/slow blocks being at the end, and the small ideal ones near the front. The F0 is different, as I explained.

The answers aren't always the same, it often depends on the context, and the parts you're using, and what you want to achieve.

I just try to manage the questions as they are thrown to me, even when people post off-topic in a thread.
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 29, 2015 at 23:14

 Ok Clive, the doubt is why we need to make a modification in programming configuration, not by default. So yes, I am grateful that I could finally move forward, as I was stuck. I have readed in the other discusion, your answer to me, thx again.