-
Notifications
You must be signed in to change notification settings - Fork 0
/
eeprom_routines.h
124 lines (111 loc) · 3.5 KB
/
eeprom_routines.h
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
// This header file should not be included directly
// Inclusion of this file is provided indirectly by including htc.h
/***********************************************************************/
/****** EEPROM memory read/write macros and function definitions *******/
/***********************************************************************/
#if _EEPROM_INT == _EEREG_INT
#ifdef __FLASHTYPE
#define _ADJ_EECON1() EECON1 &= 0x3F
#else
#define _ADJ_EECON1() (void)0
#endif
#endif
/*
* EEPROM_WRITE Implementations
*/
#define _EEREG_EEPROM_WRITE(addr, value) \
do{ \
while (WR) { \
continue; \
} \
EEADR = (addr); \
EEDATA = (value); \
_ADJ_EECON1(); \
CARRY = 0; \
if (GIE) { \
CARRY = 1; \
} \
GIE = 0; \
CFGS = 0; \
EEPGD = 0; \
WREN = 1; \
EECON2 = 0x55; \
EECON2 = 0xAA; \
WR = 1; \
WREN = 0; \
if (CARRY) { \
GIE = 1; \
} \
} while (0)
#define _NVMREG_EEPROM_WRITE(addr, value) \
do { \
while (NVMCON1bits.WR) { \
continue; \
} \
NVMCON1bits.NVMREGS = 1; \
NVMADRL = (addr) & 0xFF; \
NVMADRH = 0x70; \
NVMDATH = 0; \
NVMDATL = (value) & 0xFF; \
STATUSbits.CARRY = 0; \
if (INTCONbits.GIE) { \
STATUSbits.CARRY = 1; \
} \
INTCONbits.GIE = 0; \
NVMCON1bits.WREN = 1; \
NVMCON2 = 0x55; \
NVMCON2 = 0xAA; \
NVMCON1bits.WR = 1; \
while (NVMCON1bits.WR) { \
continue; \
} \
NVMCON1bits.WREN = 0; \
if (STATUSbits.CARRY) { \
INTCONbits.GIE = 1; \
} \
} \
while (0)
/*
* EEPROM_READ Implementations
*
* NOTE WELL:
* The macro EEPROM_READ() is NOT safe to use immediately after any
* write to EEPROM, as it does NOT wait for WR to clear. This is by
* design, to allow minimal code size if a sequence of reads is
* desired. To guarantee uncorrupted writes, use the function
* eeprom_read() or insert
* while(WR)continue;
* before calling EEPROM_READ().
*/
#define _EEREG_EEPROM_READ(addr) ( \
CFGS = 0, \
EEADR = addr, \
_ADJ_EECON1(), \
EEPGD = 0, \
RD = 1, \
EEDATA)
#define _NVMREG_EEPROM_READ(addr) ( \
NVMCON1bits.NVMREGS = 1, \
NVMADRL = (addr) & 0xFF, \
NVMADRH = 0x70, \
NVMCON1bits.RD = 1, \
NVMDATL)
//
// General Macro Functions
//
//#if EEPROM_SIZE > 0
//#if _EEPROM_INT == _EEREG_INT
#define EEPROM_WRITE(addr, value) _EEREG_EEPROM_WRITE(addr, value)
#define EEPROM_READ(addr) _EEREG_EEPROM_READ(addr)
//#elif _EEPROM_INT == _NVMREG_INT
//#define EEPROM_WRITE(addr, value) _NVMREG_EEPROM_WRITE(addr, value)
//#define EEPROM_READ(addr) _NVMREG_EEPROM_READ(addr)
//#else
//#error "Unknonwn EEPROM register interface"
//#endif
/* library function versions */
//extern void eeprom_write(unsigned char addr, unsigned char value);
//extern unsigned char eeprom_read(unsigned char addr);
//extern void eecpymem(volatile unsigned char *to, __eeprom unsigned char *from, unsigned char size);
//extern void memcpyee(__eeprom unsigned char *to, const unsigned char *from, unsigned char size);
//#endif // end EEPROM routines