Flash Memory Library
This library provides routines for accessing microcontroller's (internal) Flash memory.
Library Routines
Stellaris Specific Routines
ST Specific Routines
- FLASH_Unlock
- FLASH_Unlock_Banks
- FLASH_Unlock_Bank1
- FLASH_Unlock_Bank2
- FLASH_Lock
- FLASH_Lock_Banks
- FLASH_Lock_Bank1
- FLASH_Lock_Bank2
- FLASH_EraseSector
- FLASH_EraseAllSectors
- FLASH_ErasePage
- FLASH_EraseAllPages
- FLASH_EraseAllBank1Pages
- FLASH_EraseAllBank2Pages
- FLASH_Write_DoubleWord
- FLASH_Write_Word
- FLASH_Write_HalfWord
- FLASH_Write_Byte
- FLASH_AddressToSector
- FLASH_SectorSize
Kinetis Specific Routines
- Flash_GetSecurityState
- Flash_Init
- Flash_ProgramLongword
- Flash_ProgramPhrase
- Flash_ProgramSection
- Flash_EraseSector
- Flash_EraseBlock
- Flash_EraseAllBlocks
- Flash_Read1sAllBlocks
- Flash_Read1sBlock
- Flash_Read1sSection
- Flash_VerifyBackdoorAccessKey
- Flash_ProgramCheck
- Flash_ReadResource
- Flash_ReadOnce
- Flash_ProgramOnce
MSP432 Specific Routines
- FLASH_AddressToSector
- FLASH_MemoryInfo
- FLASH_UnprotectSectors
- FLASH_ProtectSectors
- FLASH_EraseSector
- FLASH_EraseBank0
- FLASH_EraseBank1
- FLASH_MassErase
- FLASH_ProgramMemory
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;
Since the flash is programmed one word at a time, the starting address and byte count must both be multiples of four.
|
Parameters |
|
Returns |
|
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 |
|
Returns |
|
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.
|
Parameters |
|
Returns |
|
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 |
|
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 :
|
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 :
|
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 :
|
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 :
|
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 :
|
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 :
|
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 |
|
||||||||||||||||||||||||||
Returns |
|
||||||||||||||||||||||||||
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); // for STM32 M4 devicesunsigned long FLASH_Erase_Page(unsigned long Address, unsigned long bank); |
||||||
---|---|---|---|---|---|---|---|
Description |
This routine will erases a specified FLASH memory page. |
||||||
Parameters |
|
||||||
Returns |
|
||||||
Requires |
Nothing. |
||||||
Example |
status = FLASH_ErasePage(address); // for STM32 M4 devices status = FLASH_ErasePage(address, _FLASH_BANK1); |
||||||
Notes |
This routine is valid only for ST 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 |
|
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. :
|
Parameters |
None. |
Returns |
|
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. :
|
Parameters |
None. |
Returns |
|
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 |
|
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 |
|
Returns |
|
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 |
|
Returns |
|
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 |
|
Returns |
|
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 |
|
Returns |
|
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 |
|
Returns |
|
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 |
|
||||||||||||||||||||||||||
Returns |
|
||||||||||||||||||||||||||
Requires |
Nothing. |
||||||||||||||||||||||||||
Example |
status = FLASH_SectorSize(_FLASH_SECTOR_0); |
||||||||||||||||||||||||||
Notes |
This routine is valid only for STM32F2XX and STM32F4XX devices. |
Flash_GetSecurityState
Prototype |
unsigned char Flash_GetSecurityState(); |
---|---|
Description |
This function retrieves the current Flash security status, including the security enabling state and the backdoor key enabling state. |
Parameters |
None. |
Returns |
Flash security status. The returned value can be :
|
Requires |
Nothing. |
Example |
status = Flash_GetSecurityState(); |
Notes |
This routine is valid only for Kinetis devices. |
Flash_Init
Prototype |
unsigned char Flash_Init(); |
---|---|
Description |
This function initializes program Flash memory. |
Parameters |
None. |
Returns |
Nothing. |
Requires |
Nothing. |
Example |
Flash_Init(); |
Notes |
This routine is valid only for Kinetis devices. |
Flash_ProgramLongword
Prototype |
unsigned char Flash_ProgramLongword(unsigned long address, unsigned long dataToWrite); |
---|---|
Description |
Function programs four previously-erased bytes in the program flash memory using an embedded algorithm. |
Parameters |
|
Returns |
|
Requires |
Nothing. |
Example |
|
Notes |
This routine is valid only for Kinetis K2x devices. |
Flash_ProgramPhrase
Prototype |
unsigned char Flash_ProgramPhrase(unsigned long address, unsigned char *dataToWrite); |
---|---|
Description |
Function programs 8 previously-erased bytes in the program flash memory using an embedded algorithm. |
Parameters |
|
Returns |
|
Requires |
Nothing. |
Example |
|
Notes |
This routine is valid only for Kinetis K6x devices. |
Flash_ProgramSection
Prototype |
unsigned char Flash_ProgramSection(unsigned long address, unsigned long *dataToWrite, unsigned int count); |
---|---|
Description |
Function programs the data found in the section program buffer to previously erased locations in the flash memory using an embedded algorithm. |
Parameters |
|
Returns |
|
Requires |
Nothing. |
Example |
|
Notes |
|
Flash_EraseSector
Prototype |
unsigned char Flash_EraseSector(unsigned long address); |
---|---|
Description |
Function erases program Flash memory sector. |
Parameters |
|
Returns |
|
Requires |
Nothing. |
Example |
|
Notes |
|
Flash_EraseBlock
Prototype |
unsigned char Flash_EraseBlock(unsigned long address); |
---|---|
Description |
Function erases all addresses in a single program flash or dataflash block. |
Parameters |
|
Returns |
|
Requires |
Nothing. |
Example |
|
Notes |
|
Flash_EraseAllBlocks
Prototype |
unsigned char Flash_EraseAllBlocks(); |
---|---|
Description |
Function operation erases whole Flash memory, initializes the FlexRAM, verifies all memory contents, and releases MCU security. |
Parameters |
None. |
Returns |
|
Requires |
Nothing. |
Example |
|
Notes |
|
Flash_Read1sAllBlocks
Prototype |
unsigned char Flash_Read1sAllBlocks(unsigned char margin); |
||||||||
---|---|---|---|---|---|---|---|---|---|
Description |
Function checks if the program flash blocks, data flash blocks, EEPROM backup records, and data flash IFR have been erased to the specified read margin level, if applicable, and releases security if the readout passes, i.e. all data reads as '1'. |
||||||||
Parameters |
|
||||||||
Returns |
|
||||||||
Requires |
Nothing. |
||||||||
Example |
|
||||||||
Notes |
|
Flash_Read1sBlock
Prototype |
unsigned char Flash_Read1sBlock(unsigned long address, unsigned char margin); |
||||||||
---|---|---|---|---|---|---|---|---|---|
Description |
Function checks to see if an entire program flash block has been erased to the specified margin level. |
||||||||
Parameters |
|
||||||||
Returns |
|
||||||||
Requires |
Nothing. |
||||||||
Example |
|
||||||||
Notes |
|
Flash_Read1sSection
Prototype |
unsigned char Flash_Read1sBlock(unsigned long address, unsigned char margin); |
||||||||
---|---|---|---|---|---|---|---|---|---|
Description |
Function checks if a section of program flash or data flash memory is erased to the specified read margin level. |
||||||||
Parameters |
|
||||||||
Returns |
|
||||||||
Requires |
Nothing. |
||||||||
Example |
|
||||||||
Notes |
|
Flash_VerifyBackdoorAccessKey
Prototype |
unsigned char Flash_VerifyBackdoorAccessKey(unsigned char *keys); |
---|---|
Description |
The Verify Backdoor Access Key command releases security if user-supplied keys in the FCCOB match those stored in the Backdoor Comparison Key bytes of the Flash Configuration Field. |
Parameters |
|
Returns |
|
Requires |
Nothing. |
Example |
|
Notes |
|
Flash_ProgramCheck
Prototype |
unsigned char Flash_ProgramCheck(unsigned long address, unsigned char margin, unsigned char* dataToCheck); |
---|---|
Description |
Function tests a previously programmed program flash or data flash longword to see if it reads correctly at the specified margin level. |
Parameters |
|
Returns |
|
Requires |
Nothing. |
Example |
|
Notes |
|
Flash_ReadResource
Prototype |
unsigned char Flash_ReadResource(unsigned long address, unsigned long *readData, unsigned char resourceSelectCode); |
||||||
---|---|---|---|---|---|---|---|
Description |
Function reads data from special-purpose memory resources located within the flash memory module. |
||||||
Parameters |
|
||||||
Returns |
|
||||||
Requires |
Nothing. |
||||||
Example |
|
||||||
Notes |
|
Flash_ReadOnce
Prototype |
unsigned char Flash_ReadOnce(unsigned long recordIndex, unsigned long *readData); |
---|---|
Description |
Function reads a reserved 96-byte field located in the program flash 0 IFR. |
Parameters |
|
Returns |
|
Requires |
Nothing. |
Example |
|
Notes |
|
Flash_ProgramOnce
Prototype |
unsigned char Flash_ProgramOnce(unsigned long recordIndex, unsigned long *readData); |
---|---|
Description |
Function writes to a reserved 96-byte field in the program flash 0 IFR. |
Parameters |
|
Returns |
|
Requires |
Nothing. |
Example |
|
Notes |
|
FLASH_AddressToSector
Prototype |
unsigned char FLASH_AddressToSector(unsigned long address); |
---|---|
Description |
This function gets sector number from the given Flash address. |
Parameters |
|
Returns |
|
Requires |
Nothing. |
Example |
|
Notes |
Flash can be in sizes of 256kB and 128kB. There are always two banks. Sector values from 0..31 or 0..15 bank from 0..1. |
FLASH_MemoryInfo
Prototype |
unsigned char FLASH_MemoryInfo(unsigned long address, unsigned long *sector, unsigned long *bank); |
---|---|
Description |
This function gets memory information (sector, bank) from the given Flash address. |
Parameters |
|
Returns |
|
Requires |
Nothing. |
Example |
|
Notes |
None. |
FLASH_UnprotectSectors
Prototype |
unsigned char FLASH_UnprotectSectors(unsigned long bank, unsigned long sectorMask); |
||||||
---|---|---|---|---|---|---|---|
Description |
This function will unprotect sectors for erase/write. |
||||||
Parameters |
|
||||||
Returns |
|
||||||
Requires |
Nothing. |
||||||
Example |
|
||||||
Notes |
None. |
FLASH_ProtectSectors
Prototype |
unsigned char FLASH_ProtectSectors(unsigned long bank, unsigned long sectorMask); |
||||||
---|---|---|---|---|---|---|---|
Description |
This function will protect sectors for erase/write. |
||||||
Parameters |
|
||||||
Returns |
|
||||||
Requires |
Nothing. |
||||||
Example |
|
||||||
Notes |
None. |
FLASH_EraseSector
Prototype |
unsigned char FLASH_EraseSector(unsigned long address); |
---|---|
Description |
This function will erase one sector from the given address in the Flash memory. |
Parameters |
|
Returns |
|
Requires |
Nothing. |
Example |
|
Notes |
None. |
FLASH_EraseBank0
Prototype |
unsigned char FLASH_EraseBank0(unsigned char sectorCount); |
---|---|
Description |
This function will erase sectors located in the Bank0 of the Flash memory. |
Parameters |
|
Returns |
|
Requires |
Nothing. |
Example |
|
Notes |
None. |
FLASH_EraseBank1
Prototype |
unsigned char FLASH_EraseBank1(unsigned char sectorCount); |
---|---|
Description |
This function will erase sectors located in the Bank1 of the Flash memory. |
Parameters |
|
Returns |
|
Requires |
Nothing. |
Example |
|
Notes |
None. |
FLASH_MassErase
Prototype |
unsigned char FLASH_MassErase(); |
---|---|
Description |
This function will erase all unprotected sectors. |
Parameters |
None. |
Returns |
|
Requires |
Nothing. |
Example |
|
Notes |
None. |
FLASH_ProgramMemory
Prototype |
unsigned char FLASH_ProgramMemory(unsigned long *source, unsigned long *destination, unsigned long _length); |
---|---|
Description |
This function will program given amount of bytes to the Flash memory. |
Parameters |
|
Returns |
|
Requires |
Nothing. |
Example |
|
Notes |
User must call first FLASH_UnprotectSectors. funstion to unlock sectors for write operation. Also, destination memory must be erased, all bits at logical '1'.. |
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; }
What do you think about this topic ? Send us feedback!