📄 main.c
字号:
/*
**********************************************************************************************
* Project: FS7805 SD CARD READER
* File: main.c
* Contents:
* The main function is SD and MMC card reader.
*
* $Date: 12/15/06 Derek V1.0
*
* This is the SD/MMC card reader firmware.
* When read sector,the 512 pingpong FIFO mechanism
* is used to improve performance.But when write sector,
* only single FIFO is used.
*
* $Date: 12/27/06 Derek V1.1
*
* The 512 pingpong FIFO mechanism is also used when
* writting sectors.
*
* $Date: 01/09/07 Derek V1.11
*
* Fix read sector error bug .
* Don't set FIFO_FULL bit in SetBulkInFull() function.
* Setting it in BulkSetDmaIn() function.
*
* $Date: 01/18/07 Derek V1.11
*
* Ignor the OUT_OF_RANGE error in SdSendCmd() when
* multi read the last block.
*
* $Date: 03/29/07 Derek V1.2
*
* Add TDES module for data encrypt and decrypt.
* if Macro TDES>0(define in storage.h) this firmware will encrypt/decrypt data when write/read SD Card
*
* $Date: 07/03/07 Derek V1.21
*
* Support HC SD 2.0 card.
* Add CMD8,CMD6,different read/write command parameter:
* for standard capacity card,unit in data address.
* for high capacity card,unit in block.
* SdHcFlag is used to identify HC SD and other card.
*
* $Date: 07/27/07 Derek V1.21
* Fix ILLEGALE_COMMAND bug for some MMC card(PQI MMC,e.g),
* see sd.c->SdSendCmd() function.
*
* $Date: 08/16/07 Derek V1.22
* Add support for SCSI_READ_BUF command.
* Fix HC SD recognization bug.
* Card support CMD8(SEND_IF_COND) may be standard V2.00 card or HC SD card.
* Now we confirm the HC SD from CSD register value.And we get the SD_SPEC
* version from SCR register(V1.00,V1.10 or V2.00).For V1.10 for higher,
* send CMD6 command to switch to high speed mode.
*
* $Date: 02/28/08 Lshi V2.0
* delete USB related function
* add tiny fatfs,read_only function
*
* only support FAT12&FAT16,support max 2 layer directory
* could search file by filename through all the directory
*
*
*
*
*
* Copyright (c) 2007 Fameg, Inc. All rights reserved
*
*
***********************************************************************************************
*/
#define ALLOCATE_EXTERN // Declare GLOBAL variables and functions
#include "include\Include.h"
#include "tff.h"
#include "diskio.h"
static bit SdGetFlashInfoFlag = FALSE;
xdata UINT16 DelayTimer = 0;
bit BusResetFlag = false;
bit BusResetFlagCnt = false;
extern INT8U Configuration;
void Timer0() interrupt 1
{
// Restore the counter to 10 ms
//TH0 = 0x9E; //0x9E58,CPU 30M定时10ms
//TL0 = 0x58;
TH0 = 0x3C; // 0x3CB0, CPU 60MHz, 10ms
TL0 = 0xB0;
if (DelayTimer)
DelayTimer --;
if (CmdTimer)
CmdTimer --;
if (SdGetFlashInfoTimer)
{
SdGetFlashInfoTimer --;
if (SdGetFlashInfoTimer == 0)
SdGetFlashInfoFlag = true;
}
}
FATFS fatfsentity;
BYTE Buff[512]; /* Working buffer */
char filename[20];
//search a file through its name
static
FRESULT scan_files (char* path,char* searchname)
{
DIR dirs;
FRESULT res;
int i;
FILINFO finfo;
if ((res = f_opendir(&dirs, path)) == FR_OK) {
i = strlen(path);
while (((res = f_readdir(&dirs, &finfo)) == FR_OK) && finfo.fname[0]) {
if (finfo.fattrib & AM_DIR) {
// acc_dirs++;
*(path+i) = '/';
strcpy(path+i+1, &finfo.fname[0]);
res = scan_files(path);
//*(path+i) = '\0';
if (res != FR_OK) break;
else return res;
} else {
/* if(!strcmp(&finfo.fname[0],"3.txt"))
{
*(path+i) = '/';
strcpy(path+i+1, &finfo.fname[0]);
} */
if(!strcmp(&finfo.fname[0],searchname))
{
*(path+i) = '/';
strcpy(path+i+1, &finfo.fname[0]);
strcpy(filename, path);
res = FR_OK;
return res;
}
}
}
}
return res;
}
void main (void)
{
BYTE res;
FIL file1,file2,file3,file4,file5; /* File object */
// WORD br; // File R/W count
WORD br1,br2; // File R/W count
// DWORD p1;
// DIR dirs;
McuInit();
Timer0Init();
TdInit();
StorageInit();
if(!f_mountdrv())
{
res = f_mkdir("22");
strcpy(filename, "");
if(!scan_files (filename,"1.txt"))
{
res = f_open(&file1, filename, FA_OPEN_EXISTING | FA_READ);
/*
for (;;) {
res = f_read(&file1, Buff, 0x200, &br);
if (res || br == 0) break; // error or eof
} */
}
strcpy(filename, "/22/5.txt");
res = f_open(&file5, filename, FA_OPEN_ALWAYS | FA_READ | FA_WRITE);
if(!scan_files (filename,"2.txt"))
{
res = f_open(&file2, filename, FA_OPEN_ALWAYS | FA_READ | FA_WRITE);
}
for (;;) {
res = f_read(&file1, Buff, sizeof(Buff), &br1);
if (res || br1 == 0) break; /* error or eof */
res = f_write(&file5, Buff, br1, &br2);
// p1 += br2;
if (res || br2 < br1) break; /* error or disk full */
}
/*
strcpy(filename, "");
if(!scan_files (filename,"5.txt"))
{
res = f_open(&file1, filename, FA_OPEN_EXISTING | FA_READ);
for (;;) {
res = f_read(&file1, Buff, 0x200, &br);
if (res || br == 0) break; // error or eof
}
}
strcpy(filename, "");
if(!scan_files (filename,"6.txt"))
{
res = f_open(&file1, filename, FA_OPEN_EXISTING | FA_READ);
for (;;) {
res = f_read(&file1, Buff, 0x200, &br);
if (res || br == 0) break; // error or eof
}
}
res = f_open(&file1, "/1/4.txt", FA_OPEN_EXISTING | FA_READ);
for (;;) {
res = f_read(&file1, Buff, 0x200, &br);
if (res || br == 0) break; // error or eof
} */
/*
res = f_open(&file1, "/1/3.txt", FA_OPEN_EXISTING | FA_READ);
for (;;) {
res = f_read(&file1, Buff, 0x200, &br);
if (res || br == 0) break; // error or eof
} */
// Close all files
f_close(&file1);
f_close(&file2);
f_close(&file3);
f_close(&file4);
f_close(&file5);
// Deactivate FatFs module
//FatFs = null;
}
}
/*$PAGE*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -