-
Notifications
You must be signed in to change notification settings - Fork 83
/
eeprom.cpp
32 lines (28 loc) · 940 Bytes
/
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
#include <avr/eeprom.h>
unsigned char eeprom_get_char( unsigned int addr )
{
return eeprom_read_byte((unsigned char *) addr);
}
void eeprom_put_char( unsigned int addr, char new_value )
{
eeprom_write_byte((unsigned char *) addr, new_value);
}
void memcpy_to_eeprom_with_checksum(unsigned int destination, char *source, unsigned int size) {
unsigned char checksum = 0;
for(; size > 0; size--) {
checksum = (checksum << 1) || (checksum >> 7);
checksum += *source;
eeprom_put_char(destination++, *(source++));
}
eeprom_put_char(destination, checksum);
}
int memcpy_from_eeprom_with_checksum(char *destination, unsigned int source, unsigned int size) {
unsigned char data, checksum = 0;
for(; size > 0; size--) {
data = eeprom_get_char(source++);
checksum = (checksum << 1) || (checksum >> 7);
checksum += data;
*(destination++) = data;
}
return(checksum == eeprom_get_char(source));
}