Flash Memory Library

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

Library Routines

Stellaris Specific Routines

ST Specific Routines

Kinetis Specific Routines

MSP432 Specific Routines

Flash_Write

Prototype

function Flash_Write(address, lData : dword) : dword;

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
Address := 0xC00;                  // erase block (Address must be 1024 byte aligned)

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

None.

Flash_Write_Buffer

Prototype

function Flash_Write_Buffer(address : dword; pData : ^byte) : dword;

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
var Address : dword;
var buff : array[32] of dword;

Address := 0xC00;
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

function Flash_Erase_Block(address: dword) : dword;

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
Address := 0xC00;                  // erase block (Address must be 1024 byte aligned)

Flash_Erase_Block(Address);
Notes

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

Flash_Set_uSec

Prototype

procedure Flash_Set_uSec(clocks : dword);

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

procedure 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

procedure 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

procedure 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

procedure 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

procedure 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

procedure 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

procedure 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

procedure 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

function FLASH_EraseSector(FLASH_Sector : dword) : dword;

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

function FLASH_ErasePage(Address : dword) : dword;

// for STM32 M4 devices

function FLASH_Erase_Page(Address : dword; bank : dword) : dword;

Description

This routine will erases a specified FLASH memory page.

Parameters
  • Address: Specifies the page address to be erased.
  • bank: Specifies the bank where the bank is located. Valid values :
    Value Description
    _FLASH_BANK1 FLASH memory bank 1.
    _FLASH_BANK2 FLASH memory bank 2.
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);
// for STM32 M4 devices 
status := FLASH_ErasePage(address, _FLASH_BANK1);
Notes

This routine is valid only for ST devices.

FLASH_EraseAllPages

Prototype

function FLASH_EraseAllPages() : dword;

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

function FLASH_EraseAllBank1Pages() : dword;

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

function FLASH_EraseAllBank2Pages() : dword;

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

function FLASH_EraseAllSectors() : dword;

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

function FLASH_Write_DoubleWord(Address : dword; lData : uint64) : dword;

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

function FLASH_Write_Word(Address : dword; lData : dword) : dword;

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

function FLASH_Write_HalfWord(Address : dword; lData : dword) : dword;

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

function FLASH_Write_Byte(Address : dword; lData : byte) : dword;

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

function FLASH_AddressToSector(Address : dword) : longint;

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

function FLASH_SectorSize(flash_sector: dword) : longint;

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.

Flash_GetSecurityState

Prototype

function Flash_GetSecurityState() : byte;

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 :

  • _FLASH_NOT_SECURE: Flash currently not in secure state,
  • _FLASH_SECURE_BACKDOOR_ENABLED: Flash is secured and backdoor key access enabled,
  • _FLASH_SECURE_BACKDOOR_DISABLED: Flash is secured and backdoor key access disabled.
Requires

Nothing.

Example
status := Flash_GetSecurityState();
Notes

This routine is valid only for Kinetis devices.

Flash_Init

Prototype

function Flash_Init() : byte;

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

function Flash_ProgramLongword(address : dword; dataToWrite : dword) : byte;

Description

Function programs four previously-erased bytes in the program flash memory using an embedded algorithm.

Parameters
  • address: the address of the data.
  • dataToWrite: new data to write.
Returns
  • _FLASH_STATUS_FAIL:
  • _FLASH_STATUS_OK:
Requires

Nothing.

Example


          
Notes

This routine is valid only for Kinetis K2x devices.

Flash_ProgramPhrase

Prototype

function Flash_ProgramPhrase(address : dword; dataToWrite : ^byte) : byte;

Description

Function programs 8 previously-erased bytes in the program flash memory using an embedded algorithm.

Parameters
  • address: starting address for programming 8 bytes.
  • dataToWrite: pointer to data.
Returns
  • _FLASH_STATUS_FAIL:
  • _FLASH_STATUS_OK:
Requires

Nothing.

Example


          
Notes

This routine is valid only for Kinetis K6x devices.

Flash_ProgramSection

Prototype

funcrion Flash_ProgramSection(address : dword; dataToWrite : ^dword; count : word) : byte;

