USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Revision:
7:6494da2a5c60
Parent:
6:126c4d980196
Child:
8:534fd41d8cc7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBMSD/USBMSD.h	Mon Nov 14 12:08:32 2011 +0000
@@ -0,0 +1,204 @@
+/* USBMSD.h */
+/* USB mass storage device example */
+/* Copyright (c) 2011 ARM Limited. All rights reserved. */
+
+/*
+* Guide to adapt this class to your storage chip:
+*
+*/
+
+#ifndef USBMSD_H
+#define USBMSD_H
+
+/* These headers are included for child class. */
+#include "USBEndpoints.h"
+#include "USBDescriptor.h"
+#include "USBDevice_Types.h"
+
+#include "USBDevice.h"
+
+#define DEFAULT_CONFIGURATION (1)
+
+
+// MSC Bulk-only Stage
+enum Stage {
+    READ_CBW,     // wait a CBW
+    ERROR,        // error
+    PROCESS_CBW,  // process a CBW request
+    SEND_CSW,     // send a CSW
+    WAIT_CSW,     // wait that a CSW has been effectively sent
+};
+
+// Bulk-only CBW
+typedef __packed struct {
+    uint32_t Signature;
+    uint32_t Tag;
+    uint32_t DataLength;
+    uint8_t  Flags;
+    uint8_t  LUN;
+    uint8_t  CBLength;
+    uint8_t  CB[16];
+} CBW;
+
+// Bulk-only CSW
+typedef __packed struct {
+    uint32_t Signature;
+    uint32_t Tag;
+    uint32_t DataResidue;
+    uint8_t  Status;
+} CSW;
+
+
+class USBMSD: public USBDevice {
+public:
+
+    /**
+    * Constructor
+    *
+    * @param vendor_id Your vendor_id
+    * @param product_id Your product_id
+    * @param product_release Your preoduct_release
+    */
+    USBMSD(uint16_t vendor_id = 0x0703, uint16_t product_id = 0x0104, uint16_t product_release = 0x0001);
+    
+    /*
+    * 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){return 1;};
+    
+    /*
+    * 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){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:
+
+
+    /*
+    * Get number of logical unit - 1 (here 0)
+    *
+    * @returns Pointer containing the number of logical unit - 1
+    */
+    uint8_t * getMaxLUN();
+    
+    /*
+    * Get string product descriptor
+    *
+    * @returns pointer to the string product descriptor
+    */
+    virtual uint8_t * stringIproductDesc();
+
+    /*
+    * Get string interface descriptor
+    *
+    * @returns pointer to the string interface descriptor
+    */
+    virtual uint8_t * stringIinterfaceDesc();
+
+    /*
+    * Get configuration descriptor
+    *
+    * @returns pointer to the configuration descriptor
+    */
+    virtual uint8_t * configurationDesc();
+
+    /*
+    * Callback called when a packet is received
+    */
+    virtual bool EP2_OUT_callback();
+    
+    /*
+    * Callback called when a packet has been sent
+    */
+    virtual bool EP2_IN_callback();
+
+    /*
+    * Set configuration of device. Add endpoints
+    */
+    virtual bool USBCallback_setConfiguration(uint8_t configuration);
+    
+    /*
+    * Callback called to process class specific requests
+    */
+    virtual bool USBCallback_request();
+    
+
+private:
+    //state of the bulk-only state machine
+    Stage stage;
+    
+    // current CBW
+    CBW cbw;
+    
+    // CSW which will be sent
+    CSW csw;
+    
+    // addr where will be read or written data
+    uint32_t addr;
+    
+    // length of a reading or writing
+    uint32_t length;
+    
+    // memory OK (after a memoryVerify)
+    bool memOK;
+    
+    // cache in RAM before writing in memory. Useful also to read a block.
+    uint8_t * page;
+    
+    uint16_t BlockSize;
+    uint32_t MemorySize;
+    uint16_t BlockCount;
+
+    void CBWDecode(uint8_t * buf, uint16_t size);
+    void sendCSW (void);
+    bool inquiryRequest (void);
+    bool write (uint8_t * buf, uint16_t size);
+    bool readFormatCapacity();
+    bool readCapacity (void);
+    bool infoTransfer (void);
+    void memoryRead (void);
+    bool modeSense6 (void);
+    void testUnitReady (void);
+    bool requestSense (void);
+    void memoryVerify (uint8_t * buf, uint16_t size);
+    void memoryWrite (uint8_t * buf, uint16_t size);
+    void reset();
+    void fail();
+};
+
+#endif