USBMSD SD card Hello World for Mbed platforms
Dependencies: mbed USBMSD_SD USBDevice
Revision 8:534fd41d8cc7, committed 2011-11-14
- Comitter:
- samux
- Date:
- Mon Nov 14 17:50:02 2011 +0000
- Parent:
- 7:6494da2a5c60
- Child:
- 9:9c343b9ee6d8
- Commit message:
- doesn\'t work...
Changed in this revision
--- a/ChaNFS/CHAN_FS/diskio.c Mon Nov 14 12:08:32 2011 +0000
+++ b/ChaNFS/CHAN_FS/diskio.c Mon Nov 14 17:50:02 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 12:08:32 2011 +0000
+++ b/ChaNFS/CHAN_FS/diskio.h Mon Nov 14 17:50:02 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 12:08:32 2011 +0000
+++ b/ChaNFSSD/SDFileSystem.cpp Mon Nov 14 17:50:02 2011 +0000
@@ -118,6 +118,7 @@
#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;
@@ -194,6 +195,7 @@
return SDCARD_FAIL;
}
+
int SDFileSystem::disk_initialize() {
int i = initialise_card();
@@ -234,7 +236,10 @@
return 0;
}
-int SDFileSystem::disk_status() { return 0; }
+int SDFileSystem::disk_status() { return status; }
+void SDFileSystem::set_status(int st) { status = st; }
+
+
int SDFileSystem::disk_sync() { return 0; }
int SDFileSystem::disk_sectors() { return _sectors; }
--- a/ChaNFSSD/SDFileSystem.h Mon Nov 14 12:08:32 2011 +0000
+++ b/ChaNFSSD/SDFileSystem.h Mon Nov 14 17:50:02 2011 +0000
@@ -25,6 +25,14 @@
#include "mbed.h"
#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
*
@@ -40,7 +48,7 @@
* fclose(fp);
* }
*/
-class SDFileSystem : public FATFileSystem {
+class SDFileSystem : public USBMSD, FATFileSystem {
public:
/** Create the File System for accessing an SD Card using SPI
@@ -58,6 +66,7 @@
virtual int disk_status();
virtual int disk_sync();
virtual int disk_sectors();
+ virtual void set_status(int st);
protected:
@@ -74,6 +83,8 @@
int _sd_sectors();
int _sectors;
+ int status;
+
SPI _spi;
DigitalOut _cs;
};
--- a/USBMSD/USBMSD.cpp Mon Nov 14 12:08:32 2011 +0000
+++ b/USBMSD/USBMSD.cpp Mon Nov 14 17:50:02 2011 +0000
@@ -80,22 +80,27 @@
bool USBMSD::connect() {
//disk initialization
- diskInit();
+ disk_initialize();
+
+ //set_status(1 << 1);
// get block size
- BlockSize = blockSize();
+ BlockSize = 512;
if (BlockSize != 0) {
page = (uint8_t *)malloc(BlockSize * sizeof(uint8_t));
if (page == NULL)
return false;
}
- //get memory size
- MemorySize = memorySize();
+ //get number of blocks
+ BlockCount = disk_sectors();
+
+ MemorySize = BlockCount * 512;
if (!MemorySize) {
return false;
}
- BlockCount = MemorySize/BlockSize;
+ printf("block count: %d\r\n", BlockCount);
+ printf("mem size: %d\r\n", MemorySize);
//connect the device
USBDevice::connect();
@@ -197,7 +202,7 @@
// if the array is filled, write it in memory
if (!((addr + size)%BlockSize))
- blockWrite(page, addr/BlockSize);
+ disk_write((const char *)page, addr/BlockSize);
addr += size;
length -= size;
@@ -220,7 +225,7 @@
// beginning of a new block -> load a whole block in RAM
if (!(addr%BlockSize))
- blockRead(page, addr/BlockSize);
+ disk_read((char *)page, addr/BlockSize);
// info are in RAM -> no need to re-read memory
for (n = 0; n < size; n++) {
@@ -464,7 +469,7 @@
// we read an entire block
if (!(addr%BlockSize))
- blockRead(page, addr/BlockSize);
+ disk_read((char *)page, addr/BlockSize);
// write data which are in RAM
writeNB(EPBULK_IN, &page[addr%BlockSize], n, MAX_PACKET_SIZE_EPBULK);
--- a/USBMSD/USBMSD.h Mon Nov 14 12:08:32 2011 +0000
+++ b/USBMSD/USBMSD.h Mon Nov 14 17:50:02 2011 +0000
@@ -68,7 +68,7 @@
* @param block block number
* @returns 0 if successful
*/
- virtual int blockRead(uint8_t * data, uint16_t block){return 1;};
+ virtual int disk_read(char *buffer, int block_number){return 1;};
/*
* write a block on a storage chip
@@ -77,26 +77,21 @@
* @param block block number
* @returns 0 if successful
*/
- virtual int blockWrite(uint8_t * data, uint16_t block){return 1;};
+ virtual int disk_write(const char *buffer, int block_number){return 1;};
/*
* Disk initilization
*/
- virtual int diskInit(){return -1;};
-
+ virtual int disk_initialize(){return -1;};
+
/*
- * Return block size
+ * Return number of sectors
*
- * @returns size of a block
+ * @returns number of sectors
*/
- virtual uint16_t blockSize(){return 0;};
+ virtual int disk_sectors(){return 0;};
- /*
- * Return memory size
- *
- * @returns memory size
- */
- virtual uint32_t memorySize(){return 0;};
+ virtual void set_status(int st){};
/*
* Connect the USB MSD device. Establish disk initialization before really connect the device.
@@ -182,7 +177,7 @@
uint16_t BlockSize;
uint32_t MemorySize;
- uint16_t BlockCount;
+ uint32_t BlockCount;
void CBWDecode(uint8_t * buf, uint16_t size);
void sendCSW (void);
--- a/USBMSD/USB_SDcard.cpp Mon Nov 14 12:08:32 2011 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,374 +0,0 @@
-/* mbed USB_SDcard Library, for accessing SD cards
- * Copyright (c) 2008-2010, sford
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#include "USB_SDcard.h"
-
-#define SD_COMMAND_TIMEOUT 5000
-
-USB_SDcard::USB_SDcard(PinName mosi, PinName miso, PinName sclk, PinName cs):
- _spi(mosi, miso, sclk), _cs(cs) {
- _cs = 1;
- USBMSD::connect();
-}
-
-#define R1_IDLE_STATE (1 << 0)
-#define R1_ERASE_RESET (1 << 1)
-#define R1_ILLEGAL_COMMAND (1 << 2)
-#define R1_COM_CRC_ERROR (1 << 3)
-#define R1_ERASE_SEQUENCE_ERROR (1 << 4)
-#define R1_ADDRESS_ERROR (1 << 5)
-#define R1_PARAMETER_ERROR (1 << 6)
-
-// Types
-// - v1.x Standard Capacity
-// - v2.x Standard Capacity
-// - v2.x High Capacity
-// - Not recognised as an SD Card
-
-#define SDCARD_FAIL 0
-#define SDCARD_V1 1
-#define SDCARD_V2 2
-#define SDCARD_V2HC 3
-
-int USB_SDcard::initialise_card() {
- // Set to 100kHz for initialisation, and clock card with cs = 1
- _spi.frequency(100000);
- _cs = 1;
- for(int i=0; i<16; i++) {
- _spi.write(0xFF);
- }
-
- // send CMD0, should return with all zeros except IDLE STATE set (bit 0)
- if(_cmd(0, 0) != R1_IDLE_STATE) {
- fprintf(stderr, "No disk, or could not put SD card in to SPI idle state\n");
- return SDCARD_FAIL;
- }
-
- // send CMD8 to determine whther it is ver 2.x
- int r = _cmd8();
- if(r == R1_IDLE_STATE) {
- return initialise_card_v2();
- } else if(r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {
- return initialise_card_v1();
- } else {
- fprintf(stderr, "Not in idle state after sending CMD8 (not an SD card?)\n");
- return SDCARD_FAIL;
- }
-}
-
-int USB_SDcard::initialise_card_v1() {
- for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {
- _cmd(55, 0);
- if(_cmd(41, 0) == 0) {
- return SDCARD_V1;
- }
- }
-
- fprintf(stderr, "Timeout waiting for v1.x card\n");
- return SDCARD_FAIL;
-}
-
-int USB_SDcard::initialise_card_v2() {
-
- for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {
- _cmd(55, 0);
- if(_cmd(41, 0) == 0) {
- _cmd58();
- return SDCARD_V2;
- }
- }
-
- fprintf(stderr, "Timeout waiting for v2.x card\n");
- return SDCARD_FAIL;
-}
-
-uint32_t USB_SDcard::memorySize()
-{
- return (uint32_t)capacity;
-}
-
-uint16_t USB_SDcard::blockSize()
-{
- return (uint16_t)block_len;
-}
-
-int USB_SDcard::diskInit() {
-
- int i = initialise_card();
-// printf("init card = %d\n", i);
-// printf("OK\n");
-
- _sectors = _sd_sectors();
-
- // Set block length to 512 (CMD16)
- if(_cmd(16, 512) != 0) {
- fprintf(stderr, "Set 512-byte block timed out\n");
- return 1;
- }
-
- _spi.frequency(1000000); // Set to 1MHz for data transfer
- return 0;
-}
-
-int USB_SDcard::blockWrite(uint8_t * buffer, uint16_t block_number) {
- // set write address for single block (CMD24)
- if(_cmd(24, block_number * 512) != 0) {
- return 1;
- }
-
- // send the data block
- _write((const char *)buffer, 512);
- return 0;
-}
-
-int USB_SDcard::blockRead(uint8_t * buffer, uint16_t block_number) {
- // set read address for single block (CMD17)
- if(_cmd(17, block_number * 512) != 0) {
- return 1;
- }
-
- // receive the data
- _read((char *)buffer, 512);
- return 0;
-}
-
-int USB_SDcard::disk_status() { return 0; }
-int USB_SDcard::disk_sync() { return 0; }
-int USB_SDcard::disk_sectors() { return _sectors; }
-
-// PRIVATE FUNCTIONS
-
-int USB_SDcard::_cmd(int cmd, int arg) {
- _cs = 0;
-
- // send a command
- _spi.write(0x40 | cmd);
- _spi.write(arg >> 24);
- _spi.write(arg >> 16);
- _spi.write(arg >> 8);
- _spi.write(arg >> 0);
- _spi.write(0x95);
-
- // wait for the repsonse (response[7] == 0)
- for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {
- int response = _spi.write(0xFF);
- if(!(response & 0x80)) {
- _cs = 1;
- _spi.write(0xFF);
- return response;
- }
- }
- _cs = 1;
- _spi.write(0xFF);
- return -1; // timeout
-}
-int USB_SDcard::_cmdx(int cmd, int arg) {
- _cs = 0;
-
- // send a command
- _spi.write(0x40 | cmd);
- _spi.write(arg >> 24);
- _spi.write(arg >> 16);
- _spi.write(arg >> 8);
- _spi.write(arg >> 0);
- _spi.write(0x95);
-
- // wait for the repsonse (response[7] == 0)
- for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {
- int response = _spi.write(0xFF);
- if(!(response & 0x80)) {
- return response;
- }
- }
- _cs = 1;
- _spi.write(0xFF);
- return -1; // timeout
-}
-
-
-int USB_SDcard::_cmd58() {
- _cs = 0;
- int arg = 0;
-
- // send a command
- _spi.write(0x40 | 58);
- _spi.write(arg >> 24);
- _spi.write(arg >> 16);
- _spi.write(arg >> 8);
- _spi.write(arg >> 0);
- _spi.write(0x95);
-
- // wait for the repsonse (response[7] == 0)
- for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {
- int response = _spi.write(0xFF);
- if(!(response & 0x80)) {
- int ocr = _spi.write(0xFF) << 24;
- ocr |= _spi.write(0xFF) << 16;
- ocr |= _spi.write(0xFF) << 8;
- ocr |= _spi.write(0xFF) << 0;
-// printf("OCR = 0x%08X\n", ocr);
- _cs = 1;
- _spi.write(0xFF);
- return response;
- }
- }
- _cs = 1;
- _spi.write(0xFF);
- return -1; // timeout
-}
-
-int USB_SDcard::_cmd8() {
- _cs = 0;
-
- // send a command
- _spi.write(0x40 | 8); // CMD8
- _spi.write(0x00); // reserved
- _spi.write(0x00); // reserved
- _spi.write(0x01); // 3.3v
- _spi.write(0xAA); // check pattern
- _spi.write(0x87); // crc
-
- // wait for the repsonse (response[7] == 0)
- for(int i=0; i<SD_COMMAND_TIMEOUT * 1000; i++) {
- char response[5];
- response[0] = _spi.write(0xFF);
- if(!(response[0] & 0x80)) {
- for(int j=1; j<5; j++) {
- response[i] = _spi.write(0xFF);
- }
- _cs = 1;
- _spi.write(0xFF);
- return response[0];
- }
- }
- _cs = 1;
- _spi.write(0xFF);
- return -1; // timeout
-}
-
-int USB_SDcard::_read(char *buffer, int length) {
- _cs = 0;
-
- // read until start byte (0xFF)
- while(_spi.write(0xFF) != 0xFE);
-
- // read data
- for(int i=0; i<length; i++) {
- buffer[i] = _spi.write(0xFF);
- }
- _spi.write(0xFF); // checksum
- _spi.write(0xFF);
-
- _cs = 1;
- _spi.write(0xFF);
- return 0;
-}
-
-int USB_SDcard::_write(const char *buffer, int length) {
- _cs = 0;
-
- // indicate start of block
- _spi.write(0xFE);
-
- // write the data
- for(int i=0; i<length; i++) {
- _spi.write(buffer[i]);
- }
-
- // write the checksum
- _spi.write(0xFF);
- _spi.write(0xFF);
-
- // check the repsonse token
- if((_spi.write(0xFF) & 0x1F) != 0x05) {
- _cs = 1;
- _spi.write(0xFF);
- return 1;
- }
-
- // wait for write to finish
- while(_spi.write(0xFF) == 0);
-
- _cs = 1;
- _spi.write(0xFF);
- return 0;
-}
-
-static int ext_bits(char *data, int msb, int lsb) {
- int bits = 0;
- int size = 1 + msb - lsb;
- for(int i=0; i<size; i++) {
- int position = lsb + i;
- int byte = 15 - (position >> 3);
- int bit = position & 0x7;
- int value = (data[byte] >> bit) & 1;
- bits |= value << i;
- }
- return bits;
-}
-
-int USB_SDcard::_sd_sectors() {
-
- // CMD9, Response R2 (R1 byte + 16-byte block read)
- if(_cmdx(9, 0) != 0) {
- fprintf(stderr, "Didn't get a response from the disk\n");
- return 0;
- }
-
- char csd[16];
- if(_read(csd, 16) != 0) {
- fprintf(stderr, "Couldn't read csd response from disk\n");
- return 0;
- }
-
- // csd_structure : csd[127:126]
- // c_size : csd[73:62]
- // c_size_mult : csd[49:47]
- // read_bl_len : csd[83:80] - the *maximum* read block length
-
- int csd_structure = ext_bits(csd, 127, 126);
- int c_size = ext_bits(csd, 73, 62);
- int c_size_mult = ext_bits(csd, 49, 47);
- int read_bl_len = ext_bits(csd, 83, 80);
-
-// printf("CSD_STRUCT = %d\n", csd_structure);
-
- if(csd_structure != 0) {
- fprintf(stderr, "This disk tastes funny! I only know about type 0 CSD structures\n");
- return 0;
- }
-
- // memory capacity = BLOCKNR * BLOCK_LEN
- // where
- // BLOCKNR = (C_SIZE+1) * MULT
- // MULT = 2^(C_SIZE_MULT+2) (C_SIZE_MULT < 8)
- // BLOCK_LEN = 2^READ_BL_LEN, (READ_BL_LEN < 12)
-
- block_len = 1 << read_bl_len;
- int mult = 1 << (c_size_mult + 2);
- int blocknr = (c_size + 1) * mult;
- capacity = blocknr * block_len;
-
- int blocks = capacity / 512;
-
- return blocks;
-}
--- a/USBMSD/USB_SDcard.h Mon Nov 14 12:08:32 2011 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,87 +0,0 @@
-/* mbed USB_SDcard Library, for accessing SD cards
- * Copyright (c) 2008-2010, sford
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#ifndef USB_SDCARD_H
-#define USB_SDCARD_H
-
-#include "mbed.h"
-#include "USBMSD.h"
-
-class USB_SDcard : public USBMSD {
-public:
-
- /** Create the File System for accessing an SD Card using SPI
- *
- * @param mosi SPI mosi pin connected to SD Card
- * @param miso SPI miso pin conencted to SD Card
- * @param sclk SPI sclk pin connected to SD Card
- * @param cs DigitalOut pin used as SD Card chip select
- */
- USB_SDcard(PinName mosi = p5, PinName miso = p6, PinName sclk = p7, PinName cs = p8);
-
- /*
- * read a block on a storage chip
- *
- * @param data pointer where will be stored read data
- * @param block block number
- * @returns 0 if successful
- */
- virtual int blockRead(uint8_t * data, uint16_t block);
-
- /*
- * write a block on a storage chip
- *
- * @param data data to write
- * @param block block number
- * @returns 0 if successful
- */
- virtual int blockWrite(uint8_t * data, uint16_t block);
-
- virtual int diskInit();
- virtual uint16_t blockSize();
- virtual uint32_t memorySize();
-
-protected:
-
- int disk_status();
- int disk_sync();
- int disk_sectors();
- int _cmd(int cmd, int arg);
- int _cmdx(int cmd, int arg);
- int _cmd8();
- int _cmd58();
- int initialise_card();
- int initialise_card_v1();
- int initialise_card_v2();
-
- int _read(char *buffer, int length);
- int _write(const char *buffer, int length);
- int _sd_sectors();
- int _sectors;
- int capacity;
- int block_len;
-
- SPI _spi;
- DigitalOut _cs;
-};
-
-#endif
--- a/main.cpp Mon Nov 14 12:08:32 2011 +0000
+++ b/main.cpp Mon Nov 14 17:50:02 2011 +0000
@@ -1,19 +1,22 @@
#include "mbed.h"
-#include "USB_SDcard.h"
#include "SDFileSystem.h"
-USB_SDcard sd;
-SDFileSystem sd1(p5, p6, p7, p8, "sd");
+SDFileSystem sd(p5, p6, p7, p8, "sd");
+Serial pc(USBTX, USBRX);
int main() {
- mkdir("/sd/mydir", 0777);
-
- FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
- if(fp == NULL) {
- error("Could not open file for write\n");
- }
- fprintf(fp, "Hello fun SD Card World!");
- fclose(fp);
-
+
+ //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);
}
Samuel Mokrani