Flash Memory Library

This library provides routines for accessing microcontroller's (internal) Flash memory.

Library Routines

Stellaris Specific Routines

ST Specific Routines

Flash_Write

Prototype

long Flash_Write(unsigned long address, unsigned long lData);

Description

This function will program one words into the on-chip flash. Each word in a page of Flash can only be programmed one time between an erase of that page;
Programming a word multiple times will result in an unpredictable value in that word of Flash.

Since the flash is programmed one word at a time, the starting address and byte count must both be multiples of four.
It is up to the caller to verify the programmed contents, if such verification is required.

Parameters
  • address: address of the Flash memory word.
  • lData: data to be written.
Returns
  • 0 - if writing was successful.
  • -1 - if an access violation occurred.
Requires

Nothing.

Example
unsigned long Address = 0xC00;

Flash_Write(Address, 0xAAAAAAAA); // write one word
Notes

None.

Flash_Write_Buffer

Prototype

long Flash_Write_Buffer(unsigned long address, void *pData);

Description

This function will program a sequence of words into the on-chip flash.

Parameters
  • address: address of the Flash memory word.
  • pData: pointer to an array of 32*4 = 128 bytes in length.
Returns
  • 0 - if writing was successful.
  • -1 - if an access violation occurred.
Requires

Nothing.

Example
unsigned long Address = 0xC00;
unsigned long buff[32];

Flash_Write_Buffer(Address, buff);     // write buffer with 32 long words
Notes

This function is available only for MCUs that support buffered writing operation.

Flash_Erase_Block

Prototype

long Flash_Erase_Block(unsigned long address);

Description

This function will erase a 1 kB block of the on-chip Flash. After erasing, the block is filled with 0xFF bytes. Read-only and execute-only blocks cannot be erased.
This function will not return until the block has been erased.

Parameters
  • address: starting address of the Flash memory block.
Returns
  • -1 - if an access violation or erase error occurred.
Requires

Nothing.

Example
unsigned long Address = 0xC00;

Flash_Erase_Block(Address);            // erase block (Address must be 1024 byte aligned)
Notes

Address should be 1024 byte aligned or else unpredictable behavior can occur.

Flash_Set_uSec

Prototype

void Flash_Set_uSec(unsigned long clocks);

Description

This function is used to tell the Flash controller the number of processor clocks per micro-second.

Parameters
  • clocks: the number of processor clocks per micro-second..
Returns

Nothing.

Requires

Nothing.

Example
Flash_Set_uSec(80);  // If 80 MHz is used as a processor clock
Notes

This value must be programmed correctly or the Flash most likely will not program correctly; it has no affect on reading

FLASH_Unlock

Prototype

void FLASH_Unlock();

Description

This routine will unlock the FLASH control register access.

This routine is valid only for ST devices.

Parameters

None.

Returns

Nothing.

Requires

Nothing.

Example
FLASH_Unlock();
Notes

This routine is valid only for ST devices.

FLASH_Unlock_Banks

Prototype

void FLASH_Unlock_Banks();

Description

This routine will unlock the FLASH Program Erase Controller.

This routine is valid only for STM32F10x devices :

  • For STM32F10X_XL devices this function unlocks Bank1 and Bank2.
  • For all other devices it unlocks Bank1 and it is equivalent to FLASH_Unlock_Bank1 function.

Parameters

None.

Returns

Nothing.

Requires

Nothing.

Example
FLASH_Unlock_Banks();
Notes

This routine is valid only for STM32F10x devices.

FLASH_Unlock_Bank1

Prototype

void FLASH_Unlock_Bank1();

Description

This routine will unlock the FLASH Bank1 Program Erase Controller.

This routine is valid only for STM32F10x devices :

  • For STM32F10X_XL devices this function unlocks Bank1.
  • For all other devices it unlocks Bank1 and it is equivalent to FLASH_Unlock function.

Parameters

None.

Returns

Nothing.

Requires

Nothing.

Example
FLASH_Unlock_Bank1();
Notes

This routine is valid only for STM32F10x devices.

FLASH_Unlock_Bank2

Prototype

void FLASH_Unlock_Bank2();

Description