Description

Function programs the data found in the section program buffer to previously erased locations in the flash memory using an embedded algorithm.

Parameters
  • address: the start address of the data.
  • dataToWrite: pointer to data buffer; must be n * 128bits long [n = 1 .. 64].
  • count: number of 128bits to program.
Returns
  • _FLASH_STATUS_FAIL:
  • _FLASH_STATUS_OK:
Requires

Nothing.

Example


          
Notes
  • This routine is valid only for Kinetis K6x devices.
  • Address must be 128bits aligned ([3:0] = 0000).

Flash_EraseSector

Prototype

function Flash_EraseSector(address : dword) : byte;

Description

Function erases program Flash memory sector.

Parameters
  • address: starting address of the sector.
Returns
  • _FLASH_STATUS_FAIL:
  • _FLASH_STATUS_OK:
Requires

Nothing.

Example


          
Notes
  • This routine is valid only for Kinetis devices.
  • Sector size/offset is 0x800 (2kB).

Flash_EraseBlock

Prototype

function Flash_EraseBlock(address : dword) : byte;

Description

Function erases all addresses in a single program flash or dataflash block.

Parameters
  • address: address in block.
Returns
  • _FLASH_STATUS_FAIL:
  • _FLASH_STATUS_OK:
Requires

Nothing.

Example


          
Notes
  • This routine is valid only for Kinetis devices.

Flash_EraseAllBlocks

Prototype

function Flash_EraseAllBlocks() : byte;

Description

Function operation erases whole Flash memory, initializes the FlexRAM, verifies all memory contents, and releases MCU security.

Parameters

None.

Returns
  • _FLASH_STATUS_FAIL:
  • _FLASH_STATUS_OK:
Requires

Nothing.

Example


          
Notes
  • This routine is valid only for Kinetis devices.

Flash_Read1sAllBlocks

Prototype

function Flash_Read1sAllBlocks(margin : byte) : byte;

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
  • margin: Read-1 margin choices:
    Value Description
    _FLASH_MARGIN_NORMAL
    _FLASH_MARGIN_USER
    _FLASH_MARGIN_FACTORY
Returns
  • _FLASH_STATUS_FAIL:
  • _FLASH_STATUS_OK:
Requires

Nothing.

Example


          
Notes
  • This routine is valid only for Kinetis devices.

Flash_Read1sBlock

Prototype

function Flash_Read1sBlock(address : dword; margin : byte) : byte;

Description

Function checks to see if an entire program flash block has been erased to the specified margin level.

Parameters
  • address: address in block.
  • margin: Read-1 margin choices:
    Value Description
    _FLASH_MARGIN_NORMAL
    _FLASH_MARGIN_USER
    _FLASH_MARGIN_FACTORY
Returns
  • _FLASH_STATUS_FAIL:
  • _FLASH_STATUS_OK:
Requires

Nothing.

Example


          
Notes
  • This routine is valid only for Kinetis devices.

Flash_Read1sSection

Prototype

function Flash_Read1sBlock(address : dword; margin : byte) : byte;

Description

Function checks if a section of program flash or data flash memory is erased to the specified read margin level.

Parameters
  • address: address in block.
  • margin: Read-1 margin choices:
    Value Description
    _FLASH_MARGIN_NORMAL
    _FLASH_MARGIN_USER
    _FLASH_MARGIN_FACTORY
  • numOfBits: number of bits to be checked.
Returns
  • _FLASH_STATUS_FAIL:
  • _FLASH_STATUS_OK:
Requires

Nothing.

Example


          
Notes
  • This routine is valid only for Kinetis K6x devices.

Flash_VerifyBackdoorAccessKey

Prototype

function Flash_VerifyBackdoorAccessKey(keys : ^byte);

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
  • keys: pointer to user provided keys.
Returns
  • _FLASH_STATUS_FAIL:
  • _FLASH_STATUS_OK:
Requires

Nothing.

Example


          
Notes
  • This routine is valid only for Kinetis devices.
  • The Verify Backdoor Access Key command only executes if the mode and security conditions are satisfied.
    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.

