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 var tmp : dword; ... begin tmp := Flash_Read(0x0D00); end |
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 : long = 0x200; var dat_buff : array[32] of word; ... begin FLASH_Read_Bytes(F_ADDRESS, dat_buff, 64); end. |
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 var tmp : word; ... begin tmp := Flash_Read(0x0D00); begin |
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 : dword = 0x200; var dat_buff : array[32] of word; ... begin FLASH_Read_Words(F_ADDRESS,dat_buff, 32); |
FLASH_Write_Page
Prototype |
procedure FLASH_Write_Page(address : word; dData : ^word); // for XMEGA family of MCUsprocedure FLASH_Write_Application_Page(address : word; wData : ^word); procedure FLASH_Write_Boot_Page(address : word; wData : ^word); procedure FLASH_Erase_Write_Application_Page(address : word; wData : ^word); procedure FLASH_Erase_Write_Boot_Page(address : word; wData : ^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 |
var WRITE_ADDRESS : word; var dat_buff : array[8] of word; ... begin WRITE_ADDRESS := 0x200; FLASH_Read_Words(WRITE_ADDRESS,dat_buff); end. |
FLASH_Erase_Page
Prototype |
procedure FLASH_Erase_Page(address : word); // for XMEGA family of MCUsprocedure FLASH_Erase_Boot_Page(address : word); procedure FLASH_Erase_Application_Page(address : 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 |
var ERASE_ADDRESS : word; ... begin 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, PORTC and PORTD.
program Flash_MCU_test; const F_ADDRESS : longint = 0x200; const data_ : array[32] of word = ( // 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; var counter : byte; word_ : word; dat_buff : array[32] of word; begin 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 ) do // reading 64 bytes in loop begin 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); end; FLASH_Read_Bytes(F_ADDRESS, @dat_buff, 64); // demonstration of reading 64 bytes for counter := 0 to 31 do begin PORTD := dat_buff[counter]; // output low byte to PORTD PORTB := word((dat_buff[counter] shr 8)); // output higher byte to PORTB Delay_ms(200); end; counter := 0; while (counter <= 63) do // reading 32 words in loop begin word_ := FLASH_Read_Word(F_ADDRESS + counter); // demonstration of reading single word PORTD := word_; // output low byte to PORTD PORTB := word(word_ shr 8); // output higher byte to PORTB counter := counter + 2; Delay_ms(200); end; FLASH_Read_Words(F_ADDRESS, @dat_buff, 32); // demonstration of reading 64 bytes for counter := 0 to 31 do begin PORTD := dat_buff[counter]; // output low byte to PORTD PORTB := word((dat_buff[counter] shr 8)); // output higher byte to PORTB Delay_ms(200); end; end.
What do you think about this topic ? Send us feedback!