AVR-DOS File System

Top  Previous  Next

The AVR-DOS file system is written by Josef Franz Vögel. He can be contacted via the BASCOM forum. Note that it is not permitted to use the AVR-DOS file system for commercial applications without the purchase of a license. A license comes with the ASM source.

You can buy a user license that is suited for most private users.

When you develop a commercial product with AVR-DOS you need the company license.

The ASM source is shipped with both licenses.

 

Josef has put a lot of effort in writing and especially testing the routines.

Josef nor MCS Electronics can be held responsible for any damage or data loss of your CF-cards.

 

The File-System works with Compact – Flash Cards (see AN 123 Accessing a Compact Flash Card from BASCOM and Compact Flash ) and is written for the needs for embedded systems for logging data. There are further functions for binary read and write.

 

notice You do not need AN123. AN123 was used to develop AVR-DOS. So you should use AVR-DOS.

 

The intention in developing the DOS – file system was to keep close to the equivalent VB functions.

 

The Filesystem works with:

FAT16, this means you need to use >= 32MB CF cards
FAT32
Short file name (8.3)
(Files with a long file name can be accessed by their short file name alias)
Files in Root Directory. The root dir can store 512 files. Take in mind that when you use long file names, less filenames can be stored.
Files in SUBDIRS

 

 

Requirements:

Hardware: see AN 123 on http://www.mcselec.com/an_123.htm
Software: appr. 2K-Word Code-Space (4000 Bytes)
SRAM: 561 Bytes for File system Info and DIR-Handle buffer
517 Bytes if FAT is handled in own buffer (for higher speed), otherwise it is handled with the DIR Buffer
534 Bytes for each File handle
This means that a Mega103 or Mega128 is the perfect chip. Other chips have too little internal memory. You could use XRAM memory too with a Mega8515 for example.

 

 

File System Configuration in CONFIG_AVR-DOS.BAS

 

cFileHandles:

Count of File handles: for each file opened at same time, a file handle buffer of 534 Bytes is needed

cSepFATHandle:

For higher speed in handling file operations the FAT info can be stored in a own buffer, which needs additional 517 Bytes.

 

Assign Constant cSepFATHandle with 1, if wanted, otherwise with 0.

 

 

Memory Usage of DOS – File System:

 

1. General File System information

 

Variable Name

Type

Usage

gbDOSError

Byte

holds DOS Error of last file handling routine

gbFileSystem

Byte

File System Code from Master Boot Record

glFATFirstSector

Long

Number of first Sector of FAT Area on the Card

gbNumberOfFATs

Byte

Count of FAT copies

gwSectorsPerFat

Word

Count of Sectors per FAT

glRootFirstSector

Long

Number of first Sector of Root Area on the Card

gwRootEntries

Word

Count of Root Entries

glDataFirstSector

Long

Number of first Sector of Data Area on the Card

gbSectorsPerCluster

Byte

Count of Sectors per Cluster

gwMaxClusterNumber

Word

Highest usable Cluster number

gwLastSearchedCluster

Word

Last cluster number found as free

gwFreeDirEntry

Word

Last directory entry number found as free

glFS_Temp1

Long

temporary Long variable for file system

gsTempFileName

String * 11

temporary String for converting file names

 

 

 

2. Directory

 

Variable Name

Type

Usage

gwDirRootEntry

Word

number of last handled root entry

glDirSectorNumber

Long

Number of current loaded Sector

gbDirBufferStatus

Byte

Buffer Status

gbDirBuffer

Byte (512)

Buffer for directory Sector

 

 

 

3. FAT

 

Variable Name

Type

Usage

glFATSectorNumber

Long

Number of current loaded FAT sector

gbFATBufferStatus

Byte

Buffer status

gbFATBuffer

Byte(512)

buffer for FAT sector

 

 

 

4. File handling

 

Each file handle has a block of 534 Bytes in the variable abFileHandle which is a byte-array of size (534 * cFileHandles)

 

Variable Name

Type

Usage

FileNumber

Byte

File number for identification of the file in I/O operations to the opened file

FileMode

Byte

File open mode

FileRootEntry

Word

Number of root entry

FileFirstCluster

Word

First cluster

FATCluster

Word

cluster of current loaded sector

FileSize

Long

file size in bytes

FilePosition

Long

file pointer (next read/write) 0-based

FileSectorNumber

Long

number of current loaded sector

FileBufferStatus

Byte

buffer Status

FileBuffer

Byte(512)

buffer for the file sector

SectorTerminator

Byte

additional 00 Byte (string terminator) for direct reading ASCII files from the buffer

 

 

 

Error Codes:

Code

Compiler – Alias

Remark

0

cpNoError

No Error

1

cpEndOfFile

Attempt behind End of File

17

cpNoMBR

Sector 0 on Card is not a Master Boot Record

18

cpNoPBR

No Partition Sector

19

cpFileSystemNotSupported

Only FAT16 File system is supported

20

cpSectorSizeNotSupported

Only sector size of 512 Bytes is supported

21

cpSectorsPerClusterNotSupported

Only 1, 2, 4, 8, 16, 32, 64 Sectors per Cluster is supported. This are values of normal formatted partitions. Exotic sizes, which are not power of 2 are not supported

33

cpNoNextCluster

Error in file cluster chain

34

cpNoFreeCluster

No free cluster to allocate (Disk full)

35

cpClusterError

Error in file cluster chain

49

cpNoFreeDirEntry

Directory full

50

cpFileExist

 

65

cpNoFreeFileNumber

No free file number available, only theoretical error, if 255 file handles in use

66

cpFileNotFound

File not found

67

cpFileNumberNotFound

No file handle with such file number

68

cpFileOpenNoHandle

All file handles occupied

69

cpFileOpenHandleInUse

File handle number in use, can't create a new file handle with same file number

70

cpFileOpenShareConflict

Tried to open a file in read and write modus in two file handles

71

cpFileInUse

Can't delete file, which is in use

72

cpFileReadOnly

Can't open a read only file for writing

73

cpFileNoWildCardAllowed

No wildcard allowed in this function

97

cpFilePositionError

 

98

cpFileAccessError

function not allowed in this file open mode

99

cpInvalidFilePosition

new file position pointer is invalid (minus or 0)

100

cpFileSizeToGreat

File size to great for function BLoad

 

 

 

Buffer Status: Bit definitions of Buffer Status Byte (Directory, FAT and File)

 

Bit

DIR

FAT

File

Compiler Alias

Remark

0 (LSB)

 

 

dot

dBOF

Bottom of File (not yet supported)

1

 

 

dot

dEOF

End of File

2

 

 

dot

dEOFinSector

End of File in this sector (last sector)

3

dot

dot

dot

dWritePending

Something was written to sector, it must be saved to Card, before loading next sector

4

 

dot

 

dFATSector

This is an FAT Sector, at writing to Card, Number of FAT copies must be checked and copy updated if necessary

5

 

 

dot

dFileEmpty

File is empty, no sector (Cluster) is allocated in FAT to this file

 

 

Validity of the file I/O operations regarding the opening modes

 

 

Open mode

Action

Input

Output

Append

Binary

Attr

dot

dot

dot

dot

Close

dot

dot

dot

dot

Put

 

 

 

dot

Get

 

 

 

dot

LOF

dot

dot

dot

dot

LOC

dot

dot

dot

dot

EOF

dot

1)

1)

dot

SEEK

dot

dot

dot

dot

SEEK-Set

 

 

 

dot

Line Input

dot

 

 

dot

Print

 

dot

dot

dot

Input

dot

 

 

dot

Write

 

dot

dot

dot

 

 

1) Position pointer is always at End of File

 

Supported statements and functions:

 

INITFILESYSTEM , OPEN , CLOSE, FLUSH , PRINT, LINE INPUT, LOC, LOF , EOF , FREEFILE , FILEATTR , SEEK , BSAVE , BLOAD , KILL , DISKFREE , DISKSIZE , GET , PUT ,FILEDATE , FILETIME , FILEDATETIME , DIR , WRITE , INPUT , FILELEN