-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheeprom.c
149 lines (134 loc) · 3.82 KB
/
eeprom.c
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*! \file eeprom.c
\brief Internal EEPROM functions
*/
#include "crc.h"
#include "pic.h"
#include "eeprom.h"
#include "interrupts.h"
///////////////////////////////////////////////////////////////////
/// Read data to the EEPROM
/// address = location in eeprom
/// data = pointer to data buffer
/// size = total size of data
/// return (1 for success, 0 for failure)
///////////////////////////////////////////////////////////////////
char EEPROM_Read(unsigned int address, char *data, int size)
{
unsigned int t;
if(size > EEPROM_SIZE || (address + size) > EEPROM_SIZE)
return 0;
for(t = 0;t < size ; t ++)
{
EEADR = address & 0xff;
EEADRH = address >> 8;
EECON1bits.EEPGD = 0;
EECON1bits.CFGS = 0;
EECON1bits.RD = 1;
*(char *)(data+t) = EEDATA;
address++;
}
return 1;
}
///////////////////////////////////////////////////////////////////
/// Write data to the EEPROM
/// address = location in eeprom
/// data = pointer to data buffer
/// size = total size of data
/// return (1 for success, 0 for failure)
///////////////////////////////////////////////////////////////////
char EEPROM_Write(int address, char *data, int size)
{
unsigned int t;
if(size > EEPROM_SIZE)
return 0;
if((address + size) > EEPROM_SIZE)
return 0;
for(t = 0;t < size ; t++)
{
EEADR = address & 0xff;
EEADRH = address >> 8;
EEDATA = *(char *)(data + t);
EECON1bits.EEPGD = 0;
EECON1bits.CFGS = 0;
EECON1bits.WREN = 1;
Interrupts_Disable_Global();
EECON2 = 0x55;
EECON2 = 0xaa;
EECON1bits.WR = 1;
while (EECON1bits.WR);
Interrupts_Enable_Global();
address++;
}
EECON1bits.WREN = 0;
return 1;
}
///////////////////////////////////////////////////////////////////
/// Read data to the EEPROM
/// address = location in eeprom
/// data = pointer to data buffer
/// size = total size of data
/// return (1 for success, 0 for failure)
/// The CRC EEPROM functions require four (4) additional
/// bytes. This needs to be accounted for when storing
/// multiple variables in the EEPROM.
///////////////////////////////////////////////////////////////////
char EEPROM_Read_CRC(unsigned int address, void *data, int size)
{
unsigned int t;
unsigned int crc = CRC_CCITT_SEED;
unsigned int eecrc;
if(size > EEPROM_SIZE || (address + size) > EEPROM_SIZE)
return 0;
for(t = 0;t < size ; t ++)
{
EEADR = address & 0xff;
EEADRH = address >> 8;
EECON1bits.EEPGD = 0;
EECON1bits.CFGS = 0;
EECON1bits.RD = 1;
*(char *)(data+t) = EEDATA;
CRC_CCITT_CHAR(EEDATA,&crc);
address++;
}
EEPROM_Read(address, (char *)eecrc,sizeof(eecrc));
if(eecrc != crc)
return 0;
return 1;
}
///////////////////////////////////////////////////////////////////
/// Write data to the EEPROM with a CRC following it
/// address = location in eeprom
/// data = pointer to data buffer
/// size = total size of data
/// return (1 for success, 0 for failure)
/// The CRC EEPROM functions require four (4) additional
/// bytes. This needs to be accounted for when storing
/// multiple variables in the EEPROM.
///////////////////////////////////////////////////////////////////
char EEPROM_Write_CRC(unsigned int address, void *data, int size)
{
unsigned int t;
unsigned int crc = CRC_CCITT_SEED;
if(size > EEPROM_SIZE || (address + size) > EEPROM_SIZE)
return 0;
for(t = 0;t < size ; t++)
{
EEADR = address & 0xff;
EEADRH = address >> 8;
EEDATA = *(char *)(data + t);
CRC_CCITT_CHAR(EEDATA,&crc);
EECON1bits.EEPGD = 0;
EECON1bits.CFGS = 0;
EECON1bits.WREN = 1;
Interrupts_Disable_Global();
EECON2 = 0x55;
EECON2 = 0xaa;
EECON1bits.WR = 1;
while (EECON1bits.WR);
Interrupts_Enable_Global();
address++;
}
EEPROM_Write(address,(char *)crc,sizeof(crc));
EECON1bits.WREN = 0;
return 1;
}