This routine will unlock the FLASH Bank2 Program Erase Controller.

This routine is valid only for STM32F10x devices :

  • For STM32F10X_XL devices this function unlocks Bank2.
  • For all other devices it unlocks Bank2 and it is equivalent to FLASH_Unlock function.

Parameters

None.

Returns

Nothing.

Requires

Nothing.

Example
FLASH_Unlock_Bank2();
Notes

This routine is valid only for STM32F10x devices.

FLASH_lock

Prototype

void FLASH_lock();

Description

This routine will lock the FLASH control register access.

This routine is valid only for ST devices.

Parameters

None.

Returns

Nothing.

Requires

Nothing.

Example
FLASH_lock();
Notes

This routine is valid only for ST devices.

FLASH_Lock_Banks

Prototype

void FLASH_Lock_Banks();

Description

This routine will Lock the FLASH Program Erase Controller.

This routine is valid only for STM32F10x devices :

  • For STM32F10X_XL devices this function lock Bank1 and Bank2.
  • For all other devices it locks Bank1 and it is equivalent to FLASH_Lock_Bank1 function.

Parameters

None.

Returns

Nothing.

Requires

Nothing.

Example
FLASH_Lock_Banks();
Notes

This routine is valid only for STM32F10x devices.

FLASH_Lock_Bank1

Prototype

void FLASH_Lock_Bank1();

Description

This routine will lock the FLASH Bank1 Program Erase Controller.

This routine is valid only for STM32F10x devices :

  • For STM32F10X_XL devices this function locks Bank1.
  • For all other devices it locks Bank1 and it is equivalent to FLASH_Lock function.

Parameters

None.

Returns

Nothing.

Requires

Nothing.

Example
FLASH_Lock_Bank1();
Notes

This routine is valid only for STM32F10x devices.

FLASH_Lock_Bank2

Prototype

void FLASH_Lock_Bank2();

Description

This routine will lock the FLASH Bank2 Program Erase Controller.

This routine is valid only for STM32F10x devices :

  • For STM32F10X_XL devices this function locks Bank2.
  • For all other devices it locks Bank2 and it is equivalent to FLASH_Lock function.

Parameters

None.

Returns

Nothing.

Requires

Nothing.

Example
FLASH_Lock_Bank2();
Notes

This routine is valid only for STM32F10x devices.

FLASH_EraseSector

Prototype

unsigned long FLASH_EraseSector(unsigned long FLASH_Sector);

Description

This routine will erases a specified FLASH Sector.

This routine is valid only for ST devices.

Parameters
  • FLASH_Sector: Specifies the sector number to be erased. Valid values :
    Value Description
    _FLASH_SECTOR_0 FLASH sector 0.
    _FLASH_SECTOR_1 FLASH sector 1.
    _FLASH_SECTOR_2 FLASH sector 2.
    _FLASH_SECTOR_3 FLASH sector 3.
    _FLASH_SECTOR_4 FLASH sector 4.
    _FLASH_SECTOR_5 FLASH sector 5.
    _FLASH_SECTOR_6 FLASH sector 6.
    _FLASH_SECTOR_7 FLASH sector 7.
    _FLASH_SECTOR_8 FLASH sector 8.
    _FLASH_SECTOR_9 FLASH sector 9.
    _FLASH_SECTOR_10 FLASH sector 10.
    _FLASH_SECTOR_11 FLASH sector 11.
Returns
  • FLASH memory status. The returned value can be:
    • 0 : FLASH_COMPLETE,
    • 1 : FLASH_BUSY,
    • 2 : FLASH_ERROR_PROGRAM,
    • 3 : FLASH_ERROR_WRP
    • 4 : FLASH_ERROR_OPERATION.
Requires

Nothing.

Example
// Delete FLASH sector 1
status = FLASH_EraseSector(FLASH_Sector_1);
Notes

This routine is valid only for ST devices.

FLASH_ErasePage

Prototype

unsigned long FLASH_ErasePage(unsigned long Address);

Description

This routine will erases a specified FLASH memory page.

This routine is valid only for STM32F10x devices.

Parameters
  • Address: Specifies the page address to be erased.
