USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

USBDevice/USBMSD/USBMSD.h

Committer:
samux
Date:
2011-11-12
Revision:
4:980e6470dcce
Parent:
3:0ffb2eee9e06
Child:
5:8afbc15d6892

File content as of revision 4:980e6470dcce:

/* USBMSD.h */
/* USB mass storage device example */
/* Copyright (c) 2011 ARM Limited. All rights reserved. */

/*
* 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
#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)

// Block Size
#define BlockSize   512

// MSC Bulk-only Stage
enum Stage {
    STATE_READ_CBW,     // wait a CBW
    STATE_PROCESS_CBW,  // process a CBW request
    STATE_SEND_CSW,     // send a CSW
    STATE_ERROR,        // error: out of memory
    STATE_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;};
    
    
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[BlockSize];

    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