-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAvrEeprom.cpp
More file actions
127 lines (105 loc) · 3.02 KB
/
Copy pathAvrEeprom.cpp
File metadata and controls
127 lines (105 loc) · 3.02 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
AvrEeprom.cpp is part of EepromUtils.
Copyright (c) 2011 Lorenzo Flueckiger
EepromUtils is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
EepromUtils is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with EepromUtils. If not, see <http://www.gnu.org/licenses/>.
*/
#include <avr/eeprom.h>
#define SERIAL_DEBUG 1
#ifdef SERIAL_DEBUG
#include <HardwareSerial.h>
#endif
#define LAST E2END+1
#include "AvrEeprom.h"
void AvrEeprom::write_byte(uint16_t addr, uint8_t data)
{
if ( addr < LAST )
eeprom_write_byte((uint8_t *)addr, data);
}
uint8_t AvrEeprom::read_byte(uint16_t addr)
{
return eeprom_read_byte((uint8_t *)addr);
}
void AvrEeprom::write_word(uint16_t addr, uint16_t data)
{
if ( addr < LAST-1 )
eeprom_write_word((uint16_t *)addr, data);
}
uint16_t AvrEeprom::read_word(uint16_t addr)
{
return eeprom_read_word((uint16_t *)addr);
}
void AvrEeprom::write_long(uint16_t addr, uint32_t data)
{
if ( addr < LAST-3 )
eeprom_write_dword((uint32_t *)addr, data);
}
uint32_t AvrEeprom::read_long(uint16_t addr)
{
return eeprom_read_dword((uint32_t *)addr);
}
void AvrEeprom::write_block(uint16_t addr, void* data, size_t len)
{
if ( addr < LAST+1-len )
eeprom_write_block(data, (void *)addr, len);
}
void AvrEeprom::read_block(uint16_t addr, void* data, size_t len)
{
eeprom_read_block(data, (void *)addr, len);
}
uint16_t AvrEeprom::memSize()
{
return E2END;
}
uint16_t AvrEeprom::pageSize()
{
return E2PAGESIZE;
}
void AvrEeprom::show(uint16_t start, int len)
{
uint16_t ptr; // start of the first page we will print
uint16_t end; // just after the last page we will print
// can only address bytes below the max eeprom limit
if ( start > E2END ) return;
// if no length is given, then print up to the end of the eeprom
ptr = start - start % E2PAGESIZE;
if ( len < 0 ) {
end = E2END + 1;
}
else {
// @bug It seems that we alway show one extra page...
end = start+len - (start+len) % E2PAGESIZE + E2PAGESIZE;
if ( end > E2END+1 ) end = E2END;
}
#ifdef SERIAL_DEBUG
// iterate through all pages
int page = ptr / E2PAGESIZE;
uint8_t data = 0;
while ( ptr < end ) {
Serial.print("bytes [");
Serial.print(ptr, DEC);
Serial.print("-");
Serial.print(ptr+4, DEC);
Serial.print("] (page=");
Serial.print(page, DEC);
Serial.print(") : ");
for (int i=0; i<E2PAGESIZE; i++) {
data = read_byte(ptr++);
Serial.print(data, HEX);
Serial.print(" ");
}
page++;
Serial.println();
}
#else
#warning "AvrEeprom is a noop with SERIAL_DEBUG on."
#endif
}