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
sub function FLASH_Read_Byte(dim address as word) as byte

' for MCUs with Flash memory larger than 64kb
sub function FLASH_Read_Byte(dim address as longword) as byte

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
dim tmp as longword
...
tmp = Flash_Read(0x0D00)
...

FLASH_Read_Bytes

Prototype

' for MCUs with 64kb of Flash memory or less
sub procedure FLASH_Read_Bytes(dim address as word, dim buffer as ^byte, dim NoBytes as word)

' for MCUs with Flash memory larger than 64kb
sub procedure FLASH_Read_Bytes(dim address as longword, dim buffer as ^byte, dim NoBytes as word)

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 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
sub function FLASH_Read_Word(dim address as word) as word

' for MCUs with Flash memory larger than 64kb
sub function FLASH_Read_Word(dim address as longword) as word

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
dim tmp as longword
...
tmp = Flash_Read(0x0D00)
...

FLASH_Read_Words

Prototype

' for MCUs with 64kb of Flash memory or less
sub procedure FLASH_Read_Words(dim address as word, dim buffer as ^word, dim NoWords as word)

' for MCUs with Flash memory larger than 64kb
sub procedure FLASH_Read_Words(dim address as longword, dim buffer as ^word, dim NoWords as word)

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 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 MCUs

sub 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 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
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 MCUs

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

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

Copy Code To ClipboardCopy Code To Clipboard
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.
Copyright (c) 2002-2012 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