erlerobot / smart_motor

4 stars 2 forks source link

EEPROM_module #14

Closed jlamperez closed 8 years ago

jlamperez commented 8 years ago

EEPROM module

This module also contains a .h and .c file. eeprom

The eeprom.h file has EEPROM_VERSION to define the EEPROM version. This value should be changed in code whenever changes to the OpenServo registers would cause the data stored in EEPROM to be incompatible from one version of the OpenServo firmware to the next version of the OpenServo firmware.

Functions defined in eeprom.c file:

While there are bytes in the buffer, they are added to the sum and the size value is decreased.

while (size)
    {
        sum += *buffer;

        ++buffer;
        --size;
    }
memset(buffer, 0xFF, sizeof(buffer));

Loop over the EEPROM and write the buffer to the EEPROM block.

for (i = 0; i < E2END; i += sizeof(buffer))
    {
        eeprom_write_block(buffer, (void *) i, sizeof(buffer));
    }
eeprom_read_block(&header[0], (void *) 0, 2);

If the EEPROM version does not match return error.

if (header[0] != EEPROM_VERSION) return 0;

Read the registers from EEPROM

eeprom_read_block(&registers[MIN_WRITE_PROTECT_REGISTER], (void *) 2, WRITE_PROTECT_REGISTER_COUNT + REDIRECT_REGISTER_COUNT);

If the checksum does not match throw an error else success

if (header[1] != eeprom_checksum(&registers[MIN_WRITE_PROTECT_REGISTER], WRITE_PROTECT_REGISTER_COUNT + REDIRECT_REGISTER_COUNT, EEPROM_VERSION)) return 0;
header[0] = EEPROM_VERSION;
header[1] = eeprom_checksum(&registers[MIN_WRITE_PROTECT_REGISTER], WRITE_PROTECT_REGISTER_COUNT + REDIRECT_REGISTER_COUNT, EEPROM_VERSION);

eeprom_write_block(&header[0], (void *) 0, 2);
eeprom_write_block(&registers[MIN_WRITE_PROTECT_REGISTER], (void *) 2, WRITE_PROTECT_REGISTER_COUNT + REDIRECT_REGISTER_COUNT);

Overview of ATmega168 Flash/EEPROM Map

Page size = 128 bytes

Page Address Type
00 0000-007F Flash
...
77 3B80-3BFF Flash
78 3C00-3C7F Flash (bootloader)
...
7F 3FC0-3FFF Flash (bootloader)
80 4000-407F EEPROM
...
83 4180-41FF EEPROM

Flash(App) size -> 128 pages.128bytes = 15360bytes = 15KB Flash(Bootloader) size -> 8.128 = 1024 bytes = 1KB EEPROM -> 4.128 = 512bytes

Overview of ATmega328P Flash/EEPROM Map

Page size = 128 bytes

Page Address Type
00 0000-007F Flash
...
Flash
F8 7C00-7C7F Flash (bootloader)
...
100 7F80-7FFF Flash (bootloader)
101 8000-807F EEPROM
...
108 8380-83FF EEPROM

Flash(App) size -> 248 pages.128bytes = 31744bytes = 31KB Flash(Bootloader) size -> 8.128 = 1024 bytes = 1KB EEPROM -> 8.128 = 1024bytes Descritpion The AVR flash memory is divided into two sections namely the application section and the Boot-Loader section (BLS). In case of the ATMEGA16 it has 16 KB of flash memory of which the 15KB is application section and the remaining 1KB is BLS. The memory architecture of the ATMEGA16 is shown in the following figure: image The code for both the BLS and application section can be written as normally does and there is no much difference. The only thing to be careful about is the size of the code binary. It should not be more than 1KB, otherwise it won’t be able to code programmed into the BLS.

How To load Application from EEPROM Steps with which binary of a code are read from the EEPROM memory and flashing the same into the flash memory is represented pictorially in the following: