Document fait avec Nvu Document made with Nvu
acceuilnouveautesintroductioncoeurmini_systemepages_techniquesrevue_de_presselienscourriel



Technical pages
FLASH memory files system

Instructions
Use
Example

Principle

The file system management in FLASH memory imposes constraints more austere than that of a hard disk file system. Although FLASH memories do not possess mechanical cogs, they are all the same limited in writing number. This number is generally guaranteed in 100000 minimum by the builders. If the hard disk is so limited to him in number of writings, it is generally easily replaceable in a system while FLASH memories are often welded on the same card as the processor. It is so necessary to optimize his use to obtain the maximum of life expectancy for the system set.

I chose so to implement an algorithm allowing to use FLASH memory as a sort of roller so that a new writing always makes in a new memory zone by beginning again at the beginning of this one when it is saturated (a little as a First In/First Out file or FIFO). If FLASH memory is rewritten 100000 times, it will be on all its surface. This implies the management of an index moved in every new writing file. Furthermore, it is necessary to add a management of automatic disappearance of the most ancient sectors which, to complicate the whole, have a superior size (64 kilos Bytes for FLASH memories used in FORTH core card) with regard to the size of an unitarian block ( 512 bytes).

I am not going too much to display me over the details of the principle which I use but it introduces an inconvenience which implies that if it is always possible to read several files simultaneously, only one file can be written at a time. But the writing of several files can be emulated by means of the real-time kernel and of the dynamic RAM allocation.


Instructions

Here is the FORTH instruction set necessary for the FLASH memory management. The first are reserved for the system, the last ones are optimized for the users.

- FLASH_ORIGINE address

Constant giving the beginning address of the FLASH memory zone containing the files system

- MEMOIRE_FLASH address

Variable giving the RAM address of the initial block contents

- FICHIER_CHEMIN address

Variable giving the character string RAM address containing current way

- FLASH_OCCUPE address

Variable containing the FLASH memory activity state

- FLASH_ECRITURE address

Variable indicating if a file is in writing state

num_block,adr FLASH_BLOC_LIT error

Reading of the 512 bytes block number num_block at address adr.
error is zeo when reading is correct

adr,num_block FLASH_BLOC_ECRIT error

Writing of the 512 bytes block number num_block from the address adr.
error is zero when the writing is correct

num_sec FLASH_SECTEUR_EFFACE error

Clearing of the 64 kilos Bytes sector number num_sec.
error is zero when clearing is correct

- FLASH_BLOC_ALLOUE numero/error

Demand of a 512 bytes block of.
If number is zero, it is that there is no more available block.
If number is negative, it is about an error code

numero FLASH_BLOC_LIBERE -

Restoration of a 512 bytes block with its number.

numero FLASH_BLOC_ETAT state

Return 0 if block numero is free.

- FLASH_FORMATE -

FLASH memory file system format

- FLASH_INSTALLE error

Installation of the FLASH memory file system.
error is zero so correct, otherwise:
-30 if the file system is destroyed
-31 if memory space is insufficient to format
-32 if memory space is insufficient for the installation

- BINAIRE_INDEX address

Variable containing the load address of the file " LANCE.BIN "

- S_BINAIRE address

Character string containing " . / BINAIRES / LANCE.BIN "

- BINAIRE_CREE -

Creation of the file " LANCE.BIN "

- BINAIRE_CHARGE -

Load of the file " LANCE.BIN "

- S_LANCE address

Character string containing ". /LANCE"

- LANCE -

Load of the file "LANCE" for system starting up

As already announced first, the following instructions are intended for the users.

adr_way/name,lec/ecr FLASH_FICHIER_OUVRE adr_buffer,size/error

Opening of a file describes by the character string adr_way/name in writing if lec/ecr is zero otherwise in reading.
adr_buffer the descriptor address of the opened file is.
size/error can have value following ones:
size of the file if opening in correct reading (0 if the file does not exist)
0 or size of the already existing file if opening in correct writing
-40 if it is impossible to read a block
-41 if memory space is insufficient to realize the opening
-42 if the file way is unknown
-43 if a writing file is already current while a writing is wanted
-44 if the opened file is a directory

adr_buffer FLASH_FICHIER_LIT size/error,byte

Sequential reading of a byte from file opened with adr_buffer.
size/error can have value following ones:
remaining size of the file if correct reading (> =0)
-1 if an error arose during the reading
-2 if adr_buffer does not correspond to any file in the course of reading

byte,adr_buffer FLASH_FICHIER_ECRIT size/error

Sequential writing of a byte in the file opened with adr_buffer.
size/error can have value following ones:
> 0 if correct writing, value being the number of written bytes (size)
-10 if an error arose during the writing
-11 if the file system is saturated
-12 if adr_buffer does not correspond to any file in the course of writing

adr_buffer FLASH_FICHIER_FERME error

