EEPROM Wear Leveling (AVR101)

Aus ProjectWiki
Wechseln zu:Navigation, Suche

Overview

This is an implementation of the algorithm described in the Appnote AVR101 by Atmel. Its purpose is to increase the write cycles for parameters in EEPROM.

To achieve this, there are two ring buffers for each parameter: a status buffer, which keeps track of the last written index and the parameter buffer itself. Because the write cycles are distributed over several EEPROM cells, the life time is increased.

Usage

Include the library at the top of your program:

Const Wearlevel_count = [PARAMETER COUNT]                    ' number of parameters to store in eeprom (default: 1)
' ring buffer size (estimated life time: 100.000 cell write cycles * wearlevel depth)
'Const Wearlevel_depth = [BUFFER_SIZE]                       ' uncomment to define the buffer sizes (default: 8)
'Const Wearlevel_start = [EEPROM_START]                      ' uncomment to set EEPROM start address (default: 1)
$include "wear_leveling.inc"

Define the parameters stored in EEPROM and link them to a SRAM variable:

Dim My_parameter As [TYPE]
Wearlevel_pointer([PARAMETER_ID]) = Varptr(My_parameter)     ' address of variable in SRAM
Wearlevel_size([PARAMETER_ID]) = [TYPE_SIZE]                 ' byte size of the variable data type

Load all parameters stored in EEPROM:

Wearlevel_read 0

Load specific parameter with ID:

Wearlevel_read [PARAMETER_ID]

Save all parameters to EEPROM:

Wearlevel_write 0

Save specific parameter with ID:

Wearlevel_write [PARAMETER_ID]

Sample

$regfile = "m32def.dat"
$crystal = 16000000
$baud = 57600
$framesize = 48

Const Wearlevel_count = 3                                   ' number of parameters to store in eeprom (default: 1)
' ring buffer size (estimated life time: 100.000 cell write cycles * wearlevel depth)
'Const Wearlevel_depth = 10                                  ' uncomment to define the buffer sizes (default: 8)
'Const Wearlevel_start = 20                                  ' uncomment to set EEPROM start address (default: 1)
$include "wear_leveling.inc"

' define the parameters
Dim Testbyte As Byte
Wearlevel_pointer(1) = Varptr(testbyte)                     ' address of variable in SRAM
Wearlevel_size(1) = 1                                       ' byte size of the variable data type

Dim Testword As Word
Wearlevel_pointer(2) = Varptr(testword)
Wearlevel_size(2) = 2

Dim Testsingle As Single
Wearlevel_pointer(3) = Varptr(testsingle)
Wearlevel_size(3) = 4

' load all parameter values stored in EEPROM (ID = 0), called usually during the initialisation
Wearlevel_read 0
Print "READ 0: [1] " ; Testbyte ; ", [2] " ; Testword ; ", [3] " ; Testsingle

' assign some random values
Gosub Random_values
Print "RANDOM: [1] " ; Testbyte ; ", [2] " ; Testword ; ", [3] " ; Testsingle

' just save paramter 2 (Testword)
Wearlevel_write 2
Print "WRITE 2"

' load all again
Wearlevel_read 0
Print "READ 0: [1] " ; Testbyte ; ", [2] " ; Testword ; ", [3] " ; Testsingle

' new random values
Gosub Random_values
Print "RANDOM: [1] " ; Testbyte ; ", [2] " ; Testword ; ", [3] " ; Testsingle
' save all parameters (ID = 0)
Wearlevel_write 0
Print "WRITE 0"
End

Random_values:
   Testbyte = Rnd(255)
   Testword = Rnd(65535)
   Testsingle = Testword
   Testsingle = Testsingle / 1000
   Testword = Rnd(65535)
Return

Download

Download