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
Flash_Write
Prototype |
sub function Flash_Write_Word(dim address, lData as longword) as longword) |
---|---|
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 |
Address = 0xC00 Flash_Write(Address, 0xAAAAAAAA)' write one word |
Notes |
None. |
Flash_Write_Buffer
Prototype |
sub function Flash_Write_Buffer(dim address as longword, dim ppData as ^byte) as longword |
---|---|
Description |
This function will program a sequence of words into the on-chip flash. |
Parameters |
|
Returns |
|
Requires |
Nothing. |
Example |
dim rowbuff as longword[32] 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 |
sub function Flash_Erase_Block(dim address as longword, dim rdata as ^byte) as longword |
---|---|
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 |
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 |
sub procedure Flash_Set_uSec(dim clocks as longword) |
---|---|
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 |
sub 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 |
sub procedure 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 |
sub procedure 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 |
sub procedure 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 |
sub 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 |
sub procedure 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 |
sub procedure 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 |
sub procedure 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 |
sub function FLASH_EraseSector(dim FLASH_Sector as longword) as longword |
||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
sub function FLASH_ErasePage(dim Address as longword) as longword |
---|---|
Description |
This routine will erases a specified FLASH memory page. This routine is valid only for STM32F10x devices. |
Parameters |
|
Returns |
|
Requires |
Nothing. |
Example |
status = FLASH_ErasePage(address) |
Notes |
This routine is valid only for STM32F10x devices. |
FLASH_EraseAllPages
Prototype |
sub function FLASH_EraseAllPages() as longword |
---|---|
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 |
sub function FLASH_EraseAllBank1Pages() as longword |
---|---|
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 |
sub function FLASH_EraseAllBank2Pages() as longword |
---|---|
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 |
sub function FLASH_EraseAllSectors() as longword |
---|---|
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 |
sub function FLASH_Write_DoubleWord(dim Address as longword, dim lData as uint64) as longword |
---|---|
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 |
sub function FLASH_Write_Word(dim Address as longword, dim lData as longword) as longword |
---|---|
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 |
sub function FLASH_Write_HalfWord(dim Address as longword, dim lData as longword) as longword |
---|---|
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 |
sub function FLASH_Write_Byte(dim Address as longword, dim lData as byte) as longword |
---|---|
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 |
sub function FLASH_AddressToSector(dim Address as longword) as longint |
---|---|
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 |
sub function FLASH_SectorSize(dim flash_sector as longword) as longint |
||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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. |
Library Example
Stellaris
program Flash dim rowbuff as longword[32] dim i, Address as longword dim ptr as ^longword 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 Address = 0xC00 for i = 0 to 31 rowbuff[i] = i + (i << 16) next i Flash_Erase_Block(Address) ' erase block (Address must be 1024 byte aligned) Flash_Write_Buffer(Address,@rowbuff) ' write buffer with 32 long words ptr = Address for i = 0 to 31 GPIO_PORTH_DATA = ptr^ GPIO_PORTJ_DATA = ptr^ >> 16 Inc(ptr) Delay_ms(250) next i 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 end.
STM32
program Flash dim buff as longword[32] dim i, tmp as longword dim Address, Address2 as longword dim ptr as ^longword main: 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 buff[i] = i + (i << 16) next i FLASH_ErasePage(Address) ' erase block (Address must be 2048 byte aligned) for i = 0 to 31 FLASH_Write_Word(Address + i*4, buff[i]) ' write buffer with 32 long words next i ptr = ^longword(Address) for i = 0 to 31 tmp = ptr^ GPIOD_ODR = HiWord(tmp) GPIOE_ODR = LoWord(tmp) << 8 Inc(ptr) Delay_ms(50) next i 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 = ^longword(Address2) i = ptr^ GPIOE_ODR = (LoWord(i)) << 8 end.
What do you think about this topic ? Send us feedback!