-
Notifications
You must be signed in to change notification settings - Fork 1
/
ao_Eeprom.cpp
62 lines (57 loc) · 1.66 KB
/
ao_Eeprom.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "ao_Eeprom.h"
// wait until done and read
uint8_t Eeprom::read(uint16_t address){
while(EECR & (1<<EEPE)); // Wait for completion of previous write
EEAR = address;
EECR |= (1<<EERE); // Start eeprom read
return EEDR;
}
// return false if busy
bool Eeprom::write(uint16_t address, uint8_t data){
if (EECR & (1<<EEPE)) return false; // eeprom busy return false
EEAR = address;
EEDR = data;
EECR = 0x00; // erase and write
char cSREG; // disable interrupts during timed sequence
cSREG = SREG;
cli();
EECR |= (1<<EEMPE); // start EEPROM write
EECR |= (1<<EEPE);
SREG = cSREG;
return true;
}
// try to avoid an earase cycle
bool Eeprom::update(uint16_t address, uint8_t data){
if (EECR & (1<<EEPE)) return false; // eeprom busy return false
EEAR = address;
EECR |= (1<<EERE); // Start eeprom read
uint8_t eed=EEDR;
if (eed==data) return true; // data is already in eeprom
EEDR = data;
if ((eed&data)==data){
EECR = (1<<EEPM1); // write only
}else{
EECR = 0x00; // erase and write
}
char cSREG; // disable interrupts during timed sequence
cSREG = SREG;
cli();
EECR |= (1<<EEMPE); // start EEPROM write
EECR |= (1<<EEPE);
SREG = cSREG;
return true;
}
// erase address
bool Eeprom::erase(uint16_t address){
if (EECR & (1<<EEPE)) return false; // eeprom busy return false
EEAR = address;
EEDR = 0xff;
EECR = (1<<EEPM0); // erase and write
char cSREG; // disable interrupts during timed sequence
cSREG = SREG;
cli();
EECR |= (1<<EEMPE); // start EEPROM erase
EECR |= (1<<EEPE);
SREG = cSREG;
return true;
}