Returns
  • FLASH memory status. The returned value can be:
    • 0 : FLASH_COMPLETE,
    • 1 : FLASH_BUSY,
    • 2 : FLASH_ERROR_PROGRAM,
    • 3 : FLASH_ERROR_WRP
    • 4 : FLASH_ERROR_OPERATION.
Requires

Nothing.

Example
status = FLASH_ErasePage(address);
Notes

This routine is valid only for STM32F10x devices.

FLASH_EraseAllPages

Prototype

unsigned long FLASH_EraseAllPages();

Description

This routine erases all FLASH memory pages.

This routine is valid only for STM32F10x devices.

Parameters

None.

Returns
  • FLASH memory status. The returned value can be:
    • 0 : FLASH_COMPLETE,
    • 1 : FLASH_BUSY,
    • 2 : FLASH_ERROR_PROGRAM,
    • 3 : FLASH_ERROR_WRP
    • 4 : FLASH_ERROR_OPERATION.
Requires

Nothing.

Example
status = FLASH_EraseAllPages();
Notes

This routine is valid only for STM32F10x devices.

FLASH_EraseAllBank1Pages

Prototype

unsigned long FLASH_EraseAllBank1Pages();

Description

This routine erases all Bank1 FLASH memory pagess.

This routine is valid only for STM32F10x devices. :

  • For STM32F10X_XL devices this function erases all Bank1 pages.
  • For all other devices it erases all Bank1 pages and it is equivalent to FLASH_EraseAllPages function.

Parameters

None.

Returns
  • FLASH memory status. The returned value can be:
    • 0 : FLASH_COMPLETE,
    • 1 : FLASH_BUSY,
    • 2 : FLASH_ERROR_PROGRAM,
    • 3 : FLASH_ERROR_WRP
    • 4 : FLASH_ERROR_OPERATION.
Requires

Nothing.

Example
status = FLASH_EraseAllBank1Pages();
Notes

This routine is valid only for STM32F10x devices.

FLASH_EraseAllBank2Pages

Prototype

unsigned long FLASH_EraseAllBank2Pages();

Description

This routine erases all Bank2 FLASH memory pagess.

This routine is valid only for STM32F10x devices. :

  • For STM32F10X_XL devices this function erases all Bank2 pages.
  • For all other devices it erases all Bank2 pages and it is equivalent to FLASH_EraseAllPages function.

Parameters

None.

Returns
  • FLASH memory status. The returned value can be:
    • 0 : FLASH_COMPLETE,
    • 1 : FLASH_BUSY,
    • 2 : FLASH_ERROR_PROGRAM,
    • 3 : FLASH_ERROR_WRP
    • 4 : FLASH_ERROR_OPERATION.
Requires

Nothing.

Example
status = FLASH_EraseAllBank2Pages();
Notes

This routine is valid only for STM32F10x devices.

FLASH_EraseAllSectors

Prototype

unsigned long FLASH_EraseAllSectors();

Description

This routine will erase all FLASH Sectors.

This routine is valid only for ST devices.

Parameters

None.

Returns
  • FLASH memory status. The returned value can be:
    • 0 : FLASH_COMPLETE,
    • 1 : FLASH_BUSY,
    • 2 : FLASH_ERROR_PROGRAM,
    • 3 : FLASH_ERROR_WRP
    • 4 : FLASH_ERROR_OPERATION.
Requires

Nothing.

Example
// Delete all FLASH sectors
status = FLASH_EraseAllSectors();
Notes

This routine is valid only for ST devices.

FLASH_Write_DoubleWord

Prototype

unsigned long FLASH_Write_DoubleWord(unsigned long Address, uint64 ldata);

Description

Programs a double word (64-bit) at a specified address.

This routine is valid only for ST devices.

Parameters
  • Address:: specifies the address to be programmed.
  • lData: specifies the data to be programmed.
Returns
  • FLASH memory status. The returned value can be:
    • 0 : FLASH_COMPLETE,
    • 1 : FLASH_BUSY,
    • 2 : FLASH_ERROR_PROGRAM,
    • 3 : FLASH_ERROR_WRP
    • 4 : FLASH_ERROR_OPERATION.
Requires

Nothing.