Lock of the file opened with adr_buffer.
error can have value following ones:
0 if correct lock
Arisen error code during preceding operations
-20 if an error arose during the writing
-21 if the file system is saturated
-22 if adr_buffer does not correspond to any file in the course of use

- FICHIER_CHEMIN? address

Assign some memory pointed by address and containing a characters string with the current way.
Assigned memory should be restored ( MEMOIRE_LIBERE) after use.
Return 0 if it ny has more available memory or than the system of file is not functional

adr_name,nbc CHEMIN_CONCATENE adr_mem

Assign some memory pointed by adr_mem and containing the resultant chain of character of the concatenation of the current road and the chain pointed by adr_nom of length nbc.
Assigned memory should be restored ( MEMOIRE_LIBERE) after use.
Return 0 if it ny has more available memory or than the file system is not functional

adr_name REPERTOIRE_LISTER adr_mem,error

Assign some memory which will contain the description of the directory pointed with the character string adr_name.
adr_mem, so different from 0 and error zero, point the following sequence:
number of files/directory on 4 bytes
size of the file/directory 1 on 4 bytes
file (1), directory (2) or unknown (0) type description on 4 bytes,
creation number of the file/directory 1 on 4 bytes
modifications number of the file/directory 1 on 4 bytes
character string with name of the file/directory 1 on n*4 bytes
size of the file/directory 2 on 4 bytes
...
character string with name of the last file/directory on n*4 bytes
0 for the end of the list on 4 bytes
Assigned memory should be restored ( MEMOIRE_LIBERE) after use.

adr_name,nbc,adr_mem,size_max FICHIER_LIRE error

Reading of a file at address adr_mem on a maximal size size_max and the way/name of which is pointed with the character string adr_name of length nbc.
error is zero if everything took place well

adr_mem,size,adr_name,nbc FICHIER_ECRIRE error

Writing of a file from the address adr_mem on a length size and whose way/name is pointed with the character string adr_nom of length nbc.
error is zero if everything took place well

- PWD -

Command showing the characteristics of the file system and the current way

- CD" way" -

Command allowing to move in file system

- LS" way" -

Showing of the contents of the directory "way"

- MKDIR" way/name" -

Creation of the directory "way/name"

- RM" way/name" -

Suppression of the file "way/name"

- RMDIR" way/name" -

Suppression of the directory "way/name"

- MORE" way/name" -

Printing on the screen of the text file "way/name"

- CF" way/name" -

Compilation of the FORTH source file"way/name"

- CP" way1/name1 way2/name2" -

Copy of file

- MV" way1/name1 way2/name2" -

Movement of file

- XMODEM_RECOIT" way/name" -

Reception of the file "way/name" under XMODEM protocol

- XMODEM_EMET" way/name" -

Transmission of the file "way/name" under XMODEM protocol

Use

The writing or the reading of a file requires the use of the following instructions:

  • FLASH_FICHIER_OUVRE,
  • FLASH_FICHIER_LIT or FLASH_FICHIER_ECRIT,
  • FLASH_FICHIER_FERME.

The user contents just so with spending the file name before FLASH_FICHIER_OUVRE instruction which will create then a descriptor in RAM of which the pointer should be kept by the user. This descriptor will be used till the end by the execution of FLASH_FICHIER_FERME instruction which will restore the corresponding RAM.

The user contents so with spending the file name just before the instruction OUVRIR_FICHIER and has to guarantee not to touch it before the end of the execution of the instruction FERMER_FICHIER. It will then be able to be got back for another file or another use.

FLASH_FICHIER_FERME instruction must be necessarily executed in the end of a file operation. In the case of a reading, the RAM of the descriptor will be automatically restored to the system. Furthermore, in writing, the initial block will be updated allowing to use correctly the new file.

The only way of creating a repertoire is to use instruction MKDIR. To kill one of it, it is necessary to use instruction RMDIR but this one will refuse to erase a not empty repertoire.


Example

To illustrate this page, here is the code of the instruction of file copy CP:

: CP"
 32 WORD COUNT CHEMIN_CONCATENE ?DUP
 IF
  DUP >R -1 FLASH_FICHIER_OUVRE R> MEMOIRE_LIBERE DROP DUP 0>
  IF
   34 WORD COUNT CHEMIN_CONCATENE ?DUP
   IF
    DUP >R 0 FLASH_FICHIER_OUVRE R> MEMOIRE_LIBERE DROP 0<
    IF
     SWAP DROP
    ELSE
     SWAP 0 DO
      OVER FLASH_FICHIER_LIT 3 PICK FLASH_FICHIER_ECRIT >R 0< R> 0< OR
      IF
       LEAVE
      THEN
     LOOP
    THEN
    FLASH_FICHIER_FERME
    IF
     ." **** Probleme d'ECRITURE !" CR
    THEN
   ELSE
    DROP
   THEN
   FLASH_FICHIER_FERME DROP
  ELSE
   ." **** Fichier INEXISTANT !" CR
   DROP MEMOIRE_LIBERE DROP
  THEN
 THEN
;