📄 filesystemmain.c
字号:
/*
* description: FileSystem Main
* Maker : Hiromihci Kondo
* Copyright : (C)2003,SEIKO EPSON Corp. All Rights Reserved.
*/
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <FileSystemMain.h>
#include <DebugTask.h>
#include <fat.h>
//----------------------------------------
// Define declaration
//----------------------------------------
#define FLOW_LV 3
#define MAX_COMMAND_LEN 256 // Size of buffer for API command character string of filesystem
#define TRUE 1
#define DELKEY_CODE 0x7F // Character code of Delete key
#define BSKEY_CODE 0x08 // Character code of Back space key
#define TEST_SUCCESS 0 // filesystem demo program successful
#define TEST_FAILURE -1 // filesystem demo program failure
#define ARG_COUNT 4
#define CMD_SIZE MAX_COMMAND_LEN
#define BUF_SIZE FAT_SECTOR_SIZE
#ifdef TOYA2_C
#define BUF_COUNT 128 // Suitable value temporarily
#else
#define BUF_COUNT 3 // Suitable value temporarily
#endif
//----------------------------------------
// Structure definition
//----------------------------------------
typedef struct tagKEY_INFO {
BOOL bEnable;
BOOL bEnbCmp; // Whether compare it
UCHAR* pData;
USHORT size;
} KEY_INFO, *PKEY_INFO;
//----------------------------------------
// External variable declaration
//----------------------------------------
unsigned char bCommandStr[MAX_COMMAND_LEN * 3];
unsigned long bmsgstrOrg[MAX_COMMAND_LEN/4];
char *bmsgstr = (char *)bmsgstrOrg;
FatFcs_t stFcs[FAT_MAX_FILES];
FatFile_t fpFile[FAT_MAX_FILES];
FatFile_t *pfpFp[FAT_MAX_FILES];
FatSecBuf_t stSecBuf_System[FAT_MAX_SECBUFSYSTEM];
FatSecBuf_t stSecBuf_Data[FAT_MAX_SECBUFDATA];
KEY_INFO KeyInfo; // Variable for FlowGetKey
//----------------------------------------
// Internal variable declaration
//----------------------------------------
static unsigned long dataBuf[(BUF_SIZE * BUF_COUNT)/4];
//----------------------------------------
// Function prototype declaration
//----------------------------------------
static void OpeningMessage( void );
static int StartFileSystem( void );
static int fatCommandLine( void );
static void GetCommandStr( void );
static void EndingMessage( void );
static LONG FileSytemFlowGetStr( UCHAR* pString, USHORT* pDataSize, OS_TMO tmout );
#ifdef WRITETEST
//-----------------------------------------
// Addition for FileWriteTest
//-----------------------------------------
#define DAMERICA_SIZE 4934082
static unsigned long ReadArea[(DAMERICA_SIZE+4)>>2];
unsigned char *pReadFileData=(unsigned char*)ReadArea;
#endif
/*
//=============================================================================
// Function_Name: FileSystemMain
// description : Sample task made by customer
// argument :
// return :
// flag :
// global :
//=============================================================================
*/
void FileSystemMain( void )
{
OpeningMessage();
if (StartFileSystem() != TEST_SUCCESS)
return;
fatCommandLine();
EndingMessage();
}
/************************************************************************
* OpeningMessage
* Type : void
* Ret val : None
* Argument : None
* Function : filesystem opening message
************************************************************************/
void OpeningMessage( void )
{
FlowStrPrint("\n\n\r***************************************************\n\r", FLOW_LV); /* Send message of opening */
FlowStrPrint("S1R72V05 USB HS-HOST FILESYSTEM DEMO PROGRAM \n\r", FLOW_LV); /* Send message of opening */
FlowStrPrint("***************************************************\n\r", FLOW_LV); /* Opening is transmitted */
}
/************************************************************************
* StartFileSystem
* Type : int
* Ret val : FAT_OK = Success, FAT_ERR = Error
* Argument : None
* Function : filesystem Initialization
************************************************************************/
int StartFileSystem( void )
{
int iRetVal;
#ifndef TOYA2_C
*((volatile ULONG *)0xf8000c00) = 0xFFFF; // The CF driver's hardware control
#endif
iRetVal = fatFatInit(stFcs, fpFile, pfpFp, stSecBuf_System, stSecBuf_Data);
if (iRetVal != FAT_OK) {
if (fat_stFatErr.bErrCode == FAT_TARGET_NOTFOUND) {
FlowStrPrint("Target device is not found.\n\r", FLOW_LV);
FlowStrPrint("Please check your device system.\n\r", FLOW_LV);
} else {
FlowStrPrint("Target device initialize error.\n\r", FLOW_LV);
memset(bmsgstr, 0x00, sizeof(bmsgstr));
sprintf((char *)bmsgstr, "Error Code = 0x%02x\n\r", fat_stFatErr.bErrCode);
FlowStrPrint(bmsgstr, FLOW_LV);
}
}
return iRetVal;
}
/************************************************************************
* fatCommandLine
* Type : void
* Ret val : None
* Argument : None
* Function : main filesystem demo program
************************************************************************/
int fatCommandLine( void )
{
unsigned char bCurFilePath[FAT_VFAT_FILE_LEN];
unsigned char arg[ARG_COUNT][CMD_SIZE + 1];
unsigned char *buf;
unsigned int i;
int year, month, day, hour, minute, second;
unsigned long ulFileDate = 0;
unsigned long size, size_cnt, addr;
FatFile_t *fr, *fw, *cmp_fr;
unsigned int iattr = 0x00;
unsigned char bVolume[FAT_FILE_LEN + 1];
unsigned long ulSerNum;
struct diskfree_t st_disk;
double Total_byte = 0x00;
double Avalable_byte = 0x00;
struct find_t st_find;
unsigned int status, iFn;
unsigned long n;
unsigned long row, col;
long seek_size;
int seek_pos;
unsigned long running_cnt, total_running_cnt;
unsigned long cmp_size;
unsigned int running_error;
#ifdef TOYA2_C
char *tp;
#endif
buf = (unsigned char *)dataBuf;
for ( ; ; ) {
// 1. Display of present file path
memset(bCurFilePath, 0x00, sizeof(bCurFilePath));
sprintf((char *)bCurFilePath, "\n\r%c:%s>", fat_bCurDrv + 'A', fat_bLfnCurDir); // If display LFN, it is here
FlowStrPrint((char *)bCurFilePath, FLOW_LV);
memset(buf, 0x00, (BUF_SIZE * BUF_COUNT));
// 2. Get API command character string by the user's key input
GetCommandStr();
memset(arg, 0x00, sizeof(arg));
#ifdef TOYA2_C
tp = strtok((char *)bCommandStr, " \t") ;
i=0;
while( (tp != NULL) && (i<4) )
{
strcpy((char*)arg[i], tp );
tp = strtok((char *)NULL, " \t") ;
i++;
}
#else
strcpy((char *)arg[0], strtok((char *)bCommandStr, " \t"));
for (i = 1; i < ARG_COUNT; i++)
strcpy((char *)arg[i], strtok(NULL, " \t"));
// Deal it for operation of upper strtok() is strange
for (i = 0; i < ARG_COUNT; i++) {
if ((arg[i][0] == 0x06) || (arg[i][0] == 0xEA))
arg[i][0] = 0x00;
}
#endif
////////////////////////////////////////////////////////////////
/////////////Execution of A P I command ///////////////////
////////////////////////////////////////////////////////////////
// (1)Help
if (strcmp((char *)arg[0], "help") == 0) {
//////////////////////////////////////////////////////////////////////////////////////////////
//
//Processing for message display of help (instrument) is implemented here //
//A detailed content refers to filesystem(Northern Kyushu DC version FS33) demonstration //
//program operation manual //
//////////////////////////////////////////////////////////////////////////////////////////////
;
// (2)Set information of file date
} else if (strcmp((char *)arg[0], "date") == 0) {
memset(bmsgstr, 0x00, sizeof(bmsgstr));
sprintf((char *)bmsgstr, "Input file date information to set up.\n\r");
FlowStrPrint(bmsgstr, FLOW_LV);
sprintf((char *)bmsgstr, "year(1980 and more)?: ");
FlowStrPrint(bmsgstr, FLOW_LV);
GetCommandStr();
year = atoi((char *)bCommandStr);
sprintf((char *)bmsgstr, "\n\rmonth(1-12)?: ");
FlowStrPrint(bmsgstr, FLOW_LV);
GetCommandStr();
month = atoi((char *)bCommandStr);
sprintf((char *)bmsgstr, "\n\rday(1-31)?: ");
FlowStrPrint(bmsgstr, FLOW_LV);
GetCommandStr();
day = atoi((char *)bCommandStr);
sprintf((char *)bmsgstr, "\n\rhour(0-23)?: ");
FlowStrPrint(bmsgstr, FLOW_LV);
GetCommandStr();
hour = atoi((char *)bCommandStr);
sprintf((char *)bmsgstr, "\n\rminute(0-59)?: ");
FlowStrPrint(bmsgstr, FLOW_LV);
GetCommandStr();
minute = atoi((char *)bCommandStr);
sprintf((char *)bmsgstr, "\n\rsecond(0-59)? (Notes: 2 second unit): ");
FlowStrPrint(bmsgstr, FLOW_LV);
GetCommandStr();
second = atoi((char *)bCommandStr);
sprintf((char *)bmsgstr, "\n\rYour file date information to set up is as follows.\n\r");
FlowStrPrint(bmsgstr, FLOW_LV);
sprintf((char *)bmsgstr, "%4d/%02d/%02d %02d:%02d:%02d\n\r", year, month, day, hour, minute, second);
FlowStrPrint(bmsgstr, FLOW_LV);
ulFileDate = (((year - 1980) << 9) | (month << 5) | day);
ulFileDate <<= 16;
ulFileDate |= ((hour << 11) | (minute << 5) | (second / 2));
sprintf((char *)bmsgstr, "\n\rThe field value written in a directory entry is as follows.\n\r");
FlowStrPrint(bmsgstr, FLOW_LV);
sprintf((char *)bmsgstr, "0x%08X\n\r", ulFileDate);
FlowStrPrint(bmsgstr, FLOW_LV);
fatSetDateInfo(ulFileDate);
// (3)Make new file
} else if (strcmp((char *)arg[0], "create") == 0) {
fw = fatFopen((char *)arg[1], "wb");
if (fw == 0) {
memset(bmsgstr, 0x00, sizeof(bmsgstr));
sprintf((char *)bmsgstr, "Error: Cannot open \"%s\".\n\r", arg[1]);
FlowStrPrint(bmsgstr, FLOW_LV);
} else {
size = atol((char *)arg[2]);
size_cnt = 0;
for (addr = 0; addr < size; ) {
buf[size_cnt] = (unsigned char)(addr & 0xff);
size_cnt++;
addr++;
if (size_cnt == BUF_SIZE * BUF_COUNT) {
fatFwrite(buf, 1, BUF_SIZE * BUF_COUNT, fw);
size_cnt = 0;
}
}
fatFwrite(buf, 1, size_cnt, fw);
}
fatFclose(fw);
// (4)File postscript
} else if (strcmp((char *)arg[0], "add") == 0) {
fw = fatFopen((char *)arg[1], "ab");
if (fw == 0) {
memset(bmsgstr, 0x00, sizeof(bmsgstr));
sprintf((char *)bmsgstr, "Error: Cannot open \"%s\".\n\r", arg[1]);
FlowStrPrint(bmsgstr, FLOW_LV);
} else {
size = atol((char *)arg[2]);
size_cnt = 0;
for (addr = 0; addr < size; ) {
buf[size_cnt] = (unsigned char)(addr & 0xff);
size_cnt++;
addr++;
if (size_cnt == BUF_SIZE * BUF_COUNT) {
fatFwrite(buf, 1, BUF_SIZE * BUF_COUNT, fw);
size_cnt = 0;
}
}
fatFwrite(buf, 1, size_cnt, fw);
}
fatFclose(fw);
// (5)Delete file
} else if (strcmp((char *)arg[0], "del") == 0) {
if (fatRemove((char *)arg[1], (unsigned long)NULL) != TEST_SUCCESS) {
memset(bmsgstr, 0x00, sizeof(bmsgstr));
sprintf((char *)bmsgstr, "Error: Cannot delete \"%s\".\n\r", arg[1]);
FlowStrPrint(bmsgstr, FLOW_LV);
}
// (6)File copy(normal operation)
} else if (strcmp((char *)arg[0], "copy") == 0) {
fr = fatFopen((char *)arg[1], "rb");
if (fr == 0) {
memset(bmsgstr, 0x00, sizeof(bmsgstr));
sprintf((char *)bmsgstr, "Error: Cannot open \"%s\".\n\r", arg[1]);
FlowStrPrint(bmsgstr, FLOW_LV);
} else {
// File name needs to be specified in second argument (receiving side) for specification of the command
// It is also OK to miss it for MS-DOS(The file name is the same )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -