1 |
#include <avr/pgmspace.h> |
1 |
#include <avr/pgmspace.h> |
2 |
#include "eeprom.h" |
2 |
#include "eeprom.h" |
3 |
#include "common.h" |
3 |
#include "common.h" |
4 |
|
4 |
|
5 |
void EEPROM::Wait() { |
5 |
void EEPROM::Wait() { |
6 |
while(TESTBIT(EECR,(1<<EEPE))); |
6 |
while(TESTBIT(EECR,(1<<EEPE))); |
7 |
} |
7 |
} |
8 |
|
8 |
|
9 |
uint8_t EEPROM::GetByte(uint16_t aOfs) { |
9 |
uint8_t EEPROM::GetByte(uint16_t aOfs) { |
10 |
// Wait for any pending writes to finish |
10 |
// Wait for any pending writes to finish |
11 |
while(TESTBIT(EECR,(1<<EEPE))); |
11 |
while(TESTBIT(EECR,(1<<EEPE))); |
12 |
EEARL = aOfs & 0x00ff; |
12 |
EEARL = aOfs & 0x00ff; |
13 |
#ifdef EEARH |
13 |
#ifdef EEARH |
14 |
EEARH = 0; |
14 |
EEARH = 0; |
15 |
#endif |
15 |
#endif |
16 |
SETBIT(EECR,(1 << EERE)); |
16 |
SETBIT(EECR,(1 << EERE)); |
17 |
uint8_t RetVal = EEDR; |
17 |
uint8_t RetVal = EEDR; |
18 |
return RetVal; |
18 |
return RetVal; |
19 |
} |
19 |
} |
20 |
|
20 |
|
21 |
void EEPROM::SetByte(uint16_t aOfs,uint8_t aData) { |
21 |
void EEPROM::SetByte(uint16_t aOfs,uint8_t aData) { |
22 |
// Wait for any pending writes to finish |
22 |
// Wait for any pending writes to finish |
23 |
while(TESTBIT(EECR,(1<<EEPE))); |
23 |
while(TESTBIT(EECR,(1<<EEPE))); |
24 |
EEARL = aOfs & 0x00ff; |
24 |
EEARL = aOfs & 0x00ff; |
25 |
#ifdef EEARH |
25 |
#ifdef EEARH |
26 |
EEARH = 0; |
26 |
EEARH = 0; |
27 |
#endif |
27 |
#endif |
28 |
EEDR = aData; |
28 |
EEDR = aData; |
29 |
SETBIT(EECR,(1<<EEMPE)); |
29 |
SETBIT(EECR,(1<<EEMPE)); |
30 |
SETBIT(EECR,(1<<EEPE)); |
30 |
SETBIT(EECR,(1<<EEPE)); |
31 |
} |
31 |
} |
32 |
|
32 |
|
33 |
uint16_t EEPROM::GetWord(uint16_t aOfs) { |
33 |
uint16_t EEPROM::GetWord(uint16_t aOfs) { |
34 |
uint16_t RetVal; |
34 |
uint16_t RetVal; |
35 |
((uint8_t *)(&RetVal))[0] = GetByte(aOfs+0); |
35 |
((uint8_t *)(&RetVal))[0] = GetByte(aOfs+0); |
36 |
((uint8_t *)(&RetVal))[1] = GetByte(aOfs+1); |
36 |
((uint8_t *)(&RetVal))[1] = GetByte(aOfs+1); |
37 |
return RetVal; |
37 |
return RetVal; |
38 |
} |
38 |
} |
39 |
|
39 |
|
40 |
void EEPROM::SetWord(uint16_t aOfs,uint16_t aData) { |
40 |
void EEPROM::SetWord(uint16_t aOfs,uint16_t aData) { |
41 |
SetByte(aOfs+0,((uint8_t *)(&aData))[0]); |
41 |
SetByte(aOfs+0,((uint8_t *)(&aData))[0]); |
42 |
SetByte(aOfs+1,((uint8_t *)(&aData))[1]); |
42 |
SetByte(aOfs+1,((uint8_t *)(&aData))[1]); |
43 |
} |
43 |
} |