Flash_ProgramCheck

Prototype

function Flash_ProgramCheck(address : dword; margin : byte; dataToCheck : ^byte) : byte;

Description

Function tests a previously programmed program flash or data flash longword to see if it reads correctly at the specified margin level.

Parameters
  • address: the start address of the data that will be checked.
  • margin: user provided margin choice.
  • dataToCheck: data to be verified.
Returns
  • _FLASH_STATUS_FAIL:
  • _FLASH_STATUS_OK:
Requires

Nothing.

Example


          
Notes
  • This routine is valid only for Kinetis devices.

Flash_ReadResource

Prototype

function Flash_ReadResource(address : dword; readData : ^dword; resourceSelectCode : byte) : byte;

Description

Function reads data from special-purpose memory resources located within the flash memory module.

Parameters
  • address: address for IFR or Version ID.
  • readData: pointer to read data.
  • resourceSelectCode: select resource:
    Value Description
    _FLASH_RESOURCE_CODE_IFR
    _FLASH_RESOURCE_CODE_ID
Returns
  • _FLASH_STATUS_FAIL:
  • _FLASH_STATUS_OK:
Requires

Nothing.

Example


          
Notes
  • This routine is valid only for Kinetis devices.

Flash_ReadOnce

Prototype

function Flash_ReadOnce(recordIndex : dword; readData : ^dword);

Description

Function reads a reserved 96-byte field located in the program flash 0 IFR.

Parameters
  • recordIndex: program once record index 0x00 - 0x13.
  • readData: pointer to read data.
Returns
  • _FLASH_STATUS_FAIL:
  • _FLASH_STATUS_OK:
Requires

Nothing.

Example


          
Notes
  • This routine is valid only for Kinetis devices.

Flash_ProgramOnce

Prototype

function Flash_ProgramOnce(recordIndex : dword; readData : ^dword) : byte;

Description

Function writes to a reserved 96-byte field in the program flash 0 IFR.

Parameters
  • recordIndex: program once record index 0x00 - 0x13.
  • readData: pointer to read data.
Returns
  • _FLASH_STATUS_FAIL:
  • _FLASH_STATUS_OK:
Requires

Nothing.

Example


          
Notes
  • This routine is valid only for Kinetis devices.

FLASH_AddressToSector

Prototype

function FLASH_AddressToSector(address : byte) : byte;

Description

This function gets sector number from the given Flash address.

Parameters
  • address: address in the Flash memory.
Returns
  • sector number of given address - if execution was successful.
  • _FLASH_ERROR - if an error occurred.
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

function FLASH_MemoryInfo(address : dword; sector : ^dword; bank : ^dword) : byte;

Description

This function gets memory information (sector, bank) from the given Flash address.

Parameters
  • address: address in the Flash memory.
  • sector: calc sector mask will be stored here.
  • bank: calc bank num will be stored here (_FLASH_BANK0 or _FLASH_BANK1).
Returns
  • _FLASH_OK - if execution was successful.
  • _FLASH_ERROR - if an error occurred.
Requires

Nothing.

Example


          
Notes

None.

FLASH_UnprotectSectors

Prototype

function FLASH_UnprotectSectors(bank : dword; sectorMask : dword) : byte;

Description

This function will unprotect sectors for erase/write.

Parameters
  • bank: sector's bank. Valid values :
    Value Description
    _FLASH_BANK0 FLASH bank 0.
    _FLASH_BANK1 FLASH bank 1.
  • sectorMask: mask of sectors to be unprotected.
Returns
  • _FLASH_OK - if execution was successful.
  • _FLASH_ERROR - if an error occurred.
Requires

Nothing.

Example


          
Notes

None.

FLASH_ProtectSectors

Prototype

function FLASH_ProtectSectors(bank : dword; sectorMask : dword) : byte;

Description

This function will protect sectors for erase/write.

Parameters
  • bank: sector's bank. Valid values :
    Value Description
    _FLASH_BANK0 FLASH bank 0.
    _FLASH_BANK1 FLASH bank 1.
  • sectorMask: mask of sectors to be protected.