Example
status = FLASH_Write_DoubleWord(0x08008000, data_);
Notes

This function must be used when the device voltage range is from 2.7V to 3.6V and an External Vpp is present.

This routine is valid only for ST devices.

FLASH_Write_Word

Prototype

unsigned long FLASH_Write_Word(unsigned long Address, unsigned long lData);

Description

Programs a word (32-bit) at a specified address.

This routine is valid only for ST devices.

Parameters
  • Address:: specifies the address to be programmed. This parameter can be any address in Program memory zone or in OTP zone
  • lData: specifies the data to be programmed.
Returns
  • FLASH memory status. The returned value can be:
    • 0 : FLASH_COMPLETE,
    • 1 : FLASH_BUSY,
    • 2 : FLASH_ERROR_PROGRAM,
    • 3 : FLASH_ERROR_WRP
    • 4 : FLASH_ERROR_OPERATION.
Requires

Nothing.

Example
status = FLASH_Write_Word(0x08008000, data_);
Notes

This function must be used when the device voltage range is from 2.7V to 3.6V and an External Vpp is present.

This routine is valid only for ST devices.

FLASH_Write_HalfWord

Prototype

unsigned long FLASH_Write_HalfWord(unsigned long Address, unsigned long lData);

Description

Programs a half word (16-bit) at a specified address.

This routine is valid only for ST devices.

Parameters
  • Address:: specifies the address to be programmed. This parameter can be any address in Program memory zone or in OTP zone
  • lData: specifies the data to be programmed.
Returns
  • FLASH memory status. The returned value can be:
    • 0 : FLASH_COMPLETE,
    • 1 : FLASH_BUSY,
    • 2 : FLASH_ERROR_PROGRAM,
    • 3 : FLASH_ERROR_WRP
    • 4 : FLASH_ERROR_OPERATION.
Requires

Nothing.

Example
status = FLASH_Write_HalfWord(0x08008000, data_);
Notes

This function must be used when the device voltage range is from 2.1V to 3.6V.

This routine is valid only for ST devices.

FLASH_Write_Byte

Prototype

unsigned long FLASH_Write_Byte(unsigned long Address, unsigned char lData);

Description

Programs a byte (8-bit) at a specified address.

This routine is valid only for ST devices.

Parameters
  • Address:: specifies the address to be programmed. This parameter can be any address in Program memory zone or in OTP zone
  • lData: specifies the data to be programmed.
Returns
  • FLASH memory status. The returned value can be:
    • 0 : FLASH_COMPLETE,
    • 1 : FLASH_BUSY,
    • 2 : FLASH_ERROR_PROGRAM,
    • 3 : FLASH_ERROR_WRP
    • 4 : FLASH_ERROR_OPERATION.
Requires

Nothing.

Example
status = FLASH_Write_Byte(0x08008000, data_);
Notes

This function can be used within all the device supply voltage ranges.

This routine is valid only for ST devices.

FLASH_AddressToSector

Prototype

long FLASH_AddressToSector(unsigned long Address);

Description

This routine returns the FLASH sector number of the specifed address.

This routine is valid only for STM32F2XX and STM32F4XX devices.

Parameters
  • Address: specified address.
Returns
  • FLASH memory sector number. The returned value can be:
    • 0x0000 : _FLASH_SECTOR_0,
    • 0x0008 : _FLASH_SECTOR_1,
    • 0x0010 : _FLASH_SECTOR_2,
    • 0x0018 : _FLASH_SECTOR_3
    • 0x0020 : _FLASH_SECTOR_4.
    • 0x0028 : _FLASH_SECTOR_5,
    • 0x0030 : _FLASH_SECTOR_6,
    • 0x0038 : _FLASH_SECTOR_7,
    • 0x0040 : _FLASH_SECTOR_8
    • 0x0048 : _FLASH_SECTOR_9.
    • 0x0050 : _FLASH_SECTOR_10,
    • 0x0058 : _FLASH_SECTOR_11,
Requires

Nothing.

Example
status = FLASH_AddressToSector(0x08008000);
Notes

This routine is valid only for STM32F2XX and STM32F4XX devices.

FLASH_SectorSize

Prototype

long FLASH_SectorSize(unsigned long flash_sector);

