[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[avr-chat] EEPROM read function on Tiny 13
From: |
Robert von Knobloch |
Subject: |
[avr-chat] EEPROM read function on Tiny 13 |
Date: |
Mon, 08 Oct 2007 10:13:46 +0200 |
User-agent: |
Thunderbird 1.5.0.12 (X11/20060911) |
Hallo Joerg,
thanks for responding.
Here are some code snippets. What is interesting is that (in the cold
light of Monday morning) I saw I made a mistake and have not replaced
the library function in one place out of three (marked) - still the code
works.
I understand your comments about 16-Bit addressing and the data sheet
shows this 'empty' high address register for the eeprom but doesn't say
if writing to it is harmless (I would imagine so but that is not
guaranteed).
If I load AVR Studio (which means firing up a windows box - I use
Linux), the "glibc version" of code runs in the simulator and shows that
it reads gibberish from the eeprom, whereas my version functions
correctly, so the simulator produces similar results to real hardware.
I note that the declarations for read_eeprom_byte() and
read_eeprom_word() show the address parameter as a const., whereas the
corresponding read functions do not. I wish to use a pointer here for
obvious reasons.
I would appreciate your comments.
Best regards,
Robert von Knobloch
======================================================================================================
The code:
The purpose here is to implement a circular buffer in eeprom (to extend
eeprom life to 1 million write cycles). The lower 4 bits are used as a
serial number. The upper 4 bits contain the stored data (only bit 4 used
at present).
This is the power-on routine that finds the serial number discontinuity
(buffer length[10] must NOT be equal to serial number length [16]).
// Some definitions - common to both solutions
#define FetDrive 4 // FET drive output
// also as bit position
// in circular buffer
#define elen 10 // Length of eeprom circular buffer
volatile uint8_t portbbuf;
uint8_t bufptr, sernum;
uint8_t eebuf[elen] __attribute__ ((section(".eeprom")))\
= { 0,0,0,0,0,0,0,0,0,0 } ;
//***NON-FUNCTIONAL VERSION - Uses glibc eeprom_read_byte();
bufptr = eebuf;
sernum = (eeprom_read_byte(bufptr) & 0x0f);
while (bufptr != &eebuf[elen-1])
{
bufptr++;
sernum++;
if ((sernum & 0x0f) != (eeprom_read_byte(bufptr) & 0x0f))
{ // We have found the discontinuity
bufptr--; // Point to last used entry
sernum--; // Last serial number
break;
}
}
// bufptr now points to last used eeprom entry
// and sernum contains the last used serial number
// Retrieve the last state and set FetDrive accordingly
portbbuf = (eeprom_read_byte(bufptr) & _BV(FetDrive));
//*** FUNCTIONAL VERSION - Using a 'bit-twiddled' eeprom read function
// and is a bit 'dirty' about respecting integer lenghts - but it works!
/*
******************************************************************************
*
* Read a byte from EEPROM using a pointer
*
******************************************************************************
*/
uint8_t EeReadByte( uint16_t address )
{
while(EECR & _BV(EEPE));
cli();
EEAR = address;
EECR |= _BV(EERE);
sei();
return EEDR;
}
bufptr = eebuf;
sernum = (EeReadByte(bufptr) & 0x0f);
while (bufptr != &eebuf[elen-1])
{
bufptr++;
sernum++;
if ((sernum & 0x0f) != (EeReadByte(bufptr) & 0x0f))
{ // We have found the discontinuity
bufptr--; // Point to last used entry
sernum--; // Last serial number
break;
}
}
// bufptr now points to last used eeprom entry
// and sernum contains the last used serial number
// Retrieve the last state and set FetDrive accordingly
portbbuf = (EeReadByte(bufptr) & _BV(FetDrive)); //********
MISTAKE******* I should have changed this, but forgot!!
- [avr-chat] EEPROM read function on Tiny 13,
Robert von Knobloch <=