001 |
#include "common.h" |
002 |
/*-----------------------------------------------------------------------*/ |
003 |
/* Inidialize a Drive */ |
004 |
005 |
DSTATUS disk_initialize ( |
006 |
BYTE drv /* Physical drive nmuber (0..) */ |
007 |
) |
008 |
{ |
009 |
u8 state; |
010 |
011 |
if (drv) |
012 |
{ |
013 |
return STA_NOINIT; //仅支持磁盘0的操作 |
014 |
} |
015 |
016 |
state = SD_Init(); |
017 |
if (state == STA_NODISK) |
018 |
{ |
019 |
return STA_NODISK; |
020 |
} |
021 |
else if (state != 0) |
022 |
{ |
023 |
return STA_NOINIT; //其他错误:初始化失败 |
024 |
} |
025 |
else |
026 |
{ |
027 |
return 0; //初始化成功 |
028 |
} |
029 |
} |
030 |
031 |
032 |
033 |
/*-----------------------------------------------------------------------*/ |
034 |
/* Return Disk Status */ |
035 |
036 |
DSTATUS disk_status ( |
037 |
BYTE drv /* Physical drive nmuber (0..) */ |
038 |
) |
039 |
{ |
040 |
if (drv) |
041 |
{ |
042 |
return STA_NOINIT; //仅支持磁盘0操作 |
043 |
} |
044 |
045 |
//检查SD卡是否插入 |
046 |
if (!SD_DET()) |
047 |
{ |
048 |
return STA_NODISK; |
049 |
} |
050 |
return 0; |
051 |
} |
052 |
053 |
054 |
055 |
/*-----------------------------------------------------------------------*/ |
056 |
/* Read Sector(s) */ |
057 |
058 |
DRESULT disk_read ( |
059 |
BYTE drv, /* Physical drive nmuber (0..) */ |
060 |
BYTE *buff, /* Data buffer to store read data */ |
061 |
DWORD sector, /* Sector address (LBA) */ |
062 |
BYTE count /* Number of sectors to read (1..255) */ |
063 |
) |
064 |
{ |
065 |
u8 res=0; |
066 |
if (drv || !count) |
067 |
{ |
068 |
return RES_PARERR; //仅支持单磁盘操作,count不能等于0,否则返回参数错误 |
069 |
} |
070 |
if (!SD_DET()) |
071 |
{ |
072 |
return RES_NOTRDY; //没有检测到SD卡,报NOT READY错误 |
073 |
} |
074 |
075 |
|
076 |
|
077 |
if (count==1) //1个sector的读操作 |
078 |
{ |
079 |
res = SD_ReadSingleBlock(sector, buff); |
080 |
} |
081 |
else //多个sector的读操作 |
082 |
{ |
083 |
res = SD_ReadMultiBlock(sector, buff, count); |
084 |
} |
085 |
/* |
086 |
do |
087 |
{ |
088 |
if(SD_ReadSingleBlock(sector, buff)!=0) |
089 |
{ |
090 |
res = 1; |
091 |
break; |
092 |
} |
093 |
buff+=512; |
094 |
}while(--count); |
095 |
*/ |
096 |
//处理返回值,将SPI_SD_driver.c的返回值转成ff.c的返回值 |
097 |
if (res == 0x00) |
098 |
{ |
099 |
return RES_OK; |
100 |
} |
101 |
else |
102 |
{ |
103 |
return RES_ERROR; |
104 |
} |
105 |
} |
106 |
107 |
108 |
109 |
/*-----------------------------------------------------------------------*/ |
110 |
/* Write Sector(s) */ |
111 |
112 |
#if _READONLY == 0 |
113 |
DRESULT disk_write ( |
114 |
BYTE drv, /* Physical drive nmuber (0..) */ |
115 |
const BYTE *buff, /* Data to be written */ |
116 |
DWORD sector, /* Sector address (LBA) */ |
117 |
BYTE count /* Number of sectors to write (1..255) */ |
118 |
) |
119 |
{ |
120 |
u8 res; |
121 |
122 |
if (drv || !count) |
123 |
{ |
124 |
return RES_PARERR; //仅支持单磁盘操作,count不能等于0,否则返回参数错误 |
125 |
} |
126 |
if (!SD_DET()) |
127 |
{ |
128 |
return RES_NOTRDY; //没有检测到SD卡,报NOT READY错误 |
129 |
} |
130 |
131 |
// 读写操作 |
132 |
if (count == 1) |
133 |
{ |
134 |
res = SD_WriteSingleBlock(sector, buff); |
135 |
} |
136 |
else |
137 |
{ |
138 |
res = SD_WriteMultiBlock(sector, buff, count); |
139 |
} |
140 |
// 返回值转换 |
141 |
if (res == 0) |
142 |
{ |
143 |
return RES_OK; |
144 |
} |
145 |
else |
146 |
{ |
147 |
return RES_ERROR; |
148 |
} |
149 |
} |
150 |
#endif /* _READONLY */ |
151 |
152 |
153 |
154 |
/*-----------------------------------------------------------------------*/ |
155 |
/* Miscellaneous Functions */ |
156 |
157 |
DRESULT disk_ioctl ( |
158 |
BYTE drv, /* Physical drive nmuber (0..) */ |
159 |
BYTE ctrl, /* Control code */ |
160 |
void *buff /* Buffer to send/receive control data */ |
161 |
) |
162 |
{ |
163 |
DRESULT res; |
164 |
165 |
166 |
if (drv) |
167 |
{ |
168 |
return RES_PARERR; //仅支持单磁盘操作,否则返回参数错误 |
169 |
} |
170 |
|
171 |
//FATFS目前版本仅需处理CTRL_SYNC,GET_SECTOR_COUNT,GET_BLOCK_SIZ三个命令 |
172 |
switch (ctrl) |
173 |
{ |
174 |
case CTRL_SYNC: |
175 |
SD_CS_ENABLE(); |
176 |
if (SD_WaitReady()==0) |
177 |
{ |
178 |
res = RES_OK; |
179 |
} |
180 |
else |
181 |
{ |
182 |
res = RES_ERROR; |
183 |
} |
184 |
SD_CS_DISABLE(); |
185 |
break ; |
186 |
|
187 |
case GET_BLOCK_SIZE: |
188 |
*( WORD *)buff = 512; |
189 |
res = RES_OK; |
190 |
break ; |
191 |
192 |
case GET_SECTOR_COUNT: |
193 |
*( DWORD *)buff = SD_GetCapacity(); |
194 |
res = RES_OK; |
195 |
break ; |
196 |
default : |
197 |
res = RES_PARERR; |
198 |
break ; |
199 |
} |
200 |
201 |
return res; |
202 |
} |
203 |
204 |
/*-----------------------------------------------------------------------*/ |
205 |
/* User defined function to give a current time to fatfs module */ |
206 |
/* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */ |
207 |
/* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */ |
208 |
DWORD get_fattime ( void ) |
209 |
{ |
210 |
return 0; |
211 |
} |
001 |
#include "common.h" |
002 |
#include <string.h> |
003 |
004 |
FRESULT scan_files ( char * path); |
005 |
006 |
#define F_PUTS 1 //测试向文件写入字符串 |
007 |
#define F_READ 1 //测试从文件中读出数据 |
008 |
#define F_UNLINK 0 //测试删除文件 |
009 |
#define SCAN_FILES 1 //测试目录扫描 |
010 |
011 |
FATFS fs; |
012 |
FRESULT res; |
013 |
FIL file; |
014 |
UINT br; |
015 |
BYTE buffer[4096]; //以上变量作为全局变量 可以避免一些Bug |
016 |
017 |
int main( void ) |
018 |
{ |
019 |
u16 i,n; |
020 |
|
021 |
022 |
//stm32 初始化 |
023 |
RCC_Configuration(); |
024 |
NVIC_Configuration(); |
025 |
USART_Configuration(); |
026 |
SPI_Configuration(); |
027 |
GPIO_Configuration(); |
028 |
029 |
030 |
//fatfs 操作 |
031 |
032 |
f_mount(0, &fs); |
033 |
034 |
//如果data.txt存在,则打开;否则,创建一个新文件 |
035 |
res = f_open(&file, "0:/data.txt" ,FA_OPEN_ALWAYS|FA_READ|FA_WRITE ); |
036 |
037 |
if (res!=FR_OK) |
038 |
{ |
039 |
printf ( "\r\n f_open() fail .. \r\n" ); |
040 |
} else { |
041 |
printf ( "\r\n f_open() success .. \r\n" ); |
042 |
} |
043 |
044 |
#if F_READ |
045 |
046 |
while (1){ //使用f_read读文件 |
047 |
res = f_read(&file, buffer, 1, &br); //一次读一个字节知道读完全部文件信息 |
048 |
049 |
if (res == FR_OK ) |
050 |
{ |
051 |
printf ( "%s" ,buffer); |
052 |
} else { |
053 |
printf ( "\r\n f_read() fail .. \r\n" ); |
054 |
} |
055 |
056 |
if (f_eof(&file)) { break ;} |
057 |
} |
058 |
059 |
/*if( f_gets(buffer,sizeof(buffer),&file) != NULL) //使用f_gets读文件 ,存在 Bugs 待调试 |
060 |
{ |
061 |
printf("%s",buffer); |
062 |
}else{ |
063 |
printf("\r\n f_gets() fail .. \r\n"); |
064 |
} */ |
065 |
066 |
#endif |
067 |
068 |
#if F_PUTS |
069 |
070 |
//将指针指向文件末 |
071 |
//res = f_lseek(&file,(&file)->fsize); |
072 |
res = f_lseek(&file,file.fsize); |
073 |
074 |
n = f_puts( "\r\n hello dog ..\r\n" , &file) ; //向文件末写入字符串 |
075 |
|
076 |
if (n<1) //判断写是否成功 |
077 |
{ |
078 |
printf ( "\r\n f_puts() fail .. \r\n" ); |
079 |
} else { |
080 |
printf ( "\r\n f_puts() success .. \r\n" ); |
081 |
} |
082 |
|
083 |
#endif |
084 |
085 |
#if F_UNLINK |
086 |
087 |
res = f_unlink( "test.jpg" ); //前提SD下存在一个test.jpg |
088 |
089 |
if (res!=FR_OK) |
090 |
{ |
091 |
printf ( "\r\n f_unlink() fail .. \r\n" ); |
092 |
} else { |
093 |
printf ( "\r\n f_unlink() success .. \r\n" ); |
094 |
} |
095 |
096 |
#endif |
097 |
098 |
#if SCAN_FILES |
099 |
100 |
printf ( "\r\n the directory files : \r\n" ); |
101 |
scan_files( "/" ); //扫描根目录 |
102 |
103 |
#endif |
104 |
105 |
f_close(&file); |
106 |
f_mount(0, NULL); |
107 |
108 |
while (1); |
109 |
} |
110 |
111 |
112 |
FRESULT scan_files ( |
113 |
char * path /* Start node to be scanned (also used as work area) */ |
114 |
) |
115 |
{ |
116 |
FRESULT res; |
117 |
FILINFO fno; |
118 |
DIR dir; |
119 |
int i; |
120 |
char *fn; /* This function is assuming non-Unicode cfg. */ |
121 |
#if _USE_LFN |
122 |
static char lfn[_MAX_LFN + 1]; |
123 |
fno.lfname = lfn; |
124 |
fno.lfsize = sizeof lfn; |
125 |
#endif |
126 |
127 |
128 |
res = f_opendir(&dir, path); /* Open the directory */ |
129 |
if (res == FR_OK) { |
130 |
i = strlen (path); |
131 |
for (;;) { |
132 |
res = f_readdir(&dir, &fno); /* Read a directory item */ |
133 |
if (res != FR_OK || fno.fname[0] == 0) break ; /* Break on error or end of dir */ |
134 |
if (fno.fname[0] == '.' ) continue ; /* Ignore dot entry */ |
135 |
#if _USE_LFN |
136 |
fn = *fno.lfname ? fno.lfname : fno.fname; |
137 |
#else |
138 |
fn = fno.fname; |
139 |
#endif |
140 |
if (fno.fattrib & AM_DIR) { /* It is a directory */ |
141 |
sprintf (&path[i], "/%s" , fn); |
142 |
res = scan_files(path); |
143 |
if (res != FR_OK) break ; |
144 |
path[i] = 0; |
145 |
} else { /* It is a file. */ |
146 |
printf ( "\r\n %s/%s \r\n" , path, fn); |
147 |
} |
148 |
} |
149 |
} |
150 |
151 |
return res; |
152 |
} |