-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathEepromAbstraction.cpp
56 lines (44 loc) · 1.42 KB
/
EepromAbstraction.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
/*
* Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry).
* This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
*/
#include "EepromAbstraction.h"
#ifdef __AVR__
#include <avr/eeprom.h>
uint8_t AvrEeprom::read8(EepromPosition position) {
return eeprom_read_byte((uint8_t*)position);
}
void AvrEeprom::write8(EepromPosition position, uint8_t val) {
if(read8(position) != val) {
eeprom_write_byte((uint8_t*)position, val);
}
}
uint16_t AvrEeprom::read16(EepromPosition position) {
return eeprom_read_word((uint16_t*)position);
}
void AvrEeprom::write16(EepromPosition position, uint16_t val) {
if(read16(position) != val) {
eeprom_write_word((uint16_t*)position, val);
}
}
uint32_t AvrEeprom::read32(EepromPosition position) {
return eeprom_read_dword((uint32_t*)position);
}
void AvrEeprom::write32(EepromPosition position, uint32_t val) {
if(read32(position) != val) {
eeprom_write_dword((uint32_t*)position, val);
}
}
void AvrEeprom::readIntoMemArray(uint8_t* memDest, EepromPosition romSrc, uint8_t len) {
eeprom_read_block(memDest, (uint8_t*)romSrc, len);
}
void AvrEeprom::writeArrayToRom(EepromPosition romDest, const uint8_t* memSrc, uint8_t len) {
bool changed = false;
for(uint8_t i = 0;i < len; ++i) {
changed = changed || (read8(romDest + i) != memSrc[i]);
}
if(changed) {
eeprom_write_block(memSrc, (uint8_t*)romDest, len);
}
}
#endif // AVR ONLY