fs.c
Go to the documentation of this file.
1 
23 #include "app.h"
24 
25 bank3 WORD gRcp;
26 bank3 WORD gRnor;
27 bank3 WORD gRcb;
28 bank3 WORD gUcp;
29 bank3 WORD gUnor;
30 bank3 WORD gUcb;
31 
32 void FS_FlashTest(void);
33 
34 // Algorithm 2
35 bank3 WORD gPA=0x0000;
36 bank3 WORD gPC=0x0000;
37 
42 void FS_WaitReady(void)
43 {
44  // may be more efficient to have a timeout of 1s
45  while( !IS_DF_READY(DF_ReadStatus()) )
46  continue; // wait complete
47 }
48 
49 
54 void FS_Check( void )
55 {
56  if( gPC==0 )
57  return; // nothing to do
59  FS_WaitReady();
60  gPA++;
61  if( gPA > (MAX_PAGE_1MBIT_DENSITY-1) )
62  gPA = 0; // set to the first
63  --gPC;
64 }
65 
72 BYTE FS_Init( void )
73 {
74  WORD st;
75 
76  st=DF_ReadStatus();
79  {
80 // flashtest();
81  // Read page 0 BOOT Record
82  FS_GetBoot(FSFM_OFF, (BYTE*)&st, sizeof(WORD));
83  if( st!=FSMARK ) // not formatted
84  {
85  FS_Format();
86  gRnor=0;
87  gUnor=0;
88  return FSOK;
89  }
90  FS_Length(DBREGIS, &st);
91  gRnor=st;
92  FS_Length(DBUNIT, &st);
93  gUnor=st;
94  FS_GetBoot(FSTZ_OFF, (BYTE*)&timezone, sizeof(long));
95  return FSOK;
96  }
97 return FSNOCHIP; // ERROR
98 }
99 
107 void FS_SetBoot(WORD Offset, BYTE* data, BYTE size)
108 {
109 BYTE r;
111  FS_WaitReady();
112  DF_WriteBuffer1(Offset, data, size);
113  r=3;
114  do {
116  gPC++;
117  FS_WaitReady();
119  FS_WaitReady();
121  return;
122  } while(r--);
123 }
124 
132 void FS_GetBoot(WORD Offset, BYTE* data, BYTE size)
133 {
135  FS_WaitReady();
136  DF_ReadBuffer1(Offset, data, size);
137 }
138 
143 void FS_Format( void )
144 {
145  WORD data;
146  WORD x;
147  REGIS rg;
148  UNIT ut;
149 //********************** Init each page
150  // clear buffer
151  data=0xFFFF;
152 // data=0x0101;
153  for( x=0; x<PAGESIZE; x+=2 )
154  DF_WriteBuffer1(x, (BYTE*)&data, sizeof(WORD));
155  // REGIS DB 1 to 500
156  rg.ttime=0;
157 // rg.ttime=43;
158  rg.id =0;
159  DF_WriteBuffer1(REGCRL_OFF,(BYTE*)&rg, sizeof(REGIS));
160  for( x=REGSPA; x<DEFMAXREGIS; x++ )
161  {
163  gPC++;
164  FS_WaitReady();
165  }
166 //********************** clear buffer
167  data=0xFFFF;
168  for( x=0; x<PAGESIZE; x+=2 )
169  DF_WriteBuffer1(x, (BYTE*)&data, sizeof(WORD));
170  // UNIT DB 501 to 512
171  ut.id=0;
172  ut.state=0;
173 // ut.state=87;
174  DF_WriteBuffer1(UNICRL_OFF,(BYTE*)&ut, sizeof(UNIT));
175  for( x=UNITSPA; x<DEFMAXUNIT; x++ )
176  {
178  gPC++;
179  FS_WaitReady();
180  }
181 //*************************** clear buffer
182  data=0xFFFF;
183  for( x=0; x<PAGESIZE; x+=2 )
184  DF_WriteBuffer1(x, (BYTE*)&data, sizeof(WORD));
185  // Format mark
186  data = FSMARK;
187  DF_WriteBuffer1(FSFM_OFF, (BYTE*)&data, sizeof(WORD));
188  DF_WriteBuffer1(FSDT_OFF, (BYTE*)&TIME, sizeof(time_t)); // date time
189  DF_WriteBuffer1(FSTZ_OFF, (BYTE*)&timezone, sizeof(long)); // Time zone
190  data = '1';
191  DF_WriteBuffer1(FSDN_OFF, (BYTE*)&data, sizeof(BYTE)); // Device number
192  // Write page
194  gPC++;
195  FS_WaitReady();
196 }
197 
198 
205 {
206  switch(file)
207  {
208  case DBREGIS:
209  gRcb=0;
210  gRcp=REGSPA;
211  break;
212  case DBUNIT:
213  gUcb=0;
214  gUcp=UNITSPA;
215  break;
216  default:
217  return FSNOTFOUND;
218  }
219  return FSOK;
220 }
221 
229 {
230  switch(file)
231  {
232  case DBREGIS:
233  break;
234  case DBUNIT:
235  break;
236  default:
237  return FSNOTFOUND;
238  }
239  return FSOK;
240 }
246 void FS_Length(BYTE file, WORD *p)
247 {
248  WORD x;
249  WORD l;
250  REGIS rg;
251  UNIT ut;
252 
253  l = 0;
254  *p = 0;
255  switch (file)
256  {
257  case DBREGIS: //APPEND
258  // set last page and byte buffer
259  for (x = REGSPA; x < DEFMAXREGIS; x++)
260  {
261  DF_Page2Buffer1(x);
262  FS_WaitReady();
263  DF_ReadBuffer1(REGCRL_OFF, (BYTE*) & rg, sizeof (REGIS));
264  if (rg.id != BADPAGE)
265  if ((WORD) rg.ttime == 0) {
266  *p = l;
267  return;
268  }
269  l += (WORD) rg.ttime;
270  }
271  break;
272  case DBUNIT:
273  // set last page and byte buffer
274  for (x = UNITSPA; x < DEFMAXUNIT; x++)
275  {
276  DF_Page2Buffer1(x);
277  FS_WaitReady();
278  DF_ReadBuffer1(UNICRL_OFF, (BYTE*) & ut, sizeof (UNIT));
279  if (ut.id != BADPAGE)
280  if (ut.state == 0) {
281  *p = l;
282  return;
283  }
284  l += ut.state;
285  }
286  break;
287  default:
288  return;
289  }
290  *p = l;
291 }
295 void FS_ZapRegis(void)
296 {
297  WORD x;
298  REGIS rg;
299  for (x = REGSPA; x < DEFMAXREGIS; x++)
300  {
301  DF_Page2Buffer1(x);
302  FS_WaitReady();
303  DF_ReadBuffer1(REGCRL_OFF, (BYTE*) & rg, sizeof (REGIS));
304  if (rg.ttime == 0)
305  {
306  gRnor = 0;
307  return;
308  }
309  rg.ttime = 0;
310  rg.id = 0;
311  DF_WriteBuffer1(REGCRL_OFF, (BYTE*) & rg, sizeof (REGIS));
313  gPC++;
314  FS_WaitReady();
315  }
316  gRnor = 0;
317 }
318 
325 {
326 REGIS rg;
327 BYTE r;
328  // set last page and byte buffer
329  gRcp = (gRnor/43)+REGSPA;
330  if( gRcp >= DEFMAXREGIS )
331  return FSEOF;
332  gRcb = (gRnor%43)*sizeof(REGIS);
333  if( gRcb >= (PAGESIZE-sizeof(REGIS)) )
334  return FSEOF;
335  // read page
337  FS_WaitReady();
338 
339  DF_ReadBuffer1(REGCRL_OFF, (BYTE*)&rg, sizeof(REGIS));
340  DF_WriteBuffer1(gRcb,(BYTE*)data, sizeof(REGIS));
341  rg.ttime++;
342  gRnor++;
343  DF_WriteBuffer1(REGCRL_OFF,(BYTE*)&rg, sizeof(REGIS));
344  r=3;
345  do {
347  gPC++;
348  FS_WaitReady();
349  DF_Compare(gRcp);
350  FS_WaitReady();
352  return FSOK;
353  } while(r--);
354 
355  return FSEOF;
356 }
357 
365 {
366 REGIS rg;
367  if( gRcb >= (PAGESIZE-sizeof(REGIS)) )
368  {
369  // check for page overflow
370  gRcp++;
371  if( gRcp >= DEFMAXREGIS )
372  return FSEOF;
373  gRcb=0;
374  }
375  // read page
377  FS_WaitReady();
378  DF_ReadBuffer1(REGCRL_OFF, (BYTE*)&rg, sizeof(REGIS));
379  if( rg.ttime == 0 ) return FSEOF;
380  if( gRcb > ((rg.ttime-1)*sizeof(REGIS)) )
381  return FSEOF;
382  DF_ReadBuffer1(gRcb,(BYTE*)data, sizeof(REGIS));
383  gRcb+=sizeof(REGIS);
384  return FSOK;
385 }
386 
393 {
394 UNIT ut;
395 BYTE r;
396  gUcp = (gUnor/87)+UNITSPA;
397  if( gUcp >= DEFMAXUNIT )
398  return FSEOF;
399  gUcb = (gUnor%87)*sizeof(UNIT);
400  if( gUcb >= (PAGESIZE-sizeof(UNIT)) )
401  return FSEOF;
402  // read page
404  FS_WaitReady();
405  DF_ReadBuffer1(UNICRL_OFF, (BYTE*)&ut, sizeof(UNIT));
406  DF_WriteBuffer1(gUcb,(BYTE*)data, sizeof(UNIT));
407  ut.state++;
408  gUnor++;
409  DF_WriteBuffer1(UNICRL_OFF,(BYTE*)&ut, sizeof(UNIT));
410  r=3;
411  do {
413  gPC++;
414  FS_WaitReady();
415  DF_Compare(gUcp);
416  FS_WaitReady();
418  return FSOK;
419  } while(r--);
420  return FSEOF;
421 }
422 
429 {
430 BYTE r;
431  // read page
433  FS_WaitReady();
434  DF_WriteBuffer1(gUcb,(BYTE*)data, sizeof(UNIT));
435  r=3;
436  do {
438  gPC++;
439  FS_WaitReady();
440  DF_Compare(gUcp);
441  FS_WaitReady();
443  return FSOK;
444  } while(r--);
445  return FSEOF;
446 }
447 
455 {
456 UNIT ut;
457  if( gUcb >= (PAGESIZE-sizeof(UNIT)) )
458  {
459  // check for page overflow
460  gUcp++;
461  if(gUcp >= DEFMAXUNIT)
462  return FSEOF;
463  gUcb=0;
464  }
465  // read page
467  FS_WaitReady();
468  DF_ReadBuffer1(UNICRL_OFF, (BYTE*)&ut, sizeof(UNIT));
469  if( ut.state == 0 ) return FSEOF;
470  if( gUcb > ((ut.state-1)*sizeof(UNIT)) )
471  return FSEOF;
472  DF_ReadBuffer1(gUcb,(BYTE*)data, sizeof(UNIT));
473  gUcb+=sizeof(UNIT);
474  return FSOK;
475 }
476 //*********************************************************[ENDL]***************
477 
478 
479 
void FS_ZapRegis(void)
Definition: fs.c:295
volatile bank1 time_t TIME
Timer in unix format.
Definition: jadeku.c:91
#define FSFM_OFF
Format mark.
Definition: fs.h:35
void FS_Format(void)
Definition: fs.c:143
BYTE FS_Close(BYTE file)
Definition: fs.c:228
bank1 long timezone
Definition: timecvt.c:28
#define FSEOF
Definition: fs.h:45
BYTE state
Definition: fs.h:82
#define UNITSPA
Definition: fs.h:59
BYTE FS_AppendUnit(UNIT *data)
Definition: fs.c:392
bank3 WORD gUcb
Definition: fs.c:30
WORD id
Definition: fs.h:78
void DF_ReadBuffer1(WORD Address, BYTE *data, WORD size)
Definition: at45xx.c:79
#define DF_STATUS_DENSITY_MASK
Definition: at45xx.h:78
BYTE FS_Init(void)
Definition: fs.c:72
Definition: fs.h:76
BYTE DF_ReadStatus(void)
Definition: at45xx.c:30
#define FSDN_OFF
Device number.
Definition: fs.h:38
#define BOOTSECTOR
Definition: fs.h:33
void FS_Length(BYTE file, WORD *p)
Definition: fs.c:246
BYTE FS_WriteUnit(UNIT *data)
Definition: fs.c:428
#define DF_1MBIT_DENSITY_CODE
Definition: at45xx.h:79
bank3 WORD gPA
Stored value in flash.
Definition: fs.c:35
BYTE FS_AppendRegis(REGIS *data)
Definition: fs.c:324
void FS_WaitReady(void)
Definition: fs.c:42
#define DBREGIS
Definition: fs.h:89
BYTE FS_ReadRegis(REGIS *data)
Definition: fs.c:364
WORD id
Definition: fs.h:83
void FS_Check(void)
Definition: fs.c:54
#define REGCRL_OFF
Definition: fs.h:62
#define FSNOTFOUND
Definition: fs.h:43
#define BADPAGE
Definition: fs.h:61
bank3 WORD gRcp
Definition: fs.c:25
#define REGSPA
Definition: fs.h:57
#define IS_DF_COMP_OK(_status_)
Definition: at45xx.h:74
void DF_Buffer12MainMemoryE(WORD PageNum)
Definition: at45xx.c:127
void DF_WriteBuffer1(WORD Address, BYTE *Data, WORD size)
Definition: at45xx.c:52
void DF_Compare(WORD Pagenum)
Definition: at45xx.c:192
bank3 WORD gPC
temporal
Definition: fs.c:36
#define MAX_PAGE_1MBIT_DENSITY
Definition: at45xx.h:91
void FS_GetBoot(WORD Offset, BYTE *data, BYTE size)
Definition: fs.c:132
#define FSNOCHIP
Definition: fs.h:44
BYTE FS_Open(BYTE file)
Definition: fs.c:204
void FS_SetBoot(WORD Offset, BYTE *data, BYTE size)
Definition: fs.c:107
#define DBUNIT
Definition: fs.h:90
#define FSOK
Definition: fs.h:40
#define FSMARK
Definition: fs.h:29
unsigned char BYTE
Definition: pgrl.h:38
bank3 WORD gUcp
Definition: fs.c:28
void DF_Page2Buffer1(WORD Pagenum)
Definition: at45xx.c:149
#define DEFMAXUNIT
Definition: fs.h:60
unsigned int WORD
Definition: pgrl.h:45
#define IS_DF_READY(_status_)
Definition: at45xx.h:71
JADEKU project defines and dependency headers.
#define PAGESIZE
Definition: at45xx.h:92
#define DEFMAXREGIS
Definition: fs.h:58
#define FSDT_OFF
date time
Definition: fs.h:36
bank3 WORD gRnor
Definition: fs.c:26
#define FSTZ_OFF
Time zone.
Definition: fs.h:37
Definition: fs.h:81
#define UNICRL_OFF
Definition: fs.h:63
time_t ttime
Definition: fs.h:77
BYTE FS_ReadUnit(UNIT *data)
Definition: fs.c:454
long time_t
Definition: time.h:34
void DF_AutoPageRewrite(WORD PageNum)
Definition: at45xx.c:170
void FS_FlashTest(void)
bank3 WORD gUnor
Definition: fs.c:29
bank3 WORD gRcb
Definition: fs.c:27