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 unsigned long tmp; ... tmp = FLASH_Read_Byte(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 long F_ADDRESS = 0x200; unsigned int dat_buff[32]; ... 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 unsigned long tmp; ... tmp = FLASH_Read_Word(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 long F_ADDRESS = 0x200; unsigned int dat_buff[32]; ... FLASH_Read_Words(F_ADDRESS, dat_buff, 32); |
FLASH_Write_Page
Prototype |
void FLASH_Write_Page(unsigned int address, unsigned int *dData); // for XMEGA family of MCUsvoid FLASH_Write_Boot_Page(unsigned int address, unsigned int *wData); void FLASH_Write_Application_Page(unsigned int address, unsigned int *wData); void FLASH_Erase_Write_Boot_Page(unsigned int address, unsigned int* wdata); void FLASH_Erase_Write_Application_Page(unsigned int address, unsigned int* wdata); |
---|---|
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 |
unsigned int WRITE_ADDRESS = 0x200; unsigned int dat_buff[8]; ... FLASH_Write_Page(WRITE_ADDRESS, dat_buff); |
FLASH_Erase_Page
Prototype |
void FLASH_Erase_Page(unsigned int address); // for XMEGA family of MCUsvoid FLASH_Erase_Boot_Page(unsigned int address); void FLASH_Erase_Application_Page(unsigned int address); |
---|---|
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 |
unsigned int ERASE_ADDRESS = 0x200; ... FLASH_Erase_Page(ERASE_ADDRESS); |
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.
const long F_ADDRESS = 0x200; const unsigned int data_[32] = { // constant table 0x0000,0x0001,0x0002,0x0003,0x0004,0x0005,0x0006,0x0007, // in Flash at address F_ADDRESS 0x0008,0x0009,0x000A,0x000B,0x000C,0x000D,0x000E,0x000F, 0x0000,0x0100,0x0200,0x0300,0x0400,0x0500,0x0600,0x0700, 0x0800,0x0900,0x0A00,0x0B00,0x0C00,0x0D00,0x0E00,0x0F00, } absolute F_ADDRESS; char i; unsigned int word_; unsigned int dat_buff[32]; void main() { DDRB = 0xFF; // Set signal port as output DDRC = 0xFF; // Set signal port as output DDRD = 0xFF; // Set signal port as output word_ = data_[0]; // link const table PORTB = 1; // signal program progress for (i = 0; i<64 ; i+=2) { // reading 32 words in loop word_ = FLASH_Read_Word(F_ADDRESS + i); // demonstration of reading single word PORTD = word_; // output low byte to PORTD PORTC = word_ >> 8; // output high byte to PORTC Delay_ms(100); } PORTB = 2; // signal program progress i = 0; // initialize loop variable while ( i < 64 ) { // reading 64 bytes in loop PORTD = FLASH_Read_Byte(F_ADDRESS + i++); // demonstration of reading single byte PORTC = FLASH_Read_Byte(F_ADDRESS + i++); // demonstration of reading single byte Delay_ms(100); } PORTB = 3; // signal program progress FLASH_Read_Bytes(F_ADDRESS, dat_buff, 64); // demonstration of reading 64 bytes block for (i = 0; i<32 ; i++) { // use loop to display read block PORTD = dat_buff[i]; // output low byte to PORTD PORTC = dat_buff[i] >> 8; // output high byte to PORTC Delay_ms(100); } PORTB = 4; // signal program progress FLASH_Read_Words(F_ADDRESS, dat_buff, 32); // demonstration of reading 32 words block for (i = 0; i<32 ; i++) { // use loop to display read block PORTD = dat_buff[i]; // output low byte to PORTD PORTC = dat_buff[i] >> 8; // output high byte to PORTC Delay_ms(100); } }
What do you think about this topic ? Send us feedback!