No changes between revisions
/Designs/DOGFINDER01A/PrjInfo.txt |
---|
0,0 → 1,18 |
// |
// Toto je popisný soubor pro popis obsahu adresáře (příklad) |
// |
[InfoShortDescription.en] |
Dog finder - Track Logger |
[InfoShortDescription.cs] |
Psí vyhledávač - záznamník trasy |
[InfoLongDescription.en] |
The track is logged in NMEA format on SD/MMC card. |
[InfoLongDescription.cs] |
Umožňuje zaznamanat trasu opakovaně utíkajícího psa. A tím nalézt zdroj problémů, například díru v plotě :) |
Trasa je zaznamenávána na SD/MMC kartu ve formátu NMEA. |
[End] |
/Designs/DOGFINDER01A/SW/ATMEGA168/Makefile |
---|
0,0 → 1,52 |
PRG = glg |
OBJ = main.o tff.o mmc.o |
MCU_TARGET = atmega168 |
OPTIMIZE = -Os -mcall-prologues |
DEFS = |
LIBS = |
DEBUG = dwarf-2 |
CC = avr-gcc |
ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs |
ALL_ASFLAGS = -mmcu=$(MCU_TARGET) -I. -x assembler-with-cpp $(ASFLAGS) |
CFLAGS = -g$(DEBUG) -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS) |
LDFLAGS = -Wl,-Map,$(PRG).map |
OBJCOPY = avr-objcopy |
OBJDUMP = avr-objdump |
SIZE = avr-size |
all: $(PRG).elf lst text size |
$(PRG).elf: $(OBJ) |
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) |
clean: |
rm -rf *.o $(PRG).elf *.eps *.bak *.a |
rm -rf *.lst *.map $(EXTRA_CLEAN_FILES) |
rm -rf $(PRG).hex |
size: $(PRG).elf |
$(SIZE) $(PRG).elf |
lst: $(PRG).lst |
%.lst: %.elf |
$(OBJDUMP) -h -S $< > $@ |
%.o : %.S |
$(CC) -c $(ALL_ASFLAGS) $< -o $@ |
text: hex |
hex: $(PRG).hex |
%.hex: %.elf |
$(OBJCOPY) -j .text -j .data -O ihex $< $@ |
/Designs/DOGFINDER01A/SW/ATMEGA168/diskio.h |
---|
0,0 → 1,64 |
/*----------------------------------------------------------------------- |
/ Low level disk interface modlue include file R0.04a (C)ChaN, 2007 |
/-----------------------------------------------------------------------*/ |
#ifndef _DISKIO |
#define _READONLY 0 /* 1: Read-only mode */ |
#include "integer.h" |
/* Status of Disk Functions */ |
typedef BYTE DSTATUS; |
/* Results of Disk Functions */ |
typedef enum { |
RES_OK = 0, /* 0: Successful */ |
RES_ERROR, /* 1: R/W Error */ |
RES_WRPRT, /* 2: Write Protected */ |
RES_NOTRDY, /* 3: Not Ready */ |
RES_PARERR /* 4: Invalid Parameter */ |
} DRESULT; |
/*---------------------------------------*/ |
/* Prototypes for disk control functions */ |
DSTATUS disk_initialize (BYTE); |
DSTATUS disk_status (BYTE); |
DRESULT disk_read (BYTE, BYTE*, DWORD, BYTE); |
#if _READONLY == 0 |
DRESULT disk_write (BYTE, const BYTE*, DWORD, BYTE); |
#endif |
DRESULT disk_ioctl (BYTE, BYTE, void*); |
void disk_timerproc (void); |
/* Disk Status Bits (DSTATUS) */ |
#define STA_NOINIT 0x01 /* Drive not initialized */ |
#define STA_NODISK 0x02 /* No medium in the drive */ |
#define STA_PROTECT 0x04 /* Write protected */ |
/* Command code for disk_ioctrl() */ |
#define GET_SECTOR_COUNT 1 |
#define GET_SECTOR_SIZE 2 |
#define CTRL_SYNC 3 |
#define CTRL_POWER 4 |
#define CTRL_LOCK 5 |
#define CTRL_EJECT 6 |
#define MMC_GET_CSD 10 |
#define MMC_GET_CID 11 |
#define MMC_GET_OCR 12 |
#define ATA_GET_REV 20 |
#define ATA_GET_MODEL 21 |
#define ATA_GET_SN 22 |
#define _DISKIO |
#endif |
/Designs/DOGFINDER01A/SW/ATMEGA168/ff.c |
---|
0,0 → 1,2036 |
/*----------------------------------------------------------------------------/ |
/ FatFs - FAT file system module R0.06 (C)ChaN, 2008 |
/-----------------------------------------------------------------------------/ |
/ The FatFs module is an experimenal project to implement FAT file system to |
/ cheap microcontrollers. This is a free software and is opened for education, |
/ research and development under license policy of following trems. |
/ |
/ Copyright (C) 2008, ChaN, all right reserved. |
/ |
/ * The FatFs module is a free software and there is no warranty. |
/ * You can use, modify and/or redistribute it for personal, non-profit or |
/ commercial use without restriction under your responsibility. |
/ * Redistributions of source code must retain the above copyright notice. |
/ |
/-----------------------------------------------------------------------------/ |
/ Feb 26,'06 R0.00 Prototype. |
/ |
/ Apr 29,'06 R0.01 First stable version. |
/ |
/ Jun 01,'06 R0.02 Added FAT12 support. |
/ Removed unbuffered mode. |
/ Fixed a problem on small (<32M) patition. |
/ Jun 10,'06 R0.02a Added a configuration option (_FS_MINIMUM). |
/ |
/ Sep 22,'06 R0.03 Added f_rename(). |
/ Changed option _FS_MINIMUM to _FS_MINIMIZE. |
/ Dec 11,'06 R0.03a Improved cluster scan algolithm to write files fast. |
/ Fixed f_mkdir() creates incorrect directory on FAT32. |
/ |
/ Feb 04,'07 R0.04 Supported multiple drive system. |
/ Changed some interfaces for multiple drive system. |
/ Changed f_mountdrv() to f_mount(). |
/ Added f_mkfs(). |
/ Apr 01,'07 R0.04a Supported multiple partitions on a plysical drive. |
/ Added a capability of extending file size to f_lseek(). |
/ Added minimization level 3. |
/ Fixed an endian sensitive code in f_mkfs(). |
/ May 05,'07 R0.04b Added a configuration option _USE_NTFLAG. |
/ Added FSInfo support. |
/ Fixed DBCS name can result FR_INVALID_NAME. |
/ Fixed short seek (<= csize) collapses the file object. |
/ |
/ Aug 25,'07 R0.05 Changed arguments of f_read(), f_write() and f_mkfs(). |
/ Fixed f_mkfs() on FAT32 creates incorrect FSInfo. |
/ Fixed f_mkdir() on FAT32 creates incorrect directory. |
/ Feb 03,'08 R0.05a Added f_truncate() and f_utime(). |
/ Fixed off by one error at FAT sub-type determination. |
/ Fixed btr in f_read() can be mistruncated. |
/ Fixed cached sector is not flushed when create and close |
/ without write. |
/ |
/ Apr 01,'08 R0.06 Added fputc(), fputs(), fprintf() and fgets(). |
/ Improved performance of f_lseek() on moving to the same |
/ or following cluster. |
/---------------------------------------------------------------------------*/ |
#include <string.h> |
#include "ff.h" /* FatFs declarations */ |
#include "diskio.h" /* Include file for user provided disk functions */ |
/*-------------------------------------------------------------------------- |
Module Private Functions |
---------------------------------------------------------------------------*/ |
static |
FATFS *FatFs[_DRIVES]; /* Pointer to the file system objects (logical drives) */ |
static |
WORD fsid; /* File system mount ID */ |
/*-----------------------------------------------------------------------*/ |
/* Change window offset */ |
/*-----------------------------------------------------------------------*/ |
static |
BOOL move_window ( /* TRUE: successful, FALSE: failed */ |
FATFS *fs, /* File system object */ |
DWORD sector /* Sector number to make apperance in the fs->win[] */ |
) /* Move to zero only writes back dirty window */ |
{ |
DWORD wsect; |
wsect = fs->winsect; |
if (wsect != sector) { /* Changed current window */ |
#if !_FS_READONLY |
BYTE n; |
if (fs->winflag) { /* Write back dirty window if needed */ |
if (disk_write(fs->drive, fs->win, wsect, 1) != RES_OK) |
return FALSE; |
fs->winflag = 0; |
if (wsect < (fs->fatbase + fs->sects_fat)) { /* In FAT area */ |
for (n = fs->n_fats; n >= 2; n--) { /* Refrect the change to FAT copy */ |
wsect += fs->sects_fat; |
disk_write(fs->drive, fs->win, wsect, 1); |
} |
} |
} |
#endif |
if (sector) { |
if (disk_read(fs->drive, fs->win, sector, 1) != RES_OK) |
return FALSE; |
fs->winsect = sector; |
} |
} |
return TRUE; |
} |
/*-----------------------------------------------------------------------*/ |
/* Clean-up cached data */ |
/*-----------------------------------------------------------------------*/ |
#if !_FS_READONLY |
static |
FRESULT sync ( /* FR_OK: successful, FR_RW_ERROR: failed */ |
FATFS *fs /* File system object */ |
) |
{ |
fs->winflag = 1; |
if (!move_window(fs, 0)) return FR_RW_ERROR; |
#if _USE_FSINFO |
/* Update FSInfo sector if needed */ |
if (fs->fs_type == FS_FAT32 && fs->fsi_flag) { |
fs->winsect = 0; |
memset(fs->win, 0, 512); |
ST_WORD(&fs->win[BS_55AA], 0xAA55); |
ST_DWORD(&fs->win[FSI_LeadSig], 0x41615252); |
ST_DWORD(&fs->win[FSI_StrucSig], 0x61417272); |
ST_DWORD(&fs->win[FSI_Free_Count], fs->free_clust); |
ST_DWORD(&fs->win[FSI_Nxt_Free], fs->last_clust); |
disk_write(fs->drive, fs->win, fs->fsi_sector, 1); |
fs->fsi_flag = 0; |
} |
#endif |
/* Make sure that no pending write process in the physical drive */ |
if (disk_ioctl(fs->drive, CTRL_SYNC, NULL) != RES_OK) |
return FR_RW_ERROR; |
return FR_OK; |
} |
#endif |
/*-----------------------------------------------------------------------*/ |
/* Get a cluster status */ |
/*-----------------------------------------------------------------------*/ |
static |
DWORD get_cluster ( /* 0,>=2: successful, 1: failed */ |
FATFS *fs, /* File system object */ |
DWORD clust /* Cluster# to get the link information */ |
) |
{ |
WORD wc, bc; |
DWORD fatsect; |
if (clust >= 2 && clust < fs->max_clust) { /* Is it a valid cluster#? */ |
fatsect = fs->fatbase; |
switch (fs->fs_type) { |
case FS_FAT12 : |
bc = (WORD)clust * 3 / 2; |
if (!move_window(fs, fatsect + (bc / SS(fs)))) break; |
wc = fs->win[bc & (SS(fs) - 1)]; bc++; |
if (!move_window(fs, fatsect + (bc / SS(fs)))) break; |
wc |= (WORD)fs->win[bc & (SS(fs) - 1)] << 8; |
return (clust & 1) ? (wc >> 4) : (wc & 0xFFF); |
case FS_FAT16 : |
if (!move_window(fs, fatsect + (clust / (SS(fs) / 2)))) break; |
return LD_WORD(&fs->win[((WORD)clust * 2) & (SS(fs) - 1)]); |
case FS_FAT32 : |
if (!move_window(fs, fatsect + (clust / (SS(fs) / 4)))) break; |
return LD_DWORD(&fs->win[((WORD)clust * 4) & (SS(fs) - 1)]) & 0x0FFFFFFF; |
} |
} |
return 1; /* Out of cluster range, or an error occured */ |
} |
/*-----------------------------------------------------------------------*/ |
/* Change a cluster status */ |
/*-----------------------------------------------------------------------*/ |
#if !_FS_READONLY |
static |
BOOL put_cluster ( /* TRUE: successful, FALSE: failed */ |
FATFS *fs, /* File system object */ |
DWORD clust, /* Cluster# to change (must be 2 to fs->max_clust-1) */ |
DWORD val /* New value to mark the cluster */ |
) |
{ |
WORD bc; |
BYTE *p; |
DWORD fatsect; |
fatsect = fs->fatbase; |
switch (fs->fs_type) { |
case FS_FAT12 : |
bc = (WORD)clust * 3 / 2; |
if (!move_window(fs, fatsect + (bc / SS(fs)))) return FALSE; |
p = &fs->win[bc & (SS(fs) - 1)]; |
*p = (clust & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val; |
bc++; |
fs->winflag = 1; |
if (!move_window(fs, fatsect + (bc / SS(fs)))) return FALSE; |
p = &fs->win[bc & (SS(fs) - 1)]; |
*p = (clust & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F)); |
break; |
case FS_FAT16 : |
if (!move_window(fs, fatsect + (clust / (SS(fs) / 2)))) return FALSE; |
ST_WORD(&fs->win[((WORD)clust * 2) & (SS(fs) - 1)], (WORD)val); |
break; |
case FS_FAT32 : |
if (!move_window(fs, fatsect + (clust / (SS(fs) / 4)))) return FALSE; |
ST_DWORD(&fs->win[((WORD)clust * 4) & (SS(fs) - 1)], val); |
break; |
default : |
return FALSE; |
} |
fs->winflag = 1; |
return TRUE; |
} |
#endif /* !_FS_READONLY */ |
/*-----------------------------------------------------------------------*/ |
/* Remove a cluster chain */ |
/*-----------------------------------------------------------------------*/ |
#if !_FS_READONLY |
static |
BOOL remove_chain ( /* TRUE: successful, FALSE: failed */ |
FATFS *fs, /* File system object */ |
DWORD clust /* Cluster# to remove chain from */ |
) |
{ |
DWORD nxt; |
while (clust >= 2 && clust < fs->max_clust) { |
nxt = get_cluster(fs, clust); |
if (nxt == 1) return FALSE; |
if (!put_cluster(fs, clust, 0)) return FALSE; |
if (fs->free_clust != 0xFFFFFFFF) { |
fs->free_clust++; |
#if _USE_FSINFO |
fs->fsi_flag = 1; |
#endif |
} |
clust = nxt; |
} |
return TRUE; |
} |
#endif |
/*-----------------------------------------------------------------------*/ |
/* Stretch or create a cluster chain */ |
/*-----------------------------------------------------------------------*/ |
#if !_FS_READONLY |
static |
DWORD create_chain ( /* 0: No free cluster, 1: Error, >=2: New cluster number */ |
FATFS *fs, /* File system object */ |
DWORD clust /* Cluster# to stretch, 0 means create new */ |
) |
{ |
DWORD cstat, ncl, scl, mcl = fs->max_clust; |
if (clust == 0) { /* Create new chain */ |
scl = fs->last_clust; /* Get suggested start point */ |
if (scl == 0 || scl >= mcl) scl = 1; |
} |
else { /* Stretch existing chain */ |
cstat = get_cluster(fs, clust); /* Check the cluster status */ |
if (cstat < 2) return 1; /* It is an invalid cluster */ |
if (cstat < mcl) return cstat; /* It is already followed by next cluster */ |
scl = clust; |
} |
ncl = scl; /* Start cluster */ |
for (;;) { |
ncl++; /* Next cluster */ |
if (ncl >= mcl) { /* Wrap around */ |
ncl = 2; |
if (ncl > scl) return 0; /* No free custer */ |
} |
cstat = get_cluster(fs, ncl); /* Get the cluster status */ |
if (cstat == 0) break; /* Found a free cluster */ |
if (cstat == 1) return 1; /* Any error occured */ |
if (ncl == scl) return 0; /* No free custer */ |
} |
if (!put_cluster(fs, ncl, 0x0FFFFFFF)) return 1; /* Mark the new cluster "in use" */ |
if (clust != 0 && !put_cluster(fs, clust, ncl)) return 1; /* Link it to previous one if needed */ |
fs->last_clust = ncl; /* Update fsinfo */ |
if (fs->free_clust != 0xFFFFFFFF) { |
fs->free_clust--; |
#if _USE_FSINFO |
fs->fsi_flag = 1; |
#endif |
} |
return ncl; /* Return new cluster number */ |
} |
#endif /* !_FS_READONLY */ |
/*-----------------------------------------------------------------------*/ |
/* Get sector# from cluster# */ |
/*-----------------------------------------------------------------------*/ |
static |
DWORD clust2sect ( /* !=0: sector number, 0: failed - invalid cluster# */ |
FATFS *fs, /* File system object */ |
DWORD clust /* Cluster# to be converted */ |
) |
{ |
clust -= 2; |
if (clust >= (fs->max_clust - 2)) return 0; /* Invalid cluster# */ |
return clust * fs->csize + fs->database; |
} |
/*-----------------------------------------------------------------------*/ |
/* Move directory pointer to next */ |
/*-----------------------------------------------------------------------*/ |
static |
BOOL next_dir_entry ( /* TRUE: successful, FALSE: could not move next */ |
DIR *dj /* Pointer to directory object */ |
) |
{ |
DWORD clust; |
WORD idx; |
idx = dj->index + 1; |
if ((idx & ((SS(dj->fs) - 1) / 32)) == 0) { /* Table sector changed? */ |
dj->sect++; /* Next sector */ |
if (dj->clust == 0) { /* In static table */ |
if (idx >= dj->fs->n_rootdir) return FALSE; /* Reached to end of table */ |
} else { /* In dynamic table */ |
if (((idx / (SS(dj->fs) / 32)) & (dj->fs->csize - 1)) == 0) { /* Cluster changed? */ |
clust = get_cluster(dj->fs, dj->clust); /* Get next cluster */ |
if (clust < 2 || clust >= dj->fs->max_clust) /* Reached to end of table */ |
return FALSE; |
dj->clust = clust; /* Initialize for new cluster */ |
dj->sect = clust2sect(dj->fs, clust); |
} |
} |
} |
dj->index = idx; /* Lower several bits of dj->index indicates offset in dj->sect */ |
return TRUE; |
} |
/*-----------------------------------------------------------------------*/ |
/* Get file status from directory entry */ |
/*-----------------------------------------------------------------------*/ |
#if _FS_MINIMIZE <= 1 |
static |
void get_fileinfo ( /* No return code */ |
FILINFO *finfo, /* Ptr to store the file information */ |
const BYTE *dir /* Ptr to the directory entry */ |
) |
{ |
BYTE n, c, a; |
char *p; |
p = &finfo->fname[0]; |
a = _USE_NTFLAG ? dir[DIR_NTres] : 0; /* NT flag */ |
for (n = 0; n < 8; n++) { /* Convert file name (body) */ |
c = dir[n]; |
if (c == ' ') break; |
if (c == 0x05) c = 0xE5; |
if (a & 0x08 && c >= 'A' && c <= 'Z') c += 0x20; |
*p++ = c; |
} |
if (dir[8] != ' ') { /* Convert file name (extension) */ |
*p++ = '.'; |
for (n = 8; n < 11; n++) { |
c = dir[n]; |
if (c == ' ') break; |
if (a & 0x10 && c >= 'A' && c <= 'Z') c += 0x20; |
*p++ = c; |
} |
} |
*p = '\0'; |
finfo->fattrib = dir[DIR_Attr]; /* Attribute */ |
finfo->fsize = LD_DWORD(&dir[DIR_FileSize]); /* Size */ |
finfo->fdate = LD_WORD(&dir[DIR_WrtDate]); /* Date */ |
finfo->ftime = LD_WORD(&dir[DIR_WrtTime]); /* Time */ |
} |
#endif /* _FS_MINIMIZE <= 1 */ |
/*-----------------------------------------------------------------------*/ |
/* Pick a paragraph and create the name in format of directory entry */ |
/*-----------------------------------------------------------------------*/ |
static |
char make_dirfile ( /* 1: error - detected an invalid format, '\0'or'/': next character */ |
const char **path, /* Pointer to the file path pointer */ |
char *dirname /* Pointer to directory name buffer {Name(8), Ext(3), NT flag(1)} */ |
) |
{ |
BYTE n, t, c, a, b; |
memset(dirname, ' ', 8+3); /* Fill buffer with spaces */ |
a = 0; b = 0x18; /* NT flag */ |
n = 0; t = 8; |
for (;;) { |
c = *(*path)++; |
if (c == '\0' || c == '/') { /* Reached to end of str or directory separator */ |
if (n == 0) break; |
dirname[11] = _USE_NTFLAG ? (a & b) : 0; |
return c; |
} |
if (c <= ' ' || c == 0x7F) break; /* Reject invisible chars */ |
if (c == '.') { |
if (!(a & 1) && n >= 1 && n <= 8) { /* Enter extension part */ |
n = 8; t = 11; continue; |
} |
break; |
} |
if (_USE_SJIS && |
((c >= 0x81 && c <= 0x9F) || /* Accept S-JIS code */ |
(c >= 0xE0 && c <= 0xFC))) { |
if (n == 0 && c == 0xE5) /* Change heading \xE5 to \x05 */ |
c = 0x05; |
a ^= 0x01; goto md_l2; |
} |
if (c == '"') break; /* Reject " */ |
if (c <= ')') goto md_l1; /* Accept ! # $ % & ' ( ) */ |
if (c <= ',') break; /* Reject * + , */ |
if (c <= '9') goto md_l1; /* Accept - 0-9 */ |
if (c <= '?') break; /* Reject : ; < = > ? */ |
if (!(a & 1)) { /* These checks are not applied to S-JIS 2nd byte */ |
if (c == '|') break; /* Reject | */ |
if (c >= '[' && c <= ']') break;/* Reject [ \ ] */ |
if (_USE_NTFLAG && c >= 'A' && c <= 'Z') |
(t == 8) ? (b &= 0xF7) : (b &= 0xEF); |
if (c >= 'a' && c <= 'z') { /* Convert to upper case */ |
c -= 0x20; |
if (_USE_NTFLAG) (t == 8) ? (a |= 0x08) : (a |= 0x10); |
} |
} |
md_l1: |
a &= 0xFE; |
md_l2: |
if (n >= t) break; |
dirname[n++] = c; |
} |
return 1; |
} |
/*-----------------------------------------------------------------------*/ |
/* Trace a file path */ |
/*-----------------------------------------------------------------------*/ |
static |
FRESULT trace_path ( /* FR_OK(0): successful, !=0: error code */ |
DIR *dj, /* Pointer to directory object to return last directory */ |
char *fn, /* Pointer to last segment name to return {file(8),ext(3),attr(1)} */ |
const char *path, /* Full-path string to trace a file or directory */ |
BYTE **dir /* Pointer to pointer to found entry to retutn */ |
) |
{ |
DWORD clust; |
char ds; |
BYTE *dptr = NULL; |
FATFS *fs = dj->fs; |
/* Initialize directory object */ |
clust = fs->dirbase; |
if (fs->fs_type == FS_FAT32) { |
dj->clust = dj->sclust = clust; |
dj->sect = clust2sect(fs, clust); |
} else { |
dj->clust = dj->sclust = 0; |
dj->sect = clust; |
} |
dj->index = 0; |
if (*path == '\0') { /* Null path means the root directory */ |
*dir = NULL; return FR_OK; |
} |
for (;;) { |
ds = make_dirfile(&path, fn); /* Get a paragraph into fn[] */ |
if (ds == 1) return FR_INVALID_NAME; |
for (;;) { |
if (!move_window(fs, dj->sect)) return FR_RW_ERROR; |
dptr = &fs->win[(dj->index & ((SS(fs) - 1) / 32)) * 32]; /* Pointer to the directory entry */ |
if (dptr[DIR_Name] == 0) /* Has it reached to end of dir? */ |
return !ds ? FR_NO_FILE : FR_NO_PATH; |
if (dptr[DIR_Name] != 0xE5 /* Matched? */ |
&& !(dptr[DIR_Attr] & AM_VOL) |
&& !memcmp(&dptr[DIR_Name], fn, 8+3) ) break; |
if (!next_dir_entry(dj)) /* Next directory pointer */ |
return !ds ? FR_NO_FILE : FR_NO_PATH; |
} |
if (!ds) { *dir = dptr; return FR_OK; } /* Matched with end of path */ |
if (!(dptr[DIR_Attr] & AM_DIR)) return FR_NO_PATH; /* Cannot trace because it is a file */ |
clust = ((DWORD)LD_WORD(&dptr[DIR_FstClusHI]) << 16) | LD_WORD(&dptr[DIR_FstClusLO]); /* Get cluster# of the directory */ |
dj->clust = dj->sclust = clust; /* Restart scanning at the new directory */ |
dj->sect = clust2sect(fs, clust); |
dj->index = 2; |
} |
} |
/*-----------------------------------------------------------------------*/ |
/* Reserve a directory entry */ |
/*-----------------------------------------------------------------------*/ |
#if !_FS_READONLY |
static |
FRESULT reserve_direntry ( /* FR_OK: successful, FR_DENIED: no free entry, FR_RW_ERROR: a disk error occured */ |
DIR *dj, /* Target directory to create new entry */ |
BYTE **dir /* Pointer to pointer to created entry to retutn */ |
) |
{ |
DWORD clust, sector; |
BYTE c, n, *dptr; |
FATFS *fs = dj->fs; |
/* Re-initialize directory object */ |
clust = dj->sclust; |
if (clust != 0) { /* Dyanmic directory table */ |
dj->clust = clust; |
dj->sect = clust2sect(fs, clust); |
} else { /* Static directory table */ |
dj->sect = fs->dirbase; |
} |
dj->index = 0; |
do { |
if (!move_window(fs, dj->sect)) return FR_RW_ERROR; |
dptr = &fs->win[(dj->index & ((SS(dj->fs) - 1) / 32)) * 32]; /* Pointer to the directory entry */ |
c = dptr[DIR_Name]; |
if (c == 0 || c == 0xE5) { /* Found an empty entry */ |
*dir = dptr; return FR_OK; |
} |
} while (next_dir_entry(dj)); /* Next directory pointer */ |
/* Reached to end of the directory table */ |
/* Abort when it is a static table or could not stretch dynamic table */ |
if (clust == 0 || !(clust = create_chain(fs, dj->clust))) return FR_DENIED; |
if (clust == 1 || !move_window(fs, 0)) return FR_RW_ERROR; |
/* Cleanup the expanded table */ |
fs->winsect = sector = clust2sect(fs, clust); |
memset(fs->win, 0, SS(fs)); |
for (n = fs->csize; n; n--) { |
if (disk_write(fs->drive, fs->win, sector, 1) != RES_OK) |
return FR_RW_ERROR; |
sector++; |
} |
fs->winflag = 1; |
*dir = fs->win; |
return FR_OK; |
} |
#endif /* !_FS_READONLY */ |
/*-----------------------------------------------------------------------*/ |
/* Load boot record and check if it is an FAT boot record */ |
/*-----------------------------------------------------------------------*/ |
static |
BYTE check_fs ( /* 0:The FAT boot record, 1:Valid boot record but not an FAT, 2:Not a boot record or error */ |
FATFS *fs, /* File system object */ |
DWORD sect /* Sector# (lba) to check if it is an FAT boot record or not */ |
) |
{ |
if (disk_read(fs->drive, fs->win, sect, 1) != RES_OK) /* Load boot record */ |
return 2; |
if (LD_WORD(&fs->win[BS_55AA]) != 0xAA55) /* Check record signature (always placed at offset 510 even if the sector size is >512) */ |
return 2; |
if (!memcmp(&fs->win[BS_FilSysType], "FAT", 3)) /* Check FAT signature */ |
return 0; |
if (!memcmp(&fs->win[BS_FilSysType32], "FAT32", 5) && !(fs->win[BPB_ExtFlags] & 0x80)) |
return 0; |
return 1; |
} |
/*-----------------------------------------------------------------------*/ |
/* Make sure that the file system is valid */ |
/*-----------------------------------------------------------------------*/ |
static |
FRESULT auto_mount ( /* FR_OK(0): successful, !=0: any error occured */ |
const char **path, /* Pointer to pointer to the path name (drive number) */ |
FATFS **rfs, /* Pointer to pointer to the found file system object */ |
BYTE chk_wp /* !=0: Check media write protection for write access */ |
) |
{ |
BYTE drv, fmt, *tbl; |
DSTATUS stat; |
DWORD bootsect, fatsize, totalsect, maxclust; |
const char *p = *path; |
FATFS *fs; |
/* Get drive number from the path name */ |
while (*p == ' ') p++; /* Strip leading spaces */ |
drv = p[0] - '0'; /* Is there a drive number? */ |
if (drv <= 9 && p[1] == ':') |
p += 2; /* Found a drive number, get and strip it */ |
else |
drv = 0; /* No drive number is given, use drive number 0 as default */ |
if (*p == '/') p++; /* Strip heading slash */ |
*path = p; /* Return pointer to the path name */ |
/* Check if the drive number is valid or not */ |
if (drv >= _DRIVES) return FR_INVALID_DRIVE; /* Is the drive number valid? */ |
*rfs = fs = FatFs[drv]; /* Returen pointer to the corresponding file system object */ |
if (!fs) return FR_NOT_ENABLED; /* Is the file system object registered? */ |
if (fs->fs_type) { /* If the logical drive has been mounted */ |
stat = disk_status(fs->drive); |
if (!(stat & STA_NOINIT)) { /* and physical drive is kept initialized (has not been changed), */ |
#if !_FS_READONLY |
if (chk_wp && (stat & STA_PROTECT)) /* Check write protection if needed */ |
return FR_WRITE_PROTECTED; |
#endif |
return FR_OK; /* The file system object is valid */ |
} |
} |
/* The logical drive must be re-mounted. Following code attempts to mount the logical drive */ |
memset(fs, 0, sizeof(FATFS)); /* Clean-up the file system object */ |
fs->drive = LD2PD(drv); /* Bind the logical drive and a physical drive */ |
stat = disk_initialize(fs->drive); /* Initialize low level disk I/O layer */ |
if (stat & STA_NOINIT) /* Check if the drive is ready */ |
return FR_NOT_READY; |
#if S_MAX_SIZ > 512 /* Get disk sector size if needed */ |
if (disk_ioctl(drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK || SS(fs) > S_MAX_SIZ) |
return FR_NO_FILESYSTEM; |
#endif |
#if !_FS_READONLY |
if (chk_wp && (stat & STA_PROTECT)) /* Check write protection if needed */ |
return FR_WRITE_PROTECTED; |
#endif |
/* Search FAT partition on the drive */ |
fmt = check_fs(fs, bootsect = 0); /* Check sector 0 as an SFD format */ |
if (fmt == 1) { /* Not an FAT boot record, it may be patitioned */ |
/* Check a partition listed in top of the partition table */ |
tbl = &fs->win[MBR_Table + LD2PT(drv) * 16]; /* Partition table */ |
if (tbl[4]) { /* Is the partition existing? */ |
bootsect = LD_DWORD(&tbl[8]); /* Partition offset in LBA */ |
fmt = check_fs(fs, bootsect); /* Check the partition */ |
} |
} |
if (fmt || LD_WORD(&fs->win[BPB_BytsPerSec]) != SS(fs)) /* No valid FAT patition is found */ |
return FR_NO_FILESYSTEM; |
/* Initialize the file system object */ |
fatsize = LD_WORD(&fs->win[BPB_FATSz16]); /* Number of sectors per FAT */ |
if (!fatsize) fatsize = LD_DWORD(&fs->win[BPB_FATSz32]); |
fs->sects_fat = fatsize; |
fs->n_fats = fs->win[BPB_NumFATs]; /* Number of FAT copies */ |
fatsize *= fs->n_fats; /* (Number of sectors in FAT area) */ |
fs->fatbase = bootsect + LD_WORD(&fs->win[BPB_RsvdSecCnt]); /* FAT start sector (lba) */ |
fs->csize = fs->win[BPB_SecPerClus]; /* Number of sectors per cluster */ |
fs->n_rootdir = LD_WORD(&fs->win[BPB_RootEntCnt]); /* Nmuber of root directory entries */ |
totalsect = LD_WORD(&fs->win[BPB_TotSec16]); /* Number of sectors on the file system */ |
if (!totalsect) totalsect = LD_DWORD(&fs->win[BPB_TotSec32]); |
fs->max_clust = maxclust = (totalsect /* max_clust = Last cluster# + 1 */ |
- LD_WORD(&fs->win[BPB_RsvdSecCnt]) - fatsize - fs->n_rootdir / (SS(fs)/32) |
) / fs->csize + 2; |
fmt = FS_FAT12; /* Determine the FAT sub type */ |
if (maxclust >= 0xFF7) fmt = FS_FAT16; |
if (maxclust >= 0xFFF7) fmt = FS_FAT32; |
if (fmt == FS_FAT32) |
fs->dirbase = LD_DWORD(&fs->win[BPB_RootClus]); /* Root directory start cluster */ |
else |
fs->dirbase = fs->fatbase + fatsize; /* Root directory start sector (lba) */ |
fs->database = fs->fatbase + fatsize + fs->n_rootdir / (SS(fs)/32); /* Data start sector (lba) */ |
#if !_FS_READONLY |
/* Initialize allocation information */ |
fs->free_clust = 0xFFFFFFFF; |
#if _USE_FSINFO |
/* Get fsinfo if needed */ |
if (fmt == FS_FAT32) { |
fs->fsi_sector = bootsect + LD_WORD(&fs->win[BPB_FSInfo]); |
if (disk_read(fs->drive, fs->win, fs->fsi_sector, 1) == RES_OK && |
LD_WORD(&fs->win[BS_55AA]) == 0xAA55 && |
LD_DWORD(&fs->win[FSI_LeadSig]) == 0x41615252 && |
LD_DWORD(&fs->win[FSI_StrucSig]) == 0x61417272) { |
fs->last_clust = LD_DWORD(&fs->win[FSI_Nxt_Free]); |
fs->free_clust = LD_DWORD(&fs->win[FSI_Free_Count]); |
} |
} |
#endif |
#endif |
fs->fs_type = fmt; /* FAT syb-type */ |
fs->id = ++fsid; /* File system mount ID */ |
return FR_OK; |
} |
/*-----------------------------------------------------------------------*/ |
/* Check if the file/dir object is valid or not */ |
/*-----------------------------------------------------------------------*/ |
static |
FRESULT validate ( /* FR_OK(0): The object is valid, !=0: Invalid */ |
const FATFS *fs, /* Pointer to the file system object */ |
WORD id /* Member id of the target object to be checked */ |
) |
{ |
if (!fs || !fs->fs_type || fs->id != id) |
return FR_INVALID_OBJECT; |
if (disk_status(fs->drive) & STA_NOINIT) |
return FR_NOT_READY; |
return FR_OK; |
} |
/*-------------------------------------------------------------------------- |
Public Functions |
--------------------------------------------------------------------------*/ |
/*-----------------------------------------------------------------------*/ |
/* Mount/Unmount a Locical Drive */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_mount ( |
BYTE drv, /* Logical drive number to be mounted/unmounted */ |
FATFS *fs /* Pointer to new file system object (NULL for unmount)*/ |
) |
{ |
if (drv >= _DRIVES) return FR_INVALID_DRIVE; |
if (FatFs[drv]) FatFs[drv]->fs_type = 0; /* Clear old object */ |
FatFs[drv] = fs; /* Register and clear new object */ |
if (fs) fs->fs_type = 0; |
return FR_OK; |
} |
/*-----------------------------------------------------------------------*/ |
/* Open or Create a File */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_open ( |
FIL *fp, /* Pointer to the blank file object */ |
const char *path, /* Pointer to the file name */ |
BYTE mode /* Access mode and file open mode flags */ |
) |
{ |
FRESULT res; |
DIR dj; |
BYTE *dir; |
char fn[8+3+1]; |
fp->fs = NULL; /* Clear file object */ |
#if !_FS_READONLY |
mode &= (FA_READ|FA_WRITE|FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW); |
res = auto_mount(&path, &dj.fs, (BYTE)(mode & (FA_WRITE|FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW))); |
#else |
mode &= FA_READ; |
res = auto_mount(&path, &dj.fs, 0); |
#endif |
if (res != FR_OK) return res; |
res = trace_path(&dj, fn, path, &dir); /* Trace the file path */ |
#if !_FS_READONLY |
/* Create or Open a file */ |
if (mode & (FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW)) { |
DWORD ps, rs; |
if (res != FR_OK) { /* No file, create new */ |
if (res != FR_NO_FILE) return res; |
res = reserve_direntry(&dj, &dir); |
if (res != FR_OK) return res; |
memset(dir, 0, 32); /* Initialize the new entry with open name */ |
memcpy(&dir[DIR_Name], fn, 8+3); |
dir[DIR_NTres] = fn[11]; |
mode |= FA_CREATE_ALWAYS; |
} |
else { /* Any object is already existing */ |
if (mode & FA_CREATE_NEW) /* Cannot create new */ |
return FR_EXIST; |
if (!dir || (dir[DIR_Attr] & (AM_RDO|AM_DIR))) /* Cannot overwrite it (R/O or DIR) */ |
return FR_DENIED; |
if (mode & FA_CREATE_ALWAYS) { /* Resize it to zero if needed */ |
rs = ((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | LD_WORD(&dir[DIR_FstClusLO]); /* Get start cluster */ |
ST_WORD(&dir[DIR_FstClusHI], 0); /* cluster = 0 */ |
ST_WORD(&dir[DIR_FstClusLO], 0); |
ST_DWORD(&dir[DIR_FileSize], 0); /* size = 0 */ |
dj.fs->winflag = 1; |
ps = dj.fs->winsect; /* Remove the cluster chain */ |
if (!remove_chain(dj.fs, rs) || !move_window(dj.fs, ps)) |
return FR_RW_ERROR; |
dj.fs->last_clust = rs - 1; /* Reuse the cluster hole */ |
} |
} |
if (mode & FA_CREATE_ALWAYS) { |
dir[DIR_Attr] = 0; /* Reset attribute */ |
ps = get_fattime(); |
ST_DWORD(&dir[DIR_CrtTime], ps); /* Created time */ |
dj.fs->winflag = 1; |
mode |= FA__WRITTEN; /* Set file changed flag */ |
} |
} |
/* Open an existing file */ |
else { |
#endif /* !_FS_READONLY */ |
if (res != FR_OK) return res; /* Trace failed */ |
if (!dir || (dir[DIR_Attr] & AM_DIR)) /* It is a directory */ |
return FR_NO_FILE; |
#if !_FS_READONLY |
if ((mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO)) /* R/O violation */ |
return FR_DENIED; |
} |
fp->dir_sect = dj.fs->winsect; /* Pointer to the directory entry */ |
fp->dir_ptr = dir; |
#endif |
fp->flag = mode; /* File access mode */ |
fp->org_clust = /* File start cluster */ |
((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | LD_WORD(&dir[DIR_FstClusLO]); |
fp->fsize = LD_DWORD(&dir[DIR_FileSize]); /* File size */ |
fp->fptr = 0; fp->csect = 255; /* File pointer */ |
fp->curr_sect = 0; |
fp->fs = dj.fs; fp->id = dj.fs->id; /* Owner file system object of the file */ |
return FR_OK; |
} |
/*-----------------------------------------------------------------------*/ |
/* Read File */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_read ( |
FIL *fp, /* Pointer to the file object */ |
void *buff, /* Pointer to data buffer */ |
UINT btr, /* Number of bytes to read */ |
UINT *br /* Pointer to number of bytes read */ |
) |
{ |
FRESULT res; |
DWORD clust, sect, remain; |
UINT rcnt, cc; |
BYTE *rbuff = buff; |
*br = 0; |
res = validate(fp->fs, fp->id); /* Check validity of the object */ |
if (res != FR_OK) return res; |
if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */ |
if (!(fp->flag & FA_READ)) return FR_DENIED; /* Check access mode */ |
remain = fp->fsize - fp->fptr; |
if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */ |
for ( ; btr; /* Repeat until all data transferred */ |
rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) { |
if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */ |
if (fp->csect >= fp->fs->csize) { /* On the cluster boundary? */ |
clust = (fp->fptr == 0) ? /* On the top of the file? */ |
fp->org_clust : get_cluster(fp->fs, fp->curr_clust); |
if (clust < 2 || clust >= fp->fs->max_clust) goto fr_error; |
fp->curr_clust = clust; /* Update current cluster */ |
fp->csect = 0; /* Reset sector address in the cluster */ |
} |
sect = clust2sect(fp->fs, fp->curr_clust) + fp->csect; /* Get current sector */ |
cc = btr / SS(fp->fs); /* When remaining bytes >= sector size, */ |
if (cc) { /* Read maximum contiguous sectors directly */ |
if (fp->csect + cc > fp->fs->csize) /* Clip at cluster boundary */ |
cc = fp->fs->csize - fp->csect; |
if (disk_read(fp->fs->drive, rbuff, sect, (BYTE)cc) != RES_OK) |
goto fr_error; |
fp->csect += (BYTE)cc; /* Next sector address in the cluster */ |
rcnt = SS(fp->fs) * cc; /* Number of bytes transferred */ |
continue; |
} |
if (sect != fp->curr_sect) { /* Is window offset changed? */ |
#if !_FS_READONLY |
if (fp->flag & FA__DIRTY) { /* Write back file I/O buffer if needed */ |
if (disk_write(fp->fs->drive, fp->buffer, fp->curr_sect, 1) != RES_OK) |
goto fr_error; |
fp->flag &= (BYTE)~FA__DIRTY; |
} |
#endif |
if (disk_read(fp->fs->drive, fp->buffer, sect, 1) != RES_OK) /* Fill file I/O buffer with file data */ |
goto fr_error; |
fp->curr_sect = sect; |
} |
fp->csect++; /* Next sector address in the cluster */ |
} |
rcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs)); /* Get partial sector from file I/O buffer */ |
if (rcnt > btr) rcnt = btr; |
memcpy(rbuff, &fp->buffer[fp->fptr % SS(fp->fs)], rcnt); |
} |
return FR_OK; |
fr_error: /* Abort this file due to an unrecoverable error */ |
fp->flag |= FA__ERROR; |
return FR_RW_ERROR; |
} |
#if !_FS_READONLY |
/*-----------------------------------------------------------------------*/ |
/* Write File */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_write ( |
FIL *fp, /* Pointer to the file object */ |
const void *buff, /* Pointer to the data to be written */ |
UINT btw, /* Number of bytes to write */ |
UINT *bw /* Pointer to number of bytes written */ |
) |
{ |
FRESULT res; |
DWORD clust, sect; |
UINT wcnt, cc; |
const BYTE *wbuff = buff; |
*bw = 0; |
res = validate(fp->fs, fp->id); /* Check validity of the object */ |
if (res != FR_OK) return res; |
if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */ |
if (!(fp->flag & FA_WRITE)) return FR_DENIED; /* Check access mode */ |
if (fp->fsize + btw < fp->fsize) return FR_OK; /* File size cannot reach 4GB */ |
for ( ; btw; /* Repeat until all data transferred */ |
wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) { |
if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */ |
if (fp->csect >= fp->fs->csize) { /* On the cluster boundary? */ |
if (fp->fptr == 0) { /* On the top of the file? */ |
clust = fp->org_clust; /* Follow from the origin */ |
if (clust == 0) /* When there is no cluster chain, */ |
fp->org_clust = clust = create_chain(fp->fs, 0); /* Create a new cluster chain */ |
} else { /* Middle or end of the file */ |
clust = create_chain(fp->fs, fp->curr_clust); /* Trace or streach cluster chain */ |
} |
if (clust == 0) break; /* Could not allocate a new cluster (disk full) */ |
if (clust == 1 || clust >= fp->fs->max_clust) goto fw_error; |
fp->curr_clust = clust; /* Update current cluster */ |
fp->csect = 0; /* Reset sector address in the cluster */ |
} |
sect = clust2sect(fp->fs, fp->curr_clust) + fp->csect; /* Get current sector */ |
cc = btw / SS(fp->fs); /* When remaining bytes >= sector size, */ |
if (cc) { /* Write maximum contiguous sectors directly */ |
if (fp->csect + cc > fp->fs->csize) /* Clip at cluster boundary */ |
cc = fp->fs->csize - fp->csect; |
if (disk_write(fp->fs->drive, wbuff, sect, (BYTE)cc) != RES_OK) |
goto fw_error; |
fp->csect += (BYTE)cc; /* Next sector address in the cluster */ |
wcnt = SS(fp->fs) * cc; /* Number of bytes transferred */ |
continue; |
} |
if (sect != fp->curr_sect) { /* Is window offset changed? */ |
if (fp->flag & FA__DIRTY) { /* Write back file I/O buffer if needed */ |
if (disk_write(fp->fs->drive, fp->buffer, fp->curr_sect, 1) != RES_OK) |
goto fw_error; |
fp->flag &= (BYTE)~FA__DIRTY; |
} |
if (fp->fptr < fp->fsize && /* Fill file I/O buffer with file data */ |
disk_read(fp->fs->drive, fp->buffer, sect, 1) != RES_OK) |
goto fw_error; |
fp->curr_sect = sect; |
} |
fp->csect++; /* Next sector address in the cluster */ |
} |
wcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs)); /* Put partial sector into file I/O buffer */ |
if (wcnt > btw) wcnt = btw; |
memcpy(&fp->buffer[fp->fptr % SS(fp->fs)], wbuff, wcnt); |
fp->flag |= FA__DIRTY; |
} |
if (fp->fptr > fp->fsize) fp->fsize = fp->fptr; /* Update file size if needed */ |
fp->flag |= FA__WRITTEN; /* Set file changed flag */ |
return FR_OK; |
fw_error: /* Abort this file due to an unrecoverable error */ |
fp->flag |= FA__ERROR; |
return FR_RW_ERROR; |
} |
/*-----------------------------------------------------------------------*/ |
/* Synchronize the file object */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_sync ( |
FIL *fp /* Pointer to the file object */ |
) |
{ |
FRESULT res; |
DWORD tim; |
BYTE *dir; |
res = validate(fp->fs, fp->id); /* Check validity of the object */ |
if (res == FR_OK) { |
if (fp->flag & FA__WRITTEN) { /* Has the file been written? */ |
/* Write back data buffer if needed */ |
if (fp->flag & FA__DIRTY) { |
if (disk_write(fp->fs->drive, fp->buffer, fp->curr_sect, 1) != RES_OK) |
return FR_RW_ERROR; |
fp->flag &= (BYTE)~FA__DIRTY; |
} |
/* Update the directory entry */ |
if (!move_window(fp->fs, fp->dir_sect)) |
return FR_RW_ERROR; |
dir = fp->dir_ptr; |
dir[DIR_Attr] |= AM_ARC; /* Set archive bit */ |
ST_DWORD(&dir[DIR_FileSize], fp->fsize); /* Update file size */ |
ST_WORD(&dir[DIR_FstClusLO], fp->org_clust); /* Update start cluster */ |
ST_WORD(&dir[DIR_FstClusHI], fp->org_clust >> 16); |
tim = get_fattime(); /* Updated time */ |
ST_DWORD(&dir[DIR_WrtTime], tim); |
fp->flag &= (BYTE)~FA__WRITTEN; |
res = sync(fp->fs); |
} |
} |
return res; |
} |
#endif /* !_FS_READONLY */ |
/*-----------------------------------------------------------------------*/ |
/* Close File */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_close ( |
FIL *fp /* Pointer to the file object to be closed */ |
) |
{ |
FRESULT res; |
#if !_FS_READONLY |
res = f_sync(fp); |
#else |
res = validate(fp->fs, fp->id); |
#endif |
if (res == FR_OK) fp->fs = NULL; |
return res; |
} |
#if _FS_MINIMIZE <= 2 |
/*-----------------------------------------------------------------------*/ |
/* Seek File R/W Pointer */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_lseek ( |
FIL *fp, /* Pointer to the file object */ |
DWORD ofs /* File pointer from top of file */ |
) |
{ |
FRESULT res; |
DWORD clust, csize, nsect, ifptr; |
res = validate(fp->fs, fp->id); /* Check validity of the object */ |
if (res != FR_OK) return res; |
if (fp->flag & FA__ERROR) return FR_RW_ERROR; |
if (ofs > fp->fsize /* In read-only mode, clip offset with the file size */ |
#if !_FS_READONLY |
&& !(fp->flag & FA_WRITE) |
#endif |
) ofs = fp->fsize; |
ifptr = fp->fptr; |
fp->fptr = 0; fp->csect = 255; |
nsect = 0; |
if (ofs > 0) { |
csize = (DWORD)fp->fs->csize * SS(fp->fs); /* Cluster size (byte) */ |
if (ifptr > 0 && |
(ofs - 1) / csize >= (ifptr - 1) / csize) {/* When seek to same or following cluster, */ |
fp->fptr = (ifptr - 1) & ~(csize - 1); /* start from the current cluster */ |
ofs -= fp->fptr; |
clust = fp->curr_clust; |
} else { /* When seek to back cluster, */ |
clust = fp->org_clust; /* start from the first cluster */ |
#if !_FS_READONLY |
if (clust == 0) { /* If no cluster chain, create a new chain */ |
clust = create_chain(fp->fs, 0); |
if (clust == 1) goto fk_error; |
fp->org_clust = clust; |
} |
#endif |
fp->curr_clust = clust; |
} |
if (clust != 0) { |
while (ofs > csize) { /* Cluster following loop */ |
#if !_FS_READONLY |
if (fp->flag & FA_WRITE) { /* Check if in write mode or not */ |
clust = create_chain(fp->fs, clust); /* Force streached if in write mode */ |
if (clust == 0) { /* When disk gets full, clip file size */ |
ofs = csize; break; |
} |
} else |
#endif |
clust = get_cluster(fp->fs, clust); /* Follow cluster chain if not in write mode */ |
if (clust < 2 || clust >= fp->fs->max_clust) goto fk_error; |
fp->curr_clust = clust; |
fp->fptr += csize; |
ofs -= csize; |
} |
fp->fptr += ofs; |
fp->csect = (BYTE)(ofs / SS(fp->fs)); /* Sector offset in the cluster */ |
if (ofs & (SS(fp->fs) - 1)) { |
nsect = clust2sect(fp->fs, clust) + fp->csect; /* Current sector */ |
fp->csect++; |
} |
} |
} |
if (nsect && nsect != fp->curr_sect) { |
#if !_FS_READONLY |
if (fp->flag & FA__DIRTY) { /* Write-back dirty buffer if needed */ |
if (disk_write(fp->fs->drive, fp->buffer, fp->curr_sect, 1) != RES_OK) |
goto fk_error; |
fp->flag &= (BYTE)~FA__DIRTY; |
} |
#endif |
if (disk_read(fp->fs->drive, fp->buffer, nsect, 1) != RES_OK) |
goto fk_error; |
fp->curr_sect = nsect; |
} |
#if !_FS_READONLY |
if (fp->fptr > fp->fsize) { /* Set changed flag if the file was extended */ |
fp->fsize = fp->fptr; |
fp->flag |= FA__WRITTEN; |
} |
#endif |
return FR_OK; |
fk_error: /* Abort this file due to an unrecoverable error */ |
fp->flag |= FA__ERROR; |
return FR_RW_ERROR; |
} |
#if _FS_MINIMIZE <= 1 |
/*-----------------------------------------------------------------------*/ |
/* Create a directroy object */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_opendir ( |
DIR *dj, /* Pointer to directory object to create */ |
const char *path /* Pointer to the directory path */ |
) |
{ |
FRESULT res; |
BYTE *dir; |
char fn[8+3+1]; |
res = auto_mount(&path, &dj->fs, 0); |
if (res == FR_OK) { |
res = trace_path(dj, fn, path, &dir); /* Trace the directory path */ |
if (res == FR_OK) { /* Trace completed */ |
if (dir) { /* It is not the root dir */ |
if (dir[DIR_Attr] & AM_DIR) { /* The entry is a directory */ |
dj->clust = ((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | LD_WORD(&dir[DIR_FstClusLO]); |
dj->sect = clust2sect(dj->fs, dj->clust); |
dj->index = 2; |
} else { /* The entry is not a directory */ |
res = FR_NO_FILE; |
} |
} |
dj->id = dj->fs->id; |
} |
} |
return res; |
} |
/*-----------------------------------------------------------------------*/ |
/* Read Directory Entry in Sequense */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_readdir ( |
DIR *dj, /* Pointer to the directory object */ |
FILINFO *finfo /* Pointer to file information to return */ |
) |
{ |
BYTE *dir, c, res; |
res = validate(dj->fs, dj->id); /* Check validity of the object */ |
if (res != FR_OK) return res; |
finfo->fname[0] = 0; |
while (dj->sect) { |
if (!move_window(dj->fs, dj->sect)) |
return FR_RW_ERROR; |
dir = &dj->fs->win[(dj->index & ((SS(dj->fs) - 1) >> 5)) * 32]; /* pointer to the directory entry */ |
c = dir[DIR_Name]; |
if (c == 0) break; /* Has it reached to end of dir? */ |
if (c != 0xE5 && !(dir[DIR_Attr] & AM_VOL)) /* Is it a valid entry? */ |
get_fileinfo(finfo, dir); |
if (!next_dir_entry(dj)) dj->sect = 0; /* Next entry */ |
if (finfo->fname[0]) break; /* Found valid entry */ |
} |
return FR_OK; |
} |
#if _FS_MINIMIZE == 0 |
/*-----------------------------------------------------------------------*/ |
/* Get File Status */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_stat ( |
const char *path, /* Pointer to the file path */ |
FILINFO *finfo /* Pointer to file information to return */ |
) |
{ |
FRESULT res; |
DIR dj; |
BYTE *dir; |
char fn[8+3+1]; |
res = auto_mount(&path, &dj.fs, 0); |
if (res == FR_OK) { |
res = trace_path(&dj, fn, path, &dir); /* Trace the file path */ |
if (res == FR_OK) { /* Trace completed */ |
if (dir) /* Found an object */ |
get_fileinfo(finfo, dir); |
else /* It is root dir */ |
res = FR_INVALID_NAME; |
} |
} |
return res; |
} |
#if !_FS_READONLY |
/*-----------------------------------------------------------------------*/ |
/* Truncate File */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_truncate ( |
FIL *fp /* Pointer to the file object */ |
) |
{ |
FRESULT res; |
DWORD ncl; |
res = validate(fp->fs, fp->id); /* Check validity of the object */ |
if (res != FR_OK) return res; |
if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */ |
if (!(fp->flag & FA_WRITE)) return FR_DENIED; /* Check access mode */ |
if (fp->fsize > fp->fptr) { |
fp->fsize = fp->fptr; /* Set file size to current R/W point */ |
fp->flag |= FA__WRITTEN; |
if (fp->fptr == 0) { /* When set file size to zero, remove entire cluster chain */ |
if (!remove_chain(fp->fs, fp->org_clust)) goto ft_error; |
fp->org_clust = 0; |
} else { /* When truncate a part of the file, remove remaining clusters */ |
ncl = get_cluster(fp->fs, fp->curr_clust); |
if (ncl < 2) goto ft_error; |
if (ncl < fp->fs->max_clust) { |
if (!put_cluster(fp->fs, fp->curr_clust, 0x0FFFFFFF)) goto ft_error; |
if (!remove_chain(fp->fs, ncl)) goto ft_error; |
} |
} |
} |
return FR_OK; |
ft_error: /* Abort this file due to an unrecoverable error */ |
fp->flag |= FA__ERROR; |
return FR_RW_ERROR; |
} |
/*-----------------------------------------------------------------------*/ |
/* Get Number of Free Clusters */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_getfree ( |
const char *drv, /* Pointer to the logical drive number (root dir) */ |
DWORD *nclust, /* Pointer to the variable to return number of free clusters */ |
FATFS **fatfs /* Pointer to pointer to corresponding file system object to return */ |
) |
{ |
FRESULT res; |
DWORD n, clust, sect; |
BYTE fat, f, *p; |
/* Get drive number */ |
res = auto_mount(&drv, fatfs, 0); |
if (res != FR_OK) return res; |
/* If number of free cluster is valid, return it without cluster scan. */ |
if ((*fatfs)->free_clust <= (*fatfs)->max_clust - 2) { |
*nclust = (*fatfs)->free_clust; |
return FR_OK; |
} |
/* Get number of free clusters */ |
fat = (*fatfs)->fs_type; |
n = 0; |
if (fat == FS_FAT12) { |
clust = 2; |
do { |
if ((WORD)get_cluster(*fatfs, clust) == 0) n++; |
} while (++clust < (*fatfs)->max_clust); |
} else { |
clust = (*fatfs)->max_clust; |
sect = (*fatfs)->fatbase; |
f = 0; p = 0; |
do { |
if (!f) { |
if (!move_window(*fatfs, sect++)) return FR_RW_ERROR; |
p = (*fatfs)->win; |
} |
if (fat == FS_FAT16) { |
if (LD_WORD(p) == 0) n++; |
p += 2; f += 1; |
} else { |
if (LD_DWORD(p) == 0) n++; |
p += 4; f += 2; |
} |
} while (--clust); |
} |
(*fatfs)->free_clust = n; |
#if _USE_FSINFO |
if (fat == FS_FAT32) (*fatfs)->fsi_flag = 1; |
#endif |
*nclust = n; |
return FR_OK; |
} |
/*-----------------------------------------------------------------------*/ |
/* Delete a File or Directory */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_unlink ( |
const char *path /* Pointer to the file or directory path */ |
) |
{ |
FRESULT res; |
DIR dj; |
BYTE *dir, *sdir; |
DWORD dclust, dsect; |
char fn[8+3+1]; |
res = auto_mount(&path, &dj.fs, 1); |
if (res != FR_OK) return res; |
res = trace_path(&dj, fn, path, &dir); /* Trace the file path */ |
if (res != FR_OK) return res; /* Trace failed */ |
if (!dir) return FR_INVALID_NAME; /* It is the root directory */ |
if (dir[DIR_Attr] & AM_RDO) return FR_DENIED; /* It is a R/O object */ |
dsect = dj.fs->winsect; |
dclust = ((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | LD_WORD(&dir[DIR_FstClusLO]); |
if (dir[DIR_Attr] & AM_DIR) { /* It is a sub-directory */ |
dj.clust = dclust; /* Check if the sub-dir is empty or not */ |
dj.sect = clust2sect(dj.fs, dclust); |
dj.index = 2; |
do { |
if (!move_window(dj.fs, dj.sect)) return FR_RW_ERROR; |
sdir = &dj.fs->win[(dj.index & ((SS(dj.fs) - 1) >> 5)) * 32]; |
if (sdir[DIR_Name] == 0) break; |
if (sdir[DIR_Name] != 0xE5 && !(sdir[DIR_Attr] & AM_VOL)) |
return FR_DENIED; /* The directory is not empty */ |
} while (next_dir_entry(&dj)); |
} |
if (!move_window(dj.fs, dsect)) return FR_RW_ERROR; /* Mark the directory entry 'deleted' */ |
dir[DIR_Name] = 0xE5; |
dj.fs->winflag = 1; |
if (!remove_chain(dj.fs, dclust)) return FR_RW_ERROR; /* Remove the cluster chain */ |
return sync(dj.fs); |
} |
/*-----------------------------------------------------------------------*/ |
/* Create a Directory */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_mkdir ( |
const char *path /* Pointer to the directory path */ |
) |
{ |
FRESULT res; |
DIR dj; |
BYTE *dir, *fw, n; |
char fn[8+3+1]; |
DWORD sect, dsect, dclust, pclust, tim; |
res = auto_mount(&path, &dj.fs, 1); |
if (res != FR_OK) return res; |
res = trace_path(&dj, fn, path, &dir); /* Trace the file path */ |
if (res == FR_OK) return FR_EXIST; /* Any file or directory is already existing */ |
if (res != FR_NO_FILE) return res; |
res = reserve_direntry(&dj, &dir); /* Reserve a directory entry */ |
if (res != FR_OK) return res; |
sect = dj.fs->winsect; |
dclust = create_chain(dj.fs, 0); /* Allocate a cluster for new directory table */ |
if (dclust == 1) return FR_RW_ERROR; |
dsect = clust2sect(dj.fs, dclust); |
if (!dsect) return FR_DENIED; |
if (!move_window(dj.fs, dsect)) return FR_RW_ERROR; |
fw = dj.fs->win; |
memset(fw, 0, SS(dj.fs)); /* Clear the new directory table */ |
for (n = 1; n < dj.fs->csize; n++) { |
if (disk_write(dj.fs->drive, fw, ++dsect, 1) != RES_OK) |
return FR_RW_ERROR; |
} |
memset(&fw[DIR_Name], ' ', 8+3); /* Create "." entry */ |
fw[DIR_Name] = '.'; |
fw[DIR_Attr] = AM_DIR; |
tim = get_fattime(); |
ST_DWORD(&fw[DIR_WrtTime], tim); |
memcpy(&fw[32], &fw[0], 32); fw[33] = '.'; /* Create ".." entry */ |
ST_WORD(&fw[ DIR_FstClusLO], dclust); |
ST_WORD(&fw[ DIR_FstClusHI], dclust >> 16); |
pclust = dj.sclust; |
if (dj.fs->fs_type == FS_FAT32 && pclust == dj.fs->dirbase) pclust = 0; |
ST_WORD(&fw[32+DIR_FstClusLO], pclust); |
ST_WORD(&fw[32+DIR_FstClusHI], pclust >> 16); |
dj.fs->winflag = 1; |
if (!move_window(dj.fs, sect)) return FR_RW_ERROR; |
memset(&dir[0], 0, 32); /* Initialize the new entry */ |
memcpy(&dir[DIR_Name], fn, 8+3); /* Name */ |
dir[DIR_NTres] = fn[11]; |
dir[DIR_Attr] = AM_DIR; /* Attribute */ |
ST_DWORD(&dir[DIR_WrtTime], tim); /* Crated time */ |
ST_WORD(&dir[DIR_FstClusLO], dclust); /* Table start cluster */ |
ST_WORD(&dir[DIR_FstClusHI], dclust >> 16); |
return sync(dj.fs); |
} |
/*-----------------------------------------------------------------------*/ |
/* Change File Attribute */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_chmod ( |
const char *path, /* Pointer to the file path */ |
BYTE value, /* Attribute bits */ |
BYTE mask /* Attribute mask to change */ |
) |
{ |
FRESULT res; |
DIR dj; |
BYTE *dir; |
char fn[8+3+1]; |
res = auto_mount(&path, &dj.fs, 1); |
if (res == FR_OK) { |
res = trace_path(&dj, fn, path, &dir); /* Trace the file path */ |
if (res == FR_OK) { /* Trace completed */ |
if (!dir) { |
res = FR_INVALID_NAME; /* Root directory */ |
} else { |
mask &= AM_RDO|AM_HID|AM_SYS|AM_ARC; /* Valid attribute mask */ |
dir[DIR_Attr] = (value & mask) | (dir[DIR_Attr] & (BYTE)~mask); /* Apply attribute change */ |
res = sync(dj.fs); |
} |
} |
} |
return res; |
} |
/*-----------------------------------------------------------------------*/ |
/* Change Timestamp */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_utime ( |
const char *path, /* Pointer to the file/directory name */ |
const FILINFO *finfo /* Pointer to the timestamp to be set */ |
) |
{ |
FRESULT res; |
DIR dj; |
BYTE *dir; |
char fn[8+3+1]; |
res = auto_mount(&path, &dj.fs, 1); |
if (res == FR_OK) { |
res = trace_path(&dj, fn, path, &dir); /* Trace the file path */ |
if (res == FR_OK) { /* Trace completed */ |
if (!dir) { |
res = FR_INVALID_NAME; /* Root directory */ |
} else { |
ST_WORD(&dir[DIR_WrtTime], finfo->ftime); |
ST_WORD(&dir[DIR_WrtDate], finfo->fdate); |
res = sync(dj.fs); |
} |
} |
} |
return res; |
} |
/*-----------------------------------------------------------------------*/ |
/* Rename File/Directory */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_rename ( |
const char *path_old, /* Pointer to the old name */ |
const char *path_new /* Pointer to the new name */ |
) |
{ |
FRESULT res; |
DIR dj; |
DWORD sect_old; |
BYTE *dir_old, *dir_new, direntry[32-11]; |
char fn[8+3+1]; |
res = auto_mount(&path_old, &dj.fs, 1); |
if (res != FR_OK) return res; |
res = trace_path(&dj, fn, path_old, &dir_old); /* Check old object */ |
if (res != FR_OK) return res; /* The old object is not found */ |
if (!dir_old) return FR_NO_FILE; |
sect_old = dj.fs->winsect; /* Save the object information */ |
memcpy(direntry, &dir_old[DIR_Attr], 32-11); |
res = trace_path(&dj, fn, path_new, &dir_new); /* Check new object */ |
if (res == FR_OK) return FR_EXIST; /* The new object name is already existing */ |
if (res != FR_NO_FILE) return res; /* Is there no old name? */ |
res = reserve_direntry(&dj, &dir_new); /* Reserve a directory entry */ |
if (res != FR_OK) return res; |
memcpy(&dir_new[DIR_Attr], direntry, 32-11); /* Create new entry */ |
memcpy(&dir_new[DIR_Name], fn, 8+3); |
dir_new[DIR_NTres] = fn[11]; |
dj.fs->winflag = 1; |
if (!move_window(dj.fs, sect_old)) return FR_RW_ERROR; /* Delete old entry */ |
dir_old[DIR_Name] = 0xE5; |
return sync(dj.fs); |
} |
#endif /* !_FS_READONLY */ |
#endif /* _FS_MINIMIZE == 0 */ |
#endif /* _FS_MINIMIZE <= 1 */ |
#endif /* _FS_MINIMIZE <= 2 */ |
#if _USE_MKFS && !_FS_READONLY |
/*-----------------------------------------------------------------------*/ |
/* Create File System on the Drive */ |
/*-----------------------------------------------------------------------*/ |
#define N_ROOTDIR 512 /* Multiple of 32 and <= 2048 */ |
#define N_FATS 1 /* 1 or 2 */ |
#define MAX_SECTOR 64000000UL /* Maximum partition size */ |
#define MIN_SECTOR 2000UL /* Minimum partition size */ |
FRESULT f_mkfs ( |
BYTE drv, /* Logical drive number */ |
BYTE partition, /* Partitioning rule 0:FDISK, 1:SFD */ |
WORD allocsize /* Allocation unit size [bytes] */ |
) |
{ |
BYTE fmt, m, *tbl; |
DWORD b_part, b_fat, b_dir, b_data; /* Area offset (LBA) */ |
DWORD n_part, n_rsv, n_fat, n_dir; /* Area size */ |
DWORD n_clust, n; |
FATFS *fs; |
DSTATUS stat; |
/* Check validity of the parameters */ |
if (drv >= _DRIVES) return FR_INVALID_DRIVE; |
if (partition >= 2) return FR_MKFS_ABORTED; |
for (n = 512; n <= 32768U && n != allocsize; n <<= 1); |
if (n != allocsize) return FR_MKFS_ABORTED; |
/* Check mounted drive and clear work area */ |
fs = FatFs[drv]; |
if (!fs) return FR_NOT_ENABLED; |
fs->fs_type = 0; |
drv = LD2PD(drv); |
/* Get disk statics */ |
stat = disk_initialize(drv); |
if (stat & STA_NOINIT) return FR_NOT_READY; |
if (stat & STA_PROTECT) return FR_WRITE_PROTECTED; |
if (disk_ioctl(drv, GET_SECTOR_COUNT, &n_part) != RES_OK || n_part < MIN_SECTOR) |
return FR_MKFS_ABORTED; |
if (n_part > MAX_SECTOR) n_part = MAX_SECTOR; |
b_part = (!partition) ? 63 : 0; /* Boot sector */ |
n_part -= b_part; |
#if S_MAX_SIZ > 512 /* Check disk sector size */ |
if (disk_ioctl(drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK |
|| SS(fs) > S_MAX_SIZ |
|| SS(fs) > allocsize) |
return FR_MKFS_ABORTED; |
#endif |
allocsize /= SS(fs); /* Number of sectors per cluster */ |
/* Pre-compute number of clusters and FAT type */ |
n_clust = n_part / allocsize; |
fmt = FS_FAT12; |
if (n_clust >= 0xFF5) fmt = FS_FAT16; |
if (n_clust >= 0xFFF5) fmt = FS_FAT32; |
/* Determine offset and size of FAT structure */ |
switch (fmt) { |
case FS_FAT12: |
n_fat = ((n_clust * 3 + 1) / 2 + 3 + SS(fs) - 1) / SS(fs); |
n_rsv = 1 + partition; |
n_dir = N_ROOTDIR * 32 / SS(fs); |
break; |
case FS_FAT16: |
n_fat = ((n_clust * 2) + 4 + SS(fs) - 1) / SS(fs); |
n_rsv = 1 + partition; |
n_dir = N_ROOTDIR * 32 / SS(fs); |
break; |
default: |
n_fat = ((n_clust * 4) + 8 + SS(fs) - 1) / SS(fs); |
n_rsv = 33 - partition; |
n_dir = 0; |
} |
b_fat = b_part + n_rsv; /* FATs start sector */ |
b_dir = b_fat + n_fat * N_FATS; /* Directory start sector */ |
b_data = b_dir + n_dir; /* Data start sector */ |
/* Align data start sector to erase block boundary (for flash memory media) */ |
if (disk_ioctl(drv, GET_BLOCK_SIZE, &n) != RES_OK) return FR_MKFS_ABORTED; |
n = (b_data + n - 1) & ~(n - 1); |
n_fat += (n - b_data) / N_FATS; |
/* b_dir and b_data are no longer used below */ |
/* Determine number of cluster and final check of validity of the FAT type */ |
n_clust = (n_part - n_rsv - n_fat * N_FATS - n_dir) / allocsize; |
if ( (fmt == FS_FAT16 && n_clust < 0xFF5) |
|| (fmt == FS_FAT32 && n_clust < 0xFFF5)) |
return FR_MKFS_ABORTED; |
/* Create partition table if needed */ |
if (!partition) { |
DWORD n_disk = b_part + n_part; |
tbl = &fs->win[MBR_Table]; |
ST_DWORD(&tbl[0], 0x00010180); /* Partition start in CHS */ |
if (n_disk < 63UL * 255 * 1024) { /* Partition end in CHS */ |
n_disk = n_disk / 63 / 255; |
tbl[7] = (BYTE)n_disk; |
tbl[6] = (BYTE)((n_disk >> 2) | 63); |
} else { |
ST_WORD(&tbl[6], 0xFFFF); |
} |
tbl[5] = 254; |
if (fmt != FS_FAT32) /* System ID */ |
tbl[4] = (n_part < 0x10000) ? 0x04 : 0x06; |
else |
tbl[4] = 0x0c; |
ST_DWORD(&tbl[8], 63); /* Partition start in LBA */ |
ST_DWORD(&tbl[12], n_part); /* Partition size in LBA */ |
ST_WORD(&tbl[64], 0xAA55); /* Signature */ |
if (disk_write(drv, fs->win, 0, 1) != RES_OK) |
return FR_RW_ERROR; |
} |
/* Create boot record */ |
tbl = fs->win; /* Clear buffer */ |
memset(tbl, 0, SS(fs)); |
ST_DWORD(&tbl[BS_jmpBoot], 0x90FEEB); /* Boot code (jmp $, nop) */ |
ST_WORD(&tbl[BPB_BytsPerSec], SS(fs)); /* Sector size */ |
tbl[BPB_SecPerClus] = (BYTE)allocsize; /* Sectors per cluster */ |
ST_WORD(&tbl[BPB_RsvdSecCnt], n_rsv); /* Reserved sectors */ |
tbl[BPB_NumFATs] = N_FATS; /* Number of FATs */ |
ST_WORD(&tbl[BPB_RootEntCnt], SS(fs) / 32 * n_dir); /* Number of rootdir entries */ |
if (n_part < 0x10000) { /* Number of total sectors */ |
ST_WORD(&tbl[BPB_TotSec16], n_part); |
} else { |
ST_DWORD(&tbl[BPB_TotSec32], n_part); |
} |
tbl[BPB_Media] = 0xF8; /* Media descripter */ |
ST_WORD(&tbl[BPB_SecPerTrk], 63); /* Number of sectors per track */ |
ST_WORD(&tbl[BPB_NumHeads], 255); /* Number of heads */ |
ST_DWORD(&tbl[BPB_HiddSec], b_part); /* Hidden sectors */ |
n = get_fattime(); /* Use current time as a VSN */ |
if (fmt != FS_FAT32) { |
ST_DWORD(&tbl[BS_VolID], n); /* Volume serial number */ |
ST_WORD(&tbl[BPB_FATSz16], n_fat); /* Number of secters per FAT */ |
tbl[BS_DrvNum] = 0x80; /* Drive number */ |
tbl[BS_BootSig] = 0x29; /* Extended boot signature */ |
memcpy(&tbl[BS_VolLab], "NO NAME FAT ", 19); /* Volume lavel, FAT signature */ |
} else { |
ST_DWORD(&tbl[BS_VolID32], n); /* Volume serial number */ |
ST_DWORD(&tbl[BPB_FATSz32], n_fat); /* Number of secters per FAT */ |
ST_DWORD(&tbl[BPB_RootClus], 2); /* Root directory cluster (2) */ |
ST_WORD(&tbl[BPB_FSInfo], 1); /* FSInfo record (bs+1) */ |
ST_WORD(&tbl[BPB_BkBootSec], 6); /* Backup boot record (bs+6) */ |
tbl[BS_DrvNum32] = 0x80; /* Drive number */ |
tbl[BS_BootSig32] = 0x29; /* Extended boot signature */ |
memcpy(&tbl[BS_VolLab32], "NO NAME FAT32 ", 19); /* Volume lavel, FAT signature */ |
} |
ST_WORD(&tbl[BS_55AA], 0xAA55); /* Signature */ |
if (disk_write(drv, tbl, b_part+0, 1) != RES_OK) |
return FR_RW_ERROR; |
if (fmt == FS_FAT32) |
disk_write(drv, tbl, b_part+6, 1); |
/* Initialize FAT area */ |
for (m = 0; m < N_FATS; m++) { |
memset(tbl, 0, SS(fs)); /* 1st sector of the FAT */ |
if (fmt != FS_FAT32) { |
n = (fmt == FS_FAT12) ? 0x00FFFFF8 : 0xFFFFFFF8; |
ST_DWORD(&tbl[0], n); /* Reserve cluster #0-1 (FAT12/16) */ |
} else { |
ST_DWORD(&tbl[0], 0xFFFFFFF8); /* Reserve cluster #0-1 (FAT32) */ |
ST_DWORD(&tbl[4], 0xFFFFFFFF); |
ST_DWORD(&tbl[8], 0x0FFFFFFF); /* Reserve cluster #2 for root dir */ |
} |
if (disk_write(drv, tbl, b_fat++, 1) != RES_OK) |
return FR_RW_ERROR; |
memset(tbl, 0, SS(fs)); /* Following FAT entries are filled by zero */ |
for (n = 1; n < n_fat; n++) { |
if (disk_write(drv, tbl, b_fat++, 1) != RES_OK) |
return FR_RW_ERROR; |
} |
} |
/* Initialize Root directory */ |
m = (BYTE)((fmt == FS_FAT32) ? allocsize : n_dir); |
do { |
if (disk_write(drv, tbl, b_fat++, 1) != RES_OK) |
return FR_RW_ERROR; |
} while (--m); |
/* Create FSInfo record if needed */ |
if (fmt == FS_FAT32) { |
ST_WORD(&tbl[BS_55AA], 0xAA55); |
ST_DWORD(&tbl[FSI_LeadSig], 0x41615252); |
ST_DWORD(&tbl[FSI_StrucSig], 0x61417272); |
ST_DWORD(&tbl[FSI_Free_Count], n_clust - 1); |
ST_DWORD(&tbl[FSI_Nxt_Free], 0xFFFFFFFF); |
disk_write(drv, tbl, b_part+1, 1); |
disk_write(drv, tbl, b_part+7, 1); |
} |
return (disk_ioctl(drv, CTRL_SYNC, NULL) == RES_OK) ? FR_OK : FR_RW_ERROR; |
} |
#endif /* _USE_MKFS && !_FS_READONLY */ |
#if _USE_STRFUNC >= 1 |
/*-----------------------------------------------------------------------*/ |
/* Get a string from the file */ |
/*-----------------------------------------------------------------------*/ |
char* fgets ( |
char* buff, /* Pointer to the string buffer to read */ |
int len, /* Size of string buffer */ |
FIL* fil /* Pointer to the file object */ |
) |
{ |
int i = 0; |
char *p = buff; |
UINT rc; |
while (i < len - 1) { /* Read bytes until buffer gets filled */ |
f_read(fil, p, 1, &rc); |
if (rc != 1) break; /* Break when no data to read */ |
#if _USE_STRFUNC >= 2 |
if (*p == '\r') continue; /* Strip '\r' */ |
#endif |
i++; |
if (*p++ == '\n') break; /* Break when reached end of line */ |
} |
*p = 0; |
return i ? buff : 0; /* When no data read (eof or error), return with error. */ |
} |
#if !_FS_READONLY |
#include <stdarg.h> |
/*-----------------------------------------------------------------------*/ |
/* Put a character to the file */ |
/*-----------------------------------------------------------------------*/ |
int fputc ( |
int chr, /* A character to be output */ |
FIL* fil /* Ponter to the file object */ |
) |
{ |
UINT bw; |
char c; |
#if _USE_STRFUNC >= 2 |
if (chr == '\n') fputc ('\r', fil); /* LF -> CRLF conversion */ |
#endif |
if (!fil) { /* Special value may be used to switch the destination to any other device */ |
/* put_console(chr); */ |
return chr; |
} |
c = (char)chr; |
f_write(fil, &c, 1, &bw); /* Write a byte to the file */ |
return bw ? chr : EOF; /* Return the resulut */ |
} |
/*-----------------------------------------------------------------------*/ |
/* Put a string to the file */ |
/*-----------------------------------------------------------------------*/ |
int fputs ( |
const char* str, /* Pointer to the string to be output */ |
FIL* fil /* Pointer to the file object */ |
) |
{ |
int n; |
for (n = 0; *str; str++, n++) { |
if (fputc(*str, fil) == EOF) return EOF; |
} |
return n; |
} |
/*-----------------------------------------------------------------------*/ |
/* Put a formatted string to the file */ |
/*-----------------------------------------------------------------------*/ |
int fprintf ( |
FIL* fil, /* Pointer to the file object */ |
const char* str, /* Pointer to the format string */ |
... /* Optional arguments... */ |
) |
{ |
va_list arp; |
UCHAR c, f, r; |
ULONG val; |
char s[16]; |
int i, w, res, cc; |
va_start(arp, str); |
for (cc = res = 0; cc != EOF; res += cc) { |
c = *str++; |
if (c == 0) break; /* End of string */ |
if (c != '%') { /* Non escape cahracter */ |
cc = fputc(c, fil); |
if (cc != EOF) cc = 1; |
continue; |
} |
w = f = 0; |
c = *str++; |
if (c == '0') { /* Flag: '0' padding */ |
f = 1; c = *str++; |
} |
while (c >= '0' && c <= '9') { /* Precision */ |
w = w * 10 + (c - '0'); |
c = *str++; |
} |
if (c == 'l') { /* Prefix: Size is long int */ |
f |= 2; c = *str++; |
} |
if (c == 's') { /* Type is string */ |
cc = fputs(va_arg(arp, char*), fil); |
continue; |
} |
if (c == 'c') { /* Type is character */ |
cc = fputc(va_arg(arp, char), fil); |
if (cc != EOF) cc = 1; |
continue; |
} |
r = 0; |
if (c == 'd') r = 10; /* Type is signed decimal */ |
if (c == 'u') r = 10; /* Type is unsigned decimal */ |
if (c == 'X') r = 16; /* Type is unsigned hexdecimal */ |
if (r == 0) break; /* Unknown type */ |
if (f & 2) { /* Get the value */ |
val = (ULONG)va_arg(arp, long); |
} else { |
val = (c == 'd') ? (ULONG)(long)va_arg(arp, int) : (ULONG)va_arg(arp, unsigned int); |
} |
/* Put numeral string */ |
if (c == 'd') { |
if (val >= 0x80000000) { |
val = 0 - val; |
f |= 4; |
} |
} |
i = sizeof(s) - 1; s[i] = 0; |
do { |
c = (UCHAR)(val % r + '0'); |
if (c > '9') c += 7; |
s[--i] = c; |
val /= r; |
} while (i && val); |
if (i && (f & 4)) s[--i] = '-'; |
w = sizeof(s) - 1 - w; |
while (i && i > w) s[--i] = (f & 1) ? '0' : ' '; |
cc = fputs(&s[i], fil); |
} |
va_end(arp); |
return (cc == EOF) ? cc : res; |
} |
#endif /* !_FS_READONLY */ |
#endif /* _USE_STRFUNC >= 1*/ |
/Designs/DOGFINDER01A/SW/ATMEGA168/ff.h |
---|
0,0 → 1,339 |
/*--------------------------------------------------------------------------/ |
/ FatFs - FAT file system module include file R0.06 (C)ChaN, 2008 |
/---------------------------------------------------------------------------/ |
/ FatFs module is an experimenal project to implement FAT file system to |
/ cheap microcontrollers. This is a free software and is opened for education, |
/ research and development under license policy of following trems. |
/ |
/ Copyright (C) 2008, ChaN, all right reserved. |
/ |
/ * The FatFs module is a free software and there is no warranty. |
/ * You can use, modify and/or redistribute it for personal, non-profit or |
/ commercial use without any restriction under your responsibility. |
/ * Redistributions of source code must retain the above copyright notice. |
/ |
/---------------------------------------------------------------------------*/ |
#ifndef _FATFS |
#define _MCU_ENDIAN 0 |
/* The _MCU_ENDIAN defines which access method is used to the FAT structure. |
/ 1: Enable word access. |
/ 2: Disable word access and use byte-by-byte access instead. |
/ When the architectural byte order of the MCU is big-endian and/or address |
/ miss-aligned access results incorrect behavior, the _MCU_ENDIAN must be set to 2. |
/ If it is not the case, it can also be set to 1 for good code efficiency. */ |
#define _FS_READONLY 0 |
/* Setting _FS_READONLY to 1 defines read only configuration. This removes |
/ writing functions, f_write, f_sync, f_unlink, f_mkdir, f_chmod, f_rename, |
/ f_truncate and useless f_getfree. */ |
#define _FS_MINIMIZE 0 |
/* The _FS_MINIMIZE option defines minimization level to remove some functions. |
/ 0: Full function. |
/ 1: f_stat, f_getfree, f_unlink, f_mkdir, f_chmod, f_truncate and f_rename are removed. |
/ 2: f_opendir and f_readdir are removed in addition to level 1. |
/ 3: f_lseek is removed in addition to level 2. */ |
#define _USE_STRFUNC 0 |
/* To enable string functions, set _USE_STRFUNC to 1 or 2. */ |
#define _USE_MKFS 0 |
/* When _USE_MKFS is set to 1 and _FS_READONLY is set to 0, f_mkfs function is |
/ enabled. */ |
#define _DRIVES 2 |
/* Number of logical drives to be used. This affects the size of internal table. */ |
#define _MULTI_PARTITION 0 |
/* When _MULTI_PARTITION is set to 0, each logical drive is bound to same |
/ physical drive number and can mount only 1st primaly partition. When it is |
/ set to 1, each logical drive can mount a partition listed in Drives[]. */ |
#define _USE_FSINFO 0 |
/* To enable FSInfo support on FAT32 volume, set _USE_FSINFO to 1. */ |
#define _USE_SJIS 1 |
/* When _USE_SJIS is set to 1, Shift-JIS code transparency is enabled, otherwise |
/ only US-ASCII(7bit) code can be accepted as file/directory name. */ |
#define _USE_NTFLAG 1 |
/* When _USE_NTFLAG is set to 1, upper/lower case of the file name is preserved. |
/ Note that the files are always accessed in case insensitive. */ |
#include "integer.h" |
/* Definitions corresponds to multiple sector size (not tested) */ |
#define S_MAX_SIZ 512U /* Do not change */ |
#if S_MAX_SIZ > 512U |
#define SS(fs) ((fs)->s_size) |
#else |
#define SS(fs) 512U |
#endif |
/* File system object structure */ |
typedef struct _FATFS { |
WORD id; /* File system mount ID */ |
WORD n_rootdir; /* Number of root directory entries */ |
DWORD winsect; /* Current sector appearing in the win[] */ |
DWORD sects_fat; /* Sectors per fat */ |
DWORD max_clust; /* Maximum cluster# + 1 */ |
DWORD fatbase; /* FAT start sector */ |
DWORD dirbase; /* Root directory start sector (cluster# for FAT32) */ |
DWORD database; /* Data start sector */ |
#if !_FS_READONLY |
DWORD last_clust; /* Last allocated cluster */ |
DWORD free_clust; /* Number of free clusters */ |
#if _USE_FSINFO |
DWORD fsi_sector; /* fsinfo sector */ |
BYTE fsi_flag; /* fsinfo dirty flag (1:must be written back) */ |
BYTE pad2; |
#endif |
#endif |
BYTE fs_type; /* FAT sub type */ |
BYTE csize; /* Number of sectors per cluster */ |
#if S_MAX_SIZ > 512U |
WORD s_size; /* Sector size */ |
#endif |
BYTE n_fats; /* Number of FAT copies */ |
BYTE drive; /* Physical drive number */ |
BYTE winflag; /* win[] dirty flag (1:must be written back) */ |
BYTE pad1; |
BYTE win[S_MAX_SIZ]; /* Disk access window for Directory/FAT */ |
} FATFS; |
/* Directory object structure */ |
typedef struct _DIR { |
WORD id; /* Owner file system mount ID */ |
WORD index; /* Current index */ |
FATFS* fs; /* Pointer to the owner file system object */ |
DWORD sclust; /* Start cluster */ |
DWORD clust; /* Current cluster */ |
DWORD sect; /* Current sector */ |
} DIR; |
/* File object structure */ |
typedef struct _FIL { |
WORD id; /* Owner file system mount ID */ |
BYTE flag; /* File status flags */ |
BYTE csect; /* Sector address in the cluster */ |
FATFS* fs; /* Pointer to the owner file system object */ |
DWORD fptr; /* File R/W pointer */ |
DWORD fsize; /* File size */ |
DWORD org_clust; /* File start cluster */ |
DWORD curr_clust; /* Current cluster */ |
DWORD curr_sect; /* Current sector */ |
#if _FS_READONLY == 0 |
DWORD dir_sect; /* Sector containing the directory entry */ |
BYTE* dir_ptr; /* Ponter to the directory entry in the window */ |
#endif |
BYTE buffer[S_MAX_SIZ]; /* File R/W buffer */ |
} FIL; |
/* File status structure */ |
typedef struct _FILINFO { |
DWORD fsize; /* Size */ |
WORD fdate; /* Date */ |
WORD ftime; /* Time */ |
BYTE fattrib; /* Attribute */ |
char fname[8+1+3+1]; /* Name (8.3 format) */ |
} FILINFO; |
/* Definitions corresponds to multi partition */ |
#if _MULTI_PARTITION != 0 /* Multiple partition cfg */ |
typedef struct _PARTITION { |
BYTE pd; /* Physical drive # (0-255) */ |
BYTE pt; /* Partition # (0-3) */ |
} PARTITION; |
extern |
const PARTITION Drives[]; /* Logical drive# to physical location conversion table */ |
#define LD2PD(drv) (Drives[drv].pd) /* Get physical drive# */ |
#define LD2PT(drv) (Drives[drv].pt) /* Get partition# */ |
#else /* Single partition cfg */ |
#define LD2PD(drv) (drv) /* Physical drive# is equal to logical drive# */ |
#define LD2PT(drv) 0 /* Always mounts the 1st partition */ |
#endif |
/* File function return code (FRESULT) */ |
typedef enum { |
FR_OK = 0, /* 0 */ |
FR_NOT_READY, /* 1 */ |
FR_NO_FILE, /* 2 */ |
FR_NO_PATH, /* 3 */ |
FR_INVALID_NAME, /* 4 */ |
FR_INVALID_DRIVE, /* 5 */ |
FR_DENIED, /* 6 */ |
FR_EXIST, /* 7 */ |
FR_RW_ERROR, /* 8 */ |
FR_WRITE_PROTECTED, /* 9 */ |
FR_NOT_ENABLED, /* 10 */ |
FR_NO_FILESYSTEM, /* 11 */ |
FR_INVALID_OBJECT, /* 12 */ |
FR_MKFS_ABORTED /* 13 */ |
} FRESULT; |
/*-----------------------------------------------------*/ |
/* FatFs module application interface */ |
FRESULT f_mount (BYTE, FATFS*); /* Mount/Unmount a logical drive */ |
FRESULT f_open (FIL*, const char*, BYTE); /* Open or create a file */ |
FRESULT f_read (FIL*, void*, UINT, UINT*); /* Read data from a file */ |
FRESULT f_write (FIL*, const void*, UINT, UINT*); /* Write data to a file */ |
FRESULT f_lseek (FIL*, DWORD); /* Move file pointer of a file object */ |
FRESULT f_close (FIL*); /* Close an open file object */ |
FRESULT f_opendir (DIR*, const char*); /* Open an existing directory */ |
FRESULT f_readdir (DIR*, FILINFO*); /* Read a directory item */ |
FRESULT f_stat (const char*, FILINFO*); /* Get file status */ |
FRESULT f_getfree (const char*, DWORD*, FATFS**); /* Get number of free clusters on the drive */ |
FRESULT f_truncate (FIL*); /* Truncate file */ |
FRESULT f_sync (FIL*); /* Flush cached data of a writing file */ |
FRESULT f_unlink (const char*); /* Delete an existing file or directory */ |
FRESULT f_mkdir (const char*); /* Create a new directory */ |
FRESULT f_chmod (const char*, BYTE, BYTE); /* Change file/dir attriburte */ |
FRESULT f_utime (const char*, const FILINFO*); /* Change file/dir timestamp */ |
FRESULT f_rename (const char*, const char*); /* Rename/Move a file or directory */ |
FRESULT f_mkfs (BYTE, BYTE, WORD); /* Create a file system on the drive */ |
#if _USE_STRFUNC |
#define feof(fp) ((fp)->fptr == (fp)->fsize) |
#define EOF -1 |
int fputc (int, FIL*); /* Put a character to the file */ |
int fputs (const char*, FIL*); /* Put a string to the file */ |
int fprintf (FIL*, const char*, ...); /* Put a formatted string to the file */ |
char* fgets (char*, int, FIL*); /* Get a string from the file */ |
#endif |
/* User defined function to give a current time to fatfs module */ |
DWORD get_fattime (void); /* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */ |
/* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */ |
/* File access control and file status flags (FIL.flag) */ |
#define FA_READ 0x01 |
#define FA_OPEN_EXISTING 0x00 |
#if _FS_READONLY == 0 |
#define FA_WRITE 0x02 |
#define FA_CREATE_NEW 0x04 |
#define FA_CREATE_ALWAYS 0x08 |
#define FA_OPEN_ALWAYS 0x10 |
#define FA__WRITTEN 0x20 |
#define FA__DIRTY 0x40 |
#endif |
#define FA__ERROR 0x80 |
/* FAT sub type (FATFS.fs_type) */ |
#define FS_FAT12 1 |
#define FS_FAT16 2 |
#define FS_FAT32 3 |
/* File attribute bits for directory entry */ |
#define AM_RDO 0x01 /* Read only */ |
#define AM_HID 0x02 /* Hidden */ |
#define AM_SYS 0x04 /* System */ |
#define AM_VOL 0x08 /* Volume label */ |
#define AM_LFN 0x0F /* LFN entry */ |
#define AM_DIR 0x10 /* Directory */ |
#define AM_ARC 0x20 /* Archive */ |
/* Offset of FAT structure members */ |
#define BS_jmpBoot 0 |
#define BS_OEMName 3 |
#define BPB_BytsPerSec 11 |
#define BPB_SecPerClus 13 |
#define BPB_RsvdSecCnt 14 |
#define BPB_NumFATs 16 |
#define BPB_RootEntCnt 17 |
#define BPB_TotSec16 19 |
#define BPB_Media 21 |
#define BPB_FATSz16 22 |
#define BPB_SecPerTrk 24 |
#define BPB_NumHeads 26 |
#define BPB_HiddSec 28 |
#define BPB_TotSec32 32 |
#define BS_55AA 510 |
#define BS_DrvNum 36 |
#define BS_BootSig 38 |
#define BS_VolID 39 |
#define BS_VolLab 43 |
#define BS_FilSysType 54 |
#define BPB_FATSz32 36 |
#define BPB_ExtFlags 40 |
#define BPB_FSVer 42 |
#define BPB_RootClus 44 |
#define BPB_FSInfo 48 |
#define BPB_BkBootSec 50 |
#define BS_DrvNum32 64 |
#define BS_BootSig32 66 |
#define BS_VolID32 67 |
#define BS_VolLab32 71 |
#define BS_FilSysType32 82 |
#define FSI_LeadSig 0 |
#define FSI_StrucSig 484 |
#define FSI_Free_Count 488 |
#define FSI_Nxt_Free 492 |
#define MBR_Table 446 |
#define DIR_Name 0 |
#define DIR_Attr 11 |
#define DIR_NTres 12 |
#define DIR_CrtTime 14 |
#define DIR_CrtDate 16 |
#define DIR_FstClusHI 20 |
#define DIR_WrtTime 22 |
#define DIR_WrtDate 24 |
#define DIR_FstClusLO 26 |
#define DIR_FileSize 28 |
/* Multi-byte word access macros */ |
#if _MCU_ENDIAN == 1 /* Use word access */ |
#define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr)) |
#define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr)) |
#define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val) |
#define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val) |
#elif _MCU_ENDIAN == 2 /* Use byte-by-byte access */ |
#define LD_WORD(ptr) (WORD)(((WORD)*(volatile BYTE*)((ptr)+1)<<8)|(WORD)*(volatile BYTE*)(ptr)) |
#define LD_DWORD(ptr) (DWORD)(((DWORD)*(volatile BYTE*)((ptr)+3)<<24)|((DWORD)*(volatile BYTE*)((ptr)+2)<<16)|((WORD)*(volatile BYTE*)((ptr)+1)<<8)|*(volatile BYTE*)(ptr)) |
#define ST_WORD(ptr,val) *(volatile BYTE*)(ptr)=(BYTE)(val); *(volatile BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8) |
#define ST_DWORD(ptr,val) *(volatile BYTE*)(ptr)=(BYTE)(val); *(volatile BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8); *(volatile BYTE*)((ptr)+2)=(BYTE)((DWORD)(val)>>16); *(volatile BYTE*)((ptr)+3)=(BYTE)((DWORD)(val)>>24) |
#else |
#error Do not forget to set _MCU_ENDIAN properly! |
#endif |
#define _FATFS |
#endif /* _FATFS */ |
/Designs/DOGFINDER01A/SW/ATMEGA168/glg.hex |
---|
0,0 → 1,573 |
:100000000C9441000C945E000C945E000C945E0015 |
:100010000C945E000C945E000C945E000C945E00E8 |
:100020000C945E000C945E000C945E000C94C00274 |
:100030000C945E000C945E000C945E000C945E00C8 |
:100040000C945E000C945E000C94B5000C945E0061 |
:100050000C945E000C9460000C945E000C945E00A6 |
:100060000C945E000C945E00244750524D430024D3 |
:100070004750474741002E6C6F6700244750524D50 |
:10008000430011241FBECFEFD4E0DEBFCDBF11E08F |
:10009000A0E0B1E0EAEAF3E202C005900D92A630DA |
:1000A000B107D9F714E0A6E0B1E001C01D92AF336B |
:1000B000B107E1F70E9435010C94D3110C940000B4 |
:1000C0001F920F920FB60F9211242F933F938F938D |
:1000D000209178003091790080917C00203D31059D |
:1000E00070F4809109018F3030F08091650181605A |
:1000F0008093650106C08F5F8093090102C0109252 |
:10010000090181E02C32380770F0809108018F30AE |
:1001100030F0809165018E7F8093650106C08F5F0E |
:100120008093080102C01092080130936701209368 |
:10013000660182E480937C0080917C00309302020F |
:100140002093010281E480937C008FED80937A00FC |
:100150008F913F912F910F900FBE0F901F90189588 |
:1001600060E070E081EA96E308951F920F920FB667 |
:100170000F9211248F939F93EF93FF939091C6005A |
:1001800080916A01863988F48F5F80936A0180913B |
:100190006801E82FF0E0E859FE4F93838F5F8639BE |
:1001A00008F080E08093680105C08091650182605D |
:1001B00080936501FF91EF919F918F910F900FBEFA |
:1001C0000F901F90189590E051E448E901C090E02D |
:1001D0008091650180FD46C03091690180916A017E |
:1001E000882311F420E013C0E32FF0E0E859FE4F1C |
:1001F0002381E32FEF5FF89480916A01815080930F |
:100200006A017894E63908F0E0E0E09369018091B2 |
:10021000650181FF14C0F8941092C1001092690129 |
:100220001092680110926A015093C4004093C1007B |
:10023000809165018D7F809365017894C8CF2223DA |
:1002400039F2992319F4243209F0C2CFE92FF0E0F2 |
:10025000ED5EFE4F20839F5F2A3029F0923508F033 |
:10026000B6CFB6CF90E0892F0895A2E0B0E0EBE3DF |
:10027000F1E00C94A1118DE085B98EE284B98FE391 |
:1002800088B917B88EEA8BB982E58AB980E58CBD4A |
:1002900081E08DBD83ED90E3909389008093880089 |
:1002A0008AE08093810082E080936F0082E187BDC5 |
:1002B00082E484BD81E480937C008FED80937A009A |
:1002C00078946FE172E080E00E942709EE24FF2419 |
:1002D00034E6832E77247A9423E0C22E95E0D92E3B |
:1002E0005E010894A11CB11C82E3982E1092C100FB |
:1002F0002998809212018091650180FF02C080924E |
:100300001201809112018823B1F7299A709212018B |
:100310008091650180FD05C080E00E943D0F81FF56 |
:1003200002C07092120180911201882389F791E036 |
:100330000EC0C5BCD0921201809112018823E1F752 |
:1003400015BCD0921201809112018823E1F79150DF |
:1003500080F7F8941092C100109269011092680120 |
:1003600010926A0181E48093C40088E98093C100FF |
:10037000809165018D7F80936501789402C0EE24A1 |
:10038000FF240E94E300882309F4B0CF2BE730E07C |
:10039000A3E1B1E0F9012F5F3F4F9491992329F038 |
:1003A0008C91981771F41196F5CFE3E1F1E082E0BA |
:1003B0009081913220F331969C32D1F78150C1F770 |
:1003C0007F01E114F104E9F2F70180818134C9F67B |
:1003D000E3E1F1E089E09081913208F4A6C0319622 |
:1003E0009C32C9F78150B9F77F01309709F49DC05D |
:1003F000848195819093140180931301828193816C |
:10040000909316018093150180819181909318013A |
:100410008093170166E770E089E191E00E9437114F |
:1004200042E163E171E083E092E00E947F09892B61 |
:1004300009F07DC040910D0250910E0260910F02B3 |
:100440007091100283E092E00E94E404892B09F08D |
:100450006EC092E00EC0C5BCD09212018091120114 |
:100460008823E1F715BCD0921201809112018823F4 |
:10047000E1F7915080F73FC02FE630E0A3E1B1E013 |
:10048000F9012F5F3F4F9491992389F08C91981730 |
:1004900009F064C01196F4CF1196F9012F5F3F4F18 |
:1004A0009491992321F08C919817B1F311C0042FE6 |
:1004B00010E09501A80163E171E083E092E00E9401 |
:1004C000B606892BF9F489819A8108171907D1F4A6 |
:1004D0008091650182FF0FC083E092E00E94510687 |
:1004E000892B11F096E024C0F894809165018B7FF0 |
:1004F0008093650178940E94E300482F882309F0D7 |
:10050000BBCF31C095E014C0C5BC90921201809160 |
:1005100012018823E1F715BC90921201809112011B |
:100520008823E1F7915080F7E1CE93E001C094E099 |
:100530001092C100299833E029E135BD20931201C2 |
:10054000809112018823E1F715BC2093120180915C |
:1005500012018823E1F7915081F7FFCF28E630E0C0 |
:10056000A3E1B1E09ACF1092C100299883E092E014 |
:100570000E94A806892B11F097E0DACF91E0D2CF44 |
:100580001F920F920FB60F9211242F938F939F9368 |
:1005900080911201882319F081508093120180917B |
:1005A000060190910701019690930701809306013F |
:1005B0008055964448F010920701109206018091F0 |
:1005C00065018460809365010E9417119F918F914E |
:1005D0002F910F900FBE0F901F901895FC01009760 |
:1005E00069F0808191818617970741F480E00E942D |
:1005F0003D0F282F30E02170307002C02CE030E039 |
:10060000C9010895A0E0B0E0E8E0F3E00C94A01187 |
:100610004B015C01C0910A01D0910B01CC80DD80BF |
:10062000EE80FF80C616D706E806F90609F44EC02C |
:100630008F8D8823A1F180E2682E712C6C0E7D1EB7 |
:1006400001E0A7019601B30180E00E94A30F892B6E |
:10065000D9F51F8E8C899D89A0E0B0E02885398569 |
:100660004A855B85820F931FA41FB51FC816D90644 |
:10067000EA06FB06A0F41E8D10C08C899D89A0E0BF |
:10068000B0E0C80ED91EEA1EFB1E01E0A7019601CC |
:10069000B30180E00E94A30F1150123070F7811453 |
:1006A0009104A104B10491F0BE01605E7F4F01E0AE |
:1006B000A501940180E00E94440F892B29F48C82CB |
:1006C0009D82AE82BF8202C080E001C081E0CDB7D2 |
:1006D000DEB7EEE00C94BC11A0E0B0E0E2E7F3E09E |
:1006E0000C94A011EC014B01E0900A01F0900B0179 |
:1006F000F701A084B184C284D384848D813021F039 |
:10070000823009F072C05AC08E01000F111F0C0F09 |
:100710001D1F16950795B801672F7727669580E00E |
:1007200090E06A0D7B1D8C1D9D1D0E940203882395 |
:1007300009F45BC0D801B17051E0652E712C6C22B8 |
:100740007D22C0FF0AC0F701EA0FFB1F80A18F7056 |
:10075000982D9295907F982B01C0982DAE0DBF1DBE |
:10076000FD0190A3E801219681E0F701878FBE018A |
:10077000672F7727669580E090E06A0D7B1D8C1DC2 |
:100780009D1D0E940203882381F1DE01B170611476 |
:10079000710439F034E0969487943A95E1F7982DF6 |
:1007A00008C0F701EA0FFB1F90A1907F892D8F7081 |
:1007B000982BAE0DBF1DFD0190A313C06D2F7727A1 |
:1007C00080E090E06A0D7B1D8C1D9D1D0E94020340 |
:1007D000882359F0CC0FDD1FD170CE0DDF1D99A2FB |
:1007E00088A281E0F701878F01C080E0CDB7DEB736 |
:1007F000EEE00C94BC11A0E0B0E0E1E0F4E00C9479 |
:10080000A3118C01E0900A01F0900B018230910558 |
:1008100008F467C0E7018E899F890817190708F057 |
:1008200060C0A884B984CA84DB848C8D813021F0B7 |
:10083000823009F056C040C0E801CC0FDD1FC00F68 |
:10084000D11FD695C795BE01672F7727669580E0A3 |
:1008500090E06A0D7B1D8C1D9D1D0E940203882364 |
:1008600009F43FC0FE01F170EE0DFF1D90A021962E |
:10087000BE01672F7727669580E090E06A0D7B1DAB |
:100880008C1D9D1D0E940203882359F1292D30E003 |
:10089000D170EC0EFD1EF70190A180E0282B392BC2 |
:1008A00000FF07C0F90194E0F695E7959A95E1F706 |
:1008B0001AC0F901FF7017C0612F772780E090E020 |
:1008C0006A0D7B1D8C1D9D1D0E940203882349F02B |
:1008D000000F111F1170E00EF11EE701E8A1F9A150 |
:1008E00002C0E1E0F0E0CF01CDB7DEB7EBE00C9461 |
:1008F000BF11A0E0B0E0EFE7F4E00C94A4117C019C |
:10090000C0900A01D0900B01F601A688B788009725 |
:1009100049F4008D118D0230110588F00A151B0570 |
:1009200070F40FC00E94FB039C018230910508F413 |
:1009300040C08A159B0508F441C0870102C001E050 |
:1009400010E0E8012196CA15DB0528F002301105F8 |
:1009500098F1C2E0D0E0CE010E94FB039C01009719 |
:1009600039F08130910551F1C017D10759F724C0F2 |
:100970001C2F0D2F6FEF7FEFCE010E946C03882399 |
:10098000C1F0E114F10431F0BE01C7010E946C0313 |
:10099000882379F0F601108F018F828D938DFFEF00 |
:1009A0008F3F9F0721F00197F601938F828F9E0161 |
:1009B00005C021E030E002C020E030E0C901CDB741 |
:1009C000DEB7EAE00C94C011A0E0B0E0EAEEF4E09B |
:1009D0000C94A011EC017A018B016C807D80688100 |
:1009E0007981C3010E94EE024C01009709F0D6C044 |
:1009F0008A8187FDD0C0AA84BB84CC84DD84AE14F8 |
:100A0000BF04C006D10610F481FF02C0570168017F |
:100A10001E821F821886198681E08B83A114B1047F |
:100A2000C104D10409F49DC06E857F8561157105EF |
:100A300071F480E090E00E947904BC01813091055E |
:100A400009F4A6C09F878E87009709F48AC0F30136 |
:100A5000858DE82EFF2400E010E0F9E0EE0CFF1C8D |
:100A6000001F111FFA95D1F7798B688BEA14FB04EC |
:100A70000C051D0558F58A8181FF04C0CB010E9439 |
:100A8000790403C0CB010E94FB03BC010097E1F095 |
:100A90008130910509F47CC0F30186899789681734 |
:100AA000790708F075C08E819F81A885B9858E0D64 |
:100AB0009F1DA01FB11F8E839F83A887B987AE1883 |
:100AC000BF08C00AD10AD0CF57016801D601C501BD |
:100AD0000197A109B109E9E0B695A7959795879582 |
:100AE000EA95D1F7F82E00910A0110910B0162509E |
:100AF0007040F8018689978902976817790728F06E |
:100B000020E030E040E050E014C080E090E0F801E8 |
:100B1000258D30E040E050E00E945B119B01AC016C |
:100B2000F80180899189A289B389280F391F4A1F4A |
:100B30005B1F2F0D311D411D511D2A8B3B8B4C8B93 |
:100B40005D8BF301858D8F198B838E819F81A885A5 |
:100B5000B9858A0D9B1DAC1DBD1D8E839F83A88703 |
:100B6000B9876A8161FF1AC02E813F814885598506 |
:100B70008A859B85AC85BD8582179307A407B50739 |
:100B800068F42A873B874C875D8760626A8306C06A |
:100B90008A8180688A8368E0862E912CC401CDB753 |
:100BA000DEB7EEE00C94BC11EF92FF920F931F930F |
:100BB000CF93DF938C01C0910A01D0910B01FC010E |
:100BC000E280F3800894E11CF11CC7018F709070E3 |
:100BD000892B09F05BC082859385A485B585019634 |
:100BE000A11DB11D82879387A487B5874085518554 |
:100BF0004115510539F48A819B81E816F90608F4FC |
:100C000045C049C08D8D90E001979701A4E03695CD |
:100C10002795AA95E1F782239323892BB9F5CA0179 |
:100C20000E94FB03BC0182309105A8F18E899F8947 |
:100C30006817790780F5F80171876087C0910A010C |
:100C4000D0910B01625070408E899F89029768177E |
:100C5000790728F020E030E040E050E012C080E06A |
:100C600090E02D8D30E040E050E00E945B119B0150 |
:100C7000AC0188899989AA89BB89280F391F4A1F25 |
:100C80005B1FF8012287338744875587F801F38279 |
:100C9000E28281E001C080E0CDB7DEB7E6E00C94EF |
:100CA000C411EF92FF920F931F93CF93DF93EC0148 |
:100CB000688179818C819D810E94EE027C01009780 |
:100CC00009F040C08A8185FF3DC06E897F89888D8B |
:100CD000998D0E940203882391F10A8D1B8DF801E2 |
:100CE0008385806283878A859B85AC85BD85848F5B |
:100CF000958FA68FB78F8E859F85938F828F0E9449 |
:100D0000B000F801668B778B808F918F8A818F7D01 |
:100D10008A83E0910A01F0910B0181E0878F60E006 |
:100D200070E080E090E00E940203882341F040E000 |
:100D300050E063E080E00E941F10892B19F0B8E0BA |
:100D4000EB2EF12CC701CDB7DEB7E6E00C94C41151 |
:100D5000CF93DF93EC010E9451069C01009711F4A0 |
:100D60001D821C82C901DF91CF910895A0E0B0E0FF |
:100D7000ECEBF6E00C949D11EC01162F072F7A0195 |
:100D80003901AC80BD80F9011182108268817981BE |
:100D9000C5010E94EE024C01009709F03BC18A8117 |
:100DA00087FD35C181FD04C036E0832E912C32C110 |
:100DB0002A853B854C855D85C701A0E0B0E0820FA8 |
:100DC000931FA41FB51F82179307A407B50708F444 |
:100DD00021C1812F902F9C016901412C22E0522ECC |
:100DE00033243394F8C02E813F8148855985DA0138 |
:100DF000C9019170A070B0700097A105B10509F00C |
:100E0000B1C08B8181508B83882349F02A893B892B |
:100E10004C895D892F5F3F4F4F4F5F4F51C0211568 |
:100E200031054105510569F46E857F8561157105B0 |
:100E300089F480E090E00E947904BC019F878E874E |
:100E400005C0888999890E947904BC0161157105E2 |
:100E500009F4C5C06130710509F4D6C0F501868971 |
:100E600097896817790708F0CFC0798B688B009154 |
:100E70000A0110910B0162507040F8018689978930 |
:100E800002976817790728F020E030E040E050E052 |
:100E900014C080E090E0F801258D30E040E050E0A3 |
:100EA0000E945B119B01AC01F80180899189A289A4 |
:100EB000B389280F391F4A1F5B1FF501858D8B836E |
:100EC0002A8B3B8B4C8B5D8B0F2D069561F16B81D3 |
:100ED000102F601708F4162F012FB60180E00E9432 |
:100EE000A30F892B09F090C08B818F5F811B8B83AF |
:100EF000812F90E001979C01442737FD4095542FA6 |
:100F00008C010F5F1F4F8A899B89AC89BD89820F35 |
:100F1000931FA41FB51F8A8B9B8BAC8BBD8B102F8F |
:100F20000027110F3EC02E813F81488559858A8553 |
:100F30009B85AC85BD85281739074A075B0790F06C |
:100F400060E070E080E090E00E940203882309F4F2 |
:100F50005BC08A899B89AC89BD89F50184839583AF |
:100F6000A683B7836A897B898C899D890E94020345 |
:100F7000882309F449C02E813F813170C201821B50 |
:100F8000930B87018E159F0508F48C01205E3F4F5F |
:100F90002A0D3B1DA801B601C9010E944B11F501A4 |
:100FA000378EC00ED11EC801A0E0B0E02E813F8177 |
:100FB00048855985280F391F4A1F5B1F2E833F83A1 |
:100FC00048875987F30180819181800F911F918318 |
:100FD0008083E01AF10AE114F10409F004CF2E81B4 |
:100FE0003F81488559858A859B85AC85BD8582175B |
:100FF0009307A407B50720F42A873B874C875D87B2 |
:101000008A8180628A8306C08A8180688A8388E0B8 |
:10101000882E912CC401CDB7DEB7E1E10C94B91153 |
:10102000A0E0B0E0E6E1F8E00C949E11EC01162F90 |
:10103000072F7A0139018C809D80F901118210827D |
:1010400068817981C4010E94EE025C01009709F079 |
:10105000F8C08A8187FDF2C080FD04C076E0A72E2B |
:10106000B12CEFC02A853B854C855D858E819F81A3 |
:10107000A885B985281B390B4A0B5B0BC701A0E07B |
:10108000B0E0281739074A075B0708F47901812F78 |
:10109000902F9C016901412C62E0562EC7C02E8121 |
:1010A0003F8148855985DA01C9019170A070B070FF |
:1010B0000097A105B10509F084C08B8181508B8315 |
:1010C000882349F02A893B894C895D892F5F3F4FEE |
:1010D0004F4F5F4F43C0211531054105510519F4AC |
:1010E0006E857F8505C0888999890E94FB03BC01B4 |
:1010F0006230710508F49FC0F401868997896817EA |
:10110000790708F098C0798B688B00910A011091DB |
:101110000B0162507040F801868997890297681721 |
:10112000790728F020E030E040E050E014C080E093 |
:1011300090E0F801258D30E040E050E00E945B1126 |
:101140009B01AC01F80180899189A289B389280F9C |
:10115000391F4A1F5B1FF401858D8B832A8B3B8BC4 |
:101160004C8B5D8B0F2D069561F16B81102F6017F5 |
:1011700008F4162F012FB60180E00E94440F892B3E |
:1011800009F059C08B818F5F811B8B83812F90E089 |
:1011900001979C01442737FD4095542F8C010F5F28 |
:1011A0001F4F8A899B89AC89BD89820F931FA41F19 |
:1011B000B51F8A8B9B8BAC8BBD8B102F0027110F1B |
:1011C0001BC06A897B898C899D890E9402038823C0 |
:1011D00091F18E819F8191709201281B390B8701BB |
:1011E0002E153F0508F489018096880D991DA801E8 |
:1011F000BC01C6010E944B11C00ED11EC801A0E067 |
:10120000B0E02E813F8148855985280F391F4A1F3C |
:101210005B1F2E833F8348875987F301808191812B |
:10122000800F911F91838083E01AF10AE114F10489 |
:1012300009F035CF06C08A8180688A8348E0A42EF1 |
:10124000B12CC501CDB7DEB7E0E10C94BA11CF9354 |
:10125000DF93FB01882319F025E030E01DC0A09149 |
:101260000A01B0910B0170930B0160930A01109772 |
:1012700041F080E292E0ED019C0119922150304052 |
:10128000E1F7309739F080E292E0DF01EC011D9246 |
:101290002197E9F720E030E0C901DF91CF9108956F |
:1012A0000F93CF93DF939B01AC01C0910A01D091C2 |
:1012B0000B01BE01605E7F4F01E080E00E94440FA1 |
:1012C000892BC1F4C25EDD4F88819981CE51D24015 |
:1012D00085559A4A79F443E050E060E071E0CE0130 |
:1012E0008A5A9F4F0E943E119C0180E0232B19F0E7 |
:1012F00081E001C082E0DF91CF910F910895ADE1CF |
:10130000B0E0E5E8F9E00C949C119D8F8C8F3B01D7 |
:1013100080900A0190900B01FC011582148203C099 |
:101320000894611C711CF30180818032C9F38F32F3 |
:1013300019F40894611C711C8114910409F417C1FB |
:10134000F42FFF71FB8F142F1E71F401848D8823FD |
:1013500059F080E00E943D0F80FD06C0112309F482 |
:10136000ABC382FD07C1A8C380E292E0F4019C01F7 |
:10137000119221503040E1F780E00E94600E80FF22 |
:1013800003C021E030E08BC3112311F082FDF2C0D5 |
:1013900060E070E080E090E00E945009813021F030 |
:1013A000EE24FF2487011EC082EE91E0880E991E74 |
:1013B000F40180812EE13EEF820E931E882309F412 |
:1013C000DCC086EE91E0880E991EF401E080F18089 |
:1013D000028113812AE13EEF820E931EC801B701FC |
:1013E0000E945009882309F0C8C0F40183A594A580 |
:1013F0008050924009F0C1C0F40186A997A9BC01B0 |
:1014000080E090E0611571058105910569F424E49F |
:1014100030E0820E931EF40160817181828193819C |
:101420002CEB3FEF820E931EF401758B648B20A989 |
:10143000268F30E040E050E00E945B115B016C01C0 |
:10144000F40186A597A5A0E0B0E0E80EF91E0A1FFA |
:101450001B1FE086F1860287138785A5858F21A94A |
:1014600032A93383228383A994A97C0100E010E090 |
:10147000E114F1040105110569F480E490E0880E9F |
:10148000991EF401E080F1800281138180EC9FEFCE |
:10149000880E991EF40186A597A5A0E0B0E0E81A91 |
:1014A000F90A0A0B1B0BEA18FB080C091D09A4E03A |
:1014B00036952795AA95E1F7C901A0E0B0E0E81AB2 |
:1014C000F90A0A0B1B0B258D30E040E050E0C80103 |
:1014D000B7010E947A112E5F3F4F4F4F5F4FF401CB |
:1014E000378B268B283FFFE03F07F0E04F07F0E007 |
:1014F0005F0710F461E006C0285F3F4F4040504056 |
:10150000E0F562E0F4012085318542855385D601FE |
:10151000C501820F931FA41FB51F84879587A687D7 |
:10152000B7878281938174E0969587957A95E1F7E4 |
:10153000A0E0B0E0820F931FA41FB51F8A0D9B1D72 |
:10154000AC1DBD1D808B918BA28BB38B648F8FEFF5 |
:101550009FEF938F828F80910C0190910D010196E6 |
:1015600090930D0180930C0191838083A5C22AE0A2 |
:1015700030E095C229E030E092C22BE030E08FC22B |
:101580002E010894411C511C92E0292E312C4BE075 |
:1015900050E060E270E0C2010E94541120E048E097 |
:1015A00090E038E102C028E04BE0F30111913F01E7 |
:1015B000112311F01F3249F4222309F4C7C0392343 |
:1015C0003C87113009F044C0C1C0113208F4BEC0DC |
:1015D0001F3709F4BBC01E3241F490FDB7C022236F |
:1015E00009F4B4C02930F8F2B1C0123209F4AEC027 |
:1015F0001A3220F11D3208F4A9C01A33F8F0103461 |
:1016000008F4A4C090FD1AC01C3709F49FC0812FB4 |
:101610008B55833008F49AC0865E8A3128F44830AE |
:1016200011F4377F01C03F7E812F81568A3130F41B |
:101630001052483011F4986001C09061241708F0EE |
:1016400085C09E7FF201E20FF11D10832F5FADCFA9 |
:101650006F89788D898D9A8D0E940203882331F4D9 |
:1016600090E09C01590128E030E084C02F85388942 |
:101670002F70307085E0220F331F8A95E1F7F60155 |
:10168000E20FF31F80A18823E1F0853EA1F000E284 |
:10169000A02EB12CA20EB31EAC0CBD1CF50183858F |
:1016A000082F83FD08C04BE050E0B201C5010E9445 |
:1016B0003E11009751F0CE010D960E94D40588236B |
:1016C00039F6112309F049C04FC0112319F420E065 |
:1016D00030E050C004FF41C0F501E28CF38CFE8A7B |
:1016E000ED8A00910A0110910B01B7016250704020 |
:1016F000F8018689978902976817790728F020E012 |
:1017000030E040E050E014C080E090E0F801258D2A |
:1017100030E040E050E00E945B119B01AC01F80119 |
:1017200080899189A289B389280F391F4A1F5B1FBD |
:10173000FC8AEB8A2F8B388F498F5A8F388A2F86F5 |
:1017400026CF20E030E0AA24BB2414C080E090E043 |
:101750009C01590124E030E00DC080E090E09C0144 |
:10176000590123E030E006C080E090E09C0159017F |
:1017700022E030E0FB8D4F2F50E0CA018C71907059 |
:10178000892B09F441C12115310509F4D6C0223055 |
:10179000310509F084C160900A0170900B010B893A |
:1017A0001C890115110551F11E8B0D8BB80162507A |
:1017B0007040F3018689978902976817790728F0A6 |
:1017C00020E030E040E050E014C080E090E0F30121 |
:1017D000258D30E040E050E00E945B119B01AC01A0 |
:1017E000F30180899189A289B389280F391F4A1F83 |
:1017F0005B1F2F8B388F498F5A8F09C0F301848567 |
:101800009585A685B7858F8B988FA98FBA8F188AF3 |
:101810001F86BDE0EB2EF12CEC0EFD1E6F89788D3E |
:10182000898D9A8D0E940203882309F433C1EF85C4 |
:10183000F889EF70F070A5E0EE0FFF1FAA95E1F7B1 |
:10184000B0965301AE0EBF1EB097E60DF71D80A1F6 |
:10185000882309F44CC1853E09F449C1C7010E949F |
:10186000D4058823D9F60115110509F410C18D8915 |
:101870009E890E9479048C01009709F408C1813087 |
:10188000910509F407C160E070E080E090E00E94FB |
:101890000203882309F4FEC0A0900A01B0900B0156 |
:1018A000B80162507040F5018689978902976817E0 |
:1018B000790720F0CC24DD24760114C080E090E08C |
:1018C000F501258D30E040E050E00E945B116B0196 |
:1018D0007C01F50180899189A289B389C80ED91E3E |
:1018E000EA1EFB1EF301C482D582E682F782E0E2A3 |
:1018F000AE2EB12CA60CB71C80E092E0F5019C0145 |
:10190000119221503040E1F7F301158D10C001E034 |
:10191000A7019601B50180E00E94A30F892B09F071 |
:10192000B9C00894C11CD11CE11CF11C1150112339 |
:1019300071F781E0F301878FDAC0F2FF03C027E07F |
:1019400030E0ADC0A114B10409F4A1C0F501838554 |
:10195000817109F09CC043FF42C0C28CD38C138EAE |
:10196000128E148E158E168E178E81E0F401878FDD |
:101970002480358046805780E0900A01F0900B016A |
:1019800086011CC0C8010E94FB033C018130910507 |
:1019900009F480C060E070E0C8010E946C038823F5 |
:1019A00009F478C0F701828D938DFFEF8F3F9F0779 |
:1019B00021F00196F701938F828F83010230110588 |
:1019C00008F472C0F7018689978908171907D0F2C1 |
:1019D0006BC00894C108D108F401D18EC08EFB8D74 |
:1019E000F3FF22C080E2F50183870E94B000F50179 |
:1019F000668B778B808F918F66877787808B918BB3 |
:101A000081E0F401878F10C02115310509F047C02E |
:101A1000A114B10409F441C0F501838584FD3DC0E2 |
:101A200041FF02C080FD33C0F40184819581A6810D |
:101A3000B781EC8DFD8D868B978BA08FB18FB38E88 |
:101A4000A28E2B8D2283F501828D938DEC8DFD8DE1 |
:101A500097878687F501848D958DA68DB78DEC8D42 |
:101A6000FD8D82879387A487B5871682178210869B |
:101A7000118681E0838395828482F4018081918143 |
:101A8000EC8DFD8D9183808320E030E008C026E05E |
:101A900030E005C028E030E002C022E030E0C901BB |
:101AA0006D96E2E10C94B811C201B1010E940203EB |
:101AB000882309F08ECFEECFC0900A01D0900B01A1 |
:101AC000F601848595851C8A1B8A1E8A1D8AA0E0E2 |
:101AD000B0E08F8B988FA98FBA8F188A1F86DA8A09 |
:101AE000C98AF3018081882309F04ACD2ACE80E299 |
:101AF000F50111928A95E9F7D501FE0131968BE047 |
:101B000001900D928150E1F78C85F5018487FB8D62 |
:101B1000F860FB8F64CFFC018AE080930F019FEF98 |
:101B20009EBD0DB407FEFDCF8EB58F3F29F4809189 |
:101B30000F018823A9F722C08E3F01F59FEF9EBDBC |
:101B40000DB407FEFDCF8EB580839EBD0DB407FE9C |
:101B5000FDCF8EB581836250704011F03296EFCF89 |
:101B60008FEF8EBD0DB407FEFDCF8EB58FEF8EBD0E |
:101B70000DB407FEFDCF8EB581E0089580E0089595 |
:101B8000FC0182E3809310018FEF8EBD0DB407FE40 |
:101B9000FDCF8EB59FEF9EBD0DB407FEFDCF8EB578 |
:101BA0008F3F31F0809110018823A9F790E02EC07B |
:101BB0006EBD0DB407FEFDCF6D3F11F491E026C060 |
:101BC00090E080818EBD0DB407FEFDCF81818EBD7A |
:101BD0000DB407FEFDCF915011F03296F2CF8FEF8A |
:101BE0008EBD0DB407FEFDCF8FEF8EBD0DB407FE89 |
:101BF000FDCF8FEF8EBD0DB407FEFDCF8EB590E00B |
:101C00008F71853009F091E081E09827892F089540 |
:101C1000282F82E3809310018FEF8EBD0DB407FE55 |
:101C2000FDCF8EB59FEF9EBD0DB407FEFDCF8EB5E7 |
:101C30008F3F31F0809110018823A9F78FEF08952D |
:101C40002EBD0DB407FEFDCF872F9927AA27BB27EE |
:101C50008EBD0DB407FEFDCFCB01AA27BB278EBDDD |
:101C60000DB407FEFDCFBB27A72F962F852F8EBD66 |
:101C70000DB407FEFDCF4EBD0DB407FEFDCF2034E1 |
:101C800011F485E905C0283411F487E801C080E02B |
:101C90008EBD0DB407FEFDCF2C3431F48FEF8EBD19 |
:101CA0000DB407FEFDCF8EB59AE02FEF2EBD0DB41B |
:101CB00007FEFDCF8EB587FF02C09150B9F708959A |
:101CC000A4E0B0E0E6E6FEE00C94A811882311F051 |
:101CD00081E0CFC08091040181FDC9C08AE09FEFFF |
:101CE0009EBD0DB407FEFDCF2EB58150C9F72A98D1 |
:101CF00040E050E060E070E080E40E94080E813037 |
:101D000009F0A0C084E680930F014AEA51E060E048 |
:101D100070E088E40E94080E813009F054C07E0112 |
:101D20000894E11CF11C87019FEF9E012B5F3F4F40 |
:101D30009EBD0DB407FEFDCF8EB5F80181938F01D6 |
:101D4000E217F307A9F78B81813009F07BC08C8102 |
:101D50008A3A09F077C040E050E060E070E087E741 |
:101D60000E94080E823048F440E050E060E070E4E9 |
:101D700089E60E94080E882321F080910F018823B4 |
:101D800051F780910F01882309F45CC040E050E0D6 |
:101D900060E070E08AE70E94080E882309F052C0D4 |
:101DA0009FEF9EBD0DB407FEFDCF8EB5F701819369 |
:101DB0007F01E017F107A9F7898186FD02C012E0D3 |
:101DC00042C016E040C040E050E060E070E087E7CD |
:101DD0000E94080E823058F440E050E060E070E06D |
:101DE00089E60E94080E823010F412E001C011E072 |
:101DF000123079F440E050E060E070E087E70E9444 |
:101E0000080E823078F440E050E060E070E089E64F |
:101E100005C040E050E060E070E081E40E94080E00 |
:101E2000882321F080910F01882311F780910F0101 |
:101E3000882341F040E052E060E070E080E50E94DD |
:101E4000080E811110E0109311012A9A8FEF8EBDB8 |
:101E50000DB407FEFDCF8EB5112321F08091040152 |
:101E60008E7F03C080910401816080930401809182 |
:101E700004012496E6E00C94C411882311F081E05B |
:101E800008958091040108950F93CF93DF93EB01A0 |
:101E9000882309F051C0002309F44EC08091040149 |
:101EA00080FF03C023E030E049C08091110182FD32 |
:101EB00007C069E0220F331F441F551F6A95D1F7F1 |
:101EC0002A98013089F4BA01A90181E50E94080E1F |
:101ED000882321F560E072E0CE010E948B0D00E0C6 |
:101EE0008823E1F401E01AC0BA01A90182E50E9449 |
:101EF000080E882399F460E072E0CE010E948B0DF9 |
:101F0000882329F0015019F0C050DE4FF4CF40E093 |
:101F100050E060E070E08CE40E94080E2A9A8FEF97 |
:101F20008EBD0DB407FEFDCF8EB520E030E000235E |
:101F300029F021E030E002C024E030E0C901DF9167 |
:101F4000CF910F910895CF92DF92EF92FF920F936E |
:101F5000CF93DF93EB0169017A01882309F064C014 |
:101F6000002309F461C08091040180FF03C023E0D5 |
:101F700030E05CC08091040182FF03C022E030E0C9 |
:101F800055C08091110182FD07C079E0CC0CDD1CA9 |
:101F9000EE1CFF1C7A95D1F72A98013069F4B7013D |
:101FA000A60188E50E94080E882381F56EEFCE0118 |
:101FB0000E94C00D00E027C081FF0EC040E050E04D |
:101FC00060E070E087E70E94080E402F50E060E07C |
:101FD00070E087E50E94080EB701A60189E50E941E |
:101FE000080E882399F46CEFCE010E94C00D88235F |
:101FF00029F0015019F0C050DE4FF5CF6DEF80E0B1 |
:1020000090E00E94C00D882309F401E02A9A8FEF26 |
:102010008EBD0DB407FEFDCF8EB520E030E000236D |
:1020200029F021E030E002C024E030E0C901DF9176 |
:10203000CF910F91FF90EF90DF90CF900895A0E1A6 |
:10204000B0E0E5E2F0E10C94AA118A01882319F0CE |
:1020500024E030E0E7C02A98633009F47FC06430A0 |
:1020600028F4613079F0623051F472C06B3009F4B9 |
:1020700096C06C3009F4AAC06A3009F486C024E026 |
:1020800030E0C6C040E050E060E070E089E40E94CB |
:10209000080E882309F0B7C060E170E0CE01019618 |
:1020A0000E948B0D882309F4AEC0898182958695A4 |
:1020B000869583707A8549858130B1F4872F90E0C9 |
:1020C0000196342F20E0820F931FA0E0B0E02AE0B9 |
:1020D000880F991FAA1FBB1F2A95D1F7F80180838B |
:1020E0009183A283B38392C04295469546954370EF |
:1020F000288530E0220F331F220F331F240F311D9C |
:102100002F5F3F4F8F8190E083709070982F8827CA |
:10211000990F990F280F391F40E050E08E818F7082 |
:102120006B85661F6627661F860F8E5F7370770F3D |
:10213000870F90E0099704C0220F331F441F551FDB |
:102140008A95D2F7F80120833183428353835EC09E |
:1021500080E092E0FA019183808358C082E380930B |
:1021600010018FEF8EBD0DB407FEFDCF8EB59FEF32 |
:102170009EBD0DB407FEFDCF8EB58F3F09F446C05E |
:10218000809110018823A1F73EC08091040180FD59 |
:1021900047C040E050E060E070E089E409C0809111 |
:1021A000040180FD3DC040E050E060E070E08AE462 |
:1021B0000E94080E882339F560E170E0C8010E9492 |
:1021C0008B0D20E030E0882319F51DC080910401BB |
:1021D00080FD26C040E050E060E070E08AE70E94A9 |
:1021E000080E882381F4F8019FEF98012D5F3F4F7F |
:1021F0009EBD0DB407FEFDCF8EB58083E217F307B9 |
:1022000029F03196F5CF21E030E002C020E030E047 |
:102210002A9A8FEF8EBD0DB407FEFDCF8EB502C09A |
:1022200023E030E0C9016096E4E00C94C61180918F |
:102230000F01882319F0815080930F0180911001C4 |
:10224000882319F081508093100190910E0183B181 |
:10225000817080930E01981749F48091040190FFDA |
:1022600002C0836001C08D7F809304010895FB014B |
:10227000DC0105900D920020E1F70895FB01DC01DF |
:1022800004C08D910190801921F441505040C8F74D |
:10229000881B990B0895FB01DC0102C001900D928F |
:1022A00041505040D8F70895DC0101C06D93415072 |
:1022B0005040E0F70895629FD001739FF001829F24 |
:1022C000E00DF11D649FE00DF11D929FF00D839FC5 |
:1022D000F00D749FF00D659FF00D9927729FB00D62 |
:1022E000E11DF91F639FB00DE11DF91FBD01CF0175 |
:1022F00011240895A1E21A2EAA1BBB1BFD010DC0DB |
:10230000AA1FBB1FEE1FFF1FA217B307E407F507A5 |
:1023100020F0A21BB30BE40BF50B661F771F881F81 |
:10232000991F1A9469F760957095809590959B0117 |
:10233000AC01BD01CF0108952F923F924F925F9261 |
:102340006F927F928F929F92AF92BF92CF92DF92C5 |
:10235000EF92FF920F931F93CF93DF93CDB7DEB72A |
:10236000CA1BDB0B0FB6F894DEBF0FBECDBF0994BE |
:102370002A88398848885F846E847D848C849B8415 |
:10238000AA84B984C884DF80EE80FD800C811B8123 |
:10239000AA81B981CE0FD11D0FB6F894DEBF0FBE52 |
:0A23A000CDBFED010895F894FFCFC2 |
:0623AA0046415400010051 |
:00000001FF |
/Designs/DOGFINDER01A/SW/ATMEGA168/integer.h |
---|
0,0 → 1,23 |
#ifndef _INTEGER |
typedef int INT; |
typedef unsigned int UINT; |
typedef char CHAR; |
typedef unsigned char UCHAR; |
typedef unsigned char BYTE; |
typedef short SHORT; |
typedef unsigned short USHORT; |
typedef unsigned short WORD; |
typedef long LONG; |
typedef unsigned long ULONG; |
typedef unsigned long DWORD; |
typedef unsigned char BOOL; |
#define FALSE 0 |
#define TRUE 1 |
#define _INTEGER |
#endif |
/Designs/DOGFINDER01A/SW/ATMEGA168/main.c |
---|
0,0 → 1,393 |
/*---------------------------------------------------------------*/ |
/* GPS data logger R0.02 (C)ChaN, 2008 */ |
/*---------------------------------------------------------------*/ |
#include <avr/io.h> |
#include <avr/pgmspace.h> |
#include <avr/interrupt.h> |
#include <string.h> |
#include "tff.h" |
#include "diskio.h" |
#define SYSCLK 10000000UL |
#define BEEP_ON() TCCR0B=0b011 |
#define BEEP_OFF() TCCR0B=0b000 |
#define GPS_ON() PORTB|=0x02 |
#define GPS_OFF() PORTB&=0xFD |
#define DELAY(dly) for(Timer=dly;Timer;) |
#define VTH_LOW (WORD)(8000UL*100/3838) |
#define VTH_HIGH (WORD)(11500UL*100/3838) |
#define POWER_check 0b01000000 | 1 |
#define ANALOG_IN1 0b01000000 | 2 |
FATFS fatfs; /* File system object for each logical drive */ |
FIL file1; /* File object */ |
BYTE Buff[82]; /* File/Console buffer */ |
uint16_t battery; // battery voltage |
uint16_t intensity; // radiation intensity |
volatile BYTE Timer; /* 100Hz decrement timer */ |
volatile BYTE Stat; /* Status */ |
typedef struct _fifo { |
uint8_t idx_w; |
uint8_t idx_r; |
uint8_t count; |
uint8_t buff[150]; |
} FIFO; |
volatile FIFO rxfifo; |
/*---------------------------------------------------------*/ |
/* ADC interrupt */ |
/*---------------------------------------------------------*/ |
ISR(ADC_vect) |
{ |
WORD n; |
static BYTE l, h; |
n = ADC; |
if(ADMUX == POWER_check) |
{ |
if (n < VTH_LOW) { |
if (l >= 15) { |
Stat |= 0x01; |
} |
else {l++;} |
} |
else {l = 0;} |
if (n > VTH_HIGH) { |
if (h >= 15) { |
Stat &= 0xFE; |
} |
else {h++;} |
} |
else {h = 0;} |
battery = n; |
ADMUX = ANALOG_IN1; |
} |
if(ADMUX == ANALOG_IN1) |
{ |
intensity = n; |
ADMUX = POWER_check; |
} |
//!!!! |
//Stat &= 0xFE; |
ADCSRA = _BV(ADEN)|_BV(ADSC)|_BV(ADIF)|_BV(ADIE)|0b111; |
} |
/*---------------------------------------------------------*/ |
/* 100Hz timer interrupt generated by OC1A */ |
/*---------------------------------------------------------*/ |
ISR(TIMER1_COMPA_vect) |
{ |
BYTE n; |
static WORD ivt_sync; |
n = Timer; |
if (n) Timer = n - 1; |
if (++ivt_sync >= 180 * 100) { |
ivt_sync = 0; |
Stat |= 4; |
} |
disk_timerproc(); /* Drive timer procedure of low level disk I/O module */ |
} |
/*---------------------------------------------------------*/ |
/* User Provided Timer Function for FatFs module */ |
/*---------------------------------------------------------*/ |
/* This is a real time clock service to be called from */ |
/* FatFs module. Any valid time must be returned even if */ |
/* the system does not support a real time clock. */ |
DWORD get_fattime () |
{ |
return ((2007UL - 1980) << 25) /* Fixed to 2007.5.1, 00:00:00 */ |
| ((5UL) << 21) |
| ((1UL) << 16) |
| (0 << 11) |
| (0 << 5) |
| (0 >> 1); |
} |
/*--------------------------------------------------------------------------*/ |
/* UART control */ |
static |
void uart_init (void) |
{ |
cli(); |
UCSR0B = 0; |
rxfifo.idx_r = 0; |
rxfifo.idx_w = 0; |
rxfifo.count = 0; |
UBRR0L = SYSCLK/16/9600; // Enable USRAT0 in N81,4800bps |
UCSR0B = _BV(RXCIE0)|_BV(RXEN0)|_BV(TXEN0); |
Stat &= 0xFD; // Clear overflow flag |
sei(); |
} |
static |
void uart_stop (void) |
{ |
UCSR0B = 0; |
} |
/* Get a received character */ |
static |
uint8_t uart_get () |
{ |
uint8_t d, i; |
i = rxfifo.idx_r; |
if (rxfifo.count == 0) return 0; |
d = rxfifo.buff[i++]; |
cli(); |
rxfifo.count--; |
sei(); |
if(i >= sizeof(rxfifo.buff)) |
i = 0; |
rxfifo.idx_r = i; |
return d; |
+ |
+/* USART0 RXC interrupt */ |
+ISR(USART_RX_vect) |
+{ |
+ uint8_t d, n, i; |
+ |
+ |
+ d = UDR0; |
+ n = rxfifo.count; |
+ if(n < sizeof(rxfifo.buff)) { |
+ rxfifo.count = ++n; |
+ i = rxfifo.idx_w; |
+ rxfifo.buff[i++] = d; |
+ if(i >= sizeof(rxfifo.buff)) |
+ i = 0; |
+ rxfifo.idx_w = i; |
+ } else { |
+ Stat |= 2; |
+ } |
+} |
+ |
+ |
+ |
+/*----------------------------------------------------*/ |
+/* Get a line received from GPS module */ |
+/*----------------------------------------------------*/ |
+ |
+static |
+BYTE get_line (void) // 0: Power fail occured, >0: Number of bytes received. |
+{ |
+ BYTE c, i = 0; |
+ |
+ |
+ for (;;) { |
+ if (Stat & 1) return 0; // When power fail is detected, return with zero. |
+ c = uart_get(); |
+ if (Stat & 2) { // When buffer overflow has occured, restert to receive line. |
+ uart_init(); |
+ i = 0; c = 0; |
+ } |
+ if (!c || (i == 0 && c != '$')) continue; |
+ Buff[i++] = c; |
+ if (c == '\n') break; |
+ if (i >= sizeof(Buff)) i = 0; |
+ } |
+ return i; |
+} |
+ |
+ |
+ |
+/*--------------------------------------------------------------------------*/ |
+/* Controls */ |
+ |
+static |
+void beep (BYTE len, BYTE cnt) |
+{ |
+ while (cnt--) { |
+ BEEP_ON(); |
+ DELAY(len); |
+ BEEP_OFF(); |
+ DELAY(len); |
+ } |
+} |
+ |
+ |
+ |
+ |
+/* Compare sentence header string */ |
+static |
+BYTE gp_comp (BYTE *str1, const prog_uint8_t *str2) |
+{ |
+ BYTE c; |
+ |
+ do { |
+ c = pgm_read_byte(str2++); |
+ } while (c && c == *str1++); |
+ return c; |
+} |
+ |
+/* Get a column item */ |
+static |
+BYTE* gp_col ( /* Returns pointer to the item (returns a NULL when not found) */ |
+ const BYTE* buf, /* Pointer to the sentence */ |
+ BYTE col /* Column number (0 is the 1st item) */ |
+) { |
+ BYTE c; |
+ |
+ |
+ while (col) { |
+ do { |
+ c = *buf++; |
+ if (c <= ' ') return NULL; |
+ } while (c != ','); |
+ col--; |
+ } |
+ return (BYTE*)buf; |
+} |
+ |
+ |
+ |
+static |
+void ioinit (void) |
+{ |
+ PORTB = 0b00001101; // Port B |
+ DDRB = 0b00101110; |
+ PORTC = 0b00111111; // Port C |
+ DDRC = 0b00000000; |
+ PORTD = 0b10101110; // Port D |
+ DDRD = 0b01010010; |
+ |
+ SPCR = 0b01010000; /* Initialize SPI port (Mode 0) */ |
+ SPSR = 0b00000001; |
+ |
+ OCR1A = SYSCLK/8/100-1; // Timer1: 100Hz interval (OC1A) |
+ TCCR1B = 0b00001010; |
+ TIMSK1 = _BV(OCIE1A); // Enable TC1.oca interrupt |
+ |
+ OCR0A = SYSCLK/64/4000/2-1; // Timer0: 4kHz sound (OC0A) |
+ TCCR0A = 0b01000010; |
+ |
+ ADMUX = POWER_check; // Select ADC input |
+ ADCSRA = _BV(ADEN)|_BV(ADSC)|_BV(ADIF)|_BV(ADIE)|0b111; |
+ |
+ sei(); |
+} |
+ |
+ |
+ |
+/*-----------------------------------------------------------------------*/ |
+/* Main */ |
+ |
+ |
+int main () |
+{ |
+ BYTE b, err, *p = NULL; |
+ WORD s; |
+ |
+ |
+ ioinit(); |
+ f_mount(0, &fatfs); /* Enable file I/O layer */ |
+ |
+ for (;;) { |
+ uart_stop(); |
+ GPS_OFF(); |
+ Timer = 100; |
+ do { |
+ if (Stat & 1) Timer = 100; |
+ } while (Timer); |
+ |
+ GPS_ON(); |
+ Timer = 255; |
+ do { |
+ if ((Stat & 1) || (disk_status(0) & STA_NODISK)) Timer = 255; |
+ } while (Timer); |
+ |
+ beep(5, 1); // Single beep. Start to get current time. |
+ uart_init(); |
+ do { // Wait for valid RMC sentence. |
+ b = get_line(); |
+ if (!b) break; |
+ if (gp_comp(Buff, PSTR("$GPRMC"))) continue; |
+ p = gp_col(Buff,2); |
+ } while (!p || *p != 'A'); |
+ if (!b) continue; |
+ p = gp_col(Buff,9); // Open log file with the name of current date (YYMMDD.log in UTC). |
+ |
+ if (!p) {err = 3; break;} |
+ |
+ memcpy(&Buff[0], p+4, 2); |
+ memcpy(&Buff[2], p+2, 2); |
+ memcpy(&Buff[4], p+0, 2); |
+ strcpy_P(&Buff[6], PSTR(".log")); |
+ if (f_open(&file1, Buff, FA_OPEN_ALWAYS | FA_WRITE) || f_lseek(&file1, file1.fsize)) { err = 4; break; } |
+ |
+ beep(5, 2); // Two beeps. Start logging. |
+ err = 0; |
+ while ((b = get_line()) > 0) { |
+ if ( !gp_comp(Buff, PSTR("$GPGGA")) // Which sentence is logged? |
+ || !gp_comp(Buff, PSTR("$GPRMC")) |
+ // || !gp_comp(Buff, PSTR("$GPGSA")) |
+ // || !gp_comp(Buff, PSTR("$GPGLL")) |
+ // || !gp_comp(Buff, PSTR("$GPGSV")) |
+ // || !gp_comp(Buff, PSTR("$GPZDA")) |
+ // || !gp_comp(Buff, PSTR("$GPVTG")) |
+ ) |
+ { |
+ if (f_write(&file1, Buff, b, &s) || b != s) { err = 5; break; }; |
+ } |
+ if ((Stat & 4) == 0) continue; |
+ if (f_sync(&file1)) { err = 6; break; };// Synchronize the file in interval of 300 sec. |
+ cli(); Stat &= 0xFB; sei(); // Clear sync request |
+ } |
+ if (err) break; |
+ |
+ // Turn-off GPS power and close the log file by power supply is discharged. |
+ uart_stop(); |
+ GPS_OFF(); |
+ if (f_close(&file1)) { err = 7; break; }; |
+ |
+ // When a long beep is sounded, the shutdoun process has been succeeded. |
+ beep(50, 1); |
+ } |
+ |
+ // Unrecoverble error. Enter shutdown state. |
+ uart_stop(); |
+ GPS_OFF(); |
+ beep(25, err); |
+ for (;;); |
+} |
+ |
+ |
/Designs/DOGFINDER01A/SW/ATMEGA168/mmc.c |
---|
0,0 → 1,487 |
/*-----------------------------------------------------------------------*/ |
/* MMC/SDC (in SPI mode) control module (C)ChaN, 2006 */ |
/*-----------------------------------------------------------------------*/ |
/* Only rcvr_spi(), xmit_spi(), disk_timerproc(), disk_initialize () and */ |
/* some macros are platform dependent. */ |
/*-----------------------------------------------------------------------*/ |
#include <avr/io.h> |
#include "diskio.h" |
/* Definitions for MMC/SDC command */ |
#define CMD0 (0x40+0) /* GO_IDLE_STATE */ |
#define CMD1 (0x40+1) /* SEND_OP_COND */ |
#define CMD8 (0x40+8) /* SEND_IF_COND */ |
#define CMD9 (0x40+9) /* SEND_CSD */ |
#define CMD10 (0x40+10) /* SEND_CID */ |
#define CMD12 (0x40+12) /* STOP_TRANSMISSION */ |
#define CMD16 (0x40+16) /* SET_BLOCKLEN */ |
#define CMD17 (0x40+17) /* READ_SINGLE_BLOCK */ |
#define CMD18 (0x40+18) /* READ_MULTIPLE_BLOCK */ |
#define CMD23 (0x40+23) /* SET_BLOCK_COUNT */ |
#define CMD24 (0x40+24) /* WRITE_BLOCK */ |
#define CMD25 (0x40+25) /* WRITE_MULTIPLE_BLOCK */ |
#define CMD41 (0x40+41) /* SEND_OP_COND (ACMD) */ |
#define CMD55 (0x40+55) /* APP_CMD */ |
#define CMD58 (0x40+58) /* READ_OCR */ |
/* Control signals (Platform dependent) */ |
#define SELECT() PORTB &= ~_BV(PB2) /* MMC CS = L */ |
#define DESELECT() PORTB |= _BV(PB2) /* MMC CS = H */ |
#define SOCKPORT PINB /* Socket contact port */ |
#define SOCKINS 0x01 /* Card detect switch (PB0) */ |
/*-------------------------------------------------------------------------- |
Module Private Functions |
---------------------------------------------------------------------------*/ |
static volatile |
DSTATUS Stat = STA_NOINIT; /* Disk status */ |
static volatile |
BYTE Timer1, Timer2; /* 100Hz decrement timer */ |
static |
BYTE CardType; /* b0:MMC, b1:SDC, b2:Block addressing */ |
/*-----------------------------------------------------------------------*/ |
/* Transmit a byte to MMC via SPI (Platform dependent) */ |
/*-----------------------------------------------------------------------*/ |
#define xmit_spi(dat) SPDR=(dat); loop_until_bit_is_set(SPSR,SPIF) |
/*-----------------------------------------------------------------------*/ |
/* Receive a byte from MMC via SPI (Platform dependent) */ |
/*-----------------------------------------------------------------------*/ |
static |
BYTE rcvr_spi (void) |
{ |
SPDR = 0xFF; |
loop_until_bit_is_set(SPSR, SPIF); |
return SPDR; |
} |
/* Alternative macro to receive data fast */ |
#define rcvr_spi_m(dst) SPDR=0xFF; loop_until_bit_is_set(SPSR,SPIF); *(dst)=SPDR |
/*-----------------------------------------------------------------------*/ |
/* Wait for card ready */ |
/*-----------------------------------------------------------------------*/ |
static |
BYTE wait_ready (void) |
{ |
BYTE res; |
Timer2 = 50; /* Wait for ready in timeout of 500ms */ |
rcvr_spi(); |
do |
res = rcvr_spi(); |
while ((res != 0xFF) && Timer2); |
return res; |
} |
/*-----------------------------------------------------------------------*/ |
/* Receive a data packet from MMC */ |
/*-----------------------------------------------------------------------*/ |
static |
BOOL rcvr_datablock ( |
BYTE *buff, /* Data buffer to store received data */ |
UINT btr /* Byte count (must be even number) */ |
) |
{ |
BYTE token; |
Timer1 = 10; |
do { /* Wait for data packet in timeout of 100ms */ |
token = rcvr_spi(); |
} while ((token == 0xFF) && Timer1); |
if(token != 0xFE) return FALSE; /* If not valid data token, retutn with error */ |
do { /* Receive the data block into buffer */ |
rcvr_spi_m(buff++); |
rcvr_spi_m(buff++); |
} while (btr -= 2); |
rcvr_spi(); /* Discard CRC */ |
rcvr_spi(); |
return TRUE; /* Return with success */ |
} |
/*-----------------------------------------------------------------------*/ |
/* Send a data packet to MMC */ |
/*-----------------------------------------------------------------------*/ |
#if _READONLY == 0 |
static |
BOOL xmit_datablock ( |
const BYTE *buff, /* 512 byte data block to be transmitted */ |
BYTE token /* Data/Stop token */ |
) |
{ |
BYTE resp, wc; |
if (wait_ready() != 0xFF) return FALSE; |
xmit_spi(token); /* Xmit data token */ |
if (token != 0xFD) { /* Is data token */ |
wc = 0; |
do { /* Xmit the 512 byte data block to MMC */ |
xmit_spi(*buff++); |
xmit_spi(*buff++); |
} while (--wc); |
xmit_spi(0xFF); /* CRC (Dummy) */ |
xmit_spi(0xFF); |
resp = rcvr_spi(); /* Reveive data response */ |
if ((resp & 0x1F) != 0x05) /* If not accepted, return with error */ |
return FALSE; |
} |
return TRUE; |
} |
#endif /* _READONLY */ |
/*-----------------------------------------------------------------------*/ |
/* Send a command packet to MMC */ |
/*-----------------------------------------------------------------------*/ |
static |
BYTE send_cmd ( |
BYTE cmd, /* Command byte */ |
DWORD arg /* Argument */ |
) |
{ |
BYTE n, res; |
if (wait_ready() != 0xFF) return 0xFF; |
/* Send command packet */ |
xmit_spi(cmd); /* Command */ |
xmit_spi((BYTE)(arg >> 24)); /* Argument[31..24] */ |
xmit_spi((BYTE)(arg >> 16)); /* Argument[23..16] */ |
xmit_spi((BYTE)(arg >> 8)); /* Argument[15..8] */ |
xmit_spi((BYTE)arg); /* Argument[7..0] */ |
n = 0; |
if (cmd == CMD0) n = 0x95; /* CRC for CMD0(0) */ |
if (cmd == CMD8) n = 0x87; /* CRC for CMD8(0x1AA) */ |
xmit_spi(n); |
/* Receive command response */ |
if (cmd == CMD12) rcvr_spi(); /* Skip a stuff byte when stop reading */ |
n = 10; /* Wait for a valid response in timeout of 10 attempts */ |
do |
res = rcvr_spi(); |
while ((res & 0x80) && --n); |
return res; /* Return with the response value */ |
} |
/*-------------------------------------------------------------------------- |
Public Functions |
---------------------------------------------------------------------------*/ |
/*-----------------------------------------------------------------------*/ |
/* Initialize Disk Drive */ |
/*-----------------------------------------------------------------------*/ |
DSTATUS disk_initialize ( |
BYTE drv /* Physical drive nmuber (0) */ |
) |
{ |
BYTE n, ty, ocr[4]; |
if (drv) return STA_NOINIT; /* Supports only single drive */ |
if (Stat & STA_NODISK) return Stat; /* No card in the socket */ |
for (n = 10; n; n--) rcvr_spi(); /* 80 dummy clocks */ |
SELECT(); /* CS = L */ |
ty = 0; |
if (send_cmd(CMD0, 0) == 1) { /* Enter Idle state */ |
Timer1 = 100; /* Initialization timeout of 1000 msec */ |
if (send_cmd(CMD8, 0x1AA) == 1) { /* SDC Ver2+ */ |
for (n = 0; n < 4; n++) ocr[n] = rcvr_spi(); |
if (ocr[2] == 0x01 && ocr[3] == 0xAA) { /* The card can work at vdd range of 2.7-3.6V */ |
do { |
if (send_cmd(CMD55, 0) <= 1 && send_cmd(CMD41, 1UL << 30) == 0) break; /* ACMD41 with HCS bit */ |
} while (Timer1); |
if (Timer1 && send_cmd(CMD58, 0) == 0) { /* Check CCS bit */ |
for (n = 0; n < 4; n++) ocr[n] = rcvr_spi(); |
ty = (ocr[0] & 0x40) ? 6 : 2; |
} |
} |
} else { /* SDC Ver1 or MMC */ |
ty = (send_cmd(CMD55, 0) <= 1 && send_cmd(CMD41, 0) <= 1) ? 2 : 1; /* SDC : MMC */ |
do { |
if (ty == 2) { |
if (send_cmd(CMD55, 0) <= 1 && send_cmd(CMD41, 0) == 0) break; /* ACMD41 */ |
} else { |
if (send_cmd(CMD1, 0) == 0) break; /* CMD1 */ |
} |
} while (Timer1); |
if (!Timer1 || send_cmd(CMD16, 512) != 0) /* Select R/W block length */ |
ty = 0; |
} |
} |
CardType = ty; |
DESELECT(); /* CS = H */ |
rcvr_spi(); /* Idle (Release DO) */ |
if (ty) { /* Initialization succeded */ |
Stat &= ~STA_NOINIT; /* Clear STA_NOINIT */ |
} else { /* Initialization failed */ |
Stat |= STA_NOINIT; /* Set STA_NOINIT */ |
} |
return Stat; |
} |
/*-----------------------------------------------------------------------*/ |
/* Get Disk Status */ |
/*-----------------------------------------------------------------------*/ |
DSTATUS disk_status ( |
BYTE drv /* Physical drive nmuber (0) */ |
) |
{ |
if (drv) return STA_NOINIT; /* Supports only single drive */ |
return Stat; |
} |
/*-----------------------------------------------------------------------*/ |
/* Read Sector(s) */ |
/*-----------------------------------------------------------------------*/ |
DRESULT disk_read ( |
BYTE drv, /* Physical drive nmuber (0) */ |
BYTE *buff, /* Pointer to the data buffer to store read data */ |
DWORD sector, /* Start sector number (LBA) */ |
BYTE count /* Sector count (1..255) */ |
) |
{ |
if (drv || !count) return RES_PARERR; |
if (Stat & STA_NOINIT) return RES_NOTRDY; |
if (!(CardType & 4)) sector *= 512; /* Convert to byte address if needed */ |
SELECT(); /* CS = L */ |
if (count == 1) { /* Single block read */ |
if ((send_cmd(CMD17, sector) == 0) /* READ_SINGLE_BLOCK */ |
&& rcvr_datablock(buff, 512)) |
count = 0; |
} |
else { /* Multiple block read */ |
if (send_cmd(CMD18, sector) == 0) { /* READ_MULTIPLE_BLOCK */ |
do { |
if (!rcvr_datablock(buff, 512)) break; |
buff += 512; |
} while (--count); |
send_cmd(CMD12, 0); /* STOP_TRANSMISSION */ |
} |
} |
DESELECT(); /* CS = H */ |
rcvr_spi(); /* Idle (Release DO) */ |
return count ? RES_ERROR : RES_OK; |
} |
/*-----------------------------------------------------------------------*/ |
/* Write Sector(s) */ |
/*-----------------------------------------------------------------------*/ |
#if _READONLY == 0 |
DRESULT disk_write ( |
BYTE drv, /* Physical drive nmuber (0) */ |
const BYTE *buff, /* Pointer to the data to be written */ |
DWORD sector, /* Start sector number (LBA) */ |
BYTE count /* Sector count (1..255) */ |
) |
{ |
if (drv || !count) return RES_PARERR; |
if (Stat & STA_NOINIT) return RES_NOTRDY; |
if (Stat & STA_PROTECT) return RES_WRPRT; |
if (!(CardType & 4)) sector *= 512; /* Convert to byte address if needed */ |
SELECT(); /* CS = L */ |
if (count == 1) { /* Single block write */ |
if ((send_cmd(CMD24, sector) == 0) /* WRITE_BLOCK */ |
&& xmit_datablock(buff, 0xFE)) |
count = 0; |
} |
else { /* Multiple block write */ |
if (CardType & 2) { |
send_cmd(CMD55, 0); send_cmd(CMD23, count); /* ACMD23 */ |
} |
if (send_cmd(CMD25, sector) == 0) { /* WRITE_MULTIPLE_BLOCK */ |
do { |
if (!xmit_datablock(buff, 0xFC)) break; |
buff += 512; |
} while (--count); |
if (!xmit_datablock(0, 0xFD)) /* STOP_TRAN token */ |
count = 1; |
} |
} |
DESELECT(); /* CS = H */ |
rcvr_spi(); /* Idle (Release DO) */ |
return count ? RES_ERROR : RES_OK; |
} |
#endif /* _READONLY */ |
/*-----------------------------------------------------------------------*/ |
/* Miscellaneous Functions */ |
/*-----------------------------------------------------------------------*/ |
DRESULT disk_ioctl ( |
BYTE drv, /* Physical drive nmuber (0) */ |
BYTE ctrl, /* Control code */ |
void *buff /* Buffer to send/receive data block */ |
) |
{ |
DRESULT res; |
BYTE n, csd[16], *ptr = buff; |
WORD csize; |
if (drv) return RES_PARERR; |
SELECT(); /* CS = L */ |
res = RES_ERROR; |
switch (ctrl) { |
case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */ |
if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) { |
if ((csd[0] >> 6) == 1) { /* SDC ver 2.00 */ |
csize = csd[9] + ((WORD)csd[8] << 8) + 1; |
*(DWORD*)buff = (DWORD)csize << 10; |
} else { /* MMC or SDC ver 1.XX */ |
n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2; |
csize = (csd[8] >> 6) + ((WORD)csd[7] << 2) + ((WORD)(csd[6] & 3) << 10) + 1; |
*(DWORD*)buff = (DWORD)csize << (n - 9); |
} |
res = RES_OK; |
} |
break; |
case GET_SECTOR_SIZE : /* Get sectors on the disk (WORD) */ |
*(WORD*)buff = 512; |
res = RES_OK; |
break; |
case CTRL_SYNC : /* Make sure that data has been written */ |
if (wait_ready() == 0xFF) |
res = RES_OK; |
break; |
case MMC_GET_CSD : /* Receive CSD as a data block (16 bytes) */ |
if (Stat & STA_NOINIT) return RES_NOTRDY; |
if ((send_cmd(CMD9, 0) == 0) /* READ_CSD */ |
&& rcvr_datablock(ptr, 16)) |
res = RES_OK; |
break; |
case MMC_GET_CID : /* Receive CID as a data block (16 bytes) */ |
if (Stat & STA_NOINIT) return RES_NOTRDY; |
if ((send_cmd(CMD10, 0) == 0) /* READ_CID */ |
&& rcvr_datablock(ptr, 16)) |
res = RES_OK; |
break; |
case MMC_GET_OCR : /* Receive OCR as an R3 resp (4 bytes) */ |
if (Stat & STA_NOINIT) return RES_NOTRDY; |
if (send_cmd(CMD58, 0) == 0) { /* READ_OCR */ |
for (n = 0; n < 4; n++) |
*ptr++ = rcvr_spi(); |
res = RES_OK; |
} |
break; |
default: |
res = RES_PARERR; |
} |
DESELECT(); /* CS = H */ |
rcvr_spi(); /* Idle (Release DO) */ |
return res; |
} |
/*---------------------------------------*/ |
/* Device timer interrupt procedure */ |
/* This must be called in period of 10ms */ |
/* (Platform dependent) */ |
void disk_timerproc (void) |
{ |
static BYTE pv; |
BYTE n, s; |
n = Timer1; /* 100Hz decrement timer */ |
if (n) Timer1 = --n; |
n = Timer2; |
if (n) Timer2 = --n; |
n = pv; |
pv = SOCKPORT & (SOCKINS); /* Sample socket switch */ |
if (n == pv) { /* Have contacts stabled? */ |
s = Stat; |
if (pv & SOCKINS) /* INS = H (Socket empty) */ |
s |= (STA_NODISK | STA_NOINIT); |
else /* INS = L (Card inserted) */ |
s &= ~STA_NODISK; |
Stat = s; |
} |
} |
/Designs/DOGFINDER01A/SW/ATMEGA168/tff.c |
---|
0,0 → 1,1580 |
/*--------------------------------------------------------------------------/ |
/ FatFs - Tiny FAT file system module R0.04b (C)ChaN, 2007 |
/---------------------------------------------------------------------------/ |
/ The FatFs module is an experimenal project to implement FAT file system to |
/ cheap microcontrollers. This is a free software and is opened for education, |
/ research and development under license policy of following trems. |
/ |
/ Copyright (C) 2007, ChaN, all right reserved. |
/ |
/ * The FatFs module is a free software and there is no warranty. |
/ * You can use, modify and/or redistribute it for personal, non-profit or |
/ profit use without any restriction under your responsibility. |
/ * Redistributions of source code must retain the above copyright notice. |
/ |
/---------------------------------------------------------------------------/ |
/ Feb 26, 2006 R0.00 Prototype. |
/ Apr 29, 2006 R0.01 First stable version. |
/ Jun 01, 2006 R0.02 Added FAT12 support. |
/ Removed unbuffered mode. |
/ Fixed a problem on small (<32M) patition. |
/ Jun 10, 2006 R0.02a Added a configuration option (_FS_MINIMUM). |
/ Sep 22, 2006 R0.03 Added f_rename(). |
/ Changed option _FS_MINIMUM to _FS_MINIMIZE. |
/ Dec 09, 2006 R0.03a Improved cluster scan algolithm to write files fast. |
/ Feb 04, 2007 R0.04 Added FAT32 supprt. |
/ Changed some interfaces incidental to FatFs. |
/ Changed f_mountdrv() to f_mount(). |
/ Apr 01, 2007 R0.04a Added a capability of extending file size to f_lseek(). |
/ Added minimization level 3. |
/ Fixed a problem in FAT32 support. |
/ xxx xx, 2007 R0.04b Added a configuration option _USE_NTFLAG. |
/ Added FSInfo support. |
/ Fixed some problems corresponds to FAT32 support. |
/ Fixed DBCS name can result FR_INVALID_NAME. |
/ Fixed short seek (<= csize) collapses the file object. |
/---------------------------------------------------------------------------*/ |
#include <string.h> |
#include "tff.h" /* Tiny-FatFs declarations */ |
#include "diskio.h" /* Include file for user provided disk functions */ |
static |
FATFS *FatFs; /* Pointer to the file system objects (logical drive) */ |
static |
WORD fsid; /* File system mount ID */ |
/*------------------------------------------------------------------------- |
Module Private Functions |
-------------------------------------------------------------------------*/ |
/*-----------------------------------------------------------------------*/ |
/* Change window offset */ |
/*-----------------------------------------------------------------------*/ |
static |
BOOL move_window ( /* TRUE: successful, FALSE: failed */ |
DWORD sector /* Sector number to make apperance in the FatFs->win */ |
) /* Move to zero only writes back dirty window */ |
{ |
DWORD wsect; |
FATFS *fs = FatFs; |
wsect = fs->winsect; |
if (wsect != sector) { /* Changed current window */ |
#if !_FS_READONLY |
BYTE n; |
if (fs->winflag) { /* Write back dirty window if needed */ |
if (disk_write(0, fs->win, wsect, 1) != RES_OK) |
return FALSE; |
fs->winflag = 0; |
if (wsect < (fs->fatbase + fs->sects_fat)) { /* In FAT area */ |
for (n = fs->n_fats; n >= 2; n--) { /* Refrect the change to all FAT copies */ |
wsect += fs->sects_fat; |
disk_write(0, fs->win, wsect, 1); |
} |
} |
} |
#endif |
if (sector) { |
if (disk_read(0, fs->win, sector, 1) != RES_OK) |
return FALSE; |
fs->winsect = sector; |
} |
} |
return TRUE; |
} |
/*-----------------------------------------------------------------------*/ |
/* Clean-up cached data */ |
/*-----------------------------------------------------------------------*/ |
#if !_FS_READONLY |
static |
FRESULT sync (void) /* FR_OK: successful, FR_RW_ERROR: failed */ |
{ |
FATFS *fs = FatFs; |
fs->winflag = 1; |
if (!move_window(0)) return FR_RW_ERROR; |
#if _USE_FSINFO |
if (fs->fs_type == FS_FAT32 && fs->fsi_flag) { /* Update FSInfo sector if needed */ |
fs->winsect = 0; |
memset(fs->win, 0, 512); |
ST_WORD(&fs->win[BS_55AA], 0xAA55); |
ST_DWORD(&fs->win[FSI_LeadSig], 0x41615252); |
ST_DWORD(&fs->win[FSI_StrucSig], 0x61417272); |
ST_DWORD(&fs->win[FSI_Free_Count], fs->free_clust); |
ST_DWORD(&fs->win[FSI_Nxt_Free], fs->last_clust); |
disk_write(0, fs->win, fs->fsi_sector, 1); |
fs->fsi_flag = 0; |
} |
#endif |
if (disk_ioctl(0, CTRL_SYNC, NULL) != RES_OK) return FR_RW_ERROR; |
return FR_OK; |
} |
#endif |
/*-----------------------------------------------------------------------*/ |
/* Get a cluster status */ |
/*-----------------------------------------------------------------------*/ |
static |
CLUST get_cluster ( /* 0,>=2: successful, 1: failed */ |
CLUST clust /* Cluster# to get the link information */ |
) |
{ |
WORD wc, bc; |
DWORD fatsect; |
FATFS *fs = FatFs; |
if (clust >= 2 && clust < fs->max_clust) { /* Valid cluster# */ |
fatsect = fs->fatbase; |
switch (fs->fs_type) { |
case FS_FAT12 : |
bc = (WORD)clust * 3 / 2; |
if (!move_window(fatsect + bc / 512)) break; |
wc = fs->win[bc % 512]; bc++; |
if (!move_window(fatsect + bc / 512)) break; |
wc |= (WORD)fs->win[bc % 512] << 8; |
return (clust & 1) ? (wc >> 4) : (wc & 0xFFF); |
case FS_FAT16 : |
if (!move_window(fatsect + clust / 256)) break; |
return LD_WORD(&fs->win[((WORD)clust * 2) % 512]); |
#if _FAT32 |
case FS_FAT32 : |
if (!move_window(fatsect + clust / 128)) break; |
return LD_DWORD(&fs->win[((WORD)clust * 4) % 512]) & 0x0FFFFFFF; |
#endif |
} |
} |
return 1; /* There is no cluster information, or an error occured */ |
} |
/*-----------------------------------------------------------------------*/ |
/* Change a cluster status */ |
/*-----------------------------------------------------------------------*/ |
#if !_FS_READONLY |
static |
BOOL put_cluster ( /* TRUE: successful, FALSE: failed */ |
CLUST clust, /* Cluster# to change */ |
CLUST val /* New value to mark the cluster */ |
) |
{ |
WORD bc; |
BYTE *p; |
DWORD fatsect; |
FATFS *fs = FatFs; |
fatsect = fs->fatbase; |
switch (fs->fs_type) { |
case FS_FAT12 : |
bc = (WORD)clust * 3 / 2; |
if (!move_window(fatsect + bc / 512)) return FALSE; |
p = &fs->win[bc % 512]; |
*p = (clust & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val; |
bc++; |
fs->winflag = 1; |
if (!move_window(fatsect + bc / 512)) return FALSE; |
p = &fs->win[bc % 512]; |
*p = (clust & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F)); |
break; |
case FS_FAT16 : |
if (!move_window(fatsect + clust / 256)) return FALSE; |
ST_WORD(&fs->win[((WORD)clust * 2) % 512], (WORD)val); |
break; |
#if _FAT32 |
case FS_FAT32 : |
if (!move_window(fatsect + clust / 128)) return FALSE; |
ST_DWORD(&fs->win[((WORD)clust * 4) % 512], val); |
break; |
#endif |
default : |
return FALSE; |
} |
fs->winflag = 1; |
return TRUE; |
} |
#endif /* !_FS_READONLY */ |
/*-----------------------------------------------------------------------*/ |
/* Remove a cluster chain */ |
/*-----------------------------------------------------------------------*/ |
#if !_FS_READONLY |
static |
BOOL remove_chain ( /* TRUE: successful, FALSE: failed */ |
CLUST clust /* Cluster# to remove chain from */ |
) |
{ |
CLUST nxt; |
FATFS *fs = FatFs; |
while (clust >= 2 && clust < fs->max_clust) { |
nxt = get_cluster(clust); |
if (nxt == 1) return FALSE; |
if (!put_cluster(clust, 0)) return FALSE; |
if (fs->free_clust != (CLUST)0xFFFFFFFF) { |
fs->free_clust++; |
#if _USE_FSINFO |
fs->fsi_flag = 1; |
#endif |
} |
clust = nxt; |
} |
return TRUE; |
} |
#endif |
/*-----------------------------------------------------------------------*/ |
/* Stretch or create a cluster chain */ |
/*-----------------------------------------------------------------------*/ |
#if !_FS_READONLY |
static |
CLUST create_chain ( /* 0: no free cluster, 1: error, >=2: new cluster number */ |
CLUST clust /* Cluster# to stretch, 0 means create new */ |
) |
{ |
CLUST cstat, ncl, scl, mcl; |
FATFS *fs = FatFs; |
mcl = fs->max_clust; |
if (clust == 0) { /* Create new chain */ |
scl = fs->last_clust; /* Get last allocated cluster */ |
if (scl < 2 || scl >= mcl) scl = 1; |
} |
else { /* Stretch existing chain */ |
cstat = get_cluster(clust); /* Check the cluster status */ |
if (cstat < 2) return 1; /* It is an invalid cluster */ |
if (cstat < mcl) return cstat; /* It is already followed by next cluster */ |
scl = clust; |
} |
ncl = scl; /* Start cluster */ |
for (;;) { |
ncl++; /* Next cluster */ |
if (ncl >= mcl) { /* Wrap around */ |
ncl = 2; |
if (ncl > scl) return 0; /* No free custer */ |
} |
cstat = get_cluster(ncl); /* Get the cluster status */ |
if (cstat == 0) break; /* Found a free cluster */ |
if (cstat == 1) return 1; /* Any error occured */ |
if (ncl == scl) return 0; /* No free custer */ |
} |
if (!put_cluster(ncl, (CLUST)0x0FFFFFFF)) return 1; /* Mark the new cluster "in use" */ |
if (clust && !put_cluster(clust, ncl)) return 1; /* Link it to previous one if needed */ |
fs->last_clust = ncl; /* Update fsinfo */ |
if (fs->free_clust != (CLUST)0xFFFFFFFF) { |
fs->free_clust--; |
#if _USE_FSINFO |
fs->fsi_flag = 1; |
#endif |
} |
return ncl; /* Return new cluster number */ |
} |
#endif /* !_FS_READONLY */ |
/*-----------------------------------------------------------------------*/ |
/* Get sector# from cluster# */ |
/*-----------------------------------------------------------------------*/ |
static |
DWORD clust2sect ( /* !=0: sector number, 0: failed - invalid cluster# */ |
CLUST clust /* Cluster# to be converted */ |
) |
{ |
FATFS *fs = FatFs; |
clust -= 2; |
if (clust >= (fs->max_clust - 2)) return 0; /* Invalid cluster# */ |
return (DWORD)clust * fs->sects_clust + fs->database; |
} |
/*-----------------------------------------------------------------------*/ |
/* Move directory pointer to next */ |
/*-----------------------------------------------------------------------*/ |
static |
BOOL next_dir_entry ( /* TRUE: successful, FALSE: could not move next */ |
DIR *dirobj /* Pointer to directory object */ |
) |
{ |
CLUST clust; |
WORD idx; |
FATFS *fs = FatFs; |
idx = dirobj->index + 1; |
if ((idx & 15) == 0) { /* Table sector changed? */ |
dirobj->sect++; /* Next sector */ |
if (!dirobj->clust) { /* In static table */ |
if (idx >= fs->n_rootdir) return FALSE; /* Reached to end of table */ |
} else { /* In dynamic table */ |
if (((idx / 16) & (fs->sects_clust - 1)) == 0) { /* Cluster changed? */ |
clust = get_cluster(dirobj->clust); /* Get next cluster */ |
if (clust < 2 || clust >= fs->max_clust) /* Reached to end of table */ |
return FALSE; |
dirobj->clust = clust; /* Initialize for new cluster */ |
dirobj->sect = clust2sect(clust); |
} |
} |
} |
dirobj->index = idx; /* Lower 4 bit of dirobj->index indicates offset in dirobj->sect */ |
return TRUE; |
} |
/*-----------------------------------------------------------------------*/ |
/* Get file status from directory entry */ |
/*-----------------------------------------------------------------------*/ |
#if _FS_MINIMIZE <= 1 |
static |
void get_fileinfo ( /* No return code */ |
FILINFO *finfo, /* Ptr to store the File Information */ |
const BYTE *dir /* Ptr to the directory entry */ |
) |
{ |
BYTE n, c, a; |
char *p; |
p = &finfo->fname[0]; |
a = _USE_NTFLAG ? dir[DIR_NTres] : 0; /* NT flag */ |
for (n = 0; n < 8; n++) { /* Convert file name (body) */ |
c = dir[n]; |
if (c == ' ') break; |
if (c == 0x05) c = 0xE5; |
if (a & 0x08 && c >= 'A' && c <= 'Z') c += 0x20; |
*p++ = c; |
} |
if (dir[8] != ' ') { /* Convert file name (extension) */ |
*p++ = '.'; |
for (n = 8; n < 11; n++) { |
c = dir[n]; |
if (c == ' ') break; |
if (a & 0x10 && c >= 'A' && c <= 'Z') c += 0x20; |
*p++ = c; |
} |
} |
*p = '\0'; |
finfo->fattrib = dir[DIR_Attr]; /* Attribute */ |
finfo->fsize = LD_DWORD(&dir[DIR_FileSize]); /* Size */ |
finfo->fdate = LD_WORD(&dir[DIR_WrtDate]); /* Date */ |
finfo->ftime = LD_WORD(&dir[DIR_WrtTime]); /* Time */ |
} |
#endif /* _FS_MINIMIZE <= 1 */ |
/*-----------------------------------------------------------------------*/ |
/* Pick a paragraph and create the name in format of directory entry */ |
/*-----------------------------------------------------------------------*/ |
static |
char make_dirfile ( /* 1: error - detected an invalid format, '\0'or'/': next character */ |
const char **path, /* Pointer to the file path pointer */ |
char *dirname /* Pointer to directory name buffer {Name(8), Ext(3), NT flag(1)} */ |
) |
{ |
BYTE n, t, c, a, b; |
memset(dirname, ' ', 8+3); /* Fill buffer with spaces */ |
a = 0; b = 0x18; /* NT flag */ |
n = 0; t = 8; |
for (;;) { |
c = *(*path)++; |
if (c == '\0' || c == '/') { /* Reached to end of str or directory separator */ |
if (n == 0) break; |
dirname[11] = _USE_NTFLAG ? (a & b) : 0; |
return c; |
} |
if (c <= ' ' || c == 0x7F) break; /* Reject invisible chars */ |
if (c == '.') { |
if (!(a & 1) && n >= 1 && n <= 8) { /* Enter extension part */ |
n = 8; t = 11; continue; |
} |
break; |
} |
if (_USE_SJIS && |
((c >= 0x81 && c <= 0x9F) || /* Accept S-JIS code */ |
(c >= 0xE0 && c <= 0xFC))) { |
if (n == 0 && c == 0xE5) /* Change heading \xE5 to \x05 */ |
c = 0x05; |
a ^= 1; goto md_l2; |
} |
if (c == '"') break; /* Reject " */ |
if (c <= ')') goto md_l1; /* Accept ! # $ % & ' ( ) */ |
if (c <= ',') break; /* Reject * + , */ |
if (c <= '9') goto md_l1; /* Accept - 0-9 */ |
if (c <= '?') break; /* Reject : ; < = > ? */ |
if (!(a & 1)) { /* These checks are not applied to S-JIS 2nd byte */ |
if (c == '|') break; /* Reject | */ |
if (c >= '[' && c <= ']') break;/* Reject [ \ ] */ |
if (_USE_NTFLAG && c >= 'A' && c <= 'Z') |
(t == 8) ? (b &= ~0x08) : (b &= ~0x10); |
if (c >= 'a' && c <= 'z') { /* Convert to upper case */ |
c -= 0x20; |
if (_USE_NTFLAG) (t == 8) ? (a |= 0x08) : (a |= 0x10); |
} |
} |
md_l1: |
a &= ~1; |
md_l2: |
if (n >= t) break; |
dirname[n++] = c; |
} |
return 1; |
} |
/*-----------------------------------------------------------------------*/ |
/* Trace a file path */ |
/*-----------------------------------------------------------------------*/ |
static |
FRESULT trace_path ( /* FR_OK(0): successful, !=0: error code */ |
DIR *dirobj, /* Pointer to directory object to return last directory */ |
char *fn, /* Pointer to last segment name to return */ |
const char *path, /* Full-path string to trace a file or directory */ |
BYTE **dir /* Directory pointer in Win[] to retutn */ |
) |
{ |
CLUST clust; |
char ds; |
BYTE *dptr = NULL; |
FATFS *fs = FatFs; |
/* Initialize directory object */ |
clust = fs->dirbase; |
#if _FAT32 |
if (fs->fs_type == FS_FAT32) { |
dirobj->clust = dirobj->sclust = clust; |
dirobj->sect = clust2sect(clust); |
} else |
#endif |
{ |
dirobj->clust = dirobj->sclust = 0; |
dirobj->sect = clust; |
} |
dirobj->index = 0; |
dirobj->fs = fs; |
if (*path == '\0') { /* Null path means the root directory */ |
*dir = NULL; return FR_OK; |
} |
for (;;) { |
ds = make_dirfile(&path, fn); /* Get a paragraph into fn[] */ |
if (ds == 1) return FR_INVALID_NAME; |
for (;;) { |
if (!move_window(dirobj->sect)) return FR_RW_ERROR; |
dptr = &fs->win[(dirobj->index & 15) * 32]; /* Pointer to the directory entry */ |
if (dptr[DIR_Name] == 0) /* Has it reached to end of dir? */ |
return !ds ? FR_NO_FILE : FR_NO_PATH; |
if (dptr[DIR_Name] != 0xE5 /* Matched? */ |
&& !(dptr[DIR_Attr] & AM_VOL) |
&& !memcmp(&dptr[DIR_Name], fn, 8+3) ) break; |
if (!next_dir_entry(dirobj)) /* Next directory pointer */ |
return !ds ? FR_NO_FILE : FR_NO_PATH; |
} |
if (!ds) { *dir = dptr; return FR_OK; } /* Matched with end of path */ |
if (!(dptr[DIR_Attr] & AM_DIR)) return FR_NO_PATH; /* Cannot trace because it is a file */ |
clust = /* Get cluster# of the directory */ |
#if _FAT32 |
((DWORD)LD_WORD(&dptr[DIR_FstClusHI]) << 16) | |
#endif |
LD_WORD(&dptr[DIR_FstClusLO]); |
dirobj->clust = dirobj->sclust = clust; /* Restart scannig with the new directory */ |
dirobj->sect = clust2sect(clust); |
dirobj->index = 2; |
} |
} |
/*-----------------------------------------------------------------------*/ |
/* Reserve a directory entry */ |
/*-----------------------------------------------------------------------*/ |
#if !_FS_READONLY |
static |
FRESULT reserve_direntry ( /* FR_OK: successful, FR_DENIED: no free entry, FR_RW_ERROR: a disk error occured */ |
DIR *dirobj, /* Target directory to create new entry */ |
BYTE **dir /* Pointer to pointer to created entry to retutn */ |
) |
{ |
CLUST clust; |
DWORD sector; |
BYTE c, n, *dptr; |
FATFS *fs = FatFs; |
/* Re-initialize directory object */ |
clust = dirobj->sclust; |
if (clust) { /* Dyanmic directory table */ |
dirobj->clust = clust; |
dirobj->sect = clust2sect(clust); |
} else { /* Static directory table */ |
dirobj->sect = fs->dirbase; |
} |
dirobj->index = 0; |
do { |
if (!move_window(dirobj->sect)) return FR_RW_ERROR; |
dptr = &fs->win[(dirobj->index & 15) * 32]; /* Pointer to the directory entry */ |
c = dptr[DIR_Name]; |
if (c == 0 || c == 0xE5) { /* Found an empty entry! */ |
*dir = dptr; return FR_OK; |
} |
} while (next_dir_entry(dirobj)); /* Next directory pointer */ |
/* Reached to end of the directory table */ |
/* Abort when static table or could not stretch dynamic table */ |
if (!clust || !(clust = create_chain(dirobj->clust))) return FR_DENIED; |
if (clust == 1 || !move_window(0)) return FR_RW_ERROR; |
fs->winsect = sector = clust2sect(clust); /* Cleanup the expanded table */ |
memset(fs->win, 0, 512); |
for (n = fs->sects_clust; n; n--) { |
if (disk_write(0, fs->win, sector, 1) != RES_OK) |
return FR_RW_ERROR; |
sector++; |
} |
fs->winflag = 1; |
*dir = fs->win; |
return FR_OK; |
} |
#endif /* !_FS_READONLY */ |
/*-----------------------------------------------------------------------*/ |
/* Load boot record and check if it is a FAT boot record */ |
/*-----------------------------------------------------------------------*/ |
static |
BYTE check_fs ( /* 0:The FAT boot record, 1:Valid boot record but not an FAT, 2:Not a boot record or error */ |
DWORD sect /* Sector# to check if it is a FAT boot record or not */ |
) |
{ |
FATFS *fs = FatFs; |
if (disk_read(0, fs->win, sect, 1) != RES_OK) /* Load boot record */ |
return 2; |
if (LD_WORD(&fs->win[BS_55AA]) != 0xAA55) /* Check record signature */ |
return 2; |
if (!memcmp(&fs->win[BS_FilSysType], "FAT", 3)) /* Check FAT signature */ |
return 0; |
#if _FAT32 |
if (!memcmp(&fs->win[BS_FilSysType32], "FAT32", 5) && !(fs->win[BPB_ExtFlags] & 0x80)) |
return 0; |
#endif |
return 1; |
} |
/*-----------------------------------------------------------------------*/ |
/* Make sure that the file system is valid */ |
/*-----------------------------------------------------------------------*/ |
static |
FRESULT auto_mount ( /* FR_OK(0): successful, !=0: any error occured */ |
const char **path, /* Pointer to pointer to the path name (drive number) */ |
BYTE chk_wp /* !=0: Check media write protection for wrinting fuctions */ |
) |
{ |
BYTE fmt; |
DSTATUS stat; |
DWORD bootsect, fatsize, totalsect, maxclust; |
const char *p = *path; |
FATFS *fs = FatFs; |
while (*p == ' ') p++; /* Strip leading spaces */ |
if (*p == '/') p++; /* Strip heading slash */ |
*path = p; /* Return pointer to the path name */ |
/* Is the file system object registered? */ |
if (!fs) return FR_NOT_ENABLED; |
/* Chekck if the logical drive has been mounted or not */ |
if (fs->fs_type) { |
stat = disk_status(0); |
if (!(stat & STA_NOINIT)) { /* If the physical drive is kept initialized */ |
#if !_FS_READONLY |
if (chk_wp && (stat & STA_PROTECT)) /* Check write protection if needed */ |
return FR_WRITE_PROTECTED; |
#endif |
return FR_OK; /* The file system object is valid */ |
} |
} |
/* The logical drive has not been mounted, following code attempts to mount the logical drive */ |
memset(fs, 0, sizeof(FATFS)); /* Clean-up the file system object */ |
stat = disk_initialize(0); /* Initialize low level disk I/O layer */ |
if (stat & STA_NOINIT) /* Check if the drive is ready */ |
return FR_NOT_READY; |
#if !_FS_READONLY |
if (chk_wp && (stat & STA_PROTECT)) /* Check write protection if needed */ |
return FR_WRITE_PROTECTED; |
#endif |
/* Search FAT partition on the drive */ |
fmt = check_fs(bootsect = 0); /* Check sector 0 as an SFD format */ |
if (fmt == 1) { /* Not a FAT boot record, it may be patitioned */ |
/* Check a partition listed in top of the partition table */ |
if (fs->win[MBR_Table+4]) { /* Is the 1st partition existing? */ |
bootsect = LD_DWORD(&fs->win[MBR_Table+8]); /* Partition offset in LBA */ |
fmt = check_fs(bootsect); /* Check the partition */ |
} |
} |
if (fmt || LD_WORD(&fs->win[BPB_BytsPerSec]) != 512) /* No valid FAT patition is found */ |
return FR_NO_FILESYSTEM; |
/* Initialize the file system object */ |
fatsize = LD_WORD(&fs->win[BPB_FATSz16]); /* Number of sectors per FAT */ |
if (!fatsize) fatsize = LD_DWORD(&fs->win[BPB_FATSz32]); |
fs->sects_fat = (CLUST)fatsize; |
fs->n_fats = fs->win[BPB_NumFATs]; /* Number of FAT copies */ |
fatsize *= fs->n_fats; /* (Number of sectors in FAT area) */ |
fs->fatbase = bootsect + LD_WORD(&fs->win[BPB_RsvdSecCnt]); /* FAT start sector (lba) */ |
fs->sects_clust = fs->win[BPB_SecPerClus]; /* Number of sectors per cluster */ |
fs->n_rootdir = LD_WORD(&fs->win[BPB_RootEntCnt]); /* Nmuber of root directory entries */ |
totalsect = LD_WORD(&fs->win[BPB_TotSec16]); /* Number of sectors on the file system */ |
if (!totalsect) totalsect = LD_DWORD(&fs->win[BPB_TotSec32]); |
fs->max_clust = maxclust = (totalsect /* Last cluster# + 1 */ |
- LD_WORD(&fs->win[BPB_RsvdSecCnt]) - fatsize - fs->n_rootdir / 16 |
) / fs->sects_clust + 2; |
fmt = FS_FAT12; /* Determine the FAT sub type */ |
if (maxclust > 0xFF7) fmt = FS_FAT16; |
if (maxclust > 0xFFF7) |
#if !_FAT32 |
return FR_NO_FILESYSTEM; |
#else |
fmt = FS_FAT32; |
if (fmt == FS_FAT32) |
fs->dirbase = LD_DWORD(&fs->win[BPB_RootClus]); /* Root directory start cluster */ |
else |
#endif |
fs->dirbase = fs->fatbase + fatsize; /* Root directory start sector (lba) */ |
fs->database = fs->fatbase + fatsize + fs->n_rootdir / 16; /* Data start sector (lba) */ |
fs->fs_type = fmt; /* FAT sub-type */ |
#if !_FS_READONLY |
fs->free_clust = (CLUST)0xFFFFFFFF; |
#if _USE_FSINFO |
/* Load fsinfo sector if needed */ |
if (fmt == FS_FAT32) { |
fs->fsi_sector = bootsect + LD_WORD(&fs->win[BPB_FSInfo]); |
if (disk_read(0, fs->win, fs->fsi_sector, 1) == RES_OK && |
LD_WORD(&fs->win[BS_55AA]) == 0xAA55 && |
LD_DWORD(&fs->win[FSI_LeadSig]) == 0x41615252 && |
LD_DWORD(&fs->win[FSI_StrucSig]) == 0x61417272) { |
fs->last_clust = LD_DWORD(&fs->win[FSI_Nxt_Free]); |
fs->free_clust = LD_DWORD(&fs->win[FSI_Free_Count]); |
} |
} |
#endif |
#endif |
fs->id = ++fsid; /* File system mount ID */ |
return FR_OK; |
} |
/*-----------------------------------------------------------------------*/ |
/* Check if the file/dir object is valid or not */ |
/*-----------------------------------------------------------------------*/ |
static |
FRESULT validate ( /* FR_OK(0): The id is valid, !=0: Not valid */ |
const FATFS *fs, /* Pointer to the file system object */ |
WORD id /* id member of the target object to be checked */ |
) |
{ |
if (!fs || fs->id != id) |
return FR_INVALID_OBJECT; |
if (disk_status(0) & STA_NOINIT) |
return FR_NOT_READY; |
return FR_OK; |
} |
/*-------------------------------------------------------------------------- |
Public Functions |
--------------------------------------------------------------------------*/ |
/*-----------------------------------------------------------------------*/ |
/* Mount/Unmount a Locical Drive */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_mount ( |
BYTE drv, /* Logical drive number to be mounted/unmounted */ |
FATFS *fs /* Pointer to new file system object (NULL for unmount)*/ |
) |
{ |
FATFS *fsobj; |
if (drv) return FR_INVALID_DRIVE; |
fsobj = FatFs; |
FatFs = fs; |
if (fsobj) memset(fsobj, 0, sizeof(FATFS)); |
if (fs) memset(fs, 0, sizeof(FATFS)); |
return FR_OK; |
} |
/*-----------------------------------------------------------------------*/ |
/* Open or Create a File */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_open ( |
FIL *fp, /* Pointer to the blank file object */ |
const char *path, /* Pointer to the file name */ |
BYTE mode /* Access mode and file open mode flags */ |
) |
{ |
FRESULT res; |
BYTE *dir; |
DIR dirobj; |
char fn[8+3+1]; |
FATFS *fs = FatFs; |
fp->fs = NULL; |
#if !_FS_READONLY |
mode &= (FA_READ|FA_WRITE|FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW); |
res = auto_mount(&path, (BYTE)(mode & (FA_WRITE|FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW))); |
#else |
mode &= FA_READ; |
res = auto_mount(&path, 0); |
#endif |
if (res != FR_OK) return res; |
/* Trace the file path */ |
res = trace_path(&dirobj, fn, path, &dir); /* Trace the file path */ |
#if !_FS_READONLY |
/* Create or Open a File */ |
if (mode & (FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW)) { |
CLUST rs; |
DWORD dw; |
if (res != FR_OK) { /* No file, create new */ |
if (res != FR_NO_FILE) return res; |
res = reserve_direntry(&dirobj, &dir); |
if (res != FR_OK) return res; |
memset(dir, 0, 32); /* Initialize the new entry */ |
memcpy(&dir[DIR_Name], fn, 8+3); |
dir[DIR_NTres] = fn[11]; |
mode |= FA_CREATE_ALWAYS; |
} else { /* Any object is already existing */ |
if (mode & FA_CREATE_NEW) /* Cannot create new */ |
return FR_EXIST; |
if (dir == NULL || (dir[DIR_Attr] & (AM_RDO|AM_DIR))) /* Cannot overwrite (R/O or DIR) */ |
return FR_DENIED; |
if (mode & FA_CREATE_ALWAYS) { /* Resize it to zero */ |
#if _FAT32 |
rs = ((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | LD_WORD(&dir[DIR_FstClusLO]); |
ST_WORD(&dir[DIR_FstClusHI], 0); |
#else |
rs = LD_WORD(&dir[DIR_FstClusLO]); |
#endif |
ST_WORD(&dir[DIR_FstClusLO], 0); /* cluster = 0 */ |
ST_DWORD(&dir[DIR_FileSize], 0); /* size = 0 */ |
fs->winflag = 1; |
dw = fs->winsect; /* Remove the cluster chain */ |
if (!remove_chain(rs) || !move_window(dw)) |
return FR_RW_ERROR; |
fs->last_clust = rs - 1; /* Reuse the cluster hole */ |
} |
} |
if (mode & FA_CREATE_ALWAYS) { |
dir[DIR_Attr] = AM_ARC; /* New attribute */ |
dw = get_fattime(); |
ST_DWORD(&dir[DIR_WrtTime], dw); /* Updated time */ |
ST_DWORD(&dir[DIR_CrtTime], dw); /* Created time */ |
fs->winflag = 1; |
} |
} |
/* Open a File */ |
else { |
#endif /* !_FS_READONLY */ |
if (res != FR_OK) return res; /* Trace failed */ |
if (dir == NULL || (dir[DIR_Attr] & AM_DIR)) /* It is a directory */ |
return FR_NO_FILE; |
#if !_FS_READONLY |
if ((mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO)) /* R/O violation */ |
return FR_DENIED; |
} |
fp->dir_sect = fs->winsect; /* Pointer to the directory entry */ |
fp->dir_ptr = dir; |
#endif |
fp->flag = mode; /* File access mode */ |
fp->org_clust = /* File start cluster */ |
#if _FAT32 |
((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | |
#endif |
LD_WORD(&dir[DIR_FstClusLO]); |
fp->fsize = LD_DWORD(&dir[DIR_FileSize]); /* File size */ |
fp->fptr = 0; /* File ptr */ |
fp->sect_clust = 1; /* Sector counter */ |
fp->fs = fs; fp->id = fs->id; /* Owner file system object of the file */ |
return FR_OK; |
} |
/*-----------------------------------------------------------------------*/ |
/* Read File */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_read ( |
FIL *fp, /* Pointer to the file object */ |
void *buff, /* Pointer to data buffer */ |
WORD btr, /* Number of bytes to read */ |
WORD *br /* Pointer to number of bytes read */ |
) |
{ |
DWORD sect, remain; |
WORD rcnt; |
CLUST clust; |
BYTE cc, *rbuff = buff; |
FRESULT res; |
FATFS *fs = fp->fs; |
*br = 0; |
res = validate(fs, fp->id); /* Check validity of the object */ |
if (res) return res; |
if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */ |
if (!(fp->flag & FA_READ)) return FR_DENIED; /* Check access mode */ |
remain = fp->fsize - fp->fptr; |
if (btr > remain) btr = (WORD)remain; /* Truncate read count by number of bytes left */ |
for ( ; btr; /* Repeat until all data transferred */ |
rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) { |
if ((fp->fptr % 512) == 0) { /* On the sector boundary */ |
if (--fp->sect_clust) { /* Decrement left sector counter */ |
sect = fp->curr_sect + 1; /* Get current sector */ |
} else { /* On the cluster boundary, get next cluster */ |
clust = (fp->fptr == 0) ? |
fp->org_clust : get_cluster(fp->curr_clust); |
if (clust < 2 || clust >= fs->max_clust) |
goto fr_error; |
fp->curr_clust = clust; /* Current cluster */ |
sect = clust2sect(clust); /* Get current sector */ |
fp->sect_clust = fs->sects_clust; /* Re-initialize the left sector counter */ |
} |
fp->curr_sect = sect; /* Update current sector */ |
cc = btr / 512; /* When left bytes >= 512, */ |
if (cc) { /* Read maximum contiguous sectors directly */ |
if (cc > fp->sect_clust) cc = fp->sect_clust; |
if (disk_read(0, rbuff, sect, cc) != RES_OK) |
goto fr_error; |
fp->sect_clust -= cc - 1; |
fp->curr_sect += cc - 1; |
rcnt = cc * 512; continue; |
} |
} |
if (!move_window(fp->curr_sect)) goto fr_error; /* Move sector window */ |
rcnt = 512 - (WORD)(fp->fptr % 512); /* Copy fractional bytes from sector window */ |
if (rcnt > btr) rcnt = btr; |
memcpy(rbuff, &fs->win[(WORD)fp->fptr % 512], rcnt); |
} |
return FR_OK; |
fr_error: /* Abort this function due to an unrecoverable error */ |
fp->flag |= FA__ERROR; |
return FR_RW_ERROR; |
} |
#if !_FS_READONLY |
/*-----------------------------------------------------------------------*/ |
/* Write File */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_write ( |
FIL *fp, /* Pointer to the file object */ |
const void *buff, /* Pointer to the data to be written */ |
WORD btw, /* Number of bytes to write */ |
WORD *bw /* Pointer to number of bytes written */ |
) |
{ |
DWORD sect; |
WORD wcnt; |
CLUST clust; |
BYTE cc; |
FRESULT res; |
const BYTE *wbuff = buff; |
FATFS *fs = fp->fs; |
*bw = 0; |
res = validate(fs, fp->id); /* Check validity of the object */ |
if (res) return res; |
if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */ |
if (!(fp->flag & FA_WRITE)) return FR_DENIED; /* Check access mode */ |
if (fp->fsize + btw < fp->fsize) return FR_OK; /* File size cannot reach 4GB */ |
for ( ; btw; /* Repeat until all data transferred */ |
wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) { |
if ((fp->fptr % 512) == 0) { /* On the sector boundary */ |
if (--(fp->sect_clust)) { /* Decrement left sector counter */ |
sect = fp->curr_sect + 1; /* Get current sector */ |
} else { /* On the cluster boundary, get next cluster */ |
if (fp->fptr == 0) { /* Is top of the file */ |
clust = fp->org_clust; |
if (clust == 0) /* No cluster is created yet */ |
fp->org_clust = clust = create_chain(0); /* Create a new cluster chain */ |
} else { /* Middle or end of file */ |
clust = create_chain(fp->curr_clust); /* Trace or streach cluster chain */ |
} |
if (clust == 0) break; /* Disk full */ |
if (clust == 1 || clust >= fs->max_clust) goto fw_error; |
fp->curr_clust = clust; /* Current cluster */ |
sect = clust2sect(clust); /* Get current sector */ |
fp->sect_clust = fs->sects_clust; /* Re-initialize the left sector counter */ |
} |
fp->curr_sect = sect; /* Update current sector */ |
cc = btw / 512; /* When left bytes >= 512, */ |
if (cc) { /* Write maximum contiguous sectors directly */ |
if (cc > fp->sect_clust) cc = fp->sect_clust; |
if (disk_write(0, wbuff, sect, cc) != RES_OK) |
goto fw_error; |
fp->sect_clust -= cc - 1; |
fp->curr_sect += cc - 1; |
wcnt = cc * 512; continue; |
} |
if (fp->fptr >= fp->fsize) { /* Flush R/W window if needed */ |
if (!move_window(0)) goto fw_error; |
fs->winsect = fp->curr_sect; |
} |
} |
if (!move_window(fp->curr_sect)) /* Move sector window */ |
goto fw_error; |
wcnt = 512 - (WORD)(fp->fptr % 512); /* Copy fractional bytes bytes to sector window */ |
if (wcnt > btw) wcnt = btw; |
memcpy(&fs->win[(WORD)fp->fptr % 512], wbuff, wcnt); |
fs->winflag = 1; |
} |
if (fp->fptr > fp->fsize) fp->fsize = fp->fptr; /* Update file size if needed */ |
fp->flag |= FA__WRITTEN; /* Set file changed flag */ |
return FR_OK; |
fw_error: /* Abort this function due to an unrecoverable error */ |
fp->flag |= FA__ERROR; |
return FR_RW_ERROR; |
} |
/*-----------------------------------------------------------------------*/ |
/* Synchronize between File and Disk */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_sync ( |
FIL *fp /* Pointer to the file object */ |
) |
{ |
DWORD tim; |
BYTE *dir; |
FRESULT res; |
FATFS *fs = fp->fs; |
res = validate(fs, fp->id); /* Check validity of the object */ |
if (res == FR_OK) { |
if (fp->flag & FA__WRITTEN) { /* Has the file been written? */ |
/* Update the directory entry */ |
if (!move_window(fp->dir_sect)) |
return FR_RW_ERROR; |
dir = fp->dir_ptr; |
dir[DIR_Attr] |= AM_ARC; /* Set archive bit */ |
ST_DWORD(&dir[DIR_FileSize], fp->fsize); /* Update file size */ |
ST_WORD(&dir[DIR_FstClusLO], fp->org_clust); /* Update start cluster */ |
#if _FAT32 |
ST_WORD(&dir[DIR_FstClusHI], fp->org_clust >> 16); |
#endif |
tim = get_fattime(); /* Updated time */ |
ST_DWORD(&dir[DIR_WrtTime], tim); |
fp->flag &= ~FA__WRITTEN; |
res = sync(); |
} |
} |
return res; |
} |
#endif /* !_FS_READONLY */ |
/*-----------------------------------------------------------------------*/ |
/* Close File */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_close ( |
FIL *fp /* Pointer to the file object to be closed */ |
) |
{ |
FRESULT res; |
#if !_FS_READONLY |
res = f_sync(fp); |
#else |
res = validate(fp->fs, fp->id); |
#endif |
if (res == FR_OK) |
fp->fs = NULL; |
return res; |
} |
#if _FS_MINIMIZE <= 2 |
/*-----------------------------------------------------------------------*/ |
/* Seek File Pointer */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_lseek ( |
FIL *fp, /* Pointer to the file object */ |
DWORD ofs /* File pointer from top of file */ |
) |
{ |
CLUST clust; |
DWORD csize; |
BYTE csect; |
FRESULT res; |
FATFS *fs = fp->fs; |
res = validate(fs, fp->id); /* Check validity of the object */ |
if (res) return res; |
if (fp->flag & FA__ERROR) return FR_RW_ERROR; |
#if !_FS_READONLY |
if (ofs > fp->fsize && !(fp->flag & FA_WRITE)) |
#else |
if (ofs > fp->fsize) |
#endif |
ofs = fp->fsize; |
fp->fptr = 0; fp->sect_clust = 1; /* Set file R/W pointer to top of the file */ |
/* Move file R/W pointer if needed */ |
if (ofs) { |
clust = fp->org_clust; /* Get start cluster */ |
#if !_FS_READONLY |
if (!clust) { /* If the file does not have a cluster chain, create new cluster chain */ |
clust = create_chain(0); |
if (clust == 1) goto fk_error; |
fp->org_clust = clust; |
} |
#endif |
if (clust) { /* If the file has a cluster chain, it can be followed */ |
csize = (DWORD)fs->sects_clust * 512; /* Cluster size in unit of byte */ |
for (;;) { /* Loop to skip leading clusters */ |
fp->curr_clust = clust; /* Update current cluster */ |
if (ofs <= csize) break; |
#if !_FS_READONLY |
if (fp->flag & FA_WRITE) /* Check if in write mode or not */ |
clust = create_chain(clust); /* Force streached if in write mode */ |
else |
#endif |
clust = get_cluster(clust); /* Only follow cluster chain if not in write mode */ |
if (clust == 0) { /* Stop if could not follow the cluster chain */ |
ofs = csize; break; |
} |
if (clust == 1 || clust >= fs->max_clust) goto fk_error; |
fp->fptr += csize; /* Update R/W pointer */ |
ofs -= csize; |
} |
csect = (BYTE)((ofs - 1) / 512); /* Sector offset in the cluster */ |
fp->curr_sect = clust2sect(clust) + csect; /* Current sector */ |
fp->sect_clust = fs->sects_clust - csect; /* Left sector counter in the cluster */ |
fp->fptr += ofs; /* Update file R/W pointer */ |
} |
} |
#if !_FS_READONLY |
if ((fp->flag & FA_WRITE) && fp->fptr > fp->fsize) { /* Set updated flag if in write mode */ |
fp->fsize = fp->fptr; |
fp->flag |= FA__WRITTEN; |
} |
#endif |
return FR_OK; |
fk_error: /* Abort this function due to an unrecoverable error */ |
fp->flag |= FA__ERROR; |
return FR_RW_ERROR; |
} |
#if _FS_MINIMIZE <= 1 |
/*-----------------------------------------------------------------------*/ |
/* Open a directroy */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_opendir ( |
DIR *dirobj, /* Pointer to directory object to create */ |
const char *path /* Pointer to the directory path */ |
) |
{ |
BYTE *dir; |
char fn[8+3+1]; |
FRESULT res; |
FATFS *fs = FatFs; |
res = auto_mount(&path, 0); |
if (res != FR_OK) return res; |
res = trace_path(dirobj, fn, path, &dir); /* Trace the directory path */ |
if (res == FR_OK) { /* Trace completed */ |
if (dir != NULL) { /* It is not the root dir */ |
if (dir[DIR_Attr] & AM_DIR) { /* The entry is a directory */ |
dirobj->clust = |
#if _FAT32 |
((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | |
#endif |
LD_WORD(&dir[DIR_FstClusLO]); |
dirobj->sect = clust2sect(dirobj->clust); |
dirobj->index = 2; |
} else { /* The entry is not a directory */ |
res = FR_NO_FILE; |
} |
} |
dirobj->id = fs->id; |
} |
return res; |
} |
/*-----------------------------------------------------------------------*/ |
/* Read Directory Entry in Sequense */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_readdir ( |
DIR *dirobj, /* Pointer to the directory object */ |
FILINFO *finfo /* Pointer to file information to return */ |
) |
{ |
BYTE *dir, c; |
FRESULT res; |
FATFS *fs = dirobj->fs; |
res = validate(fs, dirobj->id); /* Check validity of the object */ |
if (res) return res; |
finfo->fname[0] = 0; |
while (dirobj->sect) { |
if (!move_window(dirobj->sect)) |
return FR_RW_ERROR; |
dir = &fs->win[(dirobj->index & 15) * 32]; /* pointer to the directory entry */ |
c = dir[DIR_Name]; |
if (c == 0) break; /* Has it reached to end of dir? */ |
if (c != 0xE5 && !(dir[DIR_Attr] & AM_VOL)) /* Is it a valid entry? */ |
get_fileinfo(finfo, dir); |
if (!next_dir_entry(dirobj)) dirobj->sect = 0; /* Next entry */ |
if (finfo->fname[0]) break; /* Found valid entry */ |
} |
return FR_OK; |
} |
#if _FS_MINIMIZE == 0 |
/*-----------------------------------------------------------------------*/ |
/* Get File Status */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_stat ( |
const char *path, /* Pointer to the file path */ |
FILINFO *finfo /* Pointer to file information to return */ |
) |
{ |
BYTE *dir; |
char fn[8+3+1]; |
FRESULT res; |
DIR dirobj; |
res = auto_mount(&path, 0); |
if (res != FR_OK) return res; |
res = trace_path(&dirobj, fn, path, &dir); /* Trace the file path */ |
if (res == FR_OK) { /* Trace completed */ |
if (dir) /* Found an object */ |
get_fileinfo(finfo, dir); |
else /* It is root dir */ |
res = FR_INVALID_NAME; |
} |
return res; |
} |
#if !_FS_READONLY |
/*-----------------------------------------------------------------------*/ |
/* Get Number of Free Clusters */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_getfree ( |
const char *drv, /* Logical drive number */ |
DWORD *nclust, /* Pointer to the double word to return number of free clusters */ |
FATFS **fatfs /* Pointer to pointer to the file system object to return */ |
) |
{ |
DWORD n, sect; |
CLUST clust; |
BYTE fat, f, *p; |
FRESULT res; |
FATFS *fs; |
/* Get drive number */ |
res = auto_mount(&drv, 0); |
if (res != FR_OK) return res; |
*fatfs = fs = FatFs; |
/* If number of free cluster is valid, return it without cluster scan. */ |
if (fs->free_clust <= fs->max_clust - 2) { |
*nclust = fs->free_clust; |
return FR_OK; |
} |
/* Count number of free clusters */ |
fat = fs->fs_type; |
n = 0; |
if (fat == FS_FAT12) { |
clust = 2; |
do { |
if ((WORD)get_cluster(clust) == 0) n++; |
} while (++clust < fs->max_clust); |
} else { |
clust = fs->max_clust; |
sect = fs->fatbase; |
f = 0; p = 0; |
do { |
if (!f) { |
if (!move_window(sect++)) return FR_RW_ERROR; |
p = fs->win; |
} |
if (!_FAT32 || fat == FS_FAT16) { |
if (LD_WORD(p) == 0) n++; |
p += 2; f += 1; |
} else { |
if (LD_DWORD(p) == 0) n++; |
p += 4; f += 2; |
} |
} while (--clust); |
} |
fs->free_clust = n; |
#if _USE_FSINFO |
if (fat == FS_FAT32) fs->fsi_flag = 1; |
#endif |
*nclust = n; |
return FR_OK; |
} |
/*-----------------------------------------------------------------------*/ |
/* Delete a File or a Directory */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_unlink ( |
const char *path /* Pointer to the file or directory path */ |
) |
{ |
BYTE *dir, *sdir; |
DWORD dsect; |
char fn[8+3+1]; |
CLUST dclust; |
FRESULT res; |
DIR dirobj; |
FATFS *fs = FatFs; |
res = auto_mount(&path, 1); |
if (res != FR_OK) return res; |
res = trace_path(&dirobj, fn, path, &dir); /* Trace the file path */ |
if (res != FR_OK) return res; /* Trace failed */ |
if (dir == NULL) return FR_INVALID_NAME; /* It is the root directory */ |
if (dir[DIR_Attr] & AM_RDO) return FR_DENIED; /* It is a R/O object */ |
dsect = fs->winsect; |
dclust = |
#if _FAT32 |
((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | |
#endif |
LD_WORD(&dir[DIR_FstClusLO]); |
if (dir[DIR_Attr] & AM_DIR) { /* It is a sub-directory */ |
dirobj.clust = dclust; /* Check if the sub-dir is empty or not */ |
dirobj.sect = clust2sect(dclust); |
dirobj.index = 2; |
do { |
if (!move_window(dirobj.sect)) return FR_RW_ERROR; |
sdir = &fs->win[(dirobj.index & 15) * 32]; |
if (sdir[DIR_Name] == 0) break; |
if (sdir[DIR_Name] != 0xE5 && !(sdir[DIR_Attr] & AM_VOL)) |
return FR_DENIED; /* The directory is not empty */ |
} while (next_dir_entry(&dirobj)); |
} |
if (!move_window(dsect)) return FR_RW_ERROR; /* Mark the directory entry 'deleted' */ |
dir[DIR_Name] = 0xE5; |
fs->winflag = 1; |
if (!remove_chain(dclust)) return FR_RW_ERROR; /* Remove the cluster chain */ |
return sync(); |
} |
/*-----------------------------------------------------------------------*/ |
/* Create a Directory */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_mkdir ( |
const char *path /* Pointer to the directory path */ |
) |
{ |
BYTE *dir, *fw, n; |
char fn[8+3+1]; |
DWORD sect, dsect, tim; |
CLUST dclust, pclust; |
FRESULT res; |
DIR dirobj; |
FATFS *fs = FatFs; |
res = auto_mount(&path, 1); |
if (res != FR_OK) return res; |
res = trace_path(&dirobj, fn, path, &dir); /* Trace the file path */ |
if (res == FR_OK) return FR_EXIST; /* Any file or directory is already existing */ |
if (res != FR_NO_FILE) return res; |
res = reserve_direntry(&dirobj, &dir); /* Reserve a directory entry */ |
if (res != FR_OK) return res; |
sect = fs->winsect; |
dclust = create_chain(0); /* Allocate a cluster for new directory table */ |
if (dclust == 1) return FR_RW_ERROR; |
dsect = clust2sect(dclust); |
if (!dsect) return FR_DENIED; |
if (!move_window(dsect)) return FR_RW_ERROR; |
fw = fs->win; |
memset(fw, 0, 512); /* Clear the directory table */ |
for (n = 1; n < fs->sects_clust; n++) { |
if (disk_write(0, fw, ++dsect, 1) != RES_OK) |
return FR_RW_ERROR; |
} |
memset(&fw[DIR_Name], ' ', 8+3); /* Create "." entry */ |
fw[DIR_Name] = '.'; |
fw[DIR_Attr] = AM_DIR; |
tim = get_fattime(); |
ST_DWORD(&fw[DIR_WrtTime], tim); |
memcpy(&fw[32], &fw[0], 32); fw[33] = '.'; /* Create ".." entry */ |
pclust = dirobj.sclust; |
#if _FAT32 |
ST_WORD(&fw[ DIR_FstClusHI], dclust >> 16); |
if (fs->fs_type == FS_FAT32 && pclust == fs->dirbase) pclust = 0; |
ST_WORD(&fw[32+DIR_FstClusHI], pclust >> 16); |
#endif |
ST_WORD(&fw[ DIR_FstClusLO], dclust); |
ST_WORD(&fw[32+DIR_FstClusLO], pclust); |
fs->winflag = 1; |
if (!move_window(sect)) return FR_RW_ERROR; |
memset(&dir[0], 0, 32); /* Clean-up the new entry */ |
memcpy(&dir[DIR_Name], fn, 8+3); /* Name */ |
dir[DIR_NTres] = fn[11]; |
dir[DIR_Attr] = AM_DIR; /* Attribute */ |
ST_DWORD(&dir[DIR_WrtTime], tim); /* Crated time */ |
ST_WORD(&dir[DIR_FstClusLO], dclust); /* Table start cluster */ |
#if _FAT32 |
ST_WORD(&dir[DIR_FstClusHI], dclust >> 16); |
#endif |
return sync(); |
} |
/*-----------------------------------------------------------------------*/ |
/* Change File Attribute */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_chmod ( |
const char *path, /* Pointer to the file path */ |
BYTE value, /* Attribute bits */ |
BYTE mask /* Attribute mask to change */ |
) |
{ |
FRESULT res; |
BYTE *dir; |
DIR dirobj; |
char fn[8+3+1]; |
res = auto_mount(&path, 1); |
if (res == FR_OK) { |
res = trace_path(&dirobj, fn, path, &dir); /* Trace the file path */ |
if (res == FR_OK) { /* Trace completed */ |
if (dir == NULL) { |
res = FR_INVALID_NAME; |
} else { |
mask &= AM_RDO|AM_HID|AM_SYS|AM_ARC; /* Valid attribute mask */ |
dir[DIR_Attr] = (value & mask) | (dir[DIR_Attr] & (BYTE)~mask); /* Apply attribute change */ |
res = sync(); |
} |
} |
} |
return res; |
} |
/*-----------------------------------------------------------------------*/ |
/* Rename File/Directory */ |
/*-----------------------------------------------------------------------*/ |
FRESULT f_rename ( |
const char *path_old, /* Pointer to the old name */ |
const char *path_new /* Pointer to the new name */ |
) |
{ |
FRESULT res; |
DWORD sect_old; |
BYTE *dir_old, *dir_new, direntry[32-11]; |
DIR dirobj; |
char fn[8+3+1]; |
FATFS *fs = FatFs; |
res = auto_mount(&path_old, 1); |
if (res != FR_OK) return res; |
res = trace_path(&dirobj, fn, path_old, &dir_old); /* Check old object */ |
if (res != FR_OK) return res; /* The old object is not found */ |
if (!dir_old) return FR_NO_FILE; |
sect_old = fs->winsect; /* Save the object information */ |
memcpy(direntry, &dir_old[11], 32-11); |
res = trace_path(&dirobj, fn, path_new, &dir_new); /* Check new object */ |
if (res == FR_OK) return FR_EXIST; /* The new object name is already existing */ |
if (res != FR_NO_FILE) return res; /* Is there no old name? */ |
res = reserve_direntry(&dirobj, &dir_new); /* Reserve a directory entry */ |
if (res != FR_OK) return res; |
memcpy(&dir_new[DIR_Attr], direntry, 32-11); /* Create new entry */ |
memcpy(&dir_new[DIR_Name], fn, 8+3); |
dir_new[DIR_NTres] = fn[11]; |
fs->winflag = 1; |
if (!move_window(sect_old)) return FR_RW_ERROR; /* Remove old entry */ |
dir_old[DIR_Name] = 0xE5; |
return sync(); |
} |
#endif /* !_FS_READONLY */ |
#endif /* _FS_MINIMIZE == 0 */ |
#endif /* _FS_MINIMIZE <= 1 */ |
#endif /* _FS_MINIMIZE <= 2 */ |
/Designs/DOGFINDER01A/SW/ATMEGA168/tff.h |
---|
0,0 → 1,289 |
/*--------------------------------------------------------------------------/ |
/ Tiny-FatFs - FAT file system module include file R0.04b (C)ChaN, 2007 |
/---------------------------------------------------------------------------/ |
/ FatFs module is an experimenal project to implement FAT file system to |
/ cheap microcontrollers. This is a free software and is opened for education, |
/ research and development under license policy of following trems. |
/ |
/ Copyright (C) 2007, ChaN, all right reserved. |
/ |
/ * The FatFs module is a free software and there is no warranty. |
/ * You can use, modify and/or redistribute it for personal, non-profit or |
/ profit use without any restriction under your responsibility. |
/ * Redistributions of source code must retain the above copyright notice. |
/ |
/---------------------------------------------------------------------------*/ |
#ifndef _FATFS |
#define _MCU_ENDIAN 1 |
/* The _MCU_ENDIAN defines which access method is used to the FAT structure. |
/ 1: Enable word access. |
/ 2: Disable word access and use byte-by-byte access instead. |
/ When the architectural byte order of the MCU is big-endian and/or address |
/ miss-aligned access is prohibited, the _MCU_ENDIAN must be set to 2. |
/ If it is not the case, it can be set to 1 for good code efficiency. */ |
#define _FS_READONLY 0 |
/* Setting _FS_READONLY to 1 defines read only configuration. This removes |
/ writing functions, f_write, f_sync, f_unlink, f_mkdir, f_chmod, f_rename |
/ and useless f_getfree. */ |
#define _FS_MINIMIZE 2 |
/* The _FS_MINIMIZE option defines minimization level to remove some functions. |
/ 0: Full function. |
/ 1: f_stat, f_getfree, f_unlink, f_mkdir, f_chmod and f_rename are removed. |
/ 2: f_opendir and f_readdir are removed in addition to level 1. |
/ 3: f_lseek is removed in addition to level 2. */ |
#define _FAT32 0 |
/* To support FAT32 in addition of FAT12/16, set _FAT32 to 1. */ |
#define _USE_FSINFO 0 |
/* To support FSInfo on FAT32 volume, set _USE_FSINFO to 1. */ |
#define _USE_SJIS 0 |
/* When _USE_SJIS is set to 1, Shift-JIS code transparency is enabled, otherwise |
/ only US-ASCII(7bit) code can be accepted as file/directory name. */ |
#define _USE_NTFLAG 1 |
/* When _USE_NTFLAG is set to 1, upper/lower case of the file name is preserved. */ |
#include "integer.h" |
/* Type definition for cluster number */ |
#if _FAT32 |
typedef DWORD CLUST; |
#else |
typedef WORD CLUST; |
#undef _USE_FSINFO |
#define _USE_FSINFO 0 |
#endif |
/* File system object structure */ |
typedef struct _FATFS { |
WORD id; /* File system mount ID */ |
WORD n_rootdir; /* Number of root directory entries */ |
DWORD winsect; /* Current sector appearing in the win[] */ |
DWORD fatbase; /* FAT start sector */ |
DWORD dirbase; /* Root directory start sector */ |
DWORD database; /* Data start sector */ |
CLUST sects_fat; /* Sectors per fat */ |
CLUST max_clust; /* Maximum cluster# + 1 */ |
#if !_FS_READONLY |
CLUST last_clust; /* Last allocated cluster */ |
CLUST free_clust; /* Number of free clusters */ |
#if _USE_FSINFO |
DWORD fsi_sector; /* fsinfo sector */ |
BYTE fsi_flag; /* fsinfo dirty flag (1:must be written back) */ |
BYTE pad1; |
#endif |
#endif |
BYTE fs_type; /* FAT sub type */ |
BYTE sects_clust; /* Sectors per cluster */ |
BYTE n_fats; /* Number of FAT copies */ |
BYTE winflag; /* win[] dirty flag (1:must be written back) */ |
BYTE win[512]; /* Disk access window for Directory/FAT/File */ |
} FATFS; |
/* Directory object structure */ |
typedef struct _DIR { |
WORD id; /* Owner file system mount ID */ |
WORD index; /* Current index */ |
FATFS* fs; /* Pointer to the owner file system object */ |
CLUST sclust; /* Start cluster */ |
CLUST clust; /* Current cluster */ |
DWORD sect; /* Current sector */ |
} DIR; |
/* File object structure */ |
typedef struct _FIL { |
WORD id; /* Owner file system mount ID */ |
BYTE flag; /* File status flags */ |
BYTE sect_clust; /* Left sectors in cluster */ |
FATFS* fs; /* Pointer to owner file system */ |
DWORD fptr; /* File R/W pointer */ |
DWORD fsize; /* File size */ |
CLUST org_clust; /* File start cluster */ |
CLUST curr_clust; /* Current cluster */ |
DWORD curr_sect; /* Current sector */ |
#if !_FS_READONLY |
DWORD dir_sect; /* Sector containing the directory entry */ |
BYTE* dir_ptr; /* Ponter to the directory entry in the window */ |
#endif |
} FIL; |
/* File status structure */ |
typedef struct _FILINFO { |
DWORD fsize; /* Size */ |
WORD fdate; /* Date */ |
WORD ftime; /* Time */ |
BYTE fattrib; /* Attribute */ |
char fname[8+1+3+1]; /* Name (8.3 format) */ |
} FILINFO; |
/* File function return code (FRESULT) */ |
typedef enum { |
FR_OK = 0, /* 0 */ |
FR_NOT_READY, /* 1 */ |
FR_NO_FILE, /* 2 */ |
FR_NO_PATH, /* 3 */ |
FR_INVALID_NAME, /* 4 */ |
FR_INVALID_DRIVE, /* 5 */ |
FR_DENIED, /* 6 */ |
FR_EXIST, /* 7 */ |
FR_RW_ERROR, /* 8 */ |
FR_WRITE_PROTECTED, /* 9 */ |
FR_NOT_ENABLED, /* 10 */ |
FR_NO_FILESYSTEM, /* 11 */ |
FR_INVALID_OBJECT /* 12 */ |
} FRESULT; |
/*-----------------------------------------------------*/ |
/* FatFs module application interface */ |
FRESULT f_mount (BYTE, FATFS*); /* Mount/Unmount a logical drive */ |
FRESULT f_open (FIL*, const char*, BYTE); /* Open or create a file */ |
FRESULT f_read (FIL*, void*, WORD, WORD*); /* Read data from a file */ |
FRESULT f_write (FIL*, const void*, WORD, WORD*); /* Write data to a file */ |
FRESULT f_lseek (FIL*, DWORD); /* Move file pointer of a file object */ |
FRESULT f_close (FIL*); /* Close an open file object */ |
FRESULT f_opendir (DIR*, const char*); /* Open an existing directory */ |
FRESULT f_readdir (DIR*, FILINFO*); /* Read a directory item */ |
FRESULT f_stat (const char*, FILINFO*); /* Get file status */ |
FRESULT f_getfree (const char*, DWORD*, FATFS**); /* Get number of free clusters on the drive */ |
FRESULT f_sync (FIL*); /* Flush cached data of a writing file */ |
FRESULT f_unlink (const char*); /* Delete an existing file or directory */ |
FRESULT f_mkdir (const char*); /* Create a new directory */ |
FRESULT f_chmod (const char*, BYTE, BYTE); /* Change file/dir attriburte */ |
FRESULT f_rename (const char*, const char*); /* Rename/Move a file or directory */ |
/* User defined function to give a current time to fatfs module */ |
DWORD get_fattime (void); /* 31-25: Year(0-127 +1980), 24-21: Month(1-12), 20-16: Day(1-31) */ |
/* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */ |
/* File access control and file status flags (FIL.flag) */ |
#define FA_READ 0x01 |
#define FA_OPEN_EXISTING 0x00 |
#if !_FS_READONLY |
#define FA_WRITE 0x02 |
#define FA_CREATE_NEW 0x04 |
#define FA_CREATE_ALWAYS 0x08 |
#define FA_OPEN_ALWAYS 0x10 |
#define FA__WRITTEN 0x20 |
#endif |
#define FA__ERROR 0x80 |
/* FAT sub type (FATFS.fs_type) */ |
#define FS_FAT12 1 |
#define FS_FAT16 2 |
#define FS_FAT32 3 |
/* File attribute bits for directory entry */ |
#define AM_RDO 0x01 /* Read only */ |
#define AM_HID 0x02 /* Hidden */ |
#define AM_SYS 0x04 /* System */ |
#define AM_VOL 0x08 /* Volume label */ |
#define AM_LFN 0x0F /* LFN entry */ |
#define AM_DIR 0x10 /* Directory */ |
#define AM_ARC 0x20 /* Archive */ |
/* Offset of FAT structure members */ |
#define BS_jmpBoot 0 |
#define BS_OEMName 3 |
#define BPB_BytsPerSec 11 |
#define BPB_SecPerClus 13 |
#define BPB_RsvdSecCnt 14 |
#define BPB_NumFATs 16 |
#define BPB_RootEntCnt 17 |
#define BPB_TotSec16 19 |
#define BPB_Media 21 |
#define BPB_FATSz16 22 |
#define BPB_SecPerTrk 24 |
#define BPB_NumHeads 26 |
#define BPB_HiddSec 28 |
#define BPB_TotSec32 32 |
#define BS_55AA 510 |
#define BS_DrvNum 36 |
#define BS_BootSig 38 |
#define BS_VolID 39 |
#define BS_VolLab 43 |
#define BS_FilSysType 54 |
#define BPB_FATSz32 36 |
#define BPB_ExtFlags 40 |
#define BPB_FSVer 42 |
#define BPB_RootClus 44 |
#define BPB_FSInfo 48 |
#define BPB_BkBootSec 50 |
#define BS_DrvNum32 64 |
#define BS_BootSig32 66 |
#define BS_VolID32 67 |
#define BS_VolLab32 71 |
#define BS_FilSysType32 82 |
#define FSI_LeadSig 0 |
#define FSI_StrucSig 484 |
#define FSI_Free_Count 488 |
#define FSI_Nxt_Free 492 |
#define MBR_Table 446 |
#define DIR_Name 0 |
#define DIR_Attr 11 |
#define DIR_NTres 12 |
#define DIR_CrtTime 14 |
#define DIR_CrtDate 16 |
#define DIR_FstClusHI 20 |
#define DIR_WrtTime 22 |
#define DIR_WrtDate 24 |
#define DIR_FstClusLO 26 |
#define DIR_FileSize 28 |
/* Multi-byte word access macros */ |
#if _MCU_ENDIAN == 1 /* Use word access */ |
#define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr)) |
#define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr)) |
#define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val) |
#define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val) |
#else |
#if _MCU_ENDIAN == 2 /* Use byte-by-byte access */ |
#define LD_WORD(ptr) (WORD)(((WORD)*(BYTE*)((ptr)+1)<<8)|(WORD)*(BYTE*)(ptr)) |
#define LD_DWORD(ptr) (DWORD)(((DWORD)*(BYTE*)((ptr)+3)<<24)|((DWORD)*(BYTE*)((ptr)+2)<<16)|((WORD)*(BYTE*)((ptr)+1)<<8)|*(BYTE*)(ptr)) |
#define ST_WORD(ptr,val) *(BYTE*)(ptr)=(BYTE)(val); *(BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8) |
#define ST_DWORD(ptr,val) *(BYTE*)(ptr)=(BYTE)(val); *(BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8); *(BYTE*)((ptr)+2)=(BYTE)((DWORD)(val)>>16); *(BYTE*)((ptr)+3)=(BYTE)((DWORD)(val)>>24) |
#else |
#error Do not forget to set _MCU_ENDIAN properly! |
#endif |
#endif |
#define _FATFS |
#endif /* _FATFS */ |
/Designs/DOGFINDER01A/SW/ATMEGA168/tt.ini |
---|
0,0 → 1,0 |
bps=115200 |
/Designs/GPSRL03A/main.c |
---|
182,18 → 182,8 |
rxfifo.idx_r = i; |
return d; |
} |
- |
-/* Put a character to transmit */ |
-static |
-void uart_put (uint8_t d) |
-{ |
- while (bit_is_clear(UCSR0A, UDRE0)); |
- UDR0 = d; |
-} |
- |
- |
/* USART0 RXC interrupt */ |
ISR(USART_RX_vect) |
{ |
@@ -272,21 +262,6 @@ |
return c; |
} |
- |
- |
-/* Initialize GPS module (depends on each product) */ |
-static |
-void gp_init (void) |
-{ |
- const prog_char *s = |
- PSTR("$PSRF106,21*0F\r\n"); // Select datum of WGS84 (for EM-406A) |
- char c; |
- |
- while ((c = pgm_read_byte(s++)) != 0) uart_put(c); |
-} |
- |
- |
- |
/* Get a column item */ |
static |
BYTE* gp_col ( /* Returns pointer to the item (returns a NULL when not found) */ |
@@ -365,7 +340,6 @@ |
beep(5, 1); // Single beep. Start to get current time. |
uart_init(); |
- gp_init(); // Initialize GPS module to let output data in NMEA-0183 format. |
do { // Wait for valid RMC sentence. |
b = get_line(); |
if (!b) break; |
@@ -396,14 +370,14 @@ |
) |
{ |
if (f_write(&file1, Buff, b, &s) || b != s) { err = 5; break; }; |
- itoa(battery,&Value1,10); |
+/* itoa(battery,&Value1,10); // convert binary number to character |
itoa(intensity,&Value2,10); |
strcpy(&Buff[0], Value1); |
strcpy_P(&Buff[4], PSTR(",")); |
strcpy(&Buff[5], Value2); |
strcpy_P(&Buff[9], PSTR("\r\n")); |
- if (f_write(&file1, Buff, 11, &s) || 11 != s) { err = 5; break; }; |
- } |
+ if (f_write(&file1, Buff, 11, &s) || 11 != s) { err = 8; break; }; |
+*/ } |
if ((Stat & 4) == 0) continue; |
if (f_sync(&file1)) { err = 6; break; };// Synchronize the file in interval of 300 sec. |
cli(); Stat &= 0xFB; sei(); // Clear sync request |