Flash Memory Library
This library provides routines for accessing microcontroller Flash memory. Note that prototypes differ for MCU to MCU due to the amount of Flash memory.

- Due to the AVR family Flash specifics, Flash library is MCU dependent. Since some AVR MCU's have more or less than 64kb of Flash memory, prototypes may be different from chip to chip.
Please refer to datasheet before using Flash library. - When writting/erasing memory bear in mind that the memory is divided into pages; Size of the page is defined in the
__FLASH_PAGE_SIZE
constant (in program words) in the definition file of the appropriate MCU. - Writing/erasing memory is possible only from the boot section of the Flash memory.
Library Routines
FLASH_Read_Byte
Prototype |
' for MCUs with 64kb of Flash memory or less
' for MCUs with Flash memory larger than 64kb |
---|---|
Returns |
Returns data byte from Flash memory. |
Description |
Reads data from the specified |
Requires |
Nothing. |
Example |
' for MCUs with Flash memory larger than 64kb dim tmp as longword ... tmp = Flash_Read(0x0D00) ... |
FLASH_Read_Bytes
Prototype |
' for MCUs with 64kb of Flash memory or less
' for MCUs with Flash memory larger than 64kb |
---|---|
Returns |
Nothing. |
Description |
Reads number of data bytes defined by |
Requires |
Nothing. |
Example |
'for MCUs with Flash memory larger than 64kb const F_ADDRESS as longint = 0x200 dim dat_buff[32] as word ... FLASH_Read_Bytes(F_ADDRESS,dat_buff, 64) |
FLASH_Read_Word
Prototype |
' for MCUs with 64kb of Flash memory or less
' for MCUs with Flash memory larger than 64kb |
---|---|
Returns |
Returns data word from Flash memory. |
Description |
Reads data from the specified |
Requires |
Nothing. |
Example |
' for MCUs with Flash memory larger than 64kb dim tmp as longword ... tmp = Flash_Read(0x0D00) ... |
FLASH_Read_Words
Prototype |
' for MCUs with 64kb of Flash memory or less
' for MCUs with Flash memory larger than 64kb |
---|---|
Returns |
Nothing. |
Description |
Reads number of data words defined by |
Requires |
Nothing. |
Example |
'for MCUs with Flash memory larger than 64kb const F_ADDRESS as longint = 0x200 dim dat_buff[32] as word ... FLASH_Read_Bytes(F_ADDRESS,dat_buff, 64) |
FLASH_Write_Page
Prototype |
sub procedure FLASH_Write_Page(dim address as word, dim dData as ^word) ' for XMEGA family of MCUssub procedure FLASH_Write_Boot_Page(dim address as word, dim wData as ^word) sub procedure FLASH_Write_Application_Page(dim address as word, dim wData as ^word) sub procedure FLASH_Erase_Write_Application_Page(dim address as word, dim wData as ^word) sub procedure FLASH_Erase_Write_Boot_Page(dim address as word, dim wData as ^word) |
---|---|
Returns |
Nothing. |
Description |
Writes a data array pointed to by For XMEGA family of MCU's you can write to the both boot and application data section. Also, you can erase the appropriate page prior to writing. Parameters :
|
Requires |
Nothing. |
Example |
dim WRITE_ADDRESS as word dim dat_buff[8] as word ... main: WRITE_ADDRESS = 0x200 FLASH_Read_Words(WRITE_ADDRESS,dat_buff) end. |
FLASH_Erase_Page
Prototype |
sub procedure FLASH_Erase_Page(dim address as word) ' for XMEGA family of MCUssub procedure FLASH_Erase_Boot_Page(dim address as word) sub procedure FLASH_Erase_Application_Page(dim address as word) |
---|---|
Returns |
Nothing. |
Description |
Erases specified page in the Flash memory. For XMEGA family of MCU's you can erase both boot and application data section. Parameters :
|
Requires |
Nothing. |
Example |
dim ERASE_ADDRESS as word ... main: WRITE_ADDRESS = 0x200 FLASH_Read_Words(ERASE_ADDRESS) end. |
Library Example
The example demonstrates simple write to the flash memory for AVR, then reads the data and displays it on PORTB and PORTD.
program Flash_MCU_test const F_ADDRESS as longint = 0x200 const data_ as word[32] = ( ' constant table 0x0000,0x0001,0x0002,0x0003,0x0004,0x0005,0x0006,0x0007, 0x0008,0x0009,0x000A,0x000B,0x000C,0x000D,0x000E,0x000F, 0x0000,0x0100,0x0200,0x0300,0x0400,0x0500,0x0600,0x0700, 0x0800,0x0900,0x0A00,0x0B00,0x0C00,0x0D00,0x0E00,0x0F00 ) org 0x200 dim counter as byte word_ as word dat_buff as word[32] 'dat_buff_ as word[32] main: DDRD = 0xFF ' set direction to be output DDRB = 0xFF ' set direction to be output word_ = data_[0] ' link const table counter = 0 while ( counter < 64 ) ' reading 64 bytes in loop PORTD = FLASH_Read_Byte(F_ADDRESS + counter) ' demonstration of reading single byte Inc(counter) PORTB = FLASH_Read_Byte(F_ADDRESS + counter) ' demonstration of reading single byte Inc(counter) Delay_ms(200) wend FLASH_Read_Bytes(F_ADDRESS, @dat_buff, 64) ' demonstration of reading 64 bytes for counter = 0 to 31 PORTD = dat_buff[counter] ' output low byte to PORTD PORTB = word((dat_buff[counter] >> 8)) ' output higher byte to PORTB Delay_ms(200) next counter counter = 0 while (counter <= 63) ' reading 32 words in loop word_ = FLASH_Read_Word(F_ADDRESS + counter) ' demonstration of reading single word PORTD = word_ ' output low byte to PORTD PORTB = Hi(word_)' >> 8) ' output higher byte to PORTB counter = counter + 2 Delay_ms(200) wend FLASH_Read_Words(F_ADDRESS, @dat_buff, 32) ' demonstration of reading 64 bytes for counter = 0 to 31 PORTD = dat_buff[counter] ' output low byte to PORTD PORTB = word((dat_buff[counter] >> 8)) ' output higher byte to PORTB Delay_ms(200) next counter end.
What do you think about this topic ? Send us feedback!