/****************************************************************** microSD File-System head file For uPAC-5001, iP-8441, ip-8841, G-4500 and other controllers which supports microSD/SD Note: head file version is different to lib file version Last Update [May 13, 2014] *******************************************************************/ /* Software Specification Storage Media: Supports the SD cards with a capacity of more than 1G. File System: FAT32 format only. Disk number: 1 Disk size : 1GB, 2GB, 4GB or 8GB File number: 65,517 files max. File name : A total of 12 characters including the dot separator (8-3 format) File operation modes: 1. Read: Open a file for reading 2. Write: Truncate an existing file to zero length or create a new file for writing 3. Append: Open for appending (writing at end of file) The file is created if it does not exist File handle: 6 max. The 6 file handles can all be used for read/write/append operations. Read/Write performance: pc_read pc_write --------------------+------------------------------- 1G SD card | 76.6 KB/Sec 39.6 KB/Sec (Cluster size: 4K) | --------------------+------------------------------- 2G SD card | 75.8 KB/Sec 27.8 KB/Sec (Cluster size: 4K) | --------------------+------------------------------- 4G SD card | 74.9 KB/Sec 35.3 KB/Sec (Cluster size: 32K)| Max. length of writing data: 32767 bytes. Max. length of reading data: 32767 bytes. Hardware supported: iP-8444, iP-8841, iP-8411, iP-8811, G-4500 series, uPAC-5000 series Hardware resource occupied: non. */ /*============================================================================= microSD.h update history =============================================================================== Version 1.0.6 [May 13, 2014] Modify: 1. pc_format function formats the SD card to FAT32 file system ------------------------------------------------------------------------------- Version 1.0.5 [Jun 26, 2013] Modify: 1. Supports the FAT32 file system SD cards with a capacity of more than 4G are already available Note: If an SD card with a capacity of more than 4G is used, it is recommended to format the SD card to FAT32 with 32KB clusters. The format command can be done in DOS, and the syntax is as follows: FORMAT volume /FS:FAT32 /A:32K volume Specifies the drive letter (followed by a colon), mount point, or volume name. /FS:filesystem Specifies the type of the file system. /A:size Overrides the default allocation unit size. ------------------------------------------------------------------------------- [2010,06,08] Add comments for APIs. ------------------------------------------------------------------------------- [2009,11,02] rename 1. pc_lseek to pc_seek add 2. pc_tell ------------------------------------------------------------------------------- [2009,09,21] first released. */ #ifdef __cplusplus extern "C" { #endif #define TRUE 1 #define FALSE 0 typedef unsigned char byte; typedef unsigned short word; typedef unsigned long dword; #if (VFAT) #define EMAXPATH 260 /* Maximum path length. Change if you like */ #else #define EMAXPATH 148 /* Maximum path length. Change if you like */ #endif /* Structure for use by pc_gfirst, pc_gnext */ typedef struct dstat { char fname[9]; /* Null terminated file and extension */ char fext[4]; #if (VFAT) byte lfname[FILENAMESIZE]; /* Long file name fro vfat. */ #endif char filename[15]; /* Null terminated file.ext */ byte fattribute; /* File attributes */ word ftime; /* time & date lastmodified. See date */ word fdate; /* and time handlers for getting info */ dword fsize; /* File size */ /* INTERNAL */ int driveno; byte pname[9]; /* Pattern. */ byte pext[4]; char path[EMAXPATH]; void *pobj; /* Info for getting at the inode */ void *pmom; /* Info for getting at parent inode */ } DSTAT; /* File access flags for flag of pc_open */ #define PO_RDONLY 0x0000 /* Open for read only*/ #define PO_WRONLY 0x0001 /* Open for write only*/ #define PO_RDWR 0x0002 /* Read/write access allowed.*/ #define PO_APPEND 0x0008 /* Seek to eof on each write*/ #define PO_CREATE 0x0100 /* Create the file if it does not exist.*/ #define PO_CREAT 0x0100 /* Create the file if it does not exist.*/ #define PO_TRUNC 0x0200 /* Truncate the file if it already exists*/ #define PO_EXCL 0x0400 /* Fail if creating and already exists*/ /* File creation permissions for mode of pc_open */ #define PS_IWRITE 0000400 /* Write permitted */ #define PS_IREAD 0000200 /* Read permitted. (Always true anyway)*/ /* Error codes for file system operations, like pc_open, pc_close, pc_read, pc_write, close, seek, tell */ /* When the APIs faile or return 'FALSE', call pc_get_errno to know the reasons. */ #define FSERR_BADFD 9 /* Invalid file descriptor*/ #define FSERR_NOENT 2 /* File not found or path to file not found*/ #define FSERR_NOFD 24 /* No file descriptors available (too many files open)*/ #define FSERR_EXIST 17 /* Exclusive access requested but file already exists.*/ #define FSERR_ACCESS 13 /* Attempt to open a read only file or a special (directory)*/ #define FSERR_INVAL 22 /* Seek to negative file pointer attempted.*/ #define FSERR_NOSPC 28 /* Write failed. Presumably because of no space*/ #define FSERR_SHARE 30 /* Open failed do to sharing */ #define FSERR_DEVICE 31 /* No Valid Disk Present */ #define FSERR_BADDIR 32 /* DELTREE -- Directory structure corrupt */ /* error code for pc_ertfs_init */ #define PCERR_BAD_FORMAT 1 #define PCERR_NO_CARD 2 #define PCERR_BAD_CARD 3 #define PCERR_CHANGED_CARD 4 #define PCERR_CARD_FAILURE 5 //============================================================================= //============= API functions ================================================= //============================================================================= int pc_getLibVer(void); //example: return 100 means ver 1.0.0 void pc_getLibDate(unsigned char *LibDate); #define pc_ertfs_init pc_init int pc_init(void); /* return: TRUE: ok FALSE: error pc_get_errno returns 1=PCERR_BAD_FORMAT (format should be FAT) 2=PCERR_NO_CARD (microSD is not inserted) */ int pc_open(char *file_name, unsigned int flag, unsigned int mode); /* file_name: file name in 8.3 fomat, for example: "test.txt", "\dir1\test.txt" flag: PO_RDONLY: Read only PO_WRONLY: Write only PO_RDWR: Read/write access allowed PO_APPEND: Append data to end of file. PO_CREAT: Create the file if it does not exist. PO_TRUNC: Truncate the file if it already exists. mode: PS_IWRITE - Write permitted PS_IREAD - Read permitted. (Always true anyway) return: >=0: ok. the returned value is used as a file descriptor. -1: error. */ int pc_close(int fd); /* return: 0: ok -1: error. */ int pc_read(int fd, unsigned char *buf, unsigned int count); /* return: >=0: ok. the bytes of read. -1: error. */ int pc_write(int fd, unsigned char *buf, unsigned int count); /* return: >=0: ok. the bytes of written. -1: error. */ long pc_seek(int fd, long offset, int origin); /* Arguments to origin of pc_seek */ #define PSEEK_SET 0 /* offset from begining of file*/ #define PSEEK_CUR 1 /* offset from current file pointer*/ #define PSEEK_END 2 /* offset from end of file*/ /* Moves the file pointer to relative offset from the current offset. return: >=0: ok. the new offset. -1L: error. */ long pc_tell(int fd); /* Gets current offset of the file pointer. return: >=0: ok. the current offset. -1L: error */ int pc_eof(int fd); /* Checks for End-Of-File return: 1 (TRUE): ok 0 (FALSE): error */ int pc_format(void (*callbackfun)(void)); /* Formats the microSD to FAT. return: 1 (TRUE): ok 0 (FLASE): erorr */ int pc_mkdir(char *name); /* Creates a sudirectory in the path specified by name. Fails if a file or directory of the same name already exists or if the path is not found. *name: example: "subdir", "\dir1\subdir" return: 1 (TRUE): ok 0 (FLASE): erorr */ int pc_rmdir(char *name); /* Deletes the directory specified in name. Fail if name is not a directory, is read only or contains more than the entries . and .. *name: example: "subdir", "\dir1\subdir" return: 1 (TRUE): ok 0 (FLASE): erorr */ int pc_move(char *file_name, char *new_file_name); /* Renames the file in path (name) to newname. Fails if name is invalid, newname already exists or path not found. *name: example: "old.txt", "new.txt", "\dir1\old.txt", "dir1\new.txt" return: 1 (TRUE): ok 0 (FLASE): erorr */ int pc_del(char *file_name); /* Deletes the file in name. Fail if not a simple file,if it is open, does not exist or is read only. *name: example: "test.txt", "\dir\test.txt" return: 1 (TRUE): ok 0 (FLASE): erorr */ int pc_deltree(char *dir); /* Deletes the directory specified in name, all subdirectories of that directory, and all files contained therein. *dir: example: "subdir", "\dir1\subdir" return: 1 (TRUE): ok 0 (FLASE): erorr */ int pc_isdir(char *path_name); /* Checks if is a directory. return: 1 (TRUE): ok 0 (FLASE): erorr */ int pc_isvol(char *name); /* Checks if is a volum. return: 1 (TRUE): ok 0 (FLASE): erorr */ long pc_size(char *name); /* Gets the size of the file. return: >=0: ok. bytes of the file. -1: error */ int pc_set_cwd(char *path); int pc_get_cwd(char *path); /* Sets/Gets current working directory. *path: example: "subdir", "\dir1\subdir" return: 1 (TRUE): ok 0 (FLASE): erorr */ long pc_get_freeSize_KB(void); long pc_get_usedSize_KB(void); long pc_get_totalSize_KB(void); /* return: >=0: ok. the size in KB. */ //used to search multiple files. file_name can be *.exe, abc*.txt, *.*, etc. int pc_gfirst(DSTAT *statobj, char *file_name); int pc_gnext(DSTAT *statobj); void pc_gdone(DSTAT *statobj); int pc_get_attributes(char *file_name, unsigned char *p_return); int pc_set_attributes(char *file_name, unsigned char attributes); /* File attributes */ #define ARDONLY 0x1 //Read only #define AHIDDEN 0x2 //Hidden #define ASYSTEM 0x4 //System #define AVOLUME 0x8 //Volume #define ADIRENT 0x10 //Directory Entry #define ARCHIVE 0x20 //Rchive #define ANORMAL 0x00 //Normal #define CHICAGO_EXT 0x0f //Chicago extended filename attribute int pc_get_errno(void); //If other API function returns FALSE, //call the function to get the error number. #ifdef __cplusplus } #endif