-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathee.c
282 lines (255 loc) · 8.84 KB
/
ee.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/***********************************************************************
MODULE: EEPROM
VERSION: 1.01
CONTAINS: Routines for accessing the EEPROM on the Philips
P89LPC936
COPYRIGHT: Embedded Systems Academy, Inc. - www.esacademy.com
LICENSE: May be freely used in commercial and non-commercial code
without royalties provided this copyright notice remains
in this file and unaltered
WARNING: IF THIS FILE IS REGENERATED BY CODE ARCHITECT ANY CHANGES
MADE WILL BE LOST. WHERE POSSIBLE USE ONLY CODE ARCHITECT
TO CHANGE THE CONTENTS OF THIS FILE
GENERATED: On "Apr 04 2011" at "16:32:43" by Code Architect 2.06
***********************************************************************/
// SFR description needs to be included
#include <reg936.h>
#include "ee.h"
// flag that indicates if the EEPROM is busy or not
static bit meeprombusy;
/***********************************************************************
DESC: Initializes the EEPROM. Enables EEPROM interrupt
RETURNS: Nothing
CAUTION: Set EA to 1 after calling to enable all interrupts
************************************************************************/
void eeprom_init
(
void
)
{
// initially eeprom is not busy
meeprombusy = 0;
// set isr priority to 0
IP1 &= 0x7F;
IP1H &= 0x7F;
// disenable eeprom interrupt
EIEE = 0;
}
/***********************************************************************
DESC: Reads a location in the EEPROM.
If either global interrupts or the EEPROM interrupt is disabled
then the function will return when the operation is complete.
If global interrupts and the EEPROM interrupt are enabled, the
function will return immediately and an interrupt will occur
when the operation is complete.
RETURNS: The 8-bit value read if interrupts are disabled, otherwise
0x00 will be returned.
CAUTION: eeprom_init must be called first
************************************************************************/
unsigned char eeprom_read
(
unsigned int address // 9-bit address to read (0x000 - 0x1FF)
)
{
// wait for previous operation to complete
while (meeprombusy);
// eeprom now busy
meeprombusy = 1;
// store bit 8 of address
// byte operation, clear interrupt flag
DEECON = (address >> 8) & 0x01;
// store bits 0-7 of address
DEEADR = address & 0xFF;
// if not using interrupt then poll for end of operation
if (!EA || !EIEE)
{
// wait for operation to complete
while (!(DEECON & 0x80));
// eeprom no longer busy
meeprombusy = 0;
// return value
return DEEDAT;
}
return 0x00;
}
/***********************************************************************
DESC: Writes to a location in the EEPROM.
If either global interrupts or the EEPROM interrupt is disabled
then the function will return when the operation is complete.
If global interrupts and the EEPROM interrupt are enabled, the
function will return immediately and an interrupt will occur
when the operation is complete.
RETURNS: Nothing
CAUTION: eeprom_init must be called first
************************************************************************/
void eeprom_write
(
unsigned int address, // 9-bit address to write to (0x000 - 0x1FF)
unsigned char value // value to write
)
{
bit eacopy;
// wait for previous operation to complete
while (meeprombusy);
// eeprom now busy
meeprombusy = 1;
// store bit 8 of address
// byte operation, clear interrupt flag
DEECON = (address >> 8) & 0x01;
// disable interrupts
eacopy = EA;
EA = 0;
// store value to write
DEEDAT = value;
// store bits 0-7 of address
DEEADR = address & 0xFF;
// restore interrupts
EA = eacopy;
// if not using interrupt then poll for end of operation
if (!EA || !EIEE)
{
// wait for operation to complete
while (!(DEECON & 0x80));
// eeprom no longer busy
meeprombusy = 0;
}
}
/***********************************************************************
DESC: Writes a value to every location in a 64-byte row in
the EEPROM.
If either global interrupts or the EEPROM interrupt is disabled
then the function will return when the operation is complete.
If global interrupts and the EEPROM interrupt are enabled, the
function will return immediately and an interrupt will occur
when the operation is complete.
RETURNS: Nothing
CAUTION: eeprom_init must be called first
************************************************************************/
void eeprom_fillrow
(
unsigned int address, // 9-bit starting address of row
// (64-byte aligned)
unsigned char value // value to fill row with
)
{
bit eacopy;
// wait for previous operation to complete
while (meeprombusy);
// eeprom now busy
meeprombusy = 1;
// store bit 8 of address
// row fill operation, clear interrupt flag
DEECON = ((address >> 8) & 0x01) | 0x20;
// disable interrupts
eacopy = EA;
EA = 0;
// store fill pattern
DEEDAT = value;
// store bits 0-7 of address (note bits 0-5 are ignored by device)
DEEADR = address & 0xFF;
// restore interrupts
EA = eacopy;
// if not using interrupt then poll for end of operation
if (!EA || !EIEE)
{
// wait for operation to complete
while (!(DEECON & 0x80));
// eeprom no longer busy
meeprombusy = 0;
}
}
/***********************************************************************
DESC: Writes a value to every location in the EEPROM.
If either global interrupts or the EEPROM interrupt is disabled
then the function will return when the operation is complete.
If global interrupts and the EEPROM interrupt are enabled, the
function will return immediately and an interrupt will occur
when the operation is complete.
RETURNS: Nothing
CAUTION: eeprom_init must be called first
************************************************************************/
void eeprom_fill
(
unsigned char value // value to fill EEPROM with
)
{
bit eacopy;
// wait for previous operation to complete
while (meeprombusy);
// eeprom now busy
meeprombusy = 1;
// bit 8 of address = 1
// block fill operation, clear interrupt flag
DEECON = 0x31;
// disable interrupts
eacopy = EA;
EA = 0;
// store fill pattern
DEEDAT = value;
// store anything to address register - value ignored by device
DEEADR = 0x00;
// restore interrupts
EA = eacopy;
// if not using interrupt then poll for end of operation
if (!EA || !EIEE)
{
// wait for operation to complete
while (!(DEECON & 0x80));
// eeprom no longer busy
meeprombusy = 0;
}
}
/***********************************************************************
DESC: Erases a 64-byte row in the EEPROM.
If either global interrupts or the EEPROM interrupt is disabled
then the function will return when the operation is complete.
If global interrupts and the EEPROM interrupt are enabled, the
function will return immediately and an interrupt will occur
when the operation is complete.
Equivalent to eeprom_fillrow(address, 0x00);
RETURNS: Nothing
CAUTION: eeprom_init must be called first
************************************************************************/
void eeprom_eraserow
(
unsigned int address // 9-bit starting address of row
// (64-byte aligned)
)
{
eeprom_fillrow(address, 0x00);
}
/***********************************************************************
DESC: Erases the EEPROM.
If either global interrupts or the EEPROM interrupt is disabled
then the function will return when the operation is complete.
If global interrupts and the EEPROM interrupt are enabled, the
function will return immediately and an interrupt will occur
when the operation is complete.
Equivalent to eeprom_fill(0x00);
RETURNS: Nothing
CAUTION: eeprom_init must be called first
************************************************************************/
void eeprom_erase
(
void
)
{
eeprom_fill(0x00);
}
/***********************************************************************
DESC: EEPROM Interrupt Service Routine
Called when an EEPROM operation has completed
Uses register bank 1
RETURNS: Nothing
CAUTION: eeprom_init must be called first
************************************************************************/
void eeprom_isr
(
void
) interrupt 0 using 1
{
// clear EEIF flag
DEECON &= ~0x80;
// eeprom no longer busy
meeprombusy = 0;
}