USB MSD using the AHBSRAM banks 0 and 1 to create a 32k RAM disk. After FAT format 12kb is writable
Dependencies: USBDevice USBMSD_RAM mbed
Fork of USB-MSD_SD_HelloWorld_Mbed-RAMVersion by
Revision 9:9c343b9ee6d8, committed 2011-11-15
- Comitter:
- samux
- Date:
- Tue Nov 15 09:16:25 2011 +0000
- Parent:
- 8:534fd41d8cc7
- Child:
- 10:cf8fd2b6ca23
- Commit message:
- ok, will try to protect disk
Changed in this revision
--- a/ChaNFS/CHAN_FS/diskio.c Mon Nov 14 17:50:02 2011 +0000 +++ b/ChaNFS/CHAN_FS/diskio.c Tue Nov 15 09:16:25 2011 +0000 @@ -13,93 +13,93 @@ #include "mbed.h" DSTATUS disk_initialize ( - BYTE drv /* Physical drive nmuber (0..) */ + BYTE drv /* Physical drive nmuber (0..) */ ) { - FFSDEBUG("disk_initialize on drv [%d]\n", drv); - return (DSTATUS)FATFileSystem::_ffs[drv]->disk_initialize(); + FFSDEBUG("disk_initialize on drv [%d]\n", drv); + return (DSTATUS)FATFileSystem::_ffs[drv]->disk_initialize(); } DSTATUS disk_status ( - BYTE drv /* Physical drive nmuber (0..) */ + BYTE drv /* Physical drive nmuber (0..) */ ) { - FFSDEBUG("disk_status on drv [%d]\n", drv); - return (DSTATUS)FATFileSystem::_ffs[drv]->disk_status(); + FFSDEBUG("disk_status on drv [%d]\n", drv); + return (DSTATUS)FATFileSystem::_ffs[drv]->disk_status(); } DRESULT disk_read ( - BYTE drv, /* Physical drive nmuber (0..) */ - BYTE *buff, /* Data buffer to store read data */ - DWORD sector, /* Sector address (LBA) */ - BYTE count /* Number of sectors to read (1..255) */ + BYTE drv, /* Physical drive nmuber (0..) */ + BYTE *buff, /* Data buffer to store read data */ + DWORD sector, /* Sector address (LBA) */ + BYTE count /* Number of sectors to read (1..255) */ ) { - FFSDEBUG("disk_read(sector %d, count %d) on drv [%d]\n", sector, count, drv); - for(int s=sector; s<sector+count; s++) { - FFSDEBUG(" disk_read(sector %d)\n", s); - int res = FATFileSystem::_ffs[drv]->disk_read((char*)buff, s); - if(res) { - return RES_PARERR; - } - buff += 512; - } - return RES_OK; + FFSDEBUG("disk_read(sector %d, count %d) on drv [%d]\n", sector, count, drv); + for(int s=sector; s<sector+count; s++) { + FFSDEBUG(" disk_read(sector %d)\n", s); + int res = FATFileSystem::_ffs[drv]->disk_read((char*)buff, s); + if(res) { + return RES_PARERR; + } + buff += 512; + } + return RES_OK; } #if _READONLY == 0 DRESULT disk_write ( - BYTE drv, /* Physical drive nmuber (0..) */ - const BYTE *buff, /* Data to be written */ - DWORD sector, /* Sector address (LBA) */ - BYTE count /* Number of sectors to write (1..255) */ + BYTE drv, /* Physical drive nmuber (0..) */ + const BYTE *buff, /* Data to be written */ + DWORD sector, /* Sector address (LBA) */ + BYTE count /* Number of sectors to write (1..255) */ ) { - FFSDEBUG("disk_write(sector %d, count %d) on drv [%d]\n", sector, count, drv); - for(int s=sector; s<sector+count; s++) { - FFSDEBUG(" disk_write(sector %d)\n", s); - int res = FATFileSystem::_ffs[drv]->disk_write((char*)buff, sector); - if(res) { - return RES_PARERR; - } - buff += 512; - } - return RES_OK; + FFSDEBUG("disk_write(sector %d, count %d) on drv [%d]\n", sector, count, drv); + for(int s=sector; s<sector+count; s++) { + FFSDEBUG(" disk_write(sector %d)\n", s); + int res = FATFileSystem::_ffs[drv]->disk_write((char*)buff, sector); + if(res) { + return RES_PARERR; + } + buff += 512; + } + return RES_OK; } #endif /* _READONLY */ DRESULT disk_ioctl ( - BYTE drv, /* Physical drive nmuber (0..) */ - BYTE ctrl, /* Control code */ - void *buff /* Buffer to send/receive control data */ + BYTE drv, /* Physical drive nmuber (0..) */ + BYTE ctrl, /* Control code */ + void *buff /* Buffer to send/receive control data */ ) { - FFSDEBUG("disk_ioctl(%d)\n", ctrl); - switch(ctrl) { - case CTRL_SYNC: - if(FATFileSystem::_ffs[drv] == NULL) { - return RES_NOTRDY; - } else if(FATFileSystem::_ffs[drv]->disk_sync()) { - return RES_ERROR; - } - return RES_OK; - case GET_SECTOR_COUNT: - if(FATFileSystem::_ffs[drv] == NULL) { - return RES_NOTRDY; - } else { - int res = FATFileSystem::_ffs[drv]->disk_sectors(); - if(res > 0) { - *((DWORD*)buff) = res; // minimum allowed - return RES_OK; - } else { - return RES_ERROR; - } - } - case GET_BLOCK_SIZE: - *((DWORD*)buff) = 1; // default when not known - return RES_OK; + FFSDEBUG("disk_ioctl(%d)\n", ctrl); + switch(ctrl) { + case CTRL_SYNC: + if(FATFileSystem::_ffs[drv] == NULL) { + return RES_NOTRDY; + } else if(FATFileSystem::_ffs[drv]->disk_sync()) { + return RES_ERROR; + } + return RES_OK; + case GET_SECTOR_COUNT: + if(FATFileSystem::_ffs[drv] == NULL) { + return RES_NOTRDY; + } else { + int res = FATFileSystem::_ffs[drv]->disk_sectors(); + if(res > 0) { + *((DWORD*)buff) = res; // minimum allowed + return RES_OK; + } else { + return RES_ERROR; + } + } + case GET_BLOCK_SIZE: + *((DWORD*)buff) = 1; // default when not known + return RES_OK; - } - return RES_PARERR; + } + return RES_PARERR; }
--- a/ChaNFS/CHAN_FS/diskio.h Mon Nov 14 17:50:02 2011 +0000 +++ b/ChaNFS/CHAN_FS/diskio.h Tue Nov 15 09:16:25 2011 +0000 @@ -4,22 +4,22 @@ #ifndef _DISKIO -#define _READONLY 0 /* 1: Remove write functions */ -#define _USE_IOCTL 1 /* 1: Use disk_ioctl fucntion */ +#define _READONLY 0 /* 1: Remove write functions */ +#define _USE_IOCTL 1 /* 1: Use disk_ioctl fucntion */ #include "integer.h" /* Status of Disk Functions */ -typedef BYTE DSTATUS; +typedef BYTE DSTATUS; /* Results of Disk Functions */ typedef enum { - RES_OK = 0, /* 0: Successful */ - RES_ERROR, /* 1: R/W Error */ - RES_WRPRT, /* 2: Write Protected */ - RES_NOTRDY, /* 3: Not Ready */ - RES_PARERR /* 4: Invalid Parameter */ + RES_OK = 0, /* 0: Successful */ + RES_ERROR, /* 1: R/W Error */ + RES_WRPRT, /* 2: Write Protected */ + RES_NOTRDY, /* 3: Not Ready */ + RES_PARERR /* 4: Invalid Parameter */ } DRESULT; @@ -30,50 +30,50 @@ DSTATUS disk_initialize (BYTE); DSTATUS disk_status (BYTE); DRESULT disk_read (BYTE, BYTE*, DWORD, BYTE); -#if _READONLY == 0 +#if _READONLY == 0 DRESULT disk_write (BYTE, const BYTE*, DWORD, BYTE); #endif DRESULT disk_ioctl (BYTE, BYTE, void*); -void disk_timerproc (void); +void disk_timerproc (void); /* Disk Status Bits (DSTATUS) */ -#define STA_NOINIT 0x01 /* Drive not initialized */ -#define STA_NODISK 0x02 /* No medium in the drive */ -#define STA_PROTECT 0x04 /* Write protected */ +#define STA_NOINIT 0x01 /* Drive not initialized */ +#define STA_NODISK 0x02 /* No medium in the drive */ +#define STA_PROTECT 0x04 /* Write protected */ /* Command code for disk_ioctrl fucntion */ /* Generic command (defined for FatFs) */ -#define CTRL_SYNC 0 /* Flush disk cache (for write functions) */ -#define GET_SECTOR_COUNT 1 /* Get media size (for only f_mkfs()) */ -#define GET_SECTOR_SIZE 2 /* Get sector size (for multiple sector size (_MAX_SS >= 1024)) */ -#define GET_BLOCK_SIZE 3 /* Get erase block size (for only f_mkfs()) */ -#define CTRL_ERASE_SECTOR 4 /* Force erased a block of sectors (for only _USE_ERASE) */ +#define CTRL_SYNC 0 /* Flush disk cache (for write functions) */ +#define GET_SECTOR_COUNT 1 /* Get media size (for only f_mkfs()) */ +#define GET_SECTOR_SIZE 2 /* Get sector size (for multiple sector size (_MAX_SS >= 1024)) */ +#define GET_BLOCK_SIZE 3 /* Get erase block size (for only f_mkfs()) */ +#define CTRL_ERASE_SECTOR 4 /* Force erased a block of sectors (for only _USE_ERASE) */ /* Generic command */ -#define CTRL_POWER 5 /* Get/Set power status */ -#define CTRL_LOCK 6 /* Lock/Unlock media removal */ -#define CTRL_EJECT 7 /* Eject media */ +#define CTRL_POWER 5 /* Get/Set power status */ +#define CTRL_LOCK 6 /* Lock/Unlock media removal */ +#define CTRL_EJECT 7 /* Eject media */ /* MMC/SDC specific ioctl command */ -#define MMC_GET_TYPE 10 /* Get card type */ -#define MMC_GET_CSD 11 /* Get CSD */ -#define MMC_GET_CID 12 /* Get CID */ -#define MMC_GET_OCR 13 /* Get OCR */ -#define MMC_GET_SDSTAT 14 /* Get SD status */ +#define MMC_GET_TYPE 10 /* Get card type */ +#define MMC_GET_CSD 11 /* Get CSD */ +#define MMC_GET_CID 12 /* Get CID */ +#define MMC_GET_OCR 13 /* Get OCR */ +#define MMC_GET_SDSTAT 14 /* Get SD status */ /* ATA/CF specific ioctl command */ -#define ATA_GET_REV 20 /* Get F/W revision */ -#define ATA_GET_MODEL 21 /* Get model name */ -#define ATA_GET_SN 22 /* Get serial number */ +#define ATA_GET_REV 20 /* Get F/W revision */ +#define ATA_GET_MODEL 21 /* Get model name */ +#define ATA_GET_SN 22 /* Get serial number */ /* NAND specific ioctl command */ -#define NAND_FORMAT 30 /* Create physical format */ +#define NAND_FORMAT 30 /* Create physical format */ #define _DISKIO
--- a/ChaNFSSD/SDFileSystem.cpp Mon Nov 14 17:50:02 2011 +0000 +++ b/ChaNFSSD/SDFileSystem.cpp Tue Nov 15 09:16:25 2011 +0000 @@ -118,10 +118,10 @@ #define SD_COMMAND_TIMEOUT 5000 - SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name) : FATFileSystem(name), _spi(mosi, miso, sclk), _cs(cs) { _cs = 1; + USBMSD::connect(); } #define R1_IDLE_STATE (1 << 0) @@ -195,7 +195,6 @@ return SDCARD_FAIL; } - int SDFileSystem::disk_initialize() { int i = initialise_card(); @@ -236,10 +235,7 @@ return 0; } -int SDFileSystem::disk_status() { return status; } -void SDFileSystem::set_status(int st) { status = st; } - - +int SDFileSystem::disk_status() { return 0; } int SDFileSystem::disk_sync() { return 0; } int SDFileSystem::disk_sectors() { return _sectors; }
--- a/ChaNFSSD/SDFileSystem.h Mon Nov 14 17:50:02 2011 +0000 +++ b/ChaNFSSD/SDFileSystem.h Tue Nov 15 09:16:25 2011 +0000 @@ -27,13 +27,6 @@ #include "FATFileSystem.h" #include "USBMSD.h" -// MSC Bulk-only Stage -enum STATUS { - NOINIT = (1 << 0), - NODISK = (1 << 1), - PROTECT = (1 << 2), -}; - /** Access the filesystem on an SD Card using SPI * * @code @@ -48,7 +41,7 @@ * fclose(fp); * } */ -class SDFileSystem : public USBMSD, FATFileSystem { +class SDFileSystem : public USBMSD, public FATFileSystem { public: /** Create the File System for accessing an SD Card using SPI @@ -66,7 +59,6 @@ virtual int disk_status(); virtual int disk_sync(); virtual int disk_sectors(); - virtual void set_status(int st); protected: @@ -83,8 +75,6 @@ int _sd_sectors(); int _sectors; - int status; - SPI _spi; DigitalOut _cs; };
--- a/USBMSD/USBMSD.cpp Mon Nov 14 17:50:02 2011 +0000 +++ b/USBMSD/USBMSD.cpp Tue Nov 15 09:16:25 2011 +0000 @@ -81,8 +81,6 @@ //disk initialization disk_initialize(); - - //set_status(1 << 1); // get block size BlockSize = 512; @@ -92,15 +90,17 @@ return false; } - //get number of blocks BlockCount = disk_sectors(); - MemorySize = BlockCount * 512; + //get memory size + MemorySize = BlockCount * BlockSize; if (!MemorySize) { return false; } - printf("block count: %d\r\n", BlockCount); - printf("mem size: %d\r\n", MemorySize); + + printf("blockSize: %d\r\n", BlockSize); + printf("memSize: %d\r\n", MemorySize); + printf("number of blocks: %d\r\n", BlockCount); //connect the device USBDevice::connect();
--- a/USBMSD/USBMSD.h Mon Nov 14 17:50:02 2011 +0000 +++ b/USBMSD/USBMSD.h Tue Nov 15 09:16:25 2011 +0000 @@ -68,7 +68,7 @@ * @param block block number * @returns 0 if successful */ - virtual int disk_read(char *buffer, int block_number){return 1;}; + virtual int disk_read(char * data, int block){return 1;}; /* * write a block on a storage chip @@ -77,21 +77,26 @@ * @param block block number * @returns 0 if successful */ - virtual int disk_write(const char *buffer, int block_number){return 1;}; + virtual int disk_write(const char * data, int block){return 1;}; /* * Disk initilization */ virtual int disk_initialize(){return -1;}; - + /* - * Return number of sectors + * Return the number of blocks * - * @returns number of sectors + * @returns number of blocks */ virtual int disk_sectors(){return 0;}; - virtual void set_status(int st){}; + /* + * Return memory size + * + * @returns memory size + */ + virtual uint32_t memorySize(){return 0;}; /* * Connect the USB MSD device. Establish disk initialization before really connect the device. @@ -177,7 +182,7 @@ uint16_t BlockSize; uint32_t MemorySize; - uint32_t BlockCount; + uint16_t BlockCount; void CBWDecode(uint8_t * buf, uint16_t size); void sendCSW (void);
--- a/main.cpp Mon Nov 14 17:50:02 2011 +0000 +++ b/main.cpp Tue Nov 15 09:16:25 2011 +0000 @@ -2,21 +2,7 @@ #include "SDFileSystem.h" SDFileSystem sd(p5, p6, p7, p8, "sd"); -Serial pc(USBTX, USBRX); int main() { - - //connect USB SD card - sd.connect(); while(1); - - mkdir("/sd/dir", 0777); - - FILE *fp = fopen("/sd/dir/file.txt", "w"); - if (fp == NULL) { - error("Could not open file for write\n"); - } - fprintf(fp, "Hello fun SD Card World!"); - fclose(fp); - while(1); -} +} \ No newline at end of file