Nucleo L152RE Flash issue

07 Mar 2015

I'm trying to write some data on internal Flash of L152RE but after I reset the microcontroller either it hangs or whole flash is erased. It simple do nothing after reset.

Here is the sample code:

include the mbed library with this snippet


#include "mbed.h"

Serial pc(USBTX, USBRX);



int main()
{
				
	    uint32_t a[32]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32};
		
		uint32_t *rawdata=&a[0];
		
		HAL_StatusTypeDef status;
		
		FLASH_EraseInitTypeDef erase_pages;
		erase_pages.PageAddress=0x0807ff00;
		erase_pages.NbPages=1;
		erase_pages.TypeErase=TYPEERASE_PAGES;
		uint32_t *error; 
		
		HAL_FLASH_Unlock(); 
	    HAL_FLASHEx_Erase(&erase_pages, error);
		HAL_FLASH_Lock();
			
		
		HAL_FLASH_Unlock(); 
	    status=HAL_FLASHEx_HalfPageProgram(0x0807ff00,rawdata);
		HAL_FLASH_Lock(); 
			
		
		uint32_t address=0x0807ff00;
		for(int i=0;i<32;i++)
		{
		  uint32_t tmp = 0;
          tmp = *(__IO uint32_t*)address;
		  pc.printf("value=%d\n\r",tmp);
		  address=address+4;
		}
	
    
}

Kindly somebody tell me what's the error?

Thank you.

14 Sep 2015

Hi, I'm also trying to write some data on internal Flash of L152RE But i couldn't find out any library in mbed.so i'm stucked very badly, I'm new to this envronment.could you please help me?

Thank you.

15 Jun 2016

Mbed provides you the abstraction for "stm32l1xx_flash.h"

include the mbed library with this snippet

HAL_StatusTypeDef writeEEPROMByte(uint32_t address, uint8_t data)
 {
    HAL_StatusTypeDef  status;
    address = address + 0x08080000;
    HAL_FLASHEx_DATAEEPROM_Unlock();  //Unprotect the EEPROM to allow writing
    status = HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_BYTE, address, data);
    HAL_FLASHEx_DATAEEPROM_Lock();  // Reprotect the EEPROM
    return status;
}

I have tried it on uVision v5 and it's working fine, I'm not sure about the online compiler. For complete documentation: http://developer.mbed.org/users/mbed_official/code/mbed-src/docs/7f9d41292847/stm32l1xx__hal__flash__ex_8c.html