Returns
  • _FLASH_OK - if execution was successful.
  • _FLASH_ERROR - if an error occurred.
Requires

Nothing.

Example


          
Notes

None.

FLASH_EraseSector

Prototype

function FLASH_EraseSector(address : dword);

Description

This function will erase one sector from the given address in the Flash memory.

Parameters
  • address: Flash memory address.
Returns
  • _FLASH_OK - if execution was successful.
  • _FLASH_ERROR - if an error occurred.
Requires

Nothing.

Example


          
Notes

None.

FLASH_EraseBank0

Prototype

function FLASH_EraseBank0(sectorCount : byte) : byte;

Description

This function will erase sectors located in the Bank0 of the Flash memory.

Parameters
  • sectorCount: number of sectors in the Bank0.
Returns
  • _FLASH_OK - if execution was successful.
  • _FLASH_ERROR - if an error occurred.
Requires

Nothing.

Example


          
Notes

None.

FLASH_EraseBank1

Prototype

function FLASH_EraseBank1(sectorCount : byte) : byte;

Description

This function will erase sectors located in the Bank1 of the Flash memory.

Parameters
  • sectorCount: number of sectors in the Bank1.
Returns
  • _FLASH_OK - if execution was successful.
  • _FLASH_ERROR - if an error occurred.
Requires

Nothing.

Example


          
Notes

None.

FLASH_MassErase

Prototype

function FLASH_MassErase() : byte;

Description

This function will erase all unprotected sectors.

Parameters

None.

Returns
  • _FLASH_OK - if execution was successful.
  • _FLASH_ERROR - if an error occurred.
Requires

Nothing.

Example


          
Notes

None.

FLASH_ProgramMemory

Prototype

function FLASH_ProgramMemory(source : ^dword; destination : ^dword, _length : dword) : byte;

Description

This function will program given amount of bytes to the Flash memory.

Parameters
  • source: source data.
  • destination: write destination.
  • _length: number of bytes to program.
Returns
  • _FLASH_OK - if execution was successful.
  • _FLASH_ERROR - if an error occurred.
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

program Flash;

var buff : array[32] of dword;
var i, Address : dword;
var ptr : ^dword;

begin
  GPIO_Digital_Output(@GPIO_PORTH, _GPIO_PINMASK_ALL);    // digital output GPIO_PORTH
  GPIO_Digital_Output(@GPIO_PORTJ, _GPIO_PINMASK_ALL);    // digital output GPIO_PORTJ

  Address :=  0xC00;                  // erase block (Address must be 1024 byte aligned)

  for i := 0 to 31 do
    buff[i] := i + (i shl 16);

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

  ptr := Address;
  for i := 0 to 31 do
  begin
    GPIO_PORTH_DATA := ptr^;
    GPIO_PORTJ_DATA := ptr^ shr 16;
    Inc(ptr);
    Delay_ms(250);
  end;

  Delay_ms(1000);

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

  ptr := Address + 32*4;
  GPIO_PORTH_DATA := ptr^;
  GPIO_PORTJ_DATA := ptr^ shr 16;

end.

STM32

program Flash;

var buff : array[32] of dword;
var i, tmp : dword;
var Address, Address2 : dword;
var ptr : ^dword;

begin
  GPIO_Digital_Output(@GPIOD_BASE, _GPIO_PINMASK_ALL);    // digital output PORTD
  GPIO_Digital_Output(@GPIOE_BASE, _GPIO_PINMASK_ALL);    // digital output PORTE

  Address := 0x08008000;
  Address2 := 0x08009000;

  for i := 0 to 31 do
    buff[i] := i + (i shl 16);

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


  ptr := ^dword(Address);
  for i := 0 to 31 do
  begin
    tmp := ptr^;
    GPIOD_ODR := HiWord(tmp);
    GPIOE_ODR := LoWord(tmp) shl 8;
    Inc(ptr);
    Delay_ms(50);
  end;

  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 := ^dword(Address2);
  i := ptr^;
  GPIOE_ODR := (LoWord(i)) shl 8;

end.