为STM32移植FATFS,读取SD卡上FAT12/16/32文件系统
注:以下内容转载自网络,作为资料保存
给stm32移植fatfs文件系统,今天终于取得阶段性胜利。只需要提供这样几个函数即可
DSTATUS disk_initialize (BYTE);
DSTATUS disk_status (BYTE);
DRESULT disk_read (BYTE, BYTE*, DWORD, BYTE);
DRESULT disk_write (BYTE, const BYTE*, DWORD, BYTE); // 如果实现只读的文件系统就不需要了。
DRESULT disk_ioctl (BYTE, BYTE, void*);
移植成功后,可以用如下方式读取SD卡了,实在太方便了,和PC机上编程差不了多少。
unsigned int i;
BYTE buffer[512]; // file copy buffer
FATFS fs; // Work area (file system object) for logical drive
FIL fsrc; // file objects
FRESULT res; // FatFs function common result code
UINT br; // File R/W count
USART1_Puts("Now, I'll read file 'i2c/uart.lst'.\n");
// Register a work area for logical drive 0
f_mount(0, &fs);
// Open source file
res = f_open(&fsrc, "i2c/uart.lst", FA_OPEN_EXISTING | FA_READ);
if (res)
{
USART1_Puts("Can't open i2c/uart.lst for read. :-(\n");
goto exit;
}
for (;;) {
res = f_read(&fsrc, buffer, sizeof(buffer), &br);
if (res || br == 0) break; // error or eof
for( i = 0; i < br; ++i )
USART1_Putc(buffer[i]);
}
f_close(&fsrc);
exit:
// Unregister a work area before discard it
f_mount(0, NULL);