/[H8]/trunk/docs/Microchip TCP_IP stack/TCPIP Stack/MPFS.c
ViewVC logotype

Annotation of /trunk/docs/Microchip TCP_IP stack/TCPIP Stack/MPFS.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 15 - (hide annotations) (download)
Thu Apr 19 09:01:15 2007 UTC (17 years, 2 months ago) by hedin
File MIME type: text/plain
File size: 16933 byte(s)
added the TCP/IP stack, source code.
1 hedin 15 /*********************************************************************
2     *
3     * Microchip File System (MPFS) File Access API
4     * Module for Microchip TCP/IP Stack
5     * -Provides single API for accessing web pages and other files
6     * from internal program memory or an external serial EEPROM memory
7     * -Reference: AN833
8     *
9     *********************************************************************
10     * FileName: MPFS.c
11     * Dependencies: StackTsk.h
12     * MPFS.h
13     * Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F
14     * Complier: Microchip C18 v3.02 or higher
15     * Microchip C30 v2.01 or higher
16     * Company: Microchip Technology, Inc.
17     *
18     * Software License Agreement
19     *
20     * Copyright © 2002-2007 Microchip Technology Inc. All rights
21     * reserved.
22     *
23     * Microchip licenses to you the right to use, modify, copy, and
24     * distribute:
25     * (i) the Software when embedded on a Microchip microcontroller or
26     * digital signal controller product (“Device”) which is
27     * integrated into Licensee’s product; or
28     * (ii) ONLY the Software driver source files ENC28J60.c and
29     * ENC28J60.h ported to a non-Microchip device used in
30     * conjunction with a Microchip ethernet controller for the
31     * sole purpose of interfacing with the ethernet controller.
32     *
33     * You should refer to the license agreement accompanying this
34     * Software for additional information regarding your rights and
35     * obligations.
36     *
37     * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED “AS IS” WITHOUT
38     * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
39     * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
40     * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
41     * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
42     * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
43     * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
44     * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
45     * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
46     * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
47     * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
48     *
49     *
50     * Author Date Comment
51     *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52     * Nilesh Rajbharti 8/14/01 Original (Rev. 1.0)
53     * Nilesh Rajbharti 2/9/02 Cleanup
54     * Nilesh Rajbharti 5/22/02 Rev 2.0 (See version.log for detail)
55     * Howard Schlunder 3/31/05 Changed MPFS_ENTRY and mpfs_Flags for C30
56     ********************************************************************/
57     #define __MPFS_C
58    
59     #include "TCPIP Stack/TCPIP.h"
60    
61     #if defined(STACK_USE_MPFS)
62    
63     // This file system supports short file names i.e. 8 + 3.
64     #define MAX_FILE_NAME_LEN (12u)
65    
66     #define MPFS_DATA (0x00u)
67     #define MPFS_DELETED (0x01u)
68     #define MPFS_DLE (0x03u)
69     #define MPFS_ETX (0x04u)
70    
71     /*
72     * MPFS Structure:
73     *
74     * MPFS_Start:
75     * <MPFS_DATA><Address1><FileName1>
76     * <MPFS_DATA><Address2><FileName2>
77     * ...
78     * <MPFS_ETX><Addressn><FileNamen>
79     * Address1:
80     * <Data1>[<Data2>...<Datan>]<MPFS_ETX><MPFS_INVALID>
81     * ...
82     *
83     * Note: If File data contains either MPFS_DLE or MPFS_ETX
84     * extra MPFS_DLE is stuffed before that byte.
85     */
86     #if defined(MPFS_USE_EEPROM)
87     typedef struct _MPFS_ENTRY
88     {
89     BYTE Flag __attribute__((__packed__));
90     MPFS Address __attribute__((__packed__));
91     BYTE Name[MAX_FILE_NAME_LEN] __attribute__((__packed__));
92     } MPFS_ENTRY;
93     #else //Use program memory
94     typedef struct _MPFS_ENTRY
95     {
96     BYTE Flag;
97     MPFS Address;
98     BYTE Name[MAX_FILE_NAME_LEN];
99     } MPFS_ENTRY;
100     #if defined(__C30__)
101     static DWORD ReadProgramMemory(DWORD address);
102     #endif
103     #endif
104    
105     static union
106     {
107     struct
108     {
109     unsigned char bNotAvailable : 1;
110     } bits;
111     BYTE Val;
112     } mpfsFlags;
113    
114     BYTE mpfsOpenCount;
115    
116     #if !defined(MPFS_USE_EEPROM)
117     // An address where MPFS data starts in program memory.
118     extern MPFS MPFS_Start;
119     #else
120     #define MPFS_Start MPFS_RESERVE_BLOCK
121     #endif
122    
123     MPFS _currentHandle;
124     MPFS _currentFile;
125     BYTE _currentCount;
126    
127    
128     /*********************************************************************
129     * Function: BOOL MPFSInit(void)
130     *
131     * PreCondition: None
132     *
133     * Input: None
134     *
135     * Output: TRUE, if MPFS Storage access is initialized and
136     * MPFS is ready to be used.
137     * FALSE otherwise
138     *
139     * Side Effects: None
140     *
141     * Overview: None
142     *
143     * Note: None
144     ********************************************************************/
145     BOOL MPFSInit(void)
146     {
147     mpfsOpenCount = 0;
148     mpfsFlags.Val = 0;
149    
150     #if defined(MPFS_USE_EEPROM)
151     // Initialize the EEPROM access routines.
152     XEEInit();
153     #endif
154    
155     return TRUE;
156     }
157    
158    
159     /*********************************************************************
160     * Function: MPFS MPFSOpen(BYTE* file)
161     *
162     * PreCondition: None
163     *
164     * Input: file - NULL terminated file name.
165     *
166     * Output: A handle if file is found
167     * MPFS_INVALID if file is not found.
168     *
169     * Side Effects: None
170     *
171     * Overview: None
172     *
173     * Note: None
174     ********************************************************************/
175     MPFS MPFSOpen(BYTE* file)
176     {
177     MPFS_ENTRY entry;
178     MPFS FAT;
179     BYTE fileNameLen;
180    
181     if( mpfsFlags.bits.bNotAvailable )
182     return MPFS_NOT_AVAILABLE;
183    
184     #if defined(MPFS_USE_EEPROM)
185     FAT = MPFS_Start;
186     #else
187     FAT = (MPFS)&MPFS_Start;
188     #endif
189    
190     // If string is empty, do not attempt to find it in FAT.
191     if ( *file == '\0' )
192     return MPFS_INVALID;
193    
194     file = (BYTE*)strupr((char*)file);
195    
196     while(1)
197     {
198     // Bring current FAT entry into RAM.
199     #if defined(MPFS_USE_EEPROM)
200     XEEReadArray(FAT, (unsigned char*)&entry, sizeof(entry));
201     #else
202     #if defined(__C30__)
203     memcpypgm2ram(&entry, (ROM void*)(WORD)FAT, sizeof(entry));
204     #else
205     memcpypgm2ram(&entry, (ROM void*)FAT, sizeof(entry));
206     #endif
207     #endif
208    
209     // Make sure that it is a valid entry.
210     if (entry.Flag == MPFS_DATA)
211     {
212     // Does the file name match ?
213     fileNameLen = strlen((char*)file);
214     if ( fileNameLen > MAX_FILE_NAME_LEN )
215     fileNameLen = MAX_FILE_NAME_LEN;
216    
217     if( memcmp((void*)file, (void*)entry.Name, fileNameLen) == 0 )
218     {
219     _currentFile = (MPFS)entry.Address;
220     mpfsOpenCount++;
221     return entry.Address;
222     }
223    
224     // File does not match. Try next entry...
225     FAT += sizeof(entry);
226     }
227     else if ( entry.Flag == MPFS_ETX )
228     {
229     if ( entry.Address != (MPFS)MPFS_INVALID )
230     FAT = (MPFS)entry.Address;
231     else
232     break;
233     }
234     else
235     return (MPFS)MPFS_INVALID;
236     }
237     return (MPFS)MPFS_INVALID;
238     }
239    
240    
241     /*********************************************************************
242     * Function: void MPFSClose(void)
243     *
244     * PreCondition: None
245     *
246     * Input: handle - File handle to be closed
247     *
248     * Output: None
249     *
250     * Side Effects: None
251     *
252     * Overview: None
253     *
254     * Note: None
255     ********************************************************************/
256     void MPFSClose(void)
257     {
258     _currentCount = 0;
259     mpfsFlags.bits.bNotAvailable = FALSE;
260     if ( mpfsOpenCount )
261     mpfsOpenCount--;
262     }
263    
264    
265     /*********************************************************************
266     * Function: BOOL MPFSGetBegin(MPFS hFile)
267     *
268     * PreCondition: MPFSOpen() != MPFS_INVALID &&
269     *
270     * Input: hFile - handle of file that is to be read
271     *
272     * Output: TRUE if successful
273     * !TRUE otherwise
274     *
275     * Side Effects: None
276     *
277     * Overview: Prepares MPFS storage media for subsequent reads.
278     *
279     * Note: None
280     ********************************************************************/
281     #if defined(MPFS_USE_EEPROM)
282     BOOL MPFSGetBegin(MPFS hFile)
283     {
284     _currentHandle = hFile;
285     return (XEEBeginRead(hFile) == XEE_SUCCESS);
286     }
287     #endif
288    
289     /*********************************************************************
290     * Function: BYTE MPFSGet(void)
291     *
292     * PreCondition: MPFSOpen() != MPFS_INVALID &&
293     * MPFSGetBegin() == TRUE
294     *
295     * Input: None
296     *
297     * Output: Data byte from current address.
298     *
299     * Side Effects: None
300     *
301     * Overview: Reads a byte from current address.
302     *
303     * Note: Caller must call MPFSIsEOF() to check for end of
304     * file condition
305     ********************************************************************/
306     BYTE MPFSGet(void)
307     {
308     BYTE t;
309    
310     #if defined(MPFS_USE_EEPROM)
311     t = XEERead();
312     _currentHandle++;
313     #else
314     #if defined(__C30__)
315     {
316     DWORD_VAL i;
317    
318     // The uppermost byte, ((DWORD_VAL*)&_currentHandle)->v[3]), is the byte lane to read from.
319     // 16 bit PICs have a 24 bit wide Flash program word. Bytes 0-2 are the actual address, but
320     // odd addresses aren't implemented.
321     i.Val = ReadProgramMemory(_currentHandle & 0x00FFFFFF);
322     t = i.v[((DWORD_VAL*)&_currentHandle)->v[3]++];
323     if(((DWORD_VAL*)&_currentHandle)->v[3] >= 3)
324     {
325     _currentHandle = (_currentHandle + 2) & 0x00FFFFFF;
326     }
327     }
328     #else
329     t = (BYTE)*_currentHandle;
330     _currentHandle++;
331     #endif
332     #endif
333    
334     if(t == MPFS_DLE)
335     {
336     #if defined(MPFS_USE_EEPROM)
337     t = XEERead();
338     _currentHandle++;
339     #else
340     #if defined(__C30__)
341     {
342     DWORD_VAL i;
343    
344     // The uppermost byte, ((DWORD_VAL*)&_currentHandle)->v[3]), is the byte lane to read from.
345     // 16 bit PICs have a 24 bit wide Flash program word. Bytes 0-2 are the actual address, but
346     // odd addresses aren't implemented.
347     i.Val = ReadProgramMemory(_currentHandle & 0x00FFFFFF);
348     t = i.v[((DWORD_VAL*)&_currentHandle)->v[3]++];
349     if(((DWORD_VAL*)&_currentHandle)->v[3] >= 3)
350     {
351     _currentHandle = (_currentHandle + 2) & 0x00FFFFFF;
352     }
353     }
354     #else
355     t = (BYTE)*_currentHandle;
356     _currentHandle++;
357     #endif
358     #endif
359     }
360     else if(t == MPFS_ETX)
361     {
362     _currentHandle = MPFS_INVALID;
363     }
364    
365     return t;
366     }
367    
368    
369     #if defined(__C30__) && !defined(MPFS_USE_EEPROM)
370     /*********************************************************************
371     * Function: static DWORD ReadProgramMemory(DWORD address)
372     *
373     * PreCondition: None
374     *
375     * Input: Program memory address to read from. Should be
376     * an even number.
377     *
378     * Output: Program word at the specified address. For the
379     * PIC24, dsPIC, etc. which have a 24 bit program
380     * word size, the upper byte is 0x00.
381     *
382     * Side Effects: None
383     *
384     * Overview: Modifies and restores TBLPAG. Make sure that if
385     * using interrupts and the PSV feature of the CPU
386     * in an ISR that the TBLPAG register is preloaded
387     * with the correct value (rather than assuming
388     * TBLPAG is always pointing to the .const section.
389     *
390     * Note: None
391     ********************************************************************/
392     static DWORD ReadProgramMemory(DWORD address)
393     {
394     asm("mov TBLPAG, W2 ; Save TBLPAG\r\n"
395     "mov W1, TBLPAG ; TBLPAG = HIGHWORD(address)\r\n"
396     "tblrdh [W0], W1 ; W1:W0 = *address\r\n"
397     "tblrdl [W0], W0\r\n"
398     "mov W2, TBLPAG ; Restore TBLPAG\r\n");
399     }
400     #endif
401    
402     /*********************************************************************
403     * Function: MPFS MPFSGetEnd(void)
404     *
405     * PreCondition: MPFSOpen() != MPFS_INVALID &&
406     * MPFSGetBegin() = TRUE
407     *
408     * Input: None
409     *
410     * Output: Current mpfs handle.
411     *
412     * Side Effects: None
413     *
414     * Overview: Ends on-going read cycle.
415     * MPFS handle that is returned must be used
416     * for subsequent begin gets..
417     *
418     * Note: None
419     ********************************************************************/
420     #if defined(MPFS_USE_EEPROM)
421     MPFS MPFSGetEnd(void)
422     {
423     XEEEndRead();
424     return _currentHandle;
425     }
426     #endif
427    
428    
429     /*********************************************************************
430     * Function: MPFS MPFSFormat(void)
431     *
432     * PreCondition: None
433     *
434     * Input: None
435     *
436     * Output: A valid MPFS handle that can be used for MPFSPut
437     *
438     * Side Effects: None
439     *
440     * Overview: Prepares MPFS image to get re-written
441     * Declares MPFS as in use.
442     *
443     * Note: MPFS will be unaccessible until MPFSClose is
444     * called.
445     ********************************************************************/
446     MPFS MPFSFormat(void)
447     {
448     mpfsFlags.bits.bNotAvailable = TRUE;
449     return (MPFS)MPFS_RESERVE_BLOCK;
450     }
451    
452    
453     /*********************************************************************
454     * Function: BOOL MPFSPutBegin(MPFS handle)
455     *
456     * PreCondition: MPFSInit() and MPFSFormat() are already called.
457     *
458     * Input: handle - handle to where put to begin
459     *
460     * Output: TRUE if successful
461     * !TRUE otherwise
462     *
463     * Side Effects: None
464     *
465     * Overview: Prepares MPFS image to get re-written
466     *
467     * Note: MPFS will be unaccessible until MPFSClose is
468     * called.
469     ********************************************************************/
470     #if defined(MPFS_USE_EEPROM)
471     BOOL MPFSPutBegin(MPFS handle)
472     {
473     //_currentCount = 0;
474     _currentHandle = handle;
475     _currentCount = (BYTE)handle;
476     _currentCount &= (MPFS_WRITE_PAGE_SIZE-1);
477     return (XEEBeginWrite(handle) == XEE_SUCCESS);
478     }
479     #endif
480    
481    
482     /*********************************************************************
483     * Function: BOOL MPFSPut(BYTE b)
484     *
485     * PreCondition: MPFSFormat() or MPFSCreate() must be called
486     * MPFSPutBegin() is already called.
487     *
488     * Input: b - data to write.
489     *
490     * Output: TRUE if successfull
491     * !TRUE if failed.
492     *
493     * Side Effects: Original MPFS handle is no longer valid.
494     * Updated MPFS handle must be obtained by calling
495     * MPFSPutEnd().
496     *
497     * Overview: None
498     *
499     * Note: Actual write may not get started until internal
500     * write page is full. To ensure that previously
501     * data gets written, caller must call MPFSPutEnd()
502     * after last call to MPFSPut().
503     ********************************************************************/
504     BOOL MPFSPut(BYTE b)
505     {
506     #if defined(MPFS_USE_EEPROM)
507     if ( XEEWrite(b) )
508     return FALSE;
509    
510     _currentCount++;
511     _currentHandle++;
512     if ( _currentCount >= MPFS_WRITE_PAGE_SIZE )
513     {
514     MPFSPutEnd();
515     XEEBeginWrite(_currentHandle);
516     }
517     #endif
518     return TRUE;
519     }
520    
521     /*********************************************************************
522     * Function: MPFS MPFSPutEnd(void)
523     *
524     * PreCondition: MPFSPutBegin() is already called.
525     *
526     * Input: None
527     *
528     * Output: Up-to-date MPFS handle
529     *
530     * Side Effects: Original MPFS handle is no longer valid.
531     * Updated MPFS handle must be obtained by calling
532     * MPFSPutEnd().
533     *
534     * Overview: None
535     *
536     * Note: Actual write may not get started until internal
537     * write page is full. To ensure that previously
538     * data gets written, caller must call MPFSPutEnd()
539     * after last call to MPFSPut().
540     ********************************************************************/
541     MPFS MPFSPutEnd(void)
542     {
543     #if defined(MPFS_USE_EEPROM)
544     _currentCount = 0;
545     XEEEndWrite();
546     while(XEEIsBusy());
547     #endif
548    
549     return _currentHandle;
550     }
551    
552     /*********************************************************************
553     * Function: MPFS MPFSSeek(MPFS offset)
554     *
555     * PreCondition: MPFSGetBegin() is already called.
556     *
557     * Input: offset - Offset from current pointer
558     *
559     * Output: New MPFS handle located to given offset
560     *
561     * Side Effects: None.
562     *
563     * Overview: None
564     *
565     * Note: None.
566     ********************************************************************/
567     MPFS MPFSSeek(MPFS offset)
568     {
569     MPFS i;
570    
571     MPFSGetBegin(_currentFile);
572    
573     i = (MPFS)0;
574     while(i++ != offset)
575     MPFSGet();
576    
577     MPFSGetEnd();
578    
579     return _currentHandle;
580     }
581    
582     #endif //#if defined(STACK_USE_MPFS)

  ViewVC Help
Powered by ViewVC 1.1.20