/[H8]/trunk/docs/MCHPStack2.20/Source/mpfs.c
ViewVC logotype

Contents of /trunk/docs/MCHPStack2.20/Source/mpfs.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 62 - (show annotations) (download)
Tue May 1 08:17:39 2007 UTC (17 years, 2 months ago) by hedin
File MIME type: text/plain
File size: 13725 byte(s)
Removed tcpip stack 4.02 and added tcpip stack 2.20.
1 /*********************************************************************
2 *
3 * Microchip File System Implementaion on PIC18
4 *
5 *********************************************************************
6 * FileName: MPFS.c
7 * Dependencies: stacktsk.H
8 * mpfs.h
9 * Processor: PIC18
10 * Complier: MCC18 v1.00.50 or higher
11 * HITECH PICC-18 V8.10PL1 or higher
12 * Company: Microchip Technology, Inc.
13 *
14 * Software License Agreement
15 *
16 * The software supplied herewith by Microchip Technology Incorporated
17 * (the “Company”) for its PICmicro® Microcontroller is intended and
18 * supplied to you, the Company’s customer, for use solely and
19 * exclusively on Microchip PICmicro Microcontroller products. The
20 * software is owned by the Company and/or its supplier, and is
21 * protected under applicable copyright laws. All rights are reserved.
22 * Any use in violation of the foregoing restrictions may subject the
23 * user to criminal sanctions under applicable laws, as well as to
24 * civil liability for the breach of the terms and conditions of this
25 * license.
26 *
27 * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
28 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
29 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
30 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
31 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
32 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
33 *
34 * HiTech PICC18 Compiler Options excluding device selection:
35 * -FAKELOCAL -G -E -C
36 *
37 *
38 *
39 *
40 *
41 * Author Date Comment
42 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
43 * Nilesh Rajbharti 8/14/01 Original (Rev. 1.0)
44 * Nilesh Rajbharti 2/9/02 Cleanup
45 * Nilesh Rajbharti 5/22/02 Rev 2.0 (See version.log for detail)
46 ********************************************************************/
47 #define THIS_IS_MPFS
48
49 #include <string.h>
50 #include <stdlib.h>
51
52 #include "mpfs.h"
53
54 #if defined(MPFS_USE_EEPROM)
55 #include "xeeprom.h"
56 #endif
57
58 /*
59 * This file system supports short file names i.e. 8 + 3.
60 */
61 #define MAX_FILE_NAME_LEN (12)
62
63 #define MPFS_DATA (0x00)
64 #define MPFS_DELETED (0x01)
65 #define MPFS_DLE (0x03)
66 #define MPFS_ETX (0x04)
67
68 /*
69 * MPFS Structure:
70 *
71 * MPFS_Start:
72 * <MPFS_DATA><Address1><FileName1>
73 * <MPFS_DATA><Address2><FileName2>
74 * ...
75 * <MPFS_ETX><Addressn><FileNamen>
76 * Address1:
77 * <Data1>[<Data2>...<Datan>]<MPFS_ETX><MPFS_INVALID>
78 * ...
79 *
80 * Note: If File data contains either MPFS_DLE or MPFS_ETX
81 * extra MPFS_DLE is stuffed before that byte.
82 */
83 typedef struct _MPFS_ENTRY
84 {
85 BYTE Flag;
86
87 MPFS Address;
88 BYTE Name[MAX_FILE_NAME_LEN]; // 8 + '.' + 3
89 } MPFS_ENTRY;
90
91 static union
92 {
93 struct
94 {
95 unsigned int bNotAvailable : 1;
96 } bits;
97 BYTE Val;
98 } mpfsFlags;
99
100 BYTE mpfsOpenCount;
101
102 #if defined(MPFS_USE_PGRM)
103 /*
104 * An address where MPFS data starts in program memory.
105 */
106 extern MPFS MPFS_Start;
107
108 #else
109
110 #define MPFS_Start MPFS_RESERVE_BLOCK
111
112 #endif
113
114 MPFS _currentHandle;
115 MPFS _currentFile;
116 BYTE _currentCount;
117
118
119 /*********************************************************************
120 * Function: BOOL MPFSInit(void)
121 *
122 * PreCondition: None
123 *
124 * Input: None
125 *
126 * Output: TRUE, if MPFS Storage access is initialized and
127 * MPFS is is ready to be used.
128 * FALSE otherwise
129 *
130 * Side Effects: None
131 *
132 * Overview: None
133 *
134 * Note: None
135 ********************************************************************/
136 BOOL MPFSInit(void)
137 {
138 mpfsOpenCount = 0;
139 mpfsFlags.Val = 0;
140
141 #if defined(MPFS_USE_PGRM)
142 return TRUE;
143 #else
144 /*
145 * Initialize the EEPROM access routines.
146 * Use ~ 400 Khz.
147 */
148 XEEInit(EE_BAUD(CLOCK_FREQ, 400000));
149
150 return TRUE;
151 #endif
152 }
153
154
155 /*********************************************************************
156 * Function: MPFS MPFSOpen(BYTE* file)
157 *
158 * PreCondition: None
159 *
160 * Input: file - NULL terminated file name.
161 *
162 * Output: A handle if file is found
163 * MPFS_INVALID if file is not found.
164 *
165 * Side Effects: None
166 *
167 * Overview: None
168 *
169 * Note: None
170 ********************************************************************/
171 MPFS MPFSOpen(BYTE* file)
172 {
173 MPFS_ENTRY entry;
174 MPFS FAT;
175 BYTE fileNameLen;
176
177 if ( mpfsFlags.bits.bNotAvailable )
178 return MPFS_NOT_AVAILABLE;
179
180 #if defined(MPFS_USE_PGRM)
181 FAT = (MPFS)&MPFS_Start;
182 #else
183 FAT = MPFS_Start;
184 #endif
185
186 /*
187 * If string is empty, do not attempt to find it in FAT.
188 */
189 if ( *file == '\0' )
190 return MPFS_INVALID;
191
192 file = (BYTE*)strupr((char*)file);
193
194 while(1)
195 {
196 #if defined(MPFS_USE_PGRM)
197 // Bring current FAT entry into RAM.
198 memcpypgm2ram(&entry, (ROM void*)FAT, sizeof(entry));
199 #else
200 XEEReadArray(EEPROM_CONTROL, FAT, (unsigned char*)&entry, sizeof(entry));
201 #endif
202
203 // Make sure that it is a valid entry.
204 if ( entry.Flag == MPFS_DATA )
205 {
206 // Does the file name match ?
207 fileNameLen = strlen((char*)file);
208 if ( fileNameLen > MAX_FILE_NAME_LEN )
209 fileNameLen = MAX_FILE_NAME_LEN;
210
211 if( !strncmp((char*)file, (char*)entry.Name, fileNameLen) )
212 {
213 _currentFile = (MPFS)entry.Address;
214 mpfsOpenCount++;
215 return entry.Address;
216 }
217
218 // File does not match. Try next entry...
219 FAT += sizeof(entry);
220 }
221 else if ( entry.Flag == MPFS_ETX )
222 {
223 if ( entry.Address != (MPFS)MPFS_INVALID )
224 FAT = (MPFS)entry.Address;
225 else
226 break;
227 }
228 else
229 return (MPFS)MPFS_INVALID;
230 }
231 return (MPFS)MPFS_INVALID;
232 }
233
234
235 /*********************************************************************
236 * Function: void MPFSClose(void)
237 *
238 * PreCondition: None
239 *
240 * Input: handle - File handle to be closed
241 *
242 * Output: None
243 *
244 * Side Effects: None
245 *
246 * Overview: None
247 *
248 * Note: None
249 ********************************************************************/
250 void MPFSClose(void)
251 {
252 _currentCount = 0;
253 mpfsFlags.bits.bNotAvailable = FALSE;
254 if ( mpfsOpenCount )
255 mpfsOpenCount--;
256 }
257
258
259 /*********************************************************************
260 * Function: BOOL MPFSGetBegin(MPFS handle)
261 *
262 * PreCondition: MPFSOpen() != MPFS_INVALID &&
263 *
264 * Input: handle - handle of file that is to be read
265 *
266 * Output: TRUE if successful
267 * !TRUE otherwise
268 *
269 * Side Effects: None
270 *
271 * Overview: Prepares MPFS storage media for subsequent reads.
272 *
273 * Note: None
274 ********************************************************************/
275 #if defined(MPFS_USE_EEPROM)
276 BOOL MPFSGetBegin(MPFS handle)
277 {
278 _currentHandle = handle;
279 return (XEEBeginRead(EEPROM_CONTROL, handle) == XEE_SUCCESS);
280 }
281 #endif
282
283 /*********************************************************************
284 * Function: BYTE MPFSGet(void)
285 *
286 * PreCondition: MPFSOpen() != MPFS_INVALID &&
287 * MPFSGetBegin() == TRUE
288 *
289 * Input: None
290 *
291 * Output: Data byte from current address.
292 *
293 * Side Effects: None
294 *
295 * Overview: Reads a byte from current address.
296 *
297 * Note: Caller must call MPFSIsEOF() to check for end of
298 * file condition
299 ********************************************************************/
300 BYTE MPFSGet(void)
301 {
302 BYTE t;
303
304 #if defined(MPFS_USE_PGRM)
305 t = (BYTE)*_currentHandle;
306 #else
307 t = XEERead();
308 #endif
309 _currentHandle++;
310
311 if ( t == MPFS_DLE )
312 {
313 #if defined(MPFS_USE_PGRM)
314 t = (BYTE)*_currentHandle;
315 #else
316 t = XEERead();
317 #endif
318 _currentHandle++;
319 }
320 else if ( t == MPFS_ETX )
321 {
322 _currentHandle = MPFS_INVALID;
323 }
324
325 return t;
326
327 }
328
329
330 /*********************************************************************
331 * Function: MPFS MPFSGetEnd(void)
332 *
333 * PreCondition: MPFSOpen() != MPFS_INVALID &&
334 * MPFSGetBegin() = TRUE
335 *
336 * Input: None
337 *
338 * Output: Current mpfs handle.
339 *
340 * Side Effects: None
341 *
342 * Overview: Ends on-going read cycle.
343 * MPFS handle that is returned must be used
344 * for subsequent begin gets..
345 *
346 * Note: None
347 ********************************************************************/
348 #if defined(MPFS_USE_EEPROM)
349 MPFS MPFSGetEnd(void)
350 {
351 XEEEndRead();
352 return _currentHandle;
353 }
354 #endif
355
356
357 /*********************************************************************
358 * Function: MPFS MPFSFormat(void)
359 *
360 * PreCondition: None
361 *
362 * Input: None
363 *
364 * Output: A valid MPFS handle that can be used for MPFSPut
365 *
366 * Side Effects: None
367 *
368 * Overview: Prepares MPFS image to get re-written
369 * Declares MPFS as in use.
370 *
371 * Note: MPFS will be unaccessible until MPFSClose is
372 * called.
373 ********************************************************************/
374 MPFS MPFSFormat(void)
375 {
376 mpfsFlags.bits.bNotAvailable = TRUE;
377 return (MPFS)MPFS_RESERVE_BLOCK;
378 }
379
380
381 /*********************************************************************
382 * Function: BOOL MPFSPutBegin(MPFS handle)
383 *
384 * PreCondition: MPFSInit() and MPFSFormat() are already called.
385 *
386 * Input: handle - handle to where put to begin
387 *
388 * Output: TRUE if successful
389 * !TRUE otherwise
390 *
391 * Side Effects: None
392 *
393 * Overview: Prepares MPFS image to get re-written
394 *
395 * Note: MPFS will be unaccessible until MPFSClose is
396 * called.
397 ********************************************************************/
398 #if defined(MPFS_USE_EEPROM)
399 BOOL MPFSPutBegin(MPFS handle)
400 {
401 //_currentCount = 0;
402 _currentHandle = handle;
403 _currentCount = (BYTE)handle;
404 _currentCount &= (MPFS_WRITE_PAGE_SIZE-1);
405 return (XEEBeginWrite(EEPROM_CONTROL, handle) == XEE_SUCCESS);
406 }
407 #endif
408
409
410 /*********************************************************************
411 * Function: BOOL MPFSPut(BYTE b)
412 *
413 * PreCondition: MPFSFormat() or MPFSCreate() must be called
414 * MPFSPutBegin() is already called.
415 *
416 * Input: b - data to write.
417 *
418 * Output: TRUE if successfull
419 * !TRUE if failed.
420 *
421 * Side Effects: Original MPFS handle is no longer valid.
422 * Updated MPFS handle must be obtained by calling
423 * MPFSPutEnd().
424 *
425 * Overview: None
426 *
427 * Note: Actual write may not get started until internal
428 * write page is full. To ensure that previously
429 * data gets written, caller must call MPFSPutEnd()
430 * after last call to MPFSPut().
431 ********************************************************************/
432 BOOL MPFSPut(BYTE b)
433 {
434 #if defined(MPFS_USE_EEPROM)
435 if ( XEEWrite(b) )
436 return FALSE;
437
438 _currentCount++;
439 _currentHandle++;
440 if ( _currentCount >= MPFS_WRITE_PAGE_SIZE )
441 {
442 MPFSPutEnd();
443 XEEBeginWrite(EEPROM_CONTROL, _currentHandle);
444 }
445 #endif
446 return TRUE;
447 }
448
449 /*********************************************************************
450 * Function: MPFS MPFSPutEnd(void)
451 *
452 * PreCondition: MPFSPutBegin() is already called.
453 *
454 * Input: None
455 *
456 * Output: Up-to-date MPFS handle
457 *
458 * Side Effects: Original MPFS handle is no longer valid.
459 * Updated MPFS handle must be obtained by calling
460 * MPFSPutEnd().
461 *
462 * Overview: None
463 *
464 * Note: Actual write may not get started until internal
465 * write page is full. To ensure that previously
466 * data gets written, caller must call MPFSPutEnd()
467 * after last call to MPFSPut().
468 ********************************************************************/
469 MPFS MPFSPutEnd(void)
470 {
471 #if defined(MPFS_USE_EEPROM)
472 _currentCount = 0;
473 XEEEndWrite();
474 while( XEEIsBusy(EEPROM_CONTROL) );
475 #endif
476
477 return _currentHandle;
478 }
479
480 /*********************************************************************
481 * Function: MPFS MPFSSeek(MPFS_OFFSET offset)
482 *
483 * PreCondition: MPFSGetBegin() is already called.
484 *
485 * Input: offset - Offset from current pointer
486 *
487 * Output: New MPFS handle located to given offset
488 *
489 * Side Effects: None.
490 *
491 * Overview: None
492 *
493 * Note: None.
494 ********************************************************************/
495 MPFS MPFSSeek(MPFS_OFFSET offset)
496 {
497 WORD i;
498
499 MPFSGetBegin(_currentFile);
500
501 i = 0;
502 while(i++ != offset)
503 MPFSGet();
504
505 MPFSGetEnd();
506
507 return _currentHandle;
508 }

  ViewVC Help
Powered by ViewVC 1.1.20