1 |
hedin |
15 |
/*********************************************************************
|
2 |
|
|
*
|
3 |
|
|
* Data I2C EEPROM Access Routines
|
4 |
|
|
*
|
5 |
|
|
*********************************************************************
|
6 |
|
|
* FileName: I2CEEPROM.c
|
7 |
|
|
* Dependencies: Compiler.h
|
8 |
|
|
* XEEPROM.h
|
9 |
|
|
* Processor: PIC18
|
10 |
|
|
* Complier: Microchip C18 v3.03 or higher
|
11 |
|
|
* Company: Microchip Technology, Inc.
|
12 |
|
|
*
|
13 |
|
|
* Software License Agreement
|
14 |
|
|
*
|
15 |
|
|
* Copyright © 2002-2007 Microchip Technology Inc. All rights
|
16 |
|
|
* reserved.
|
17 |
|
|
*
|
18 |
|
|
* Microchip licenses to you the right to use, modify, copy, and
|
19 |
|
|
* distribute:
|
20 |
|
|
* (i) the Software when embedded on a Microchip microcontroller or
|
21 |
|
|
* digital signal controller product (“Device”) which is
|
22 |
|
|
* integrated into Licensee’s product; or
|
23 |
|
|
* (ii) ONLY the Software driver source files ENC28J60.c and
|
24 |
|
|
* ENC28J60.h ported to a non-Microchip device used in
|
25 |
|
|
* conjunction with a Microchip ethernet controller for the
|
26 |
|
|
* sole purpose of interfacing with the ethernet controller.
|
27 |
|
|
*
|
28 |
|
|
* You should refer to the license agreement accompanying this
|
29 |
|
|
* Software for additional information regarding your rights and
|
30 |
|
|
* obligations.
|
31 |
|
|
*
|
32 |
|
|
* THE SOFTWARE AND DOCUMENTATION ARE PROVIDED “AS IS” WITHOUT
|
33 |
|
|
* WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
|
34 |
|
|
* LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
|
35 |
|
|
* PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
|
36 |
|
|
* MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
|
37 |
|
|
* CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
|
38 |
|
|
* PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
|
39 |
|
|
* BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
|
40 |
|
|
* THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
|
41 |
|
|
* SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
|
42 |
|
|
* (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
|
43 |
|
|
*
|
44 |
|
|
*
|
45 |
|
|
* Author Date Comment
|
46 |
|
|
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
47 |
|
|
* Nilesh Rajbharti 5/20/02 Original (Rev. 1.0)
|
48 |
|
|
* Howard Schlunder 8/10/06 Renamed registers/bits to use
|
49 |
|
|
* C18/C30 style standard names
|
50 |
|
|
********************************************************************/
|
51 |
|
|
#include "TCPIP Stack/TCPIP.h"
|
52 |
|
|
|
53 |
|
|
#if defined(MPFS_USE_EEPROM) && defined(EEPROM_SCL_TRIS) && defined(STACK_USE_MPFS)
|
54 |
|
|
|
55 |
|
|
// 24LC256 I/O pins
|
56 |
|
|
#define EEPROM_SCL_TRIS (TRISCbits.TRISC3)
|
57 |
|
|
#define EEPROM_SDA_TRIS (TRISCbits.TRISC4)
|
58 |
|
|
#define EEPROM_SCL_IO (PORTCbits.RC3)
|
59 |
|
|
#define EEPROM_SDA_IO (PORTCbits.RC4)
|
60 |
|
|
#define EEPROM_SSPBUF (SSPBUF)
|
61 |
|
|
#define EEPROM_SSPCON1 (SSPCON1)
|
62 |
|
|
#define EEPROM_SSPCON1bits (SSPCON1bits)
|
63 |
|
|
#define EEPROM_SSPCON2 (SSPCON2)
|
64 |
|
|
#define EEPROM_SSPCON2bits (SSPCON2bits)
|
65 |
|
|
#define EEPROM_SSPSTATbits (SSPSTATbits)
|
66 |
|
|
|
67 |
|
|
|
68 |
|
|
// I2C bits are in different registers for 8-bit and 16-bit
|
69 |
|
|
// microcontrollers
|
70 |
|
|
#if defined(__18CXX)
|
71 |
|
|
#define I2CBIT_ACKDT (EEPROM_SSPCON2bits.ACKDT)
|
72 |
|
|
#define I2CBIT_ACKEN (EEPROM_SSPCON2bits.ACKEN)
|
73 |
|
|
#define I2CBIT_RCEN (EEPROM_SSPCON2bits.RCEN)
|
74 |
|
|
#define I2CBIT_PEN (EEPROM_SSPCON2bits.PEN)
|
75 |
|
|
#define I2CBIT_SEN (EEPROM_SSPCON2bits.SEN)
|
76 |
|
|
#define I2CBIT_ACKSTAT (EEPROM_SSPCON2bits.ACKSTAT)
|
77 |
|
|
#define I2CBIT_TRSTAT (EEPROM_SSPSTATbits.R_W)
|
78 |
|
|
// #define I2CBIT_TRSTAT (EEPROM_SSPSTATbits.BF)
|
79 |
|
|
#define I2CBIT_IWCOL (EEPROM_SSPCON1bits.WCOL)
|
80 |
|
|
#define I2C_DATA_IN (EEPROM_SSPBUF)
|
81 |
|
|
#define I2C_DATA_OUT (EEPROM_SSPBUF)
|
82 |
|
|
#elif defined(__C30__)
|
83 |
|
|
#define I2CBIT_ACKDT (EEPROM_I2CCONbits.ACKDT)
|
84 |
|
|
#define I2CBIT_ACKEN (EEPROM_I2CCONbits.ACKEN)
|
85 |
|
|
#define I2CBIT_RCEN (EEPROM_I2CCONbits.RCEN)
|
86 |
|
|
#define I2CBIT_PEN (EEPROM_I2CCONbits.PEN)
|
87 |
|
|
#define I2CBIT_SEN (EEPROM_I2CCONbits.SEN)
|
88 |
|
|
#define I2CBIT_ACKSTAT (EEPROM_I2CSTATbits.ACKSTAT)
|
89 |
|
|
#define I2CBIT_TRSTAT (EEPROM_I2CSTATbits.TRSTAT)
|
90 |
|
|
#define I2CBIT_IWCOL (EEPROM_I2CSTATbits.IWCOL)
|
91 |
|
|
#define I2C_DATA_IN (EEPROM_I2CRCV)
|
92 |
|
|
#define I2C_DATA_OUT (EEPROM_I2CTRN)
|
93 |
|
|
#endif
|
94 |
|
|
|
95 |
|
|
|
96 |
|
|
#define READ (0x01)
|
97 |
|
|
#define WRITE (0x00)
|
98 |
|
|
|
99 |
|
|
static BOOL NeedACK = FALSE;
|
100 |
|
|
static BOOL NeedEndRead = FALSE;
|
101 |
|
|
static BOOL NeedEndWrite = FALSE;
|
102 |
|
|
|
103 |
|
|
|
104 |
|
|
static void Delayus(BYTE v);
|
105 |
|
|
static void I2CStart(void);
|
106 |
|
|
static void I2CStop(void);
|
107 |
|
|
static void I2CAck(BOOL Not);
|
108 |
|
|
static BOOL I2CPut(BYTE c);
|
109 |
|
|
static BYTE I2CGet(void);
|
110 |
|
|
|
111 |
|
|
static void Delayus(BYTE v)
|
112 |
|
|
{
|
113 |
|
|
while(v--)
|
114 |
|
|
{
|
115 |
|
|
Nop();
|
116 |
|
|
}
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
|
120 |
|
|
static void I2CStart(void)
|
121 |
|
|
{
|
122 |
|
|
EEPROM_SCL_TRIS = 1; // Clock line must be high
|
123 |
|
|
while(EEPROM_SCL_IO == 0);
|
124 |
|
|
Delayus(3);
|
125 |
|
|
|
126 |
|
|
// Generate a start condition
|
127 |
|
|
EEPROM_SDA_TRIS = 0;
|
128 |
|
|
Delayus(3);
|
129 |
|
|
}
|
130 |
|
|
|
131 |
|
|
static void I2CStop(void)
|
132 |
|
|
{
|
133 |
|
|
// Pull data low
|
134 |
|
|
EEPROM_SDA_TRIS = 0;
|
135 |
|
|
Delayus(1);
|
136 |
|
|
|
137 |
|
|
|
138 |
|
|
EEPROM_SCL_TRIS = 1; // Clock line must be high
|
139 |
|
|
while(EEPROM_SCL_IO == 0);
|
140 |
|
|
Delayus(3);
|
141 |
|
|
|
142 |
|
|
// Generate a stop condition
|
143 |
|
|
EEPROM_SDA_TRIS = 1;
|
144 |
|
|
Delayus(3);
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
static void I2CAck(BOOL Not)
|
148 |
|
|
{
|
149 |
|
|
// Generate the (N)Ack
|
150 |
|
|
EEPROM_SDA_TRIS = Not;
|
151 |
|
|
Delayus(3);
|
152 |
|
|
|
153 |
|
|
EEPROM_SCL_TRIS = 1; // Release the clock
|
154 |
|
|
while(EEPROM_SCL_IO == 0);
|
155 |
|
|
Delayus(3);
|
156 |
|
|
|
157 |
|
|
EEPROM_SCL_TRIS = 0; // Pull clock low
|
158 |
|
|
Delayus(1);
|
159 |
|
|
EEPROM_SDA_TRIS = 1;
|
160 |
|
|
Delayus(3);
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
static BOOL I2CPut(BYTE c)
|
164 |
|
|
{
|
165 |
|
|
BYTE i;
|
166 |
|
|
|
167 |
|
|
EEPROM_SCL_TRIS = 0; // Clock line must be low
|
168 |
|
|
Delayus(1);
|
169 |
|
|
|
170 |
|
|
for(i = 0; i < 8; i++)
|
171 |
|
|
{
|
172 |
|
|
// Generate the data bit
|
173 |
|
|
EEPROM_SDA_TRIS = ((BYTE_VAL*)&c)->bits.b7;
|
174 |
|
|
Delayus(3);
|
175 |
|
|
EEPROM_SCL_TRIS = 1; // Release the clock
|
176 |
|
|
while(EEPROM_SCL_IO == 0);
|
177 |
|
|
Delayus(3);
|
178 |
|
|
EEPROM_SCL_TRIS = 0; // Pull clock low
|
179 |
|
|
Delayus(1);
|
180 |
|
|
c <<= 1;
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
// Read the ACK response
|
184 |
|
|
EEPROM_SDA_TRIS = 1;
|
185 |
|
|
Delayus(3);
|
186 |
|
|
|
187 |
|
|
EEPROM_SCL_TRIS = 1; // Release the clock
|
188 |
|
|
while(EEPROM_SCL_IO == 0);
|
189 |
|
|
Delayus(1);
|
190 |
|
|
i = EEPROM_SDA_IO;
|
191 |
|
|
Delayus(1);
|
192 |
|
|
i += EEPROM_SDA_IO;
|
193 |
|
|
Delayus(1);
|
194 |
|
|
i += EEPROM_SDA_IO;
|
195 |
|
|
|
196 |
|
|
EEPROM_SCL_TRIS = 0; // Pull clock low
|
197 |
|
|
Delayus(3);
|
198 |
|
|
|
199 |
|
|
return i >= 2;
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
|
203 |
|
|
static BYTE I2CGet(void)
|
204 |
|
|
{
|
205 |
|
|
BYTE i;
|
206 |
|
|
BYTE c;
|
207 |
|
|
|
208 |
|
|
EEPROM_SCL_TRIS = 0; // Clock line must be low
|
209 |
|
|
Delayus(1);
|
210 |
|
|
EEPROM_SDA_TRIS = 1;
|
211 |
|
|
Delayus(3);
|
212 |
|
|
|
213 |
|
|
for(i = 0; i < 8; i++)
|
214 |
|
|
{
|
215 |
|
|
EEPROM_SCL_TRIS = 1; // Release the clock
|
216 |
|
|
while(EEPROM_SCL_IO == 0);
|
217 |
|
|
Delayus(3);
|
218 |
|
|
|
219 |
|
|
// Get the data bit
|
220 |
|
|
c <<= 1;
|
221 |
|
|
((BYTE_VAL*)&c)->bits.b0 = EEPROM_SDA_IO;
|
222 |
|
|
EEPROM_SCL_TRIS = 0; // Pull clock low
|
223 |
|
|
Delayus(3);
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
return c;
|
227 |
|
|
}
|
228 |
|
|
|
229 |
|
|
void XEEInit(void)
|
230 |
|
|
{
|
231 |
|
|
LATCbits.LATC3 = 0;
|
232 |
|
|
LATCbits.LATC4 = 0;
|
233 |
|
|
// EEPROM_SCL_IO = 0;
|
234 |
|
|
// EEPROM_SDA_IO = 0;
|
235 |
|
|
EEPROM_SCL_TRIS = 1; // Set SCL pin to output
|
236 |
|
|
EEPROM_SDA_TRIS = 1; // Set SDA pin to input
|
237 |
|
|
}
|
238 |
|
|
|
239 |
|
|
XEE_RESULT XEEBeginWrite(XEE_ADDR address)
|
240 |
|
|
{
|
241 |
|
|
if(NeedEndRead)
|
242 |
|
|
while(1);
|
243 |
|
|
if(NeedEndWrite)
|
244 |
|
|
while(1);
|
245 |
|
|
|
246 |
|
|
while(XEEIsBusy());
|
247 |
|
|
|
248 |
|
|
NeedEndWrite = TRUE;
|
249 |
|
|
|
250 |
|
|
// Generate a start condition
|
251 |
|
|
I2CStart();
|
252 |
|
|
|
253 |
|
|
// Send "control" byte with device address to slave
|
254 |
|
|
if(I2CPut(EEPROM_CONTROL | WRITE))
|
255 |
|
|
while(1);
|
256 |
|
|
// Send address bytes
|
257 |
|
|
if(I2CPut(((WORD_VAL*)&address)->v[1]))
|
258 |
|
|
while(1);
|
259 |
|
|
if(I2CPut(((WORD_VAL*)&address)->v[0]))
|
260 |
|
|
while(1);
|
261 |
|
|
|
262 |
|
|
return XEE_SUCCESS;
|
263 |
|
|
}
|
264 |
|
|
|
265 |
|
|
XEE_RESULT XEEBeginRead(XEE_ADDR address)
|
266 |
|
|
{
|
267 |
|
|
// Set the address to read from. This procedure is identical
|
268 |
|
|
// for reading and writing.
|
269 |
|
|
XEEBeginWrite(address);
|
270 |
|
|
|
271 |
|
|
NeedEndRead = TRUE;
|
272 |
|
|
NeedEndWrite = FALSE;
|
273 |
|
|
|
274 |
|
|
// Generate a start condition
|
275 |
|
|
I2CStart();
|
276 |
|
|
|
277 |
|
|
// Send "control" byte with device address to slave
|
278 |
|
|
if(I2CPut(EEPROM_CONTROL | READ))
|
279 |
|
|
while(1);
|
280 |
|
|
|
281 |
|
|
return XEE_SUCCESS;
|
282 |
|
|
}
|
283 |
|
|
|
284 |
|
|
XEE_RESULT XEEEndWrite(void)
|
285 |
|
|
{
|
286 |
|
|
if(!NeedEndWrite)
|
287 |
|
|
while(1);
|
288 |
|
|
if(NeedEndRead)
|
289 |
|
|
while(1);
|
290 |
|
|
|
291 |
|
|
NeedEndWrite = FALSE;
|
292 |
|
|
|
293 |
|
|
// Generate a STOP condition
|
294 |
|
|
I2CStop();
|
295 |
|
|
}
|
296 |
|
|
|
297 |
|
|
XEE_RESULT XEEWrite(unsigned char val)
|
298 |
|
|
{
|
299 |
|
|
if(!NeedEndWrite)
|
300 |
|
|
while(1);
|
301 |
|
|
if(NeedEndRead)
|
302 |
|
|
while(1);
|
303 |
|
|
|
304 |
|
|
// Send data byte
|
305 |
|
|
if(I2CPut(val))
|
306 |
|
|
while(1);
|
307 |
|
|
|
308 |
|
|
return XEE_SUCCESS;
|
309 |
|
|
}
|
310 |
|
|
|
311 |
|
|
unsigned char XEERead(void)
|
312 |
|
|
{
|
313 |
|
|
if(NeedEndWrite)
|
314 |
|
|
while(1);
|
315 |
|
|
if(!NeedEndRead)
|
316 |
|
|
while(1);
|
317 |
|
|
|
318 |
|
|
if(NeedACK)
|
319 |
|
|
{
|
320 |
|
|
NeedACK = FALSE;
|
321 |
|
|
I2CAck(FALSE);
|
322 |
|
|
}
|
323 |
|
|
|
324 |
|
|
NeedACK = TRUE;
|
325 |
|
|
|
326 |
|
|
// Receive a byte
|
327 |
|
|
return I2CGet();
|
328 |
|
|
}
|
329 |
|
|
|
330 |
|
|
XEE_RESULT XEEEndRead(void)
|
331 |
|
|
{
|
332 |
|
|
if(NeedEndWrite)
|
333 |
|
|
while(1);
|
334 |
|
|
if(!NeedEndRead)
|
335 |
|
|
while(1);
|
336 |
|
|
|
337 |
|
|
NeedEndRead = FALSE;
|
338 |
|
|
|
339 |
|
|
// Send a NACK to complete the read
|
340 |
|
|
I2CAck(TRUE);
|
341 |
|
|
NeedACK = FALSE;
|
342 |
|
|
|
343 |
|
|
// Generate a STOP condition
|
344 |
|
|
I2CStop();
|
345 |
|
|
|
346 |
|
|
DelayMs(1);//TODO: debug
|
347 |
|
|
|
348 |
|
|
return XEE_SUCCESS;
|
349 |
|
|
}
|
350 |
|
|
|
351 |
|
|
XEE_RESULT XEEReadArray(XEE_ADDR address, unsigned char *buffer, unsigned char length)
|
352 |
|
|
{
|
353 |
|
|
XEEBeginRead(address);
|
354 |
|
|
while(length--)
|
355 |
|
|
*buffer++ = XEERead();
|
356 |
|
|
XEEEndRead();
|
357 |
|
|
|
358 |
|
|
return XEE_SUCCESS;
|
359 |
|
|
}
|
360 |
|
|
|
361 |
|
|
BOOL XEEIsBusy(void)
|
362 |
|
|
{
|
363 |
|
|
BOOL i;
|
364 |
|
|
|
365 |
|
|
if(NeedEndWrite)
|
366 |
|
|
while(1);
|
367 |
|
|
if(NeedEndRead)
|
368 |
|
|
while(1);
|
369 |
|
|
|
370 |
|
|
// Generate a start condition
|
371 |
|
|
I2CStart();
|
372 |
|
|
|
373 |
|
|
// Send "control" byte with device address to slave
|
374 |
|
|
i = I2CPut(EEPROM_CONTROL | WRITE);
|
375 |
|
|
|
376 |
|
|
// Generate a STOP condition
|
377 |
|
|
I2CStop();
|
378 |
|
|
|
379 |
|
|
return i;
|
380 |
|
|
}
|
381 |
|
|
|
382 |
|
|
|
383 |
|
|
//
|
384 |
|
|
///*********************************************************************
|
385 |
|
|
// * Function: void XEEInit(unsigned char baud)
|
386 |
|
|
// *
|
387 |
|
|
// * PreCondition: None
|
388 |
|
|
// *
|
389 |
|
|
// * Input: baud - SSPADD value for bit rate.
|
390 |
|
|
// *
|
391 |
|
|
// * Output: None
|
392 |
|
|
// *
|
393 |
|
|
// * Side Effects: None
|
394 |
|
|
// *
|
395 |
|
|
// * Overview: Initialize I2C module to communicate to serial
|
396 |
|
|
// * EEPROM.
|
397 |
|
|
// *
|
398 |
|
|
// * Note: None
|
399 |
|
|
// ********************************************************************/
|
400 |
|
|
//void XEEInit(BYTE baud)
|
401 |
|
|
//{
|
402 |
|
|
//
|
403 |
|
|
//#if defined(__18CXX)
|
404 |
|
|
// EEPROM_SCL_TRIS = 1; // Set SCL pin to output
|
405 |
|
|
// EEPROM_SDA_TRIS = 1; // Set SDA pin to input
|
406 |
|
|
// SSPCON1 = 0x28; // Set SSPEN and set I2C Master mode
|
407 |
|
|
// SSPADD = (INSTR_FREQ+I2CBAUD/2)/I2CBAUD - 1;
|
408 |
|
|
//#elif defined(__C30__)
|
409 |
|
|
// EEPROM_I2CCONbits.I2CEN = 1;
|
410 |
|
|
// #if defined(__dsPIC30F__)
|
411 |
|
|
// I2CBRG = ((INSTR_FREQ+I2CBAUD/2)/I2CBAUD - (INSTR_FREQ+1111111/2)/1111111)-1; //dsPIC30F only
|
412 |
|
|
// #else
|
413 |
|
|
// I2CBRG = (INSTR_FREQ+I2CBAUD)/(2*I2CBAUD)-1 // PIC24, dsPIC33
|
414 |
|
|
// #endif
|
415 |
|
|
//#endif
|
416 |
|
|
//}
|
417 |
|
|
//
|
418 |
|
|
//
|
419 |
|
|
///*********************************************************************
|
420 |
|
|
// * Macro: XEE_RESULT XEEBeginWrite(XEE_ADDR address)
|
421 |
|
|
// *
|
422 |
|
|
// * PreCondition: XEEInit() is already called.
|
423 |
|
|
// *
|
424 |
|
|
// * Input: control - data EEPROM control code
|
425 |
|
|
// * address - address to where to write
|
426 |
|
|
// *
|
427 |
|
|
// * Output: XEE_SUCCESS if successful
|
428 |
|
|
// * other value if failed.
|
429 |
|
|
// *
|
430 |
|
|
// * Side Effects: None
|
431 |
|
|
// *
|
432 |
|
|
// * Overview: Sets up internal address counter of EEPROM.
|
433 |
|
|
// *
|
434 |
|
|
// * Note: This function does not release the I2C bus.
|
435 |
|
|
// * User must close XEEEndWrite() after this function
|
436 |
|
|
// * is called to relase the I2C bus.
|
437 |
|
|
// ********************************************************************/
|
438 |
|
|
//XEE_RESULT XEEBeginWrite(BYTE control, XEE_ADDR address)
|
439 |
|
|
//{
|
440 |
|
|
// if(NeedEndRead)
|
441 |
|
|
// while(1);
|
442 |
|
|
// if(NeedEndWrite)
|
443 |
|
|
// while(1);
|
444 |
|
|
//
|
445 |
|
|
// NeedEndWrite = TRUE;
|
446 |
|
|
//
|
447 |
|
|
// // Generate a start condition
|
448 |
|
|
// I2CBIT_SEN = 1;
|
449 |
|
|
// while(I2CBIT_SEN);
|
450 |
|
|
//
|
451 |
|
|
// // Send "control" byte with device address to slave
|
452 |
|
|
// do
|
453 |
|
|
// {
|
454 |
|
|
// I2CBIT_IWCOL = 0;
|
455 |
|
|
// I2C_DATA_OUT = EEPROM_CONTROL | WRITE;
|
456 |
|
|
// } while(I2CBIT_IWCOL);
|
457 |
|
|
// while(I2CBIT_TRSTAT);
|
458 |
|
|
// if(I2CBIT_ACKSTAT)
|
459 |
|
|
// while(1);
|
460 |
|
|
//
|
461 |
|
|
// // Send address bytes
|
462 |
|
|
// do
|
463 |
|
|
// {
|
464 |
|
|
// I2CBIT_IWCOL = 0;
|
465 |
|
|
// I2C_DATA_OUT = ((WORD_VAL*)&address)->v[1];
|
466 |
|
|
// } while(I2CBIT_IWCOL);
|
467 |
|
|
// while(I2CBIT_TRSTAT);
|
468 |
|
|
// if(I2CBIT_ACKSTAT)
|
469 |
|
|
// while(1);
|
470 |
|
|
// do
|
471 |
|
|
// {
|
472 |
|
|
// I2CBIT_IWCOL = 0;
|
473 |
|
|
// I2C_DATA_OUT = ((WORD_VAL*)&address)->v[0];
|
474 |
|
|
// } while(I2CBIT_IWCOL);
|
475 |
|
|
// while(I2CBIT_TRSTAT);
|
476 |
|
|
// if(I2CBIT_ACKSTAT)
|
477 |
|
|
// while(1);
|
478 |
|
|
//
|
479 |
|
|
// return XEE_SUCCESS;
|
480 |
|
|
//}
|
481 |
|
|
//
|
482 |
|
|
///*********************************************************************
|
483 |
|
|
// * Function: XEE_RESULT XEEBeginRead(XEE_ADDR address)
|
484 |
|
|
// *
|
485 |
|
|
// * PreCondition: XEEInit() is already called.
|
486 |
|
|
// *
|
487 |
|
|
// * Input: control - EEPROM control and address code.
|
488 |
|
|
// * address - Address at which read is to be performed.
|
489 |
|
|
// *
|
490 |
|
|
// * Output: XEE_SUCCESS if successful
|
491 |
|
|
// * other value if failed.
|
492 |
|
|
// *
|
493 |
|
|
// * Side Effects: None
|
494 |
|
|
// *
|
495 |
|
|
// * Overview: Sets internal address counter to given address.
|
496 |
|
|
// * Puts EEPROM in sequential read mode.
|
497 |
|
|
// *
|
498 |
|
|
// * Note: This function does not release I2C bus.
|
499 |
|
|
// * User must call XEEEndRead() when read is not longer
|
500 |
|
|
// * needed; I2C bus will released after XEEEndRead()
|
501 |
|
|
// * is called.
|
502 |
|
|
// ********************************************************************/
|
503 |
|
|
//XEE_RESULT XEEBeginRead(BYTE control, XEE_ADDR address)
|
504 |
|
|
//{
|
505 |
|
|
// if(NeedEndWrite)
|
506 |
|
|
// while(1);
|
507 |
|
|
// if(NeedEndRead)
|
508 |
|
|
// while(1);
|
509 |
|
|
//
|
510 |
|
|
//
|
511 |
|
|
// // Set the address to read from. This procedure is identical
|
512 |
|
|
// // for reading and writing.
|
513 |
|
|
// XEEBeginWrite(0, address);
|
514 |
|
|
//
|
515 |
|
|
// NeedEndRead = TRUE;
|
516 |
|
|
// NeedEndWrite = FALSE;
|
517 |
|
|
//
|
518 |
|
|
//// // Generate a STOP condition
|
519 |
|
|
//// I2CBIT_PEN = 1;
|
520 |
|
|
//// while(I2CBIT_PEN);
|
521 |
|
|
//
|
522 |
|
|
//
|
523 |
|
|
// EEPROM_SCL_TRIS = 1;
|
524 |
|
|
//
|
525 |
|
|
// // Generate a start condition
|
526 |
|
|
// I2CBIT_SEN = 1;
|
527 |
|
|
// while(I2CBIT_SEN);
|
528 |
|
|
//
|
529 |
|
|
// // Send "control" byte with device address to slave
|
530 |
|
|
// do
|
531 |
|
|
// {
|
532 |
|
|
// I2CBIT_IWCOL = 0;
|
533 |
|
|
// I2C_DATA_OUT = EEPROM_CONTROL | READ;
|
534 |
|
|
// } while(I2CBIT_IWCOL);
|
535 |
|
|
// while(I2CBIT_TRSTAT);
|
536 |
|
|
// if(I2CBIT_ACKSTAT)
|
537 |
|
|
// while(1);
|
538 |
|
|
//
|
539 |
|
|
// return XEE_SUCCESS;
|
540 |
|
|
//}
|
541 |
|
|
//
|
542 |
|
|
//
|
543 |
|
|
//XEE_RESULT XEEWrite(unsigned char val)
|
544 |
|
|
//{
|
545 |
|
|
// if(!NeedEndWrite)
|
546 |
|
|
// while(1);
|
547 |
|
|
// if(NeedEndRead)
|
548 |
|
|
// while(1);
|
549 |
|
|
//
|
550 |
|
|
// // Send data byte
|
551 |
|
|
// do
|
552 |
|
|
// {
|
553 |
|
|
// I2CBIT_IWCOL = 0;
|
554 |
|
|
// I2C_DATA_OUT = val;
|
555 |
|
|
// } while(I2CBIT_IWCOL);
|
556 |
|
|
// while(I2CBIT_TRSTAT);
|
557 |
|
|
// if(I2CBIT_ACKSTAT)
|
558 |
|
|
// while(1);
|
559 |
|
|
//
|
560 |
|
|
// return XEE_SUCCESS;
|
561 |
|
|
//}
|
562 |
|
|
//
|
563 |
|
|
///*********************************************************************
|
564 |
|
|
// * Function: XEE_RESULT XEEEndWrite(void)
|
565 |
|
|
// *
|
566 |
|
|
// * PreCondition: XEEInit() && XEEBeginWrite() are already called.
|
567 |
|
|
// *
|
568 |
|
|
// * Input: None
|
569 |
|
|
// *
|
570 |
|
|
// * Output: XEE_SUCCESS if successful
|
571 |
|
|
// * other value if failed.
|
572 |
|
|
// *
|
573 |
|
|
// * Side Effects: None
|
574 |
|
|
// *
|
575 |
|
|
// * Overview: Instructs EEPROM to begin write cycle.
|
576 |
|
|
// *
|
577 |
|
|
// * Note: Call this function after either page full of bytes
|
578 |
|
|
// * written or no more bytes are left to load.
|
579 |
|
|
// * This function initiates the write cycle.
|
580 |
|
|
// * User must call for XEEWait() to ensure that write
|
581 |
|
|
// * cycle is finished before calling any other
|
582 |
|
|
// * routine.
|
583 |
|
|
// ********************************************************************/
|
584 |
|
|
//XEE_RESULT XEEEndWrite(void)
|
585 |
|
|
//{
|
586 |
|
|
// if(!NeedEndWrite)
|
587 |
|
|
// while(1);
|
588 |
|
|
// if(NeedEndRead)
|
589 |
|
|
// while(1);
|
590 |
|
|
//
|
591 |
|
|
// NeedEndWrite = FALSE;
|
592 |
|
|
//
|
593 |
|
|
// // Generate a STOP condition
|
594 |
|
|
// I2CBIT_PEN = 1;
|
595 |
|
|
// while(I2CBIT_PEN);
|
596 |
|
|
//
|
597 |
|
|
// DelayMs(5);
|
598 |
|
|
//
|
599 |
|
|
//}
|
600 |
|
|
//
|
601 |
|
|
//
|
602 |
|
|
//
|
603 |
|
|
///*********************************************************************
|
604 |
|
|
// * Function: XEE_RESULT XEERead(void)
|
605 |
|
|
// *
|
606 |
|
|
// * PreCondition: XEEInit() && XEEBeginRead() are already called.
|
607 |
|
|
// *
|
608 |
|
|
// * Input: None
|
609 |
|
|
// *
|
610 |
|
|
// * Output: XEE_SUCCESS if successful
|
611 |
|
|
// * other value if failed.
|
612 |
|
|
// *
|
613 |
|
|
// * Side Effects: None
|
614 |
|
|
// *
|
615 |
|
|
// * Overview: Reads next byte from EEPROM; internal address
|
616 |
|
|
// * is incremented by one.
|
617 |
|
|
// *
|
618 |
|
|
// * Note: This function does not release I2C bus.
|
619 |
|
|
// * User must call XEEEndRead() when read is not longer
|
620 |
|
|
// * needed; I2C bus will released after XEEEndRead()
|
621 |
|
|
// * is called.
|
622 |
|
|
// ********************************************************************/
|
623 |
|
|
//unsigned char XEERead(void)
|
624 |
|
|
//{
|
625 |
|
|
// volatile BYTE i;
|
626 |
|
|
//
|
627 |
|
|
// if(NeedEndWrite)
|
628 |
|
|
// while(1);
|
629 |
|
|
// if(!NeedEndRead)
|
630 |
|
|
// while(1);
|
631 |
|
|
//
|
632 |
|
|
// if(NeedACK)
|
633 |
|
|
// {
|
634 |
|
|
// NeedACK = FALSE;
|
635 |
|
|
// I2CBIT_ACKDT = 0;
|
636 |
|
|
// I2CBIT_ACKEN = 1;
|
637 |
|
|
// while(I2CBIT_ACKEN);
|
638 |
|
|
// }
|
639 |
|
|
//
|
640 |
|
|
// // Receive a byte
|
641 |
|
|
// I2CBIT_RCEN = 1;
|
642 |
|
|
// while(I2CBIT_RCEN);
|
643 |
|
|
// NeedACK = TRUE;
|
644 |
|
|
//
|
645 |
|
|
// i = I2C_DATA_IN;
|
646 |
|
|
// return i;
|
647 |
|
|
//}
|
648 |
|
|
//
|
649 |
|
|
///*********************************************************************
|
650 |
|
|
// * Function: XEE_RESULT XEEEndRead(void)
|
651 |
|
|
// *
|
652 |
|
|
// * PreCondition: XEEInit() && XEEBeginRead() are already called.
|
653 |
|
|
// *
|
654 |
|
|
// * Input: None
|
655 |
|
|
// *
|
656 |
|
|
// * Output: XEE_SUCCESS if successful
|
657 |
|
|
// * other value if failed.
|
658 |
|
|
// *
|
659 |
|
|
// * Side Effects: None
|
660 |
|
|
// *
|
661 |
|
|
// * Overview: Ends sequential read cycle.
|
662 |
|
|
// *
|
663 |
|
|
// * Note: This function ends seuential cycle that was in
|
664 |
|
|
// * progress. It releases I2C bus.
|
665 |
|
|
// ********************************************************************/
|
666 |
|
|
//XEE_RESULT XEEEndRead(void)
|
667 |
|
|
//{
|
668 |
|
|
// if(NeedEndWrite)
|
669 |
|
|
// while(1);
|
670 |
|
|
// if(!NeedEndRead)
|
671 |
|
|
// while(1);
|
672 |
|
|
//
|
673 |
|
|
// NeedEndRead = FALSE;
|
674 |
|
|
//
|
675 |
|
|
// // Send a NACK to complete the read
|
676 |
|
|
// I2CBIT_ACKDT = 1;
|
677 |
|
|
// I2CBIT_ACKEN = 1;
|
678 |
|
|
// while(I2CBIT_ACKEN);
|
679 |
|
|
// NeedACK = FALSE;
|
680 |
|
|
//
|
681 |
|
|
// // Generate a STOP condition
|
682 |
|
|
// I2CBIT_PEN = 1;
|
683 |
|
|
// while(I2CBIT_PEN);
|
684 |
|
|
//
|
685 |
|
|
// return XEE_SUCCESS;
|
686 |
|
|
//}
|
687 |
|
|
//
|
688 |
|
|
//
|
689 |
|
|
///*********************************************************************
|
690 |
|
|
// * Function: XEE_RESULT XEEReadArray(XEE_ADDR address,
|
691 |
|
|
// * unsigned char *buffer,
|
692 |
|
|
// * unsigned char length)
|
693 |
|
|
// *
|
694 |
|
|
// * PreCondition: XEEInit() is already called.
|
695 |
|
|
// *
|
696 |
|
|
// * Input: control - EEPROM control and address code.
|
697 |
|
|
// * address - Address from where array is to be read
|
698 |
|
|
// * buffer - Caller supplied buffer to hold the data
|
699 |
|
|
// * length - Number of bytes to read.
|
700 |
|
|
// *
|
701 |
|
|
// * Output: XEE_SUCCESS if successful
|
702 |
|
|
// * other value if failed.
|
703 |
|
|
// *
|
704 |
|
|
// * Side Effects: None
|
705 |
|
|
// *
|
706 |
|
|
// * Overview: Reads desired number of bytes in sequential mode.
|
707 |
|
|
// * This function performs all necessary steps
|
708 |
|
|
// * and releases the bus when finished.
|
709 |
|
|
// *
|
710 |
|
|
// * Note: None
|
711 |
|
|
// ********************************************************************/
|
712 |
|
|
//XEE_RESULT XEEReadArray(BYTE control, XEE_ADDR address,
|
713 |
|
|
// unsigned char *buffer,
|
714 |
|
|
// unsigned char length)
|
715 |
|
|
//{
|
716 |
|
|
// XEEBeginRead(0, address);
|
717 |
|
|
// while(length--)
|
718 |
|
|
// *buffer++ = XEERead();
|
719 |
|
|
// XEEEndRead();
|
720 |
|
|
//
|
721 |
|
|
// return XEE_SUCCESS;
|
722 |
|
|
//}
|
723 |
|
|
//
|
724 |
|
|
//
|
725 |
|
|
///*********************************************************************
|
726 |
|
|
// * Function: BOOL XEEIsBusy(void)
|
727 |
|
|
// *
|
728 |
|
|
// * PreCondition: XEEInit() is already called.
|
729 |
|
|
// *
|
730 |
|
|
// * Input: None
|
731 |
|
|
// *
|
732 |
|
|
// * Output: FALSE if EEPROM is not busy
|
733 |
|
|
// * TRUE if EEPROM is busy or operation failed
|
734 |
|
|
// *
|
735 |
|
|
// * Side Effects: None
|
736 |
|
|
// *
|
737 |
|
|
// * Overview: Requests ACK from EEPROM.
|
738 |
|
|
// *
|
739 |
|
|
// * Note: None
|
740 |
|
|
// ********************************************************************/
|
741 |
|
|
//BOOL XEEIsBusy(BYTE control)
|
742 |
|
|
//{
|
743 |
|
|
// if(NeedEndWrite)
|
744 |
|
|
// while(1);
|
745 |
|
|
// if(NeedEndRead)
|
746 |
|
|
// while(1);
|
747 |
|
|
//
|
748 |
|
|
// // Generate a start condition
|
749 |
|
|
// I2CBIT_SEN = 1;
|
750 |
|
|
// while(I2CBIT_SEN);
|
751 |
|
|
//
|
752 |
|
|
// // Send "control" byte with device address to slave
|
753 |
|
|
// do
|
754 |
|
|
// {
|
755 |
|
|
// I2CBIT_IWCOL = 0;
|
756 |
|
|
// I2C_DATA_OUT = EEPROM_CONTROL | WRITE;
|
757 |
|
|
// } while(I2CBIT_IWCOL);
|
758 |
|
|
// while(I2CBIT_TRSTAT);
|
759 |
|
|
//
|
760 |
|
|
// // Generate a STOP condition
|
761 |
|
|
// I2CBIT_PEN = 1;
|
762 |
|
|
// while(I2CBIT_PEN);
|
763 |
|
|
//
|
764 |
|
|
// return I2CBIT_ACKSTAT;
|
765 |
|
|
//}
|
766 |
|
|
//
|
767 |
|
|
|
768 |
|
|
#endif //#if defined(MPFS_USE_EEPROM) && defined(EEPROM_SCL_TRIS) && defined(STACK_USE_MPFS)
|