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.

  Important :

Library Routines

FLASH_Read_Byte

Prototype

// for MCUs with 64kb of Flash memory or less
char FLASH_Read_Byte(unsigned int address);

// for MCUs with Flash memory larger than 64kb
char FLASH_Read_Byte(unsigned long address);

Returns

Returns data byte from Flash memory.

Description

Reads data from the specified address in Flash memory.

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
void FLASH_Read_Bytes(unsigned int address, char *buffer, unsigned NoBytes);

// for MCUs with Flash memory larger than 64kb
void FLASH_Read_Bytes(unsigned long address, char *buffer, unsigned NoBytes);

Returns

Nothing.

Description

Reads number of data bytes defined by NoBytes parameter from the specified address in Flash memory to variable pointed by buffer.

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
char FLASH_Read_Word(unsigned int address);

// for MCUs with Flash memory larger than 64kb
char FLASH_Read_Word(unsigned long address);

Returns

Returns data word from Flash memory.

Description

Reads data from the specified address in Flash memory.

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
void FLASH_Read_Words(unsigned int address, char *buffer, unsigned NoWords);

// for MCUs with Flash memory larger than 64kb
void FLASH_Read_Words(unsigned long address, char *buffer, unsigned NoWords);

Returns

Nothing.

Description

Reads number of data words defined by NoWords parameter from the specified address in Flash memory to variable pointed by buffer.

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 MCUs

void 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 dData parameter to the specified address in Flash memory.

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 :

  • address: address of the page used for writting.
  • dData: pointer to a data array which is being written.
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 MCUs

void 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 :

  • address: address of the page being erased.
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.

Copy Code To ClipboardCopy Code To Clipboard
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);
  }

}
Copyright (c) 2002-2017 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code