From 13222fb54985089149c2b2161d30287253a509e9 Mon Sep 17 00:00:00 2001 From: batt Date: Tue, 6 Jul 2010 15:58:29 +0000 Subject: [PATCH] Add generic KBlock disk interface for FatFs module. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@4000 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/fs/fatfs/diskio.c | 177 ++++++++++++++------------------------- bertos/fs/fatfs/diskio.h | 4 +- 2 files changed, 67 insertions(+), 114 deletions(-) diff --git a/bertos/fs/fatfs/diskio.c b/bertos/fs/fatfs/diskio.c index 756a45a9..2fd9a6c7 100644 --- a/bertos/fs/fatfs/diskio.c +++ b/bertos/fs/fatfs/diskio.c @@ -6,14 +6,22 @@ /*-----------------------------------------------------------------------*/ #include "diskio.h" +#include "ff.h" -/*-----------------------------------------------------------------------*/ -/* Correspondence between physical drive number and physical drive. */ +#include -#define ATA 0 -#define MMC 1 -#define USB 2 +#include "cfg/cfg_fat.h" +#define LOG_LEVEL FAT_LOG_LEVEL +#define LOG_FORMAT FAT_LOG_FORMAT +#include +static KBlock *devs[_DRIVES]; + +void disk_assignDrive(KBlock *dev, int dev_num) +{ + ASSERT(dev_num < _DRIVES); + devs[dev_num] = dev; +} /*-----------------------------------------------------------------------*/ @@ -23,29 +31,7 @@ DSTATUS disk_initialize ( BYTE drv /* Physical drive nmuber (0..) */ ) { - DSTATUS stat; - int result; - - switch (drv) { - case ATA : - result = ATA_disk_initialize(); - // translate the reslut code here - - return stat; - - case MMC : - result = MMC_disk_initialize(); - // translate the reslut code here - - return stat; - - case USB : - result = USB_disk_initialize(); - // translate the reslut code here - - return stat; - } - return STA_NOINIT; + return disk_status(drv); } @@ -57,29 +43,13 @@ DSTATUS disk_status ( BYTE drv /* Physical drive nmuber (0..) */ ) { - DSTATUS stat; - int result; - - switch (drv) { - case ATA : - result = ATA_disk_status(); - // translate the reslut code here - - return stat; + KBlock *dev = devs[drv]; + ASSERT(dev); - case MMC : - result = MMC_disk_status(); - // translate the reslut code here - - return stat; - - case USB : - result = USB_disk_status(); - // translate the reslut code here - - return stat; - } - return STA_NOINIT; + if (kblock_error(dev) != 0) + return STA_NOINIT; + else + return RES_OK; } @@ -94,29 +64,17 @@ DRESULT disk_read ( BYTE count /* Number of sectors to read (1..255) */ ) { - DRESULT res; - int result; + KBlock *dev = devs[drv]; + ASSERT(dev); - switch (drv) { - case ATA : - result = ATA_disk_read(buff, sector, count); - // translate the reslut code here - return res; - - case MMC : - result = MMC_disk_read(buff, sector, count); - // translate the reslut code here - - return res; - - case USB : - result = USB_disk_read(buff, sector, count); - // translate the reslut code here - - return res; + while (count--) + { + if (kblock_read(dev, sector++, buff, 0, dev->blk_size) != dev->blk_size) + return RES_ERROR; + buff += dev->blk_size; } - return RES_PARERR; + return RES_OK; } @@ -132,29 +90,16 @@ DRESULT disk_write ( BYTE count /* Number of sectors to write (1..255) */ ) { - DRESULT res; - int result; - - switch (drv) { - case ATA : - result = ATA_disk_write(buff, sector, count); - // translate the reslut code here - - return res; - - case MMC : - result = MMC_disk_write(buff, sector, count); - // translate the reslut code here - - return res; - - case USB : - result = USB_disk_write(buff, sector, count); - // translate the reslut code here - - return res; + KBlock *dev = devs[drv]; + ASSERT(dev); + + while (count--) + { + if (kblock_write(dev, sector++, buff, 0, dev->blk_size) != dev->blk_size) + return RES_ERROR; + buff += dev->blk_size; } - return RES_PARERR; + return RES_OK; } #endif /* _READONLY */ @@ -169,34 +114,40 @@ DRESULT disk_ioctl ( void *buff /* Buffer to send/receive control data */ ) { - DRESULT res; - int result; + KBlock *dev = devs[drv]; + ASSERT(dev); - switch (drv) { - case ATA : - // pre-process here - result = ATA_disk_ioctl(ctrl, buff); - // post-process here + switch (ctrl) + { + case CTRL_SYNC: + if (kblock_flush(dev) == 0) + return RES_OK; + else + return RES_ERROR; - return res; + case GET_SECTOR_SIZE: + *(WORD *)buff = dev->blk_size; + return RES_OK; - case MMC : - // pre-process here + case GET_SECTOR_COUNT: + *(DWORD *)buff = dev->blk_cnt; + return RES_OK; - result = MMC_disk_ioctl(ctrl, buff); - // post-process here + case GET_BLOCK_SIZE: + *(DWORD *)buff = 1; + return RES_OK; - return res; - - case USB : - // pre-process here + default: + LOG_ERR("unknown command: [%d]\n", ctrl); + return RES_PARERR; + } +} - result = USB_disk_ioctl(ctrl, buff); - // post-process here - return res; - } - return RES_PARERR; +DWORD get_fattime(void) +{ + return 0; } + diff --git a/bertos/fs/fatfs/diskio.h b/bertos/fs/fatfs/diskio.h index 6fde2dde..ea2ca943 100644 --- a/bertos/fs/fatfs/diskio.h +++ b/bertos/fs/fatfs/diskio.h @@ -8,6 +8,7 @@ #define _USE_IOCTL 1 #include "integer.h" +#include /* Status of Disk Functions */ @@ -25,7 +26,7 @@ typedef enum { /*---------------------------------------*/ /* Prototypes for disk control functions */ - +void disk_assignDrive(KBlock *dev, int dev_num); BOOL assign_drives (int argc, char *argv[]); DSTATUS disk_initialize (BYTE); DSTATUS disk_status (BYTE); @@ -37,6 +38,7 @@ DRESULT disk_ioctl (BYTE, BYTE, void*); + /* Disk Status Bits (DSTATUS) */ #define STA_NOINIT 0x01 /* Drive not initialized */ -- 2.25.1