新聞中心

stm32 Fatfs 讀寫(xiě)SD卡

作者: 時(shí)間:2016-11-26 來(lái)源:網(wǎng)絡(luò) 收藏
165
166if(drv)
167{
168returnRES_PARERR;//僅支持單磁盤(pán)操作,否則返回參數(shù)錯(cuò)誤
169}
170
171//FATFS目前版本僅需處理CTRL_SYNC,GET_SECTOR_COUNT,GET_BLOCK_SIZ三個(gè)命令
172switch(ctrl)
173{
174caseCTRL_SYNC:
175SD_CS_ENABLE();
176if(SD_WaitReady()==0)
177{
178res = RES_OK;
179}
180else
181{
182res = RES_ERROR;
183}
184SD_CS_DISABLE();
185break;
186
187caseGET_BLOCK_SIZE:
188*(WORD*)buff = 512;
189res = RES_OK;
190break;
191
192caseGET_SECTOR_COUNT:
193*(DWORD*)buff = SD_GetCapacity();
194res = RES_OK;
195break;
196default:
197res = RES_PARERR;
198break;
199}
200
201returnres;
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) */
208DWORDget_fattime (void)
209{
210return0;
211}
這里的結(jié)構(gòu)函數(shù)為Fatfs提供和SD卡的通信接口。 在 最新版本的Fatfs中還加入了對(duì)中文文件名的支持,需要修改 ffconf.h
#define _CODE_PAGE 936 //- Simplified Chinese GBK (DBCS, OEM, Windows)
同時(shí)應(yīng)該添加 option/cc936.c文件。但是這個(gè)文件有700多K占相當(dāng)大的ROM, 像stm32F103RBT6這種小FLASH的MCU根本不行 ,加入當(dāng)前工程文件中代碼將增加160KB 左右。
配置好Stm32的串口和SPI等IO口設(shè)置后,就可以使用Fatfs做一些文件操作了。
4. Fatfs 文件操作
文件分配表FAT(File AllocationTable)用來(lái)記錄文件所在位置的表格.它對(duì)于硬盤(pán)的使用是非常重要的,假若丟失文件分配表,那么硬盤(pán)上的數(shù)據(jù)就會(huì)因無(wú)法定位而不能使用了。
Fatfs 文件系統(tǒng)減輕了操作SD卡的工作量,調(diào)用其提供的函數(shù)就可以方便的操作文件,讀寫(xiě)刪改等。
這里提供一個(gè)main.c 示例:
001#include "common.h"
002#include
003
004FRESULT scan_files (char* path);
005
006#define F_PUTS 1 //測(cè)試向文件寫(xiě)入字符串
007#define F_READ 1 //測(cè)試從文件中讀出數(shù)據(jù)
008#define F_UNLINK 0 //測(cè)試刪除文件
009#define SCAN_FILES 1 //測(cè)試目錄掃描
010
011FATFS fs;
012FRESULT res;
013FIL file;
014UINTbr;
015BYTEbuffer[4096];//以上變量作為全局變量 可以避免一些Bug
016
017intmain(void)
018{
019u16 i,n;
020
021
022//stm32 初始化
023RCC_Configuration();
024NVIC_Configuration();
025USART_Configuration();
026SPI_Configuration();
027GPIO_Configuration();
028
029
030//fatfs 操作
031
032f_mount(0, &fs);
033
034//如果data.txt存在,則打開(kāi);否則,創(chuàng)建一個(gè)新文件
035res = f_open(&file,"0:/data.txt",FA_OPEN_ALWAYS|FA_READ|FA_WRITE );
036
037if(res!=FR_OK)
038{
039printf("f_open() fail .. ");
040}else{
041printf("f_open() success .. ");
042}
043
044#if F_READ
045
046while(1){//使用f_read讀文件
047res = f_read(&file, buffer, 1, &br);//一次讀一個(gè)字節(jié)知道讀完全部文件信息
048
049if(res == FR_OK )
050{
051printf("%s",buffer);
052}else{
053printf("f_read() fail .. ");
054}
055
056if(f_eof(&file)) {break;}
057}
058
059/*if( f_gets(buffer,sizeof(buffer),&file) != NULL) //使用f_gets讀文件 ,存在 Bugs 待調(diào)試
060{
061printf("%s",buffer);
062}else{
063printf("f_gets() fail .. ");
064} */
065
066#endif
067
068#if F_PUTS
069
070//將指針指向文件末
071//res = f_lseek(&file,(&file)->fsize);
072res = f_lseek(&file,file.fsize);
073
074n = f_puts("hello dog ..", &file) ;//向文件末寫(xiě)入字符串
075
076if(n<1)//判斷寫(xiě)是否成功
077{
078printf("f_puts() fail .. ");
079}else{
080printf("f_puts() success .. ");
081}
082
083#endif
084
085#if F_UNLINK
086
087res = f_unlink("test.jpg");//前提SD下存在一個(gè)test.jpg
088
089if(res!=FR_OK)
090{
091printf("f_unlink() fail .. ");
092}else{
093printf("f_unlink() success .. ");
094}
095
096#endif
097
098#if SCAN_FILES
099
100printf("the directory files : ");
101scan_files("/");//掃描根目錄
102
103#endif
104
105f_close(&file);
106f_mount(0, NULL);
107
108while(1);
109}
110
111
112FRESULT scan_files (
113char* path/* Start node to be scanned (also used as work area) */
114)
115{
116FRESULT res;
117FILINFO fno;
118DIR dir;
119inti;
120char*fn;/* This function is assuming non-Unicode cfg. */
121#if _USE_LFN
122staticcharlfn[_MAX_LFN + 1];
123fno.lfname = lfn;
124fno.lfsize =sizeoflfn;
125#endif
126
127
128res = f_opendir(&dir, path);/* Open the directory */
129if(res == FR_OK) {
130i =strlen(path);
131for(;;) {
132res = f_readdir(&dir, &fno);/* Read a directory item */
133if(res != FR_OK || fno.fname[0] == 0)break;/* Break on error or end of dir */
134if(fno.fname[0] ==.)continue;/* Ignore dot entry */
135#if _USE_LFN
136fn = *fno.lfname ? fno.lfname : fno.fname;
137#else
138fn = fno.fname;
139#endif
140if(fno.fattrib & AM_DIR) {/* It is a directory */
141sprintf(&path[i],"/%s", fn);
142res = scan_files(path);
143if(res != FR_OK)break;
144path[i] = 0;
145}else{/* It is a file. */
146printf("%s/%s ", path, fn);
147}
148}
149}
150
151returnres;
152}
其中 目錄掃描函數(shù) scan_files( char * path) 參數(shù)格式如下:
這里使用到了f_puts()函數(shù),所以必須在ffconf.h 中修改 #define _USE_STRFUNC 1
上一頁(yè) 1 2 下一頁(yè)

關(guān)鍵詞: stm32Fatfs讀寫(xiě)SD

評(píng)論


技術(shù)專區(qū)

關(guān)閉