USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Revision:
6:126c4d980196
Parent:
5:8afbc15d6892
--- 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;