USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Files at this revision

API Documentation at this revision

Comitter:
samux
Date:
Mon Nov 14 10:00:07 2011 +0000
Parent:
5:8afbc15d6892
Child:
7:6494da2a5c60
Commit message:
now we have a connect inside USBMSD to get all capacities from storage chip

Changed in this revision

USBDevice/USBDevice/USBDevice.cpp Show annotated file Show diff for this revision Revisions of this file
USBDevice/USBMSD/USBMSD.cpp Show annotated file Show diff for this revision Revisions of this file
USBDevice/USBMSD/USBMSD.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/USBMSD/USB_SDcard.cpp Show annotated file Show diff for this revision Revisions of this file
USBDevice/USBMSD/USB_SDcard.h Show annotated file Show diff for this revision Revisions of this file
--- a/USBDevice/USBDevice/USBDevice.cpp	Sun Nov 13 12:30:43 2011 +0000
+++ b/USBDevice/USBDevice/USBDevice.cpp	Mon Nov 14 10:00:07 2011 +0000
@@ -737,7 +737,7 @@
     device.configuration = 0;
     device.suspended = false;
 
-    connect();
+    //connect();
 };
 
 
--- a/USBDevice/USBMSD/USBMSD.cpp	Sun Nov 13 12:30:43 2011 +0000
+++ b/USBDevice/USBMSD/USBMSD.cpp	Mon Nov 14 10:00:07 2011 +0000
@@ -37,12 +37,6 @@
 // Max In/Out Packet Size on the bulk endpoint */
 #define MAX_PACKET  MAX_PACKET_SIZE_EPBULK
 