Description

This routine returns the size of the specified FLASH sector number.

This routine is valid only for STM32F2XX and STM32F4XX devices.

Parameters
  • flash_sector: specified FLASH sector.
  • FLASH_Sector: Specifies the sector number to be erased. Valid values :
    Value Description
    _FLASH_SECTOR_0 FLASH sector 0.
    _FLASH_SECTOR_1 FLASH sector 1.
    _FLASH_SECTOR_2 FLASH sector 2.
    _FLASH_SECTOR_3 FLASH sector 3.
    _FLASH_SECTOR_4 FLASH sector 4.
    _FLASH_SECTOR_5 FLASH sector 5.
    _FLASH_SECTOR_6 FLASH sector 6.
    _FLASH_SECTOR_7 FLASH sector 7.
    _FLASH_SECTOR_8 FLASH sector 8.
    _FLASH_SECTOR_9 FLASH sector 9.
    _FLASH_SECTOR_10 FLASH sector 10.
    _FLASH_SECTOR_11 FLASH sector 11.
Returns
  • FLASH memory sector size. The returned value can be:
    • 16*1024: for FLASH sectors from 0 to 3,
    • 64*1024: for FLASH sector 4,
    • 128*1024: for the rest of the FLASH sectors,
    • -1 : if an error occured.
Requires

Nothing.

Example
status = FLASH_SectorSize(_FLASH_SECTOR_0);
Notes

This routine is valid only for STM32F2XX and STM32F4XX devices.

Library Example

Stellaris

unsigned long buff[32];
unsigned long i;
unsigned long Address = 0xC00;
unsigned long* ptr;

void main() {
  GPIO_Digital_Output(&GPIO_PORTH, _GPIO_PINMASK_ALL);    // digital output GPIO_PORTH
  GPIO_Digital_Output(&GPIO_PORTJ, _GPIO_PINMASK_ALL);    // digital output GPIO_PORTJ

  for(i=0; i<32; i++)
    buff[i]=i+(i<<16);

  Flash_Erase_Block(Address);            // erase block (Address must be 1024 byte aligned)
  Flash_Write_Buffer(Address, buff);     // write buffer with 32 long words

  ptr = (unsigned long*)Address;
  for(i=0; i<32; i++) {
    GPIO_PORTH_DATA = *ptr;
    GPIO_PORTJ_DATA = *ptr >> 16;
    ptr++;
    Delay_ms(250);
  }

  Delay_ms(1000);
  
  Flash_Write(Address + 32*4, 0xAAAAAAAA); // write one word

  ptr = Address + 32*4;
  GPIO_PORTH_DATA = *ptr;
  GPIO_PORTJ_DATA = *ptr >> 16;
}

STM32

#include <built_in.h>

unsigned long buff[32];
unsigned long i, tmp;
unsigned long Address = 0x08008000;
unsigned long Address2 = 0x08009000;
unsigned long* ptr;

void main() {
  GPIO_Digital_Output(&GPIOD_BASE, _GPIO_PINMASK_ALL);    // digital output PORTD
  GPIO_Digital_Output(&GPIOE_BASE, _GPIO_PINMASK_ALL);    // digital output PORTE

  for(i=0; i<32; i++)
    buff[i]=i+(i<<16);

  FLASH_ErasePage(Address);            // erase block (Address must be 2048 byte aligned)
  for(i=0; i<32; i++)
    FLASH_Write_Word(Address + i*4, buff[i]);     // write buffer with 32 long words

  ptr = (unsigned long*)Address;
  for(i=0; i<32; i++) {
    tmp = *ptr;
    GPIOD_ODR = HiWord(tmp);
    GPIOE_ODR = LoWord(tmp) << 8;
    ptr++;
    Delay_ms(50);
  }

  Delay_ms(1000);
  FLASH_ErasePage(Address2);            // erase block (Address must be 2048 byte aligned)
  FLASH_Write_HalfWord(Address2, 0xAAAA); // write one word
  GPIOD_ODR = 0;
  ptr = (unsigned long*)Address2;
  i = *ptr;
  GPIOE_ODR = (LoWord(i)) << 8;
  
}
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code