Update to FatFs R0.07

This commit is contained in:
dhewg 2009-04-09 14:20:12 +02:00 committed by bushing
parent 7715064060
commit 1c137199ac
7 changed files with 2031 additions and 1148 deletions

View File

@ -6,8 +6,12 @@
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
#include "diskio.h" #include "diskio.h"
#include "string.h"
#include "sdhc.h" #include "sdhc.h"
#include <string.h>
#ifndef MEM2_BSS
#define MEM2_BSS
#endif
static sdhci_t sdhci; static sdhci_t sdhci;
static u8 buffer[512] MEM2_BSS ALIGNED(32); static u8 buffer[512] MEM2_BSS ALIGNED(32);
@ -23,7 +27,8 @@ DSTATUS disk_initialize (
sd_init(&sdhci, 0); sd_init(&sdhci, 0);
ret = sd_mount(&sdhci); ret = sd_mount(&sdhci);
if(ret < 0)
if (ret < 0)
return STA_NOINIT; return STA_NOINIT;
else else
return disk_status(drv); return disk_status(drv);
@ -38,8 +43,9 @@ DSTATUS disk_status (
BYTE drv /* Physical drive nmuber (0..) */ BYTE drv /* Physical drive nmuber (0..) */
) )
{ {
if(sd_inserted(&sdhci) == 0) if (sd_inserted(&sdhci) == 0)
return STA_NODISK; return STA_NODISK;
return 0; return 0;
} }
@ -59,15 +65,15 @@ DRESULT disk_read (
DRESULT res; DRESULT res;
res = RES_OK; res = RES_OK;
for(i = 0; i < count; i++) for (i = 0; i < count; i++) {
{ if (sd_read(&sdhci, sector + i, 1, buffer) != 0) {
if(sd_read(&sdhci, sector + i, 1, buffer) != 0)
{
res = RES_ERROR; res = RES_ERROR;
break; break;
} }
memcpy(buff + i * 512, buffer, 512); memcpy(buff + i * 512, buffer, 512);
} }
return res; return res;
} }
@ -77,7 +83,6 @@ DRESULT disk_read (
/* Write Sector(s) */ /* Write Sector(s) */
#if _READONLY == 0 #if _READONLY == 0
DRESULT disk_write ( DRESULT disk_write (
BYTE drv, /* Physical drive nmuber (0..) */ BYTE drv, /* Physical drive nmuber (0..) */
const BYTE *buff, /* Data to be written */ const BYTE *buff, /* Data to be written */
@ -89,15 +94,15 @@ DRESULT disk_write (
DRESULT res; DRESULT res;
res = RES_OK; res = RES_OK;
for(i = 0; i < count; i++) for (i = 0; i < count; i++) {
{
memcpy(buffer, buff + i * 512, 512); memcpy(buffer, buff + i * 512, 512);
if(sd_write(&sdhci, sector + i, 1, buffer) != 0)
{ if(sd_write(&sdhci, sector + i, 1, buffer) != 0) {
res = RES_ERROR; res = RES_ERROR;
break; break;
} }
} }
return res; return res;
} }
#endif /* _READONLY */ #endif /* _READONLY */
@ -113,13 +118,9 @@ DRESULT disk_ioctl (
void *buff /* Buffer to send/receive control data */ void *buff /* Buffer to send/receive control data */
) )
{ {
if(ctrl == CTRL_SYNC) if (ctrl == CTRL_SYNC)
return RES_OK; return RES_OK;
return RES_PARERR; return RES_PARERR;
} }
DWORD get_fattime (void)
{
return 0; // TODO
}

View File

@ -1,5 +1,5 @@
/*----------------------------------------------------------------------- /*-----------------------------------------------------------------------
/ Low level disk interface modlue include file R0.06 (C)ChaN, 2007 / Low level disk interface modlue include file R0.07 (C)ChaN, 2009
/-----------------------------------------------------------------------*/ /-----------------------------------------------------------------------*/
#ifndef _DISKIO #ifndef _DISKIO
@ -26,6 +26,7 @@ typedef enum {
/*---------------------------------------*/ /*---------------------------------------*/
/* Prototypes for disk control functions */ /* Prototypes for disk control functions */
BOOL assign_drives (int argc, char *argv[]);
DSTATUS disk_initialize (BYTE); DSTATUS disk_initialize (BYTE);
DSTATUS disk_status (BYTE); DSTATUS disk_status (BYTE);
DRESULT disk_read (BYTE, BYTE*, DWORD, BYTE); DRESULT disk_read (BYTE, BYTE*, DWORD, BYTE);
@ -33,8 +34,6 @@ DRESULT disk_read (BYTE, BYTE*, DWORD, BYTE);
DRESULT disk_write (BYTE, const BYTE*, DWORD, BYTE); DRESULT disk_write (BYTE, const BYTE*, DWORD, BYTE);
#endif #endif
DRESULT disk_ioctl (BYTE, BYTE, void*); DRESULT disk_ioctl (BYTE, BYTE, void*);
void disk_timerproc (void);
@ -48,7 +47,7 @@ void disk_timerproc (void);
/* Command code for disk_ioctrl() */ /* Command code for disk_ioctrl() */
/* Generic command */ /* Generic command */
#define CTRL_SYNC 0 /* Mandatory for read/write configuration */ #define CTRL_SYNC 0 /* Mandatory for write functions */
#define GET_SECTOR_COUNT 1 /* Mandatory for only f_mkfs() */ #define GET_SECTOR_COUNT 1 /* Mandatory for only f_mkfs() */
#define GET_SECTOR_SIZE 2 #define GET_SECTOR_SIZE 2
#define GET_BLOCK_SIZE 3 /* Mandatory for only f_mkfs() */ #define GET_BLOCK_SIZE 3 /* Mandatory for only f_mkfs() */

2472
ff.c

File diff suppressed because it is too large Load Diff

437
ff.h
View File

@ -1,198 +1,367 @@
/*--------------------------------------------------------------------------/ /*---------------------------------------------------------------------------/
/ FatFs - FAT file system module include file R0.06 (C)ChaN, 2008 / FatFs - FAT file system module include file R0.07 (C)ChaN, 2009
/---------------------------------------------------------------------------/ /----------------------------------------------------------------------------/
/ FatFs module is an experimenal project to implement FAT file system to / FatFs module is an open source project to implement FAT file system to small
/ cheap microcontrollers. This is a free software and is opened for education, / embedded systems. It is opened for education, research and development under
/ research and development under license policy of following trems. / license policy of following trems.
/ /
/ Copyright (C) 2008, ChaN, all right reserved. / Copyright (C) 2009, ChaN, all right reserved.
/ /
/ * The FatFs module is a free software and there is no warranty. / * 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 / * You can use, modify and/or redistribute it for personal, non-profit or
/ commercial use without any restriction under your responsibility. / commercial use without any restriction under your responsibility.
/ * Redistributions of source code must retain the above copyright notice. / * Redistributions of source code must retain the above copyright notice.
/ /
/---------------------------------------------------------------------------*/ /----------------------------------------------------------------------------*/
#include "integer.h"
/*---------------------------------------------------------------------------/
/ FatFs Configuration Options
/
/ CAUTION! Do not forget to make clean the project after any changes to
/ the configuration options.
/
/----------------------------------------------------------------------------*/
#ifndef _FATFS #ifndef _FATFS
#define _FATFS
#define _WORD_ACCESS 0
/* The _WORD_ACCESS option defines which access method is used to the word
/ data in the FAT structure.
/
/ 0: Byte-by-byte access. Always compatible with all platforms.
/ 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 efficiency. */
#define _MCU_ENDIAN 2
/* 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 1 #define _FS_READONLY 1
/* Setting _FS_READONLY to 1 defines read only configuration. This removes /* 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, / writing functions, f_write, f_sync, f_unlink, f_mkdir, f_chmod, f_rename,
/ f_truncate and useless f_getfree. */ / f_truncate and useless f_getfree. */
#define _FS_MINIMIZE 2 #define _FS_MINIMIZE 2
/* The _FS_MINIMIZE option defines minimization level to remove some functions. /* 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. / 0: Full function.
/ 2: f_opendir and f_readdir are removed in addition to level 1. / 1: f_stat, f_getfree, f_unlink, f_mkdir, f_chmod, f_truncate and f_rename
/ 3: f_lseek is removed in addition to level 2. */ / 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 _FS_TINY 1
/* 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 _DRIVES 1
/* Number of volumes (logical drives) to be used. */
#define _USE_STRFUNC 0 #define _USE_STRFUNC 0
/* To enable string functions, set _USE_STRFUNC to 1 or 2. */ /* 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 #define _USE_MKFS 0
/* Number of logical drives to be used. This affects the size of internal table. */ /* To enable f_mkfs function, set _USE_MKFS to 1 and set _FS_READONLY to 0 */
#define _USE_FORWARD 0
/* To enable f_forward function, set _USE_FORWARD to 1 and set _FS_TINY to 1. */
#define _USE_LFN 0
#define _MAX_LFN 255 /* Maximum LFN length to handle (max:255) */
/* The _USE_LFN option switches the LFN support.
/
/ 0: Disable LFN.
/ 1: Enable LFN with static working buffer on the bss. Not re-entrant.
/ 2: Enable LFN with dynamic working buffer on the caller's 'stack'.
/
/ The working buffer occupies (_MAX_LFN + 1) * 2 bytes. When enable LFN,
/ a Unicode - OEM code conversion function ff_convert() must be linked. */
#define _CODE_PAGE 437
/* The _CODE_PAGE specifies the OEM code page to be used on the target system.
/ When it is non LFN configuration, there is no difference between SBCS code
/ pages. When LFN is enabled, the code page must always be set correctly.
/ 437 - U.S.
/ 720 - Arabic
/ 737 - Greek
/ 775 - Baltic
/ 850 - Multilingual Latin 1
/ 852 - Latin 2
/ 855 - Cyrillic
/ 857 - Turkish
/ 858 - Multilingual Latin 1 + Euro
/ 862 - Hebrew
/ 866 - Russian
/ 874 - Thai
/ 932 - Japanese Shift-JIS (DBCS)
/ 936 - Simplified Chinese GBK (DBCS)
/ 949 - Korean (DBCS)
/ 950 - Traditional Chinese Big5 (DBCS)
/ 1258 - Vietnam
*/
#define _MULTI_PARTITION 0 #define _MULTI_PARTITION 0
/* When _MULTI_PARTITION is set to 0, each logical drive is bound to same /* When _MULTI_PARTITION is set to 0, each volume is bound to same physical
/ physical drive number and can mount only 1st primaly partition. When it is / drive number and can mount only 1st primaly partition. When it is set to 1,
/ set to 1, each logical drive can mount a partition listed in Drives[]. */ / each volume is tied to the 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" #define _FS_REENTRANT 0
#define _TIMEOUT 1000
/* To make the FatFs module re-entrant, set 1 and re-write platform dependent
/ lock out code that defined arownd _FS_REENTRANT. The _TIMEOUT defines the
/ time out value in unit of milliseconds on the multi access exclusion. */
#define _EXCLUDE_LIB 0
/* When _EXCLUDE_LIB is set to 1, FatFs module does not use standard library. */
/* Definitions corresponds to multiple sector size (not tested) */ /* End of configuration options. Do not change followings without care. */
#define S_MAX_SIZ 512U /* Do not change */ /*--------------------------------------------------------------------------*/
#if S_MAX_SIZ > 512U
/* Definitions corresponds to multiple sector size (Not tested) */
#define MAX_SS 512U /* Do not change */
#if MAX_SS > 512U
#define SS(fs) ((fs)->s_size) #define SS(fs) ((fs)->s_size)
#else #else
#define SS(fs) 512U #define SS(fs) 512U
#endif #endif
/* File system object structure */ /* File system object structure */
typedef struct _FATFS { typedef struct _FATFS {
WORD id; /* File system mount ID */ BYTE fs_type; /* FAT sub type */
WORD n_rootdir; /* Number of root directory entries */ BYTE drive; /* Physical drive number */
DWORD winsect; /* Current sector appearing in the win[] */ BYTE csize; /* Number of sectors per cluster */
DWORD sects_fat; /* Sectors per fat */ BYTE n_fats; /* Number of FAT copies */
DWORD max_clust; /* Maximum cluster# + 1 */ BYTE wflag; /* win[] dirty flag (1:must be written back) */
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 pad1;
BYTE win[S_MAX_SIZ]; /* Disk access window for Directory/FAT */ WORD id; /* File system mount ID */
WORD n_rootdir; /* Number of root directory entries (0 on FAT32) */
#if _FS_REENTRANT
HANDLE h_mutex; /* Handle to the mutex (Platform dependent) */
#endif
#if MAX_SS > 512U
WORD s_size; /* Sector size */
#endif
#if !_FS_READONLY
BYTE fsi_flag; /* fsinfo dirty flag (1:must be written back) */
BYTE pad2;
DWORD last_clust; /* Last allocated cluster */
DWORD free_clust; /* Number of free clusters */
DWORD fsi_sector; /* fsinfo sector */
#endif
DWORD sects_fat; /* Sectors per fat */
DWORD max_clust; /* Maximum cluster# + 1. Number of clusters is max_clust - 2 */
DWORD fatbase; /* FAT start sector */
DWORD dirbase; /* Root directory start sector (Cluster# on FAT32) */
DWORD database; /* Data start sector */
DWORD winsect; /* Current sector appearing in the win[] */
BYTE win[MAX_SS];/* Disk access window for Directory/FAT */
} FATFS; } FATFS;
/* Directory object structure */ /* Directory object structure */
typedef struct _DIR { typedef struct _DIR {
WORD id; /* Owner file system mount ID */ WORD id; /* Owner file system mount ID */
WORD index; /* Current index */ WORD index; /* Current index number */
FATFS* fs; /* Pointer to the owner file system object */ FATFS* fs; /* Pointer to the owner file system object */
DWORD sclust; /* Start cluster */ DWORD sclust; /* Table start cluster (0:Static table) */
DWORD clust; /* Current cluster */ DWORD clust; /* Current cluster */
DWORD sect; /* Current sector */ 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 (0xFFFF:No LFN) */
#endif
} DIR; } DIR;
/* File object structure */ /* File object structure */
typedef struct _FIL { typedef struct _FIL {
WORD id; /* Owner file system mount ID */ FATFS* fs; /* Pointer to the owner file system object */
BYTE flag; /* File status flags */ WORD id; /* Owner file system mount ID */
BYTE csect; /* Sector address in the cluster */ BYTE flag; /* File status flags */
FATFS* fs; /* Pointer to the owner file system object */ BYTE csect; /* Sector address in the cluster */
DWORD fptr; /* File R/W pointer */ DWORD fptr; /* File R/W pointer */
DWORD fsize; /* File size */ DWORD fsize; /* File size */
DWORD org_clust; /* File start cluster */ DWORD org_clust; /* File start cluster */
DWORD curr_clust; /* Current cluster */ DWORD curr_clust; /* Current cluster */
DWORD curr_sect; /* Current sector */ DWORD dsect; /* Current data sector */
#if _FS_READONLY == 0 #if _FS_READONLY == 0
DWORD dir_sect; /* Sector containing the directory entry */ DWORD dir_sect; /* Sector containing the directory entry */
BYTE* dir_ptr; /* Ponter to the directory entry in the window */ BYTE* dir_ptr; /* Ponter to the directory entry in the window */
#endif
#if !_FS_TINY
BYTE buf[MAX_SS];/* File R/W buffer */
#endif #endif
BYTE buffer[S_MAX_SIZ]; /* File R/W buffer */
} FIL; } FIL;
/* File status structure */ /* File status structure */
typedef struct _FILINFO { typedef struct _FILINFO {
DWORD fsize; /* Size */ DWORD fsize; /* File size */
WORD fdate; /* Date */ WORD fdate; /* Last modified date */
WORD ftime; /* Time */ WORD ftime; /* Last modified time */
BYTE fattrib; /* Attribute */ BYTE fattrib; /* Attribute */
char fname[8+1+3+1]; /* Name (8.3 format) */ char fname[13]; /* Short file name (8.3 format) */
#if _USE_LFN
char *lfname; /* Pointer to the LFN buffer */
int lfsize; /* Size of LFN buffer [bytes] */
#endif
} FILINFO; } FILINFO;
/* DBCS code ranges */
#if _CODE_PAGE == 932 /* CP932 (Japanese Shift-JIS) */
#define _DF1S 0x81 /* DBC 1st byte range 1 start */
#define _DF1E 0x9F /* DBC 1st byte range 1 end */
#define _DF2S 0xE0 /* DBC 1st byte range 2 start */
#define _DF2E 0xFC /* DBC 1st byte range 2 end */
#define _DS1S 0x40 /* DBC 2nd byte range 1 start */
#define _DS1E 0x7E /* DBC 2nd byte range 1 end */
#define _DS2S 0x80 /* DBC 2nd byte range 2 start */
#define _DS2E 0xFC /* DBC 2nd byte range 2 end */
#elif _CODE_PAGE == 936 /* CP936 (Simplified Chinese GBK) */
#define _DF1S 0x81
#define _DF1E 0xFE
#define _DS1S 0x40
#define _DS1E 0x7E
#define _DS2S 0x80
#define _DS2E 0xFE
#elif _CODE_PAGE == 949 /* CP949 (Korean) */
#define _DF1S 0x81
#define _DF1E 0xFE
#define _DS1S 0x41
#define _DS1E 0x5A
#define _DS2S 0x61
#define _DS2E 0x7A
#define _DS3S 0x81
#define _DS3E 0xFE
#elif _CODE_PAGE == 950 /* CP950 (Traditional Chinese Big5) */
#define _DF1S 0x81
#define _DF1E 0xFE
#define _DS1S 0x40
#define _DS1E 0x7E
#define _DS2S 0xA1
#define _DS2E 0xFE
#else /* SBCS code pages */
#define _DF1S 0
#endif
/* Character code support macros */
#define IsUpper(c) (((c)>='A')&&((c)<='Z'))
#define IsLower(c) (((c)>='a')&&((c)<='z'))
#define IsDigit(c) (((c)>='0')&&((c)<='9'))
#if _DF1S /* DBCS configuration */
#if _DF2S /* Two 1st byte areas */
#define IsDBCS1(c) (((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) || ((BYTE)(c) >= _DF2S && (BYTE)(c) <= _DF2E))
#else /* One 1st byte area */
#define IsDBCS1(c) ((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E)
#endif
#if _DS3S /* Three 2nd byte areas */
#define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E) || ((BYTE)(c) >= _DS3S && (BYTE)(c) <= _DS3E))
#else /* Two 2nd byte areas */
#define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E))
#endif
#else /* SBCS configuration */
#define IsDBCS1(c) 0
#define IsDBCS2(c) 0
#endif /* _DF1S */
/* Definitions corresponds to multi partition */ /* Definitions corresponds to multi partition */
#if _MULTI_PARTITION != 0 /* Multiple partition cfg */ #if _MULTI_PARTITION /* Multiple partition configuration */
typedef struct _PARTITION { typedef struct _PARTITION {
BYTE pd; /* Physical drive # (0-255) */ BYTE pd; /* Physical drive# */
BYTE pt; /* Partition # (0-3) */ BYTE pt; /* Partition # (0-3) */
} PARTITION; } PARTITION;
extern extern
const PARTITION Drives[]; /* Logical drive# to physical location conversion table */ const PARTITION Drives[]; /* Logical drive# to physical location conversion table */
#define LD2PD(drv) (Drives[drv].pd) /* Get physical drive# */ #define LD2PD(drv) (Drives[drv].pd) /* Get physical drive# */
#define LD2PT(drv) (Drives[drv].pt) /* Get partition# */ #define LD2PT(drv) (Drives[drv].pt) /* Get partition# */
#else /* Single partition cfg */ #else /* Single partition configuration */
#define LD2PD(drv) (drv) /* Physical drive# is equal to logical drive# */ #define LD2PD(drv) (drv) /* Physical drive# is equal to the logical drive# */
#define LD2PT(drv) 0 /* Always mounts the 1st partition */ #define LD2PT(drv) 0 /* Always mounts the 1st partition */
#endif #endif
/* File function return code (FRESULT) */ /* File function return code (FRESULT) */
typedef enum { typedef enum {
FR_OK = 0, /* 0 */ FR_OK = 0, /* 0 */
FR_NOT_READY, /* 1 */ FR_DISK_ERR, /* 1 */
FR_NO_FILE, /* 2 */ FR_INT_ERR, /* 2 */
FR_NO_PATH, /* 3 */ FR_NOT_READY, /* 3 */
FR_INVALID_NAME, /* 4 */ FR_NO_FILE, /* 4 */
FR_INVALID_DRIVE, /* 5 */ FR_NO_PATH, /* 5 */
FR_DENIED, /* 6 */ FR_INVALID_NAME, /* 6 */
FR_EXIST, /* 7 */ FR_DENIED, /* 7 */
FR_RW_ERROR, /* 8 */ FR_EXIST, /* 8 */
FR_WRITE_PROTECTED, /* 9 */ FR_INVALID_OBJECT, /* 9 */
FR_NOT_ENABLED, /* 10 */ FR_WRITE_PROTECTED, /* 10 */
FR_NO_FILESYSTEM, /* 11 */ FR_INVALID_DRIVE, /* 11 */
FR_INVALID_OBJECT, /* 12 */ FR_NOT_ENABLED, /* 12 */
FR_MKFS_ABORTED /* 13 */ FR_NO_FILESYSTEM, /* 13 */
FR_MKFS_ABORTED, /* 14 */
FR_TIMEOUT /* 15 */
} FRESULT; } FRESULT;
/*-----------------------------------------------------*/ /*--------------------------------------------------------------*/
/* FatFs module application interface */ /* FatFs module application interface */
FRESULT f_mount (BYTE, FATFS*); /* Mount/Unmount a logical drive */ FRESULT f_mount (BYTE, FATFS*); /* Mount/Unmount a logical drive */
FRESULT f_open (FIL*, const char*, BYTE); /* Open or create a file */ FRESULT f_open (FIL*, const char*, BYTE); /* Open or create a file */
@ -208,24 +377,43 @@ FRESULT f_truncate (FIL*); /* Truncate file */
FRESULT f_sync (FIL*); /* Flush cached data of a writing 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_unlink (const char*); /* Delete an existing file or directory */
FRESULT f_mkdir (const char*); /* Create a new directory */ FRESULT f_mkdir (const char*); /* Create a new directory */
FRESULT f_chmod (const char*, BYTE, BYTE); /* Change file/dir attriburte */ FRESULT f_chmod (const char*, BYTE, BYTE); /* Change attriburte of the file/dir */
FRESULT f_utime (const char*, const FILINFO*); /* Change file/dir timestamp */ FRESULT f_utime (const char*, const FILINFO*); /* Change timestamp of the file/dir */
FRESULT f_rename (const char*, const char*); /* Rename/Move a file or directory */ FRESULT f_rename (const char*, const char*); /* Rename/Move a file or directory */
FRESULT f_forward (FIL*, UINT(*)(const BYTE*,UINT), UINT, UINT*); /* Forward data to the stream */
FRESULT f_mkfs (BYTE, BYTE, WORD); /* Create a file system on the drive */ FRESULT f_mkfs (BYTE, BYTE, WORD); /* Create a file system on the drive */
#if _USE_STRFUNC #if _USE_STRFUNC
#define feof(fp) ((fp)->fptr == (fp)->fsize) int f_putc (int, FIL*); /* Put a character to the file */
int f_puts (const char*, FIL*); /* Put a string to the file */
int f_printf (FIL*, const char*, ...); /* Put a formatted string to the file */
char* f_gets (char*, 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)
#ifndef EOF
#define EOF -1 #define EOF -1
int fputc (int, FIL*); /* Put a character to the file */ #endif
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 #endif
/* User defined function to give a current time to fatfs module */
/*--------------------------------------------------------------*/
/* User defined functions */
/* Real time clock */
DWORD get_fattime (void); /* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */ 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) */ /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
/* Unicode - OEM code conversion */
#if _USE_LFN
WCHAR ff_convert (WCHAR, UINT);
#endif
/*--------------------------------------------------------------*/
/* Flags and offset address */
/* File access control and file status flags (FIL.flag) */ /* File access control and file status flags (FIL.flag) */
@ -259,10 +447,12 @@ DWORD get_fattime (void); /* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20
#define AM_LFN 0x0F /* LFN entry */ #define AM_LFN 0x0F /* LFN entry */
#define AM_DIR 0x10 /* Directory */ #define AM_DIR 0x10 /* Directory */
#define AM_ARC 0x20 /* Archive */ #define AM_ARC 0x20 /* Archive */
#define AM_MASK 0x3F /* Mask of defined bits */
/* FatFs refers the members in the FAT structures with byte offset instead
/* Offset of FAT structure members */ / of structure member because there are incompatibility of the packing option
/ between various compilers. */
#define BS_jmpBoot 0 #define BS_jmpBoot 0
#define BS_OEMName 3 #define BS_OEMName 3
@ -315,25 +505,28 @@ DWORD get_fattime (void); /* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20
#define DIR_WrtDate 24 #define DIR_WrtDate 24
#define DIR_FstClusLO 26 #define DIR_FstClusLO 26
#define DIR_FileSize 28 #define DIR_FileSize 28
#define LDIR_Ord 0
#define LDIR_Attr 11
#define LDIR_Type 12
#define LDIR_Chksum 13
#define LDIR_FstClusLO 26
/*--------------------------------*/
/* Multi-byte word access macros */ /* Multi-byte word access macros */
#if _MCU_ENDIAN == 1 /* Use word access */ #if _WORD_ACCESS == 1 /* Enable word access to the FAT structure */
#define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr)) #define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr))
#define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr)) #define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr))
#define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val) #define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val)
#define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val) #define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val)
#elif _MCU_ENDIAN == 2 /* Use byte-by-byte access */ #else /* Use byte-by-byte access to the FAT structure */
#define LD_WORD(ptr) (WORD)(((WORD)*(volatile BYTE*)((ptr)+1)<<8)|(WORD)*(volatile BYTE*)(ptr)) #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 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_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) #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 #endif
#define _FATFS
#endif /* _FATFS */ #endif /* _FATFS */

View File

@ -4,6 +4,10 @@
#ifndef _INTEGER #ifndef _INTEGER
#if 0
#include <windows.h>
#else
/* These types must be 16-bit, 32-bit or larger integer */ /* These types must be 16-bit, 32-bit or larger integer */
typedef int INT; typedef int INT;
typedef unsigned int UINT; typedef unsigned int UINT;
@ -17,6 +21,7 @@ typedef unsigned char BYTE;
typedef short SHORT; typedef short SHORT;
typedef unsigned short USHORT; typedef unsigned short USHORT;
typedef unsigned short WORD; typedef unsigned short WORD;
typedef unsigned short WCHAR;
/* These types must be 32-bit integer */ /* These types must be 32-bit integer */
typedef long LONG; typedef long LONG;
@ -26,5 +31,7 @@ typedef unsigned long DWORD;
/* Boolean type */ /* Boolean type */
typedef enum { FALSE = 0, TRUE } BOOL; typedef enum { FALSE = 0, TRUE } BOOL;
#endif
#define _INTEGER #define _INTEGER
#endif #endif

208
lcd.c
View File

@ -1,208 +0,0 @@
#include "types.h"
#include "lcd.h"
#include "start.h"
#include "vsprintf.h"
#include "string.h"
#include "utils.h"
#include "hollywood.h"
void lcdinit(void);
char ddram[0x80];
u8 ddaddr = 0;
u32 chardelay = 0;
#define WIDTH 16
#define HEIGHT 2
u8 linestarts[HEIGHT] = { 0x00, 0x40 };
u8 cx, cy;
// for 4x20
//u8 linestarts[HEIGHT] = { 0x00, 0x40, 0x14, 0x54 };
#define LCD_HOME_CLEAR 0x01
#define LCD_HOME 0x02
#define LCD_ENTRY_MODE 0x04
# define EM_INC 0x02
# define EM_DEC 0x00
# define EM_SHIFT 0x01
#define LCD_DISPLAY_ONOFF 0x08
# define D_DISPLAY 0x04
# define D_CURSOR 0x02
# define D_BLINK 0x01
#define LCD_SHIFT 0x10
# define SH_SHIFT 0x08
# define SH_CURSOR 0x00
# define SH_RIGHT 0x04
# define SH_LEFT 0x00
#define LCD_FUNCTION_SET 0x20
# define FS_8BIT 0x10
# define FS_4BIT 0x00
# define FS_2LINE 0x08
# define FS_1LINE 0x00
# define FS_5X10 0x04
# define FS_5X8 0x00
#define LCD_CGRAM 0x40
#define LCD_DDRAM 0x80
#define LCD_PORT HW_GPIO1OUT
#define LCD_MASK 0x00FC0000
#define LCD_E 0x00040000
#define LCD_RS 0x00080000
#define LCD_DSHIFT 20
static void _lcdnybble(char rs, char n)
{
if(rs)
mask32(LCD_PORT, LCD_MASK, ((n&0xF) << LCD_DSHIFT) | LCD_RS);
else
mask32(LCD_PORT, LCD_MASK, ((n&0xF) << LCD_DSHIFT));
udelay(4);
set32(LCD_PORT, LCD_E);
udelay(4);
clear32(LCD_PORT, LCD_E);
udelay(4);
}
static void _lcdcmd(char c)
{
_lcdnybble(0, c>>4);
_lcdnybble(0, c&0x0F);
udelay(50);
}
static void _lcdwrite(char c)
{
_lcdnybble(1, c>>4);
_lcdnybble(1, c&0x0F);
ddram[ddaddr] = c;
ddaddr = (ddaddr+1)&0x7F;
udelay(50);
}
static void _lcdgoto(u8 addr)
{
_lcdcmd(LCD_DDRAM | addr);
ddaddr = addr;
}
static void _lcdgotoxy(u8 x, u8 y)
{
u8 addr = (x + linestarts[y]);
_lcdcmd(LCD_DDRAM | addr);
ddaddr = addr;
}
static void _lcdclear(void)
{
int i;
_lcdcmd(LCD_HOME_CLEAR);
udelay(2000);
for(i=0;i<0x80;i++) {
ddram[i] = ' ';
}
}
static void _lcdscroll(u8 count)
{
int x,y;
_lcdcmd(LCD_HOME_CLEAR);
udelay(2000);
for(y=0;y<(HEIGHT-count);y++) {
_lcdgotoxy(0,y);
for(x=0;x<WIDTH;x++) {
_lcdwrite(ddram[x + linestarts[y+count]]);
}
}
for(;y<HEIGHT;y++) {
for(x=0;x<WIDTH;x++) {
ddram[linestarts[y] + x] = ' ';
}
}
_lcdgoto(linestarts[HEIGHT-1]);
}
void lcd_init(void)
{
cx = cy = 0;
ddaddr = 0;
memset(ddram, ' ', 0x80);
clear32(LCD_PORT, LCD_MASK);
udelay(100000);
_lcdnybble(0, (LCD_FUNCTION_SET | FS_8BIT)>>4);
udelay(10000);
_lcdnybble(0, (LCD_FUNCTION_SET | FS_8BIT)>>4);
udelay(1000);
_lcdnybble(0, (LCD_FUNCTION_SET | FS_8BIT)>>4);
udelay(1000);
_lcdnybble(0, (LCD_FUNCTION_SET | FS_4BIT)>>4);
udelay(1000);
_lcdcmd(LCD_FUNCTION_SET | FS_4BIT | FS_2LINE | FS_5X8);
_lcdcmd(LCD_DISPLAY_ONOFF);
_lcdcmd(LCD_HOME_CLEAR);
udelay(2000);
_lcdcmd(LCD_ENTRY_MODE | EM_INC);
_lcdcmd(LCD_DISPLAY_ONOFF | D_DISPLAY | D_CURSOR);
}
int lcd_putchar(int ic)
{
char c = ic;
// defer newlines to keep last line of LCD full
if(c == '\n') {
cx = 0;
cy++;
if(cy < HEIGHT)
_lcdgotoxy(cx,cy);
} else {
if(cx >= 16) {
cx = 0;
cy++;
if(cy < HEIGHT)
_lcdgotoxy(cx,cy);
}
if(cy >= (2*HEIGHT-1)) {
_lcdclear();
_lcdgoto(linestarts[HEIGHT-1]);
cy = HEIGHT - 1;
cx = 0;
} else if(cy >= HEIGHT) {
_lcdscroll(cy - HEIGHT + 1);
cy = HEIGHT - 1;
cx = 0;
}
_lcdwrite(c);
cx++;
}
return (u8)c;
}
void lcd_setdelay(u32 delay)
{
chardelay = delay;
}
int lcd_puts(const char *s)
{
while(*s) {
lcd_putchar(*s++);
udelay(chardelay);
}
return 0;
}
int lcd_printf( const char *fmt, ...)
{
va_list args;
char buffer[1024];
int i;
va_start(args, fmt);
i = vsprintf(buffer, fmt, args);
va_end(args);
lcd_puts(buffer);
return i;
}

11
lcd.h
View File

@ -1,11 +0,0 @@
#ifndef __LCD_H__
#define __LCD_H__
#include "types.h"
void lcd_init(void);
int lcd_putchar(int c);
int lcd_puts(const char *s);
void lcd_setdelay(u32 delay);
int lcd_printf( const char *fmt, ...);
#endif