-// memory size
-#define MemorySize  0x400000
-
-//number of blocks
-#define BlockCount  (MemorySize / BlockSize)
-
 // CSW Status
 enum Status {
     CSW_PASSED,
@@ -54,6 +48,8 @@
 USBMSD::USBMSD(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {
 }
 
+
+DigitalOut l2(LED2);
 // Called in ISR context to process a class specific request
 bool USBMSD::USBCallback_request(void) {
 
@@ -80,6 +76,34 @@
     return success;
 }
 
+
+bool USBMSD::connect()
+{
+
+    //disk initialization
+    diskInit();
+    
+    // get block size
+    BlockSize = blockSize();
+    if(BlockSize != 0) {
+        page = (uint8_t *)malloc(BlockSize * sizeof(uint8_t));
+        if(page == NULL)
+            return false; 
+    }
+    
+    //get memory size
+    MemorySize = memorySize();
+    if(!MemorySize) {
+        return false;
+    }
+    BlockCount = MemorySize/BlockSize;
+    
+    //connect the device
+    USBDevice::connect();
+    return true;
+}
+
+
 void USBMSD::reset()
 {
     stage = READ_CBW;
@@ -269,7 +293,6 @@
         return false;
     }
     return true;
-
 }
 
 bool USBMSD::write (uint8_t * buf, uint16_t size) {
@@ -337,6 +360,7 @@
     sendCSW();
 }
 
+DigitalOut l1(LED1); 
 void USBMSD::CBWDecode(uint8_t * buf, uint16_t size) {
     if (size == sizeof(cbw)) {
         memcpy((uint8_t *)&cbw, buf, size);
@@ -453,12 +477,9 @@
 
     csw.DataResidue -= n;
 
-    if (!length) {
-        stage = SEND_CSW;
-    }
-
-    if (stage != PROCESS_CBW) {
-        csw.Status = (stage == ERROR) ? CSW_FAILED : CSW_PASSED;
+    if ( !length || (stage != PROCESS_CBW)) {
+        csw.Status = (stage == PROCESS_CBW) ? CSW_PASSED : CSW_FAILED;
+        sendCSW();
     }
 }
 
@@ -541,8 +562,8 @@
 
 uint8_t * USBMSD::stringIproductDesc() {
     static uint8_t stringIproductDescriptor[] = {
-        0x12,                                                       //bLength
-        STRING_DESCRIPTOR,                                          //bDescriptorType 0x03
+        0x12,                                           //bLength
+        STRING_DESCRIPTOR,                              //bDescriptorType 0x03
         'M',0,'b',0,'e',0,'d',0,' ',0,'M',0,'S',0,'D',0 //bString iProduct - Mbed Audio
     };
     return stringIproductDescriptor;
--- a/USBDevice/USBMSD/USBMSD.h	Sun Nov 13 12:30:43 2011 +0000
+++ b/USBDevice/USBMSD/USBMSD.h	Mon Nov 14 10:00:07 2011 +0000
@@ -5,14 +5,6 @@
 /*
 * Guide to adapt this class to your storage chip:
 *
-* - Adapt the BlockSize symbol in USBMSD.h
-* - Adapt the MemorySize symbol in USBMSD.cpp
-* - Declare a class which inherits from USBMSD
-* - Define two virtual functions:
-*        - blockRead(uint8_t * page, uint16_t block_number);
-*        - blockWrite(uint8_t * page, uint16_t block_number);
-*    These functions are used by USBMSD class to read or write data
-* - Instanciate your object
 */
 
 #ifndef USBMSD_H
@@ -27,15 +19,13 @@
 
 #define DEFAULT_CONFIGURATION (1)
 
-// Block Size
-#define BlockSize   512
 
 // MSC Bulk-only Stage
 enum Stage {
     READ_CBW,     // wait a CBW
+    ERROR,        // error
     PROCESS_CBW,  // process a CBW request
     SEND_CSW,     // send a CSW
-    ERROR,        // error
     WAIT_CSW,     // wait that a CSW has been effectively sent
 };
 
@@ -89,6 +79,32 @@
     */
     virtual int blockWrite(uint8_t * data, uint16_t block){return 1;};
     
+    /*
+    * Disk initilization
+    */
+    virtual int diskInit(){return -1;};
+    
+    /*
+    * Return block size
+    *
+    * @returns size of a block
+    */
+    virtual uint16_t blockSize(){return 0;};
+    
+    /*
+    * 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.
+    *
+    * @returns
+    */
+    bool connect();
+    
     
 protected:
 
@@ -162,7 +178,11 @@
     bool memOK;
     
     // cache in RAM before writing in memory. Useful also to read a block.
-    uint8_t page[BlockSize];
+    uint8_t * page;
+    
+    uint16_t BlockSize;
+    uint32_t MemorySize;
+    uint16_t BlockCount;
 
     void CBWDecode(uint8_t * buf, uint16_t size);
     void sendCSW (void);
--- a/USBDevice/USBMSD/USB_SDcard.cpp	Sun Nov 13 12:30:43 2011 +0000
+++ b/USBDevice/USBMSD/USB_SDcard.cpp	Mon Nov 14 10:00:07 2011 +0000
@@ -27,7 +27,7 @@
 USB_SDcard::USB_SDcard(PinName mosi, PinName miso, PinName sclk, PinName cs):
   _spi(mosi, miso, sclk), _cs(cs) {
     _cs = 1; 
-    init();
+    USBMSD::connect();
 }
 
 #define R1_IDLE_STATE           (1 << 0)
@@ -101,7 +101,17 @@
     return SDCARD_FAIL;
 }
 
-int USB_SDcard::init() {
+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);
@@ -353,10 +363,10 @@
     //  MULT = 2^(C_SIZE_MULT+2) (C_SIZE_MULT < 8)
     //  BLOCK_LEN = 2^READ_BL_LEN, (READ_BL_LEN < 12)         
                             
-    int block_len = 1 << read_bl_len;
+    block_len = 1 << read_bl_len;
     int mult = 1 << (c_size_mult + 2);
     int blocknr = (c_size + 1) * mult;
-    int capacity = blocknr * block_len;
+    capacity = blocknr * block_len;
         
     int blocks = capacity / 512;
         
--- a/USBDevice/USBMSD/USB_SDcard.h	Sun Nov 13 12:30:43 2011 +0000
+++ b/USBDevice/USBMSD/USB_SDcard.h	Mon Nov 14 10:00:07 2011 +0000
@@ -55,10 +55,13 @@
     * @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 init();
     int disk_status();
     int disk_sync();
     int disk_sectors();
@@ -74,6 +77,8 @@
     int _write(const char *buffer, int length);
     int _sd_sectors();
     int _sectors;
+    int capacity;
+    int block_len;
     
     SPI _spi;
     DigitalOut _cs;