added free FatFs Module files (c) ChaN
This commit is contained in:
51
SD_CARD/BaS_gcc/sources/diskio.h
Normal file
51
SD_CARD/BaS_gcc/sources/diskio.h
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
/*-----------------------------------------------------------------------
|
||||||
|
/ Low level disk interface modlue include file (C)ChaN, 2009
|
||||||
|
/-----------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#ifndef _DISKIO
|
||||||
|
#define _DISKIO
|
||||||
|
|
||||||
|
#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 */
|
||||||
|
|
||||||
|
int assign_drives (int, int);
|
||||||
|
DSTATUS disk_initialize (BYTE);
|
||||||
|
DSTATUS disk_status (BYTE);
|
||||||
|
DRESULT disk_read (BYTE, BYTE*, DWORD, BYTE);
|
||||||
|
DRESULT disk_write (BYTE, const BYTE*, DWORD, BYTE);
|
||||||
|
DRESULT disk_ioctl (BYTE, BYTE, 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 fucntion */
|
||||||
|
|
||||||
|
/* Generic command (mandatory for FatFs) */
|
||||||
|
#define CTRL_SYNC 0 /* Flush disk cache (for write functions) */
|
||||||
|
#define GET_SECTOR_COUNT 1 /* Get media size (for only f_mkfs()) */
|
||||||
|
#define GET_SECTOR_SIZE 2 /* Get sector size (for multiple sector size (_MAX_SS >= 1024)) */
|
||||||
|
#define GET_BLOCK_SIZE 3 /* Get erase block size (for only f_mkfs()) */
|
||||||
|
|
||||||
|
#endif
|
||||||
4139
SD_CARD/BaS_gcc/sources/ff.c
Normal file
4139
SD_CARD/BaS_gcc/sources/ff.c
Normal file
File diff suppressed because it is too large
Load Diff
337
SD_CARD/BaS_gcc/sources/ff.h
Normal file
337
SD_CARD/BaS_gcc/sources/ff.h
Normal file
@@ -0,0 +1,337 @@
|
|||||||
|
/*---------------------------------------------------------------------------/
|
||||||
|
/ FatFs - FAT file system module include file R0.09a (C)ChaN, 2012
|
||||||
|
/----------------------------------------------------------------------------/
|
||||||
|
/ FatFs module is a generic FAT file system module for small embedded systems.
|
||||||
|
/ This is a free software that opened for education, research and commercial
|
||||||
|
/ developments under license policy of following terms.
|
||||||
|
/
|
||||||
|
/ Copyright (C) 2012, ChaN, all right reserved.
|
||||||
|
/
|
||||||
|
/ * The FatFs module is a free software and there is NO WARRANTY.
|
||||||
|
/ * No restriction on use. You can use, modify and redistribute it for
|
||||||
|
/ personal, non-profit or commercial product UNDER YOUR RESPONSIBILITY.
|
||||||
|
/ * Redistributions of source code must retain the above copyright notice.
|
||||||
|
/
|
||||||
|
/----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#ifndef _FATFS
|
||||||
|
#define _FATFS 4004 /* Revision ID */
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "integer.h" /* Basic integer types */
|
||||||
|
#include "ffconf.h" /* FatFs configuration options */
|
||||||
|
|
||||||
|
#if _FATFS != _FFCONF
|
||||||
|
#error Wrong configuration file (ffconf.h).
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Definitions of volume management */
|
||||||
|
|
||||||
|
#if _MULTI_PARTITION /* Multiple partition configuration */
|
||||||
|
typedef struct {
|
||||||
|
BYTE pd; /* Physical drive number */
|
||||||
|
BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */
|
||||||
|
} PARTITION;
|
||||||
|
extern PARTITION VolToPart[]; /* Volume - Partition resolution table */
|
||||||
|
#define LD2PD(vol) (VolToPart[vol].pd) /* Get physical drive number */
|
||||||
|
#define LD2PT(vol) (VolToPart[vol].pt) /* Get partition index */
|
||||||
|
|
||||||
|
#else /* Single partition configuration */
|
||||||
|
#define LD2PD(vol) (BYTE)(vol) /* Each logical drive is bound to the same physical drive number */
|
||||||
|
#define LD2PT(vol) 0 /* Always mounts the 1st partition or in SFD */
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Type of path name strings on FatFs API */
|
||||||
|
|
||||||
|
#if _LFN_UNICODE /* Unicode string */
|
||||||
|
#if !_USE_LFN
|
||||||
|
#error _LFN_UNICODE must be 0 in non-LFN cfg.
|
||||||
|
#endif
|
||||||
|
#ifndef _INC_TCHAR
|
||||||
|
typedef WCHAR TCHAR;
|
||||||
|
#define _T(x) L ## x
|
||||||
|
#define _TEXT(x) L ## x
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#else /* ANSI/OEM string */
|
||||||
|
#ifndef _INC_TCHAR
|
||||||
|
typedef char TCHAR;
|
||||||
|
#define _T(x) x
|
||||||
|
#define _TEXT(x) x
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* File system object structure (FATFS) */
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
BYTE fs_type; /* FAT sub-type (0:Not mounted) */
|
||||||
|
BYTE drv; /* Physical drive number */
|
||||||
|
BYTE csize; /* Sectors per cluster (1,2,4...128) */
|
||||||
|
BYTE n_fats; /* Number of FAT copies (1,2) */
|
||||||
|
BYTE wflag; /* win[] dirty flag (1:must be written back) */
|
||||||
|
BYTE fsi_flag; /* fsinfo dirty flag (1:must be written back) */
|
||||||
|
WORD id; /* File system mount ID */
|
||||||
|
WORD n_rootdir; /* Number of root directory entries (FAT12/16) */
|
||||||
|
#if _MAX_SS != 512
|
||||||
|
WORD ssize; /* Bytes per sector (512, 1024, 2048 or 4096) */
|
||||||
|
#endif
|
||||||
|
#if _FS_REENTRANT
|
||||||
|
_SYNC_t sobj; /* Identifier of sync object */
|
||||||
|
#endif
|
||||||
|
#if !_FS_READONLY
|
||||||
|
DWORD last_clust; /* Last allocated cluster */
|
||||||
|
DWORD free_clust; /* Number of free clusters */
|
||||||
|
DWORD fsi_sector; /* fsinfo sector (FAT32) */
|
||||||
|
#endif
|
||||||
|
#if _FS_RPATH
|
||||||
|
DWORD cdir; /* Current directory start cluster (0:root) */
|
||||||
|
#endif
|
||||||
|
DWORD n_fatent; /* Number of FAT entries (= number of clusters + 2) */
|
||||||
|
DWORD fsize; /* Sectors per FAT */
|
||||||
|
DWORD fatbase; /* FAT start sector */
|
||||||
|
DWORD dirbase; /* Root directory start sector (FAT32:Cluster#) */
|
||||||
|
DWORD database; /* Data start sector */
|
||||||
|
DWORD winsect; /* Current sector appearing in the win[] */
|
||||||
|
BYTE win[_MAX_SS]; /* Disk access window for Directory, FAT (and Data on tiny cfg) */
|
||||||
|
} FATFS;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* File object structure (FIL) */
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
FATFS* fs; /* Pointer to the related file system object */
|
||||||
|
WORD id; /* File system mount ID of the related file system object */
|
||||||
|
BYTE flag; /* File status flags */
|
||||||
|
BYTE pad1;
|
||||||
|
DWORD fptr; /* File read/write pointer (0ed on file open) */
|
||||||
|
DWORD fsize; /* File size */
|
||||||
|
DWORD sclust; /* File data start cluster (0:no data cluster, always 0 when fsize is 0) */
|
||||||
|
DWORD clust; /* Current cluster of fpter */
|
||||||
|
DWORD dsect; /* Current data sector of fpter */
|
||||||
|
#if !_FS_READONLY
|
||||||
|
DWORD dir_sect; /* Sector containing the directory entry */
|
||||||
|
BYTE* dir_ptr; /* Pointer to the directory entry in the window */
|
||||||
|
#endif
|
||||||
|
#if _USE_FASTSEEK
|
||||||
|
DWORD* cltbl; /* Pointer to the cluster link map table (null on file open) */
|
||||||
|
#endif
|
||||||
|
#if _FS_LOCK
|
||||||
|
UINT lockid; /* File lock ID (index of file semaphore table Files[]) */
|
||||||
|
#endif
|
||||||
|
#if !_FS_TINY
|
||||||
|
BYTE buf[_MAX_SS]; /* File data read/write buffer */
|
||||||
|
#endif
|
||||||
|
} FIL;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Directory object structure (DIR) */
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
FATFS* fs; /* Pointer to the owner file system object */
|
||||||
|
WORD id; /* Owner file system mount ID */
|
||||||
|
WORD index; /* Current read/write index number */
|
||||||
|
DWORD sclust; /* Table start cluster (0:Root dir) */
|
||||||
|
DWORD clust; /* Current cluster */
|
||||||
|
DWORD sect; /* Current sector */
|
||||||
|
BYTE* dir; /* Pointer to the current SFN entry in the win[] */
|
||||||
|
BYTE* fn; /* Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */
|
||||||
|
#if _USE_LFN
|
||||||
|
WCHAR* lfn; /* Pointer to the LFN working buffer */
|
||||||
|
WORD lfn_idx; /* Last matched LFN index number (0xFFFF:No LFN) */
|
||||||
|
#endif
|
||||||
|
} DIR;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* File status structure (FILINFO) */
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
DWORD fsize; /* File size */
|
||||||
|
WORD fdate; /* Last modified date */
|
||||||
|
WORD ftime; /* Last modified time */
|
||||||
|
BYTE fattrib; /* Attribute */
|
||||||
|
TCHAR fname[13]; /* Short file name (8.3 format) */
|
||||||
|
#if _USE_LFN
|
||||||
|
TCHAR* lfname; /* Pointer to the LFN buffer */
|
||||||
|
UINT lfsize; /* Size of LFN buffer in TCHAR */
|
||||||
|
#endif
|
||||||
|
} FILINFO;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* File function return code (FRESULT) */
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
FR_OK = 0, /* (0) Succeeded */
|
||||||
|
FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
|
||||||
|
FR_INT_ERR, /* (2) Assertion failed */
|
||||||
|
FR_NOT_READY, /* (3) The physical drive cannot work */
|
||||||
|
FR_NO_FILE, /* (4) Could not find the file */
|
||||||
|
FR_NO_PATH, /* (5) Could not find the path */
|
||||||
|
FR_INVALID_NAME, /* (6) The path name format is invalid */
|
||||||
|
FR_DENIED, /* (7) Access denied due to prohibited access or directory full */
|
||||||
|
FR_EXIST, /* (8) Access denied due to prohibited access */
|
||||||
|
FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
|
||||||
|
FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
|
||||||
|
FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
|
||||||
|
FR_NOT_ENABLED, /* (12) The volume has no work area */
|
||||||
|
FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */
|
||||||
|
FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any parameter error */
|
||||||
|
FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */
|
||||||
|
FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */
|
||||||
|
FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */
|
||||||
|
FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > _FS_SHARE */
|
||||||
|
FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
|
||||||
|
} FRESULT;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------*/
|
||||||
|
/* FatFs module application interface */
|
||||||
|
|
||||||
|
FRESULT f_mount (BYTE, FATFS*); /* Mount/Unmount a logical drive */
|
||||||
|
FRESULT f_open (FIL*, const TCHAR*, BYTE); /* Open or create a file */
|
||||||
|
FRESULT f_read (FIL*, void*, UINT, UINT*); /* Read data from 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 TCHAR*); /* Open an existing directory */
|
||||||
|
FRESULT f_readdir (DIR*, FILINFO*); /* Read a directory item */
|
||||||
|
FRESULT f_stat (const TCHAR*, FILINFO*); /* Get file status */
|
||||||
|
FRESULT f_write (FIL*, const void*, UINT, UINT*); /* Write data to a file */
|
||||||
|
FRESULT f_getfree (const TCHAR*, 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 TCHAR*); /* Delete an existing file or directory */
|
||||||
|
FRESULT f_mkdir (const TCHAR*); /* Create a new directory */
|
||||||
|
FRESULT f_chmod (const TCHAR*, BYTE, BYTE); /* Change attribute of the file/dir */
|
||||||
|
FRESULT f_utime (const TCHAR*, const FILINFO*); /* Change times-tamp of the file/dir */
|
||||||
|
FRESULT f_rename (const TCHAR*, const TCHAR*); /* Rename/Move a file or directory */
|
||||||
|
FRESULT f_chdrive (BYTE); /* Change current drive */
|
||||||
|
FRESULT f_chdir (const TCHAR*); /* Change current directory */
|
||||||
|
FRESULT f_getcwd (TCHAR*, UINT); /* Get current directory */
|
||||||
|
FRESULT f_forward (FIL*, UINT(*)(const BYTE*,UINT), UINT, UINT*); /* Forward data to the stream */
|
||||||
|
FRESULT f_mkfs (BYTE, BYTE, UINT); /* Create a file system on the drive */
|
||||||
|
FRESULT f_fdisk (BYTE, const DWORD[], void*); /* Divide a physical drive into some partitions */
|
||||||
|
int f_putc (TCHAR, FIL*); /* Put a character to the file */
|
||||||
|
int f_puts (const TCHAR*, FIL*); /* Put a string to the file */
|
||||||
|
int f_printf (FIL*, const TCHAR*, ...); /* Put a formatted string to the file */
|
||||||
|
TCHAR* f_gets (TCHAR*, int, FIL*); /* Get a string from the file */
|
||||||
|
|
||||||
|
#define f_eof(fp) (((fp)->fptr == (fp)->fsize) ? 1 : 0)
|
||||||
|
#define f_error(fp) (((fp)->flag & FA__ERROR) ? 1 : 0)
|
||||||
|
#define f_tell(fp) ((fp)->fptr)
|
||||||
|
#define f_size(fp) ((fp)->fsize)
|
||||||
|
|
||||||
|
#ifndef EOF
|
||||||
|
#define EOF (-1)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------*/
|
||||||
|
/* Additional user defined functions */
|
||||||
|
|
||||||
|
/* RTC function */
|
||||||
|
#if !_FS_READONLY
|
||||||
|
DWORD get_fattime (void);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Unicode support functions */
|
||||||
|
#if _USE_LFN /* Unicode - OEM code conversion */
|
||||||
|
WCHAR ff_convert (WCHAR, UINT); /* OEM-Unicode bidirectional conversion */
|
||||||
|
WCHAR ff_wtoupper (WCHAR); /* Unicode upper-case conversion */
|
||||||
|
#if _USE_LFN == 3 /* Memory functions */
|
||||||
|
void* ff_memalloc (UINT); /* Allocate memory block */
|
||||||
|
void ff_memfree (void*); /* Free memory block */
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Sync functions */
|
||||||
|
#if _FS_REENTRANT
|
||||||
|
int ff_cre_syncobj (BYTE, _SYNC_t*);/* Create a sync object */
|
||||||
|
int ff_req_grant (_SYNC_t); /* Lock sync object */
|
||||||
|
void ff_rel_grant (_SYNC_t); /* Unlock sync object */
|
||||||
|
int ff_del_syncobj (_SYNC_t); /* Delete a sync object */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------*/
|
||||||
|
/* Flags and offset address */
|
||||||
|
|
||||||
|
|
||||||
|
/* File access control and file status flags (FIL.flag) */
|
||||||
|
|
||||||
|
#define FA_READ 0x01
|
||||||
|
#define FA_OPEN_EXISTING 0x00
|
||||||
|
#define FA__ERROR 0x80
|
||||||
|
|
||||||
|
#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
|
||||||
|
#define FA__DIRTY 0x40
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/* 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 */
|
||||||
|
#define AM_MASK 0x3F /* Mask of defined bits */
|
||||||
|
|
||||||
|
|
||||||
|
/* Fast seek feature */
|
||||||
|
#define CREATE_LINKMAP 0xFFFFFFFF
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*--------------------------------*/
|
||||||
|
/* Multi-byte word access macros */
|
||||||
|
|
||||||
|
#if _WORD_ACCESS == 1 /* Enable word access to the FAT structure */
|
||||||
|
#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 /* Use byte-by-byte access to the FAT structure */
|
||||||
|
#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)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* _FATFS */
|
||||||
190
SD_CARD/BaS_gcc/sources/ffconf.h
Normal file
190
SD_CARD/BaS_gcc/sources/ffconf.h
Normal file
@@ -0,0 +1,190 @@
|
|||||||
|
/*---------------------------------------------------------------------------/
|
||||||
|
/ FatFs - FAT file system module configuration file R0.09a (C)ChaN, 2012
|
||||||
|
/----------------------------------------------------------------------------/
|
||||||
|
/
|
||||||
|
/ CAUTION! Do not forget to make clean the project after any changes to
|
||||||
|
/ the configuration options.
|
||||||
|
/
|
||||||
|
/----------------------------------------------------------------------------*/
|
||||||
|
#ifndef _FFCONF
|
||||||
|
#define _FFCONF 4004 /* Revision ID */
|
||||||
|
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------/
|
||||||
|
/ Functions and Buffer Configurations
|
||||||
|
/----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#define _FS_TINY 0 /* 0:Normal or 1:Tiny */
|
||||||
|
/* When _FS_TINY is set to 1, FatFs uses the sector buffer in the file system
|
||||||
|
/ object instead of the sector buffer in the individual file object for file
|
||||||
|
/ data transfer. This reduces memory consumption 512 bytes each file object. */
|
||||||
|
|
||||||
|
|
||||||
|
#define _FS_READONLY 0 /* 0:Read/Write or 1:Read only */
|
||||||
|
/* 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 /* 0 to 3 */
|
||||||
|
/* 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 1.
|
||||||
|
/ 3: f_lseek is removed in addition to 2. */
|
||||||
|
|
||||||
|
|
||||||
|
#define _USE_STRFUNC 0 /* 0:Disable or 1-2:Enable */
|
||||||
|
/* To enable string functions, set _USE_STRFUNC to 1 or 2. */
|
||||||
|
|
||||||
|
|
||||||
|
#define _USE_MKFS 0 /* 0:Disable or 1:Enable */
|
||||||
|
/* To enable f_mkfs function, set _USE_MKFS to 1 and set _FS_READONLY to 0 */
|
||||||
|
|
||||||
|
|
||||||
|
#define _USE_FORWARD 0 /* 0:Disable or 1:Enable */
|
||||||
|
/* To enable f_forward function, set _USE_FORWARD to 1 and set _FS_TINY to 1. */
|
||||||
|
|
||||||
|
|
||||||
|
#define _USE_FASTSEEK 0 /* 0:Disable or 1:Enable */
|
||||||
|
/* To enable fast seek feature, set _USE_FASTSEEK to 1. */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------/
|
||||||
|
/ Locale and Namespace Configurations
|
||||||
|
/----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#define _CODE_PAGE 932
|
||||||
|
/* The _CODE_PAGE specifies the OEM code page to be used on the target system.
|
||||||
|
/ Incorrect setting of the code page can cause a file open failure.
|
||||||
|
/
|
||||||
|
/ 932 - Japanese Shift-JIS (DBCS, OEM, Windows)
|
||||||
|
/ 936 - Simplified Chinese GBK (DBCS, OEM, Windows)
|
||||||
|
/ 949 - Korean (DBCS, OEM, Windows)
|
||||||
|
/ 950 - Traditional Chinese Big5 (DBCS, OEM, Windows)
|
||||||
|
/ 1250 - Central Europe (Windows)
|
||||||
|
/ 1251 - Cyrillic (Windows)
|
||||||
|
/ 1252 - Latin 1 (Windows)
|
||||||
|
/ 1253 - Greek (Windows)
|
||||||
|
/ 1254 - Turkish (Windows)
|
||||||
|
/ 1255 - Hebrew (Windows)
|
||||||
|
/ 1256 - Arabic (Windows)
|
||||||
|
/ 1257 - Baltic (Windows)
|
||||||
|
/ 1258 - Vietnam (OEM, Windows)
|
||||||
|
/ 437 - U.S. (OEM)
|
||||||
|
/ 720 - Arabic (OEM)
|
||||||
|
/ 737 - Greek (OEM)
|
||||||
|
/ 775 - Baltic (OEM)
|
||||||
|
/ 850 - Multilingual Latin 1 (OEM)
|
||||||
|
/ 858 - Multilingual Latin 1 + Euro (OEM)
|
||||||
|
/ 852 - Latin 2 (OEM)
|
||||||
|
/ 855 - Cyrillic (OEM)
|
||||||
|
/ 866 - Russian (OEM)
|
||||||
|
/ 857 - Turkish (OEM)
|
||||||
|
/ 862 - Hebrew (OEM)
|
||||||
|
/ 874 - Thai (OEM, Windows)
|
||||||
|
/ 1 - ASCII only (Valid for non LFN cfg.)
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#define _USE_LFN 0 /* 0 to 3 */
|
||||||
|
#define _MAX_LFN 255 /* Maximum LFN length to handle (12 to 255) */
|
||||||
|
/* The _USE_LFN option switches the LFN support.
|
||||||
|
/
|
||||||
|
/ 0: Disable LFN feature. _MAX_LFN and _LFN_UNICODE have no effect.
|
||||||
|
/ 1: Enable LFN with static working buffer on the BSS. Always NOT reentrant.
|
||||||
|
/ 2: Enable LFN with dynamic working buffer on the STACK.
|
||||||
|
/ 3: Enable LFN with dynamic working buffer on the HEAP.
|
||||||
|
/
|
||||||
|
/ The LFN working buffer occupies (_MAX_LFN + 1) * 2 bytes. To enable LFN,
|
||||||
|
/ Unicode handling functions ff_convert() and ff_wtoupper() must be added
|
||||||
|
/ to the project. When enable to use heap, memory control functions
|
||||||
|
/ ff_memalloc() and ff_memfree() must be added to the project. */
|
||||||
|
|
||||||
|
|
||||||
|
#define _LFN_UNICODE 0 /* 0:ANSI/OEM or 1:Unicode */
|
||||||
|
/* To switch the character code set on FatFs API to Unicode,
|
||||||
|
/ enable LFN feature and set _LFN_UNICODE to 1. */
|
||||||
|
|
||||||
|
|
||||||
|
#define _FS_RPATH 0 /* 0 to 2 */
|
||||||
|
/* The _FS_RPATH option configures relative path feature.
|
||||||
|
/
|
||||||
|
/ 0: Disable relative path feature and remove related functions.
|
||||||
|
/ 1: Enable relative path. f_chdrive() and f_chdir() are available.
|
||||||
|
/ 2: f_getcwd() is available in addition to 1.
|
||||||
|
/
|
||||||
|
/ Note that output of the f_readdir fnction is affected by this option. */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------/
|
||||||
|
/ Physical Drive Configurations
|
||||||
|
/----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#define _VOLUMES 1
|
||||||
|
/* Number of volumes (logical drives) to be used. */
|
||||||
|
|
||||||
|
|
||||||
|
#define _MAX_SS 512 /* 512, 1024, 2048 or 4096 */
|
||||||
|
/* Maximum sector size to be handled.
|
||||||
|
/ Always set 512 for memory card and hard disk but a larger value may be
|
||||||
|
/ required for on-board flash memory, floppy disk and optical disk.
|
||||||
|
/ When _MAX_SS is larger than 512, it configures FatFs to variable sector size
|
||||||
|
/ and GET_SECTOR_SIZE command must be implememted to the disk_ioctl function. */
|
||||||
|
|
||||||
|
|
||||||
|
#define _MULTI_PARTITION 0 /* 0:Single partition, 1/2:Enable multiple partition */
|
||||||
|
/* When set to 0, each volume is bound to the same physical drive number and
|
||||||
|
/ it can mount only first primaly partition. When it is set to 1, each volume
|
||||||
|
/ is tied to the partitions listed in VolToPart[]. */
|
||||||
|
|
||||||
|
|
||||||
|
#define _USE_ERASE 0 /* 0:Disable or 1:Enable */
|
||||||
|
/* To enable sector erase feature, set _USE_ERASE to 1. CTRL_ERASE_SECTOR command
|
||||||
|
/ should be added to the disk_ioctl functio. */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------/
|
||||||
|
/ System Configurations
|
||||||
|
/----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#define _WORD_ACCESS 0 /* 0 or 1 */
|
||||||
|
/* Set 0 first and it is always compatible with all platforms. The _WORD_ACCESS
|
||||||
|
/ option defines which access method is used to the word data on the FAT volume.
|
||||||
|
/
|
||||||
|
/ 0: Byte-by-byte access.
|
||||||
|
/ 1: Word access. Do not choose this unless following condition is met.
|
||||||
|
/
|
||||||
|
/ When the byte order on the memory is big-endian or address miss-aligned word
|
||||||
|
/ access results incorrect behavior, the _WORD_ACCESS must be set to 0.
|
||||||
|
/ If it is not the case, the value can also be set to 1 to improve the
|
||||||
|
/ performance and code size.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/* A header file that defines sync object types on the O/S, such as
|
||||||
|
/ windows.h, ucos_ii.h and semphr.h, must be included prior to ff.h. */
|
||||||
|
|
||||||
|
#define _FS_REENTRANT 0 /* 0:Disable or 1:Enable */
|
||||||
|
#define _FS_TIMEOUT 1000 /* Timeout period in unit of time ticks */
|
||||||
|
#define _SYNC_t HANDLE /* O/S dependent type of sync object. e.g. HANDLE, OS_EVENT*, ID and etc.. */
|
||||||
|
|
||||||
|
/* The _FS_REENTRANT option switches the reentrancy (thread safe) of the FatFs module.
|
||||||
|
/
|
||||||
|
/ 0: Disable reentrancy. _SYNC_t and _FS_TIMEOUT have no effect.
|
||||||
|
/ 1: Enable reentrancy. Also user provided synchronization handlers,
|
||||||
|
/ ff_req_grant, ff_rel_grant, ff_del_syncobj and ff_cre_syncobj
|
||||||
|
/ function must be added to the project. */
|
||||||
|
|
||||||
|
|
||||||
|
#define _FS_LOCK 0 /* 0:Disable or >=1:Enable */
|
||||||
|
/* To enable file lock control feature, set _FS_LOCK to 1 or greater.
|
||||||
|
The value defines how many files can be opened simultaneously. */
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* _FFCONFIG */
|
||||||
37
SD_CARD/BaS_gcc/sources/integer.h
Normal file
37
SD_CARD/BaS_gcc/sources/integer.h
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
/*-------------------------------------------*/
|
||||||
|
/* Integer type definitions for FatFs module */
|
||||||
|
/*-------------------------------------------*/
|
||||||
|
|
||||||
|
#ifndef _INTEGER
|
||||||
|
#define _INTEGER
|
||||||
|
|
||||||
|
#ifdef _WIN32 /* FatFs development platform */
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
|
#include <tchar.h>
|
||||||
|
|
||||||
|
#else /* Embedded platform */
|
||||||
|
|
||||||
|
/* These types must be 16-bit, 32-bit or larger integer */
|
||||||
|
typedef int INT;
|
||||||
|
typedef unsigned int UINT;
|
||||||
|
|
||||||
|
/* These types must be 8-bit integer */
|
||||||
|
typedef char CHAR;
|
||||||
|
typedef unsigned char UCHAR;
|
||||||
|
typedef unsigned char BYTE;
|
||||||
|
|
||||||
|
/* These types must be 16-bit integer */
|
||||||
|
typedef short SHORT;
|
||||||
|
typedef unsigned short USHORT;
|
||||||
|
typedef unsigned short WORD;
|
||||||
|
typedef unsigned short WCHAR;
|
||||||
|
|
||||||
|
/* These types must be 32-bit integer */
|
||||||
|
typedef long LONG;
|
||||||
|
typedef unsigned long ULONG;
|
||||||
|
typedef unsigned long DWORD;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
100
SD_CARD/BaS_gcc/sources/main.c
Normal file
100
SD_CARD/BaS_gcc/sources/main.c
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
/*----------------------------------------------------------------------*/
|
||||||
|
/* FatFs sample project for generic microcontrollers (C)ChaN, 2012 */
|
||||||
|
/*----------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "ff.h"
|
||||||
|
|
||||||
|
|
||||||
|
FATFS Fatfs; /* File system object */
|
||||||
|
FIL Fil; /* File object */
|
||||||
|
BYTE Buff[128]; /* File read buffer */
|
||||||
|
|
||||||
|
|
||||||
|
void die ( /* Stop with dying message */
|
||||||
|
FRESULT rc /* FatFs return value */
|
||||||
|
)
|
||||||
|
{
|
||||||
|
printf("Failed with rc=%u.\n", rc);
|
||||||
|
for (;;) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
/* Program Main */
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
int main (void)
|
||||||
|
{
|
||||||
|
FRESULT rc; /* Result code */
|
||||||
|
DIR dir; /* Directory object */
|
||||||
|
FILINFO fno; /* File information object */
|
||||||
|
UINT bw, br, i;
|
||||||
|
|
||||||
|
|
||||||
|
f_mount(0, &Fatfs); /* Register volume work area (never fails) */
|
||||||
|
|
||||||
|
printf("\nOpen an existing file (message.txt).\n");
|
||||||
|
rc = f_open(&Fil, "MESSAGE.TXT", FA_READ);
|
||||||
|
if (rc) die(rc);
|
||||||
|
|
||||||
|
printf("\nType the file content.\n");
|
||||||
|
for (;;) {
|
||||||
|
rc = f_read(&Fil, Buff, sizeof Buff, &br); /* Read a chunk of file */
|
||||||
|
if (rc || !br) break; /* Error or end of file */
|
||||||
|
for (i = 0; i < br; i++) /* Type the data */
|
||||||
|
putchar(Buff[i]);
|
||||||
|
}
|
||||||
|
if (rc) die(rc);
|
||||||
|
|
||||||
|
printf("\nClose the file.\n");
|
||||||
|
rc = f_close(&Fil);
|
||||||
|
if (rc) die(rc);
|
||||||
|
|
||||||
|
printf("\nCreate a new file (hello.txt).\n");
|
||||||
|
rc = f_open(&Fil, "HELLO.TXT", FA_WRITE | FA_CREATE_ALWAYS);
|
||||||
|
if (rc) die(rc);
|
||||||
|
|
||||||
|
printf("\nWrite a text data. (Hello world!)\n");
|
||||||
|
rc = f_write(&Fil, "Hello world!\r\n", 14, &bw);
|
||||||
|
if (rc) die(rc);
|
||||||
|
printf("%u bytes written.\n", bw);
|
||||||
|
|
||||||
|
printf("\nClose the file.\n");
|
||||||
|
rc = f_close(&Fil);
|
||||||
|
if (rc) die(rc);
|
||||||
|
|
||||||
|
printf("\nOpen root directory.\n");
|
||||||
|
rc = f_opendir(&dir, "");
|
||||||
|
if (rc) die(rc);
|
||||||
|
|
||||||
|
printf("\nDirectory listing...\n");
|
||||||
|
for (;;) {
|
||||||
|
rc = f_readdir(&dir, &fno); /* Read a directory item */
|
||||||
|
if (rc || !fno.fname[0]) break; /* Error or end of dir */
|
||||||
|
if (fno.fattrib & AM_DIR)
|
||||||
|
printf(" <dir> %s\n", fno.fname);
|
||||||
|
else
|
||||||
|
printf("%8lu %s\n", fno.fsize, fno.fname);
|
||||||
|
}
|
||||||
|
if (rc) die(rc);
|
||||||
|
|
||||||
|
printf("\nTest completed.\n");
|
||||||
|
for (;;) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*---------------------------------------------------------*/
|
||||||
|
/* User Provided Timer Function for FatFs module */
|
||||||
|
/*---------------------------------------------------------*/
|
||||||
|
|
||||||
|
DWORD get_fattime (void)
|
||||||
|
{
|
||||||
|
return ((DWORD)(2012 - 1980) << 25) /* Year = 2012 */
|
||||||
|
| ((DWORD)1 << 21) /* Month = 1 */
|
||||||
|
| ((DWORD)1 << 16) /* Day_m = 1*/
|
||||||
|
| ((DWORD)0 << 11) /* Hour = 0 */
|
||||||
|
| ((DWORD)0 << 5) /* Min = 0 */
|
||||||
|
| ((DWORD)0 >> 1); /* Sec = 0 */
|
||||||
|
}
|
||||||
558
SD_CARD/BaS_gcc/sources/mmcbb.c
Normal file
558
SD_CARD/BaS_gcc/sources/mmcbb.c
Normal file
@@ -0,0 +1,558 @@
|
|||||||
|
/*------------------------------------------------------------------------/
|
||||||
|
/ Bitbanging MMCv3/SDv1/SDv2 (in SPI mode) control module
|
||||||
|
/-------------------------------------------------------------------------/
|
||||||
|
/
|
||||||
|
/ Copyright (C) 2012, ChaN, all right reserved.
|
||||||
|
/
|
||||||
|
/ * This software is a free software and there is NO WARRANTY.
|
||||||
|
/ * No restriction on use. You can use, modify and redistribute it for
|
||||||
|
/ personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
|
||||||
|
/ * Redistributions of source code must retain the above copyright notice.
|
||||||
|
/
|
||||||
|
/--------------------------------------------------------------------------/
|
||||||
|
Features and Limitations:
|
||||||
|
|
||||||
|
* Very Easy to Port
|
||||||
|
It uses only 4 bit of GPIO port. No interrupt, no SPI port is used.
|
||||||
|
|
||||||
|
* Platform Independent
|
||||||
|
You need to modify only a few macros to control GPIO ports.
|
||||||
|
|
||||||
|
* Low Speed
|
||||||
|
The data transfer rate will be several times slower than hardware SPI.
|
||||||
|
|
||||||
|
/-------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
|
||||||
|
#include "diskio.h" /* Common include file for FatFs and disk I/O layer */
|
||||||
|
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
/* Platform dependent macros and functions needed to be modified */
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#include <device.h> /* Include device specific declareation file here */
|
||||||
|
|
||||||
|
#define INIT_PORT() init_port() /* Initialize MMC control port (CS=H, CLK=L, DI=H, DO=in) */
|
||||||
|
#define DLY_US(n) dly_us(n) /* Delay n microseconds */
|
||||||
|
|
||||||
|
#define CS_H() PORT |= 0x01 /* Set MMC CS "high" */
|
||||||
|
#define CS_L() PORT &= 0xFE /* Set MMC CS "low" */
|
||||||
|
#define CK_H() PORT |= 0x02 /* Set MMC SCLK "high" */
|
||||||
|
#define CK_L() PORT &= 0xFD /* Set MMC SCLK "low" */
|
||||||
|
#define DI_H() PORT |= 0x04 /* Set MMC DI "high" */
|
||||||
|
#define DI_L() PORT &= 0xFB /* Set MMC DI "low" */
|
||||||
|
#define DO (PORT & 0x08) /* Test for MMC DO ('H':true, 'L':false) */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------
|
||||||
|
|
||||||
|
Module Private Functions
|
||||||
|
|
||||||
|
---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
/* MMC/SD command (SPI mode) */
|
||||||
|
#define CMD0 (0) /* GO_IDLE_STATE */
|
||||||
|
#define CMD1 (1) /* SEND_OP_COND */
|
||||||
|
#define ACMD41 (0x80+41) /* SEND_OP_COND (SDC) */
|
||||||
|
#define CMD8 (8) /* SEND_IF_COND */
|
||||||
|
#define CMD9 (9) /* SEND_CSD */
|
||||||
|
#define CMD10 (10) /* SEND_CID */
|
||||||
|
#define CMD12 (12) /* STOP_TRANSMISSION */
|
||||||
|
#define CMD13 (13) /* SEND_STATUS */
|
||||||
|
#define ACMD13 (0x80+13) /* SD_STATUS (SDC) */
|
||||||
|
#define CMD16 (16) /* SET_BLOCKLEN */
|
||||||
|
#define CMD17 (17) /* READ_SINGLE_BLOCK */
|
||||||
|
#define CMD18 (18) /* READ_MULTIPLE_BLOCK */
|
||||||
|
#define CMD23 (23) /* SET_BLOCK_COUNT */
|
||||||
|
#define ACMD23 (0x80+23) /* SET_WR_BLK_ERASE_COUNT (SDC) */
|
||||||
|
#define CMD24 (24) /* WRITE_BLOCK */
|
||||||
|
#define CMD25 (25) /* WRITE_MULTIPLE_BLOCK */
|
||||||
|
#define CMD41 (41) /* SEND_OP_COND (ACMD) */
|
||||||
|
#define CMD55 (55) /* APP_CMD */
|
||||||
|
#define CMD58 (58) /* READ_OCR */
|
||||||
|
|
||||||
|
/* Card type flags (CardType) */
|
||||||
|
#define CT_MMC 0x01 /* MMC ver 3 */
|
||||||
|
#define CT_SD1 0x02 /* SD ver 1 */
|
||||||
|
#define CT_SD2 0x04 /* SD ver 2 */
|
||||||
|
#define CT_SDC 0x06 /* SD */
|
||||||
|
#define CT_BLOCK 0x08 /* Block addressing */
|
||||||
|
|
||||||
|
|
||||||
|
static
|
||||||
|
DSTATUS Stat = STA_NOINIT; /* Disk status */
|
||||||
|
|
||||||
|
static
|
||||||
|
BYTE CardType; /* b0:MMC, b1:SDv1, b2:SDv2, b3:Block addressing */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
/* Transmit bytes to the card (bitbanging) */
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static
|
||||||
|
void xmit_mmc (
|
||||||
|
const BYTE* buff, /* Data to be sent */
|
||||||
|
UINT bc /* Number of bytes to send */
|
||||||
|
)
|
||||||
|
{
|
||||||
|
BYTE d;
|
||||||
|
|
||||||
|
|
||||||
|
do {
|
||||||
|
d = *buff++; /* Get a byte to be sent */
|
||||||
|
if (d & 0x80) DI_H(); else DI_L(); /* bit7 */
|
||||||
|
CK_H(); CK_L();
|
||||||
|
if (d & 0x40) DI_H(); else DI_L(); /* bit6 */
|
||||||
|
CK_H(); CK_L();
|
||||||
|
if (d & 0x20) DI_H(); else DI_L(); /* bit5 */
|
||||||
|
CK_H(); CK_L();
|
||||||
|
if (d & 0x10) DI_H(); else DI_L(); /* bit4 */
|
||||||
|
CK_H(); CK_L();
|
||||||
|
if (d & 0x08) DI_H(); else DI_L(); /* bit3 */
|
||||||
|
CK_H(); CK_L();
|
||||||
|
if (d & 0x04) DI_H(); else DI_L(); /* bit2 */
|
||||||
|
CK_H(); CK_L();
|
||||||
|
if (d & 0x02) DI_H(); else DI_L(); /* bit1 */
|
||||||
|
CK_H(); CK_L();
|
||||||
|
if (d & 0x01) DI_H(); else DI_L(); /* bit0 */
|
||||||
|
CK_H(); CK_L();
|
||||||
|
} while (--bc);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
/* Receive bytes from the card (bitbanging) */
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static
|
||||||
|
void rcvr_mmc (
|
||||||
|
BYTE *buff, /* Pointer to read buffer */
|
||||||
|
UINT bc /* Number of bytes to receive */
|
||||||
|
)
|
||||||
|
{
|
||||||
|
BYTE r;
|
||||||
|
|
||||||
|
|
||||||
|
DI_H(); /* Send 0xFF */
|
||||||
|
|
||||||
|
do {
|
||||||
|
r = 0; if (DO) r++; /* bit7 */
|
||||||
|
CK_H(); CK_L();
|
||||||
|
r <<= 1; if (DO) r++; /* bit6 */
|
||||||
|
CK_H(); CK_L();
|
||||||
|
r <<= 1; if (DO) r++; /* bit5 */
|
||||||
|
CK_H(); CK_L();
|
||||||
|
r <<= 1; if (DO) r++; /* bit4 */
|
||||||
|
CK_H(); CK_L();
|
||||||
|
r <<= 1; if (DO) r++; /* bit3 */
|
||||||
|
CK_H(); CK_L();
|
||||||
|
r <<= 1; if (DO) r++; /* bit2 */
|
||||||
|
CK_H(); CK_L();
|
||||||
|
r <<= 1; if (DO) r++; /* bit1 */
|
||||||
|
CK_H(); CK_L();
|
||||||
|
r <<= 1; if (DO) r++; /* bit0 */
|
||||||
|
CK_H(); CK_L();
|
||||||
|
*buff++ = r; /* Store a received byte */
|
||||||
|
} while (--bc);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
/* Wait for card ready */
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static
|
||||||
|
int wait_ready (void) /* 1:OK, 0:Timeout */
|
||||||
|
{
|
||||||
|
BYTE d;
|
||||||
|
UINT tmr;
|
||||||
|
|
||||||
|
|
||||||
|
for (tmr = 5000; tmr; tmr--) { /* Wait for ready in timeout of 500ms */
|
||||||
|
rcvr_mmc(&d, 1);
|
||||||
|
if (d == 0xFF) break;
|
||||||
|
DLY_US(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
return tmr ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
/* Deselect the card and release SPI bus */
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static
|
||||||
|
void deselect (void)
|
||||||
|
{
|
||||||
|
BYTE d;
|
||||||
|
|
||||||
|
CS_H();
|
||||||
|
rcvr_mmc(&d, 1); /* Dummy clock (force DO hi-z for multiple slave SPI) */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
/* Select the card and wait for ready */
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static
|
||||||
|
int select (void) /* 1:OK, 0:Timeout */
|
||||||
|
{
|
||||||
|
BYTE d;
|
||||||
|
|
||||||
|
CS_L();
|
||||||
|
rcvr_mmc(&d, 1); /* Dummy clock (force DO enabled) */
|
||||||
|
|
||||||
|
if (wait_ready()) return 1; /* OK */
|
||||||
|
deselect();
|
||||||
|
return 0; /* Failed */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
/* Receive a data packet from the card */
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static
|
||||||
|
int rcvr_datablock ( /* 1:OK, 0:Failed */
|
||||||
|
BYTE *buff, /* Data buffer to store received data */
|
||||||
|
UINT btr /* Byte count */
|
||||||
|
)
|
||||||
|
{
|
||||||
|
BYTE d[2];
|
||||||
|
UINT tmr;
|
||||||
|
|
||||||
|
|
||||||
|
for (tmr = 1000; tmr; tmr--) { /* Wait for data packet in timeout of 100ms */
|
||||||
|
rcvr_mmc(d, 1);
|
||||||
|
if (d[0] != 0xFF) break;
|
||||||
|
DLY_US(100);
|
||||||
|
}
|
||||||
|
if (d[0] != 0xFE) return 0; /* If not valid data token, return with error */
|
||||||
|
|
||||||
|
rcvr_mmc(buff, btr); /* Receive the data block into buffer */
|
||||||
|
rcvr_mmc(d, 2); /* Discard CRC */
|
||||||
|
|
||||||
|
return 1; /* Return with success */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
/* Send a data packet to the card */
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static
|
||||||
|
int xmit_datablock ( /* 1:OK, 0:Failed */
|
||||||
|
const BYTE *buff, /* 512 byte data block to be transmitted */
|
||||||
|
BYTE token /* Data/Stop token */
|
||||||
|
)
|
||||||
|
{
|
||||||
|
BYTE d[2];
|
||||||
|
|
||||||
|
|
||||||
|
if (!wait_ready()) return 0;
|
||||||
|
|
||||||
|
d[0] = token;
|
||||||
|
xmit_mmc(d, 1); /* Xmit a token */
|
||||||
|
if (token != 0xFD) { /* Is it data token? */
|
||||||
|
xmit_mmc(buff, 512); /* Xmit the 512 byte data block to MMC */
|
||||||
|
rcvr_mmc(d, 2); /* Xmit dummy CRC (0xFF,0xFF) */
|
||||||
|
rcvr_mmc(d, 1); /* Receive data response */
|
||||||
|
if ((d[0] & 0x1F) != 0x05) /* If not accepted, return with error */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
/* Send a command packet to the card */
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static
|
||||||
|
BYTE send_cmd ( /* Returns command response (bit7==1:Send failed)*/
|
||||||
|
BYTE cmd, /* Command byte */
|
||||||
|
DWORD arg /* Argument */
|
||||||
|
)
|
||||||
|
{
|
||||||
|
BYTE n, d, buf[6];
|
||||||
|
|
||||||
|
|
||||||
|
if (cmd & 0x80) { /* ACMD<n> is the command sequense of CMD55-CMD<n> */
|
||||||
|
cmd &= 0x7F;
|
||||||
|
n = send_cmd(CMD55, 0);
|
||||||
|
if (n > 1) return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Select the card and wait for ready */
|
||||||
|
deselect();
|
||||||
|
if (!select()) return 0xFF;
|
||||||
|
|
||||||
|
/* Send a command packet */
|
||||||
|
buf[0] = 0x40 | cmd; /* Start + Command index */
|
||||||
|
buf[1] = (BYTE)(arg >> 24); /* Argument[31..24] */
|
||||||
|
buf[2] = (BYTE)(arg >> 16); /* Argument[23..16] */
|
||||||
|
buf[3] = (BYTE)(arg >> 8); /* Argument[15..8] */
|
||||||
|
buf[4] = (BYTE)arg; /* Argument[7..0] */
|
||||||
|
n = 0x01; /* Dummy CRC + Stop */
|
||||||
|
if (cmd == CMD0) n = 0x95; /* (valid CRC for CMD0(0)) */
|
||||||
|
if (cmd == CMD8) n = 0x87; /* (valid CRC for CMD8(0x1AA)) */
|
||||||
|
buf[5] = n;
|
||||||
|
xmit_mmc(buf, 6);
|
||||||
|
|
||||||
|
/* Receive command response */
|
||||||
|
if (cmd == CMD12) rcvr_mmc(&d, 1); /* Skip a stuff byte when stop reading */
|
||||||
|
n = 10; /* Wait for a valid response in timeout of 10 attempts */
|
||||||
|
do
|
||||||
|
rcvr_mmc(&d, 1);
|
||||||
|
while ((d & 0x80) && --n);
|
||||||
|
|
||||||
|
return d; /* Return with the response value */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------
|
||||||
|
|
||||||
|
Public Functions
|
||||||
|
|
||||||
|
---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
/* Get Disk Status */
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
DSTATUS disk_status (
|
||||||
|
BYTE drv /* Drive number (always 0) */
|
||||||
|
)
|
||||||
|
{
|
||||||
|
DSTATUS s;
|
||||||
|
BYTE d;
|
||||||
|
|
||||||
|
|
||||||
|
if (drv) return STA_NOINIT;
|
||||||
|
|
||||||
|
/* Check if the card is kept initialized */
|
||||||
|
s = Stat;
|
||||||
|
if (!(s & STA_NOINIT)) {
|
||||||
|
if (send_cmd(CMD13, 0)) /* Read card status */
|
||||||
|
s = STA_NOINIT;
|
||||||
|
rcvr_mmc(&d, 1); /* Receive following half of R2 */
|
||||||
|
deselect();
|
||||||
|
}
|
||||||
|
Stat = s;
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
/* Initialize Disk Drive */
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
DSTATUS disk_initialize (
|
||||||
|
BYTE drv /* Physical drive nmuber (0) */
|
||||||
|
)
|
||||||
|
{
|
||||||
|
BYTE n, ty, cmd, buf[4];
|
||||||
|
UINT tmr;
|
||||||
|
DSTATUS s;
|
||||||
|
|
||||||
|
|
||||||
|
if (drv) return RES_NOTRDY;
|
||||||
|
|
||||||
|
INIT_PORT(); /* Initialize control port */
|
||||||
|
for (n = 10; n; n--) rcvr_mmc(buf, 1); /* 80 dummy clocks */
|
||||||
|
|
||||||
|
ty = 0;
|
||||||
|
if (send_cmd(CMD0, 0) == 1) { /* Enter Idle state */
|
||||||
|
if (send_cmd(CMD8, 0x1AA) == 1) { /* SDv2? */
|
||||||
|
rcvr_mmc(buf, 4); /* Get trailing return value of R7 resp */
|
||||||
|
if (buf[2] == 0x01 && buf[3] == 0xAA) { /* The card can work at vdd range of 2.7-3.6V */
|
||||||
|
for (tmr = 1000; tmr; tmr--) { /* Wait for leaving idle state (ACMD41 with HCS bit) */
|
||||||
|
if (send_cmd(ACMD41, 1UL << 30) == 0) break;
|
||||||
|
DLY_US(1000);
|
||||||
|
}
|
||||||
|
if (tmr && send_cmd(CMD58, 0) == 0) { /* Check CCS bit in the OCR */
|
||||||
|
rcvr_mmc(buf, 4);
|
||||||
|
ty = (buf[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2; /* SDv2 */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else { /* SDv1 or MMCv3 */
|
||||||
|
if (send_cmd(ACMD41, 0) <= 1) {
|
||||||
|
ty = CT_SD1; cmd = ACMD41; /* SDv1 */
|
||||||
|
} else {
|
||||||
|
ty = CT_MMC; cmd = CMD1; /* MMCv3 */
|
||||||
|
}
|
||||||
|
for (tmr = 1000; tmr; tmr--) { /* Wait for leaving idle state */
|
||||||
|
if (send_cmd(cmd, 0) == 0) break;
|
||||||
|
DLY_US(1000);
|
||||||
|
}
|
||||||
|
if (!tmr || send_cmd(CMD16, 512) != 0) /* Set R/W block length to 512 */
|
||||||
|
ty = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CardType = ty;
|
||||||
|
s = ty ? 0 : STA_NOINIT;
|
||||||
|
Stat = s;
|
||||||
|
|
||||||
|
deselect();
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
/* 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..128) */
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (disk_status(drv) & STA_NOINIT) return RES_NOTRDY;
|
||||||
|
if (!count) return RES_PARERR;
|
||||||
|
if (!(CardType & CT_BLOCK)) sector *= 512; /* Convert LBA to byte address if needed */
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
return count ? RES_ERROR : RES_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
/* Write Sector(s) */
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
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..128) */
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (disk_status(drv) & STA_NOINIT) return RES_NOTRDY;
|
||||||
|
if (!count) return RES_PARERR;
|
||||||
|
if (!(CardType & CT_BLOCK)) sector *= 512; /* Convert LBA to byte address if needed */
|
||||||
|
|
||||||
|
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 & CT_SDC) send_cmd(ACMD23, count);
|
||||||
|
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();
|
||||||
|
|
||||||
|
return count ? RES_ERROR : RES_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
/* Miscellaneous Functions */
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
DRESULT disk_ioctl (
|
||||||
|
BYTE drv, /* Physical drive nmuber (0) */
|
||||||
|
BYTE ctrl, /* Control code */
|
||||||
|
void *buff /* Buffer to send/receive control data */
|
||||||
|
)
|
||||||
|
{
|
||||||
|
DRESULT res;
|
||||||
|
BYTE n, csd[16];
|
||||||
|
DWORD cs;
|
||||||
|
|
||||||
|
|
||||||
|
if (disk_status(drv) & STA_NOINIT) return RES_NOTRDY; /* Check if card is in the socket */
|
||||||
|
|
||||||
|
res = RES_ERROR;
|
||||||
|
switch (ctrl) {
|
||||||
|
case CTRL_SYNC : /* Make sure that no pending write process */
|
||||||
|
if (select()) {
|
||||||
|
deselect();
|
||||||
|
res = RES_OK;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
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 */
|
||||||
|
cs = csd[9] + ((WORD)csd[8] << 8) + ((DWORD)(csd[7] & 63) << 8) + 1;
|
||||||
|
*(DWORD*)buff = cs << 10;
|
||||||
|
} else { /* SDC ver 1.XX or MMC */
|
||||||
|
n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2;
|
||||||
|
cs = (csd[8] >> 6) + ((WORD)csd[7] << 2) + ((WORD)(csd[6] & 3) << 10) + 1;
|
||||||
|
*(DWORD*)buff = cs << (n - 9);
|
||||||
|
}
|
||||||
|
res = RES_OK;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case GET_BLOCK_SIZE : /* Get erase block size in unit of sector (DWORD) */
|
||||||
|
*(DWORD*)buff = 128;
|
||||||
|
res = RES_OK;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
res = RES_PARERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
deselect();
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------*/
|
||||||
|
/* This function is defined for only project compatibility */
|
||||||
|
|
||||||
|
void disk_timerproc (void)
|
||||||
|
{
|
||||||
|
/* Nothing to do */
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user