USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

USBDevice/USBMSD/USBMSD.h

Committer:
samux
Date:
2011-11-11
Revision:
2:27a7e7f8d399
Child:
3:0ffb2eee9e06

File content as of revision 2:27a7e7f8d399:

/* 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 your own object to store data: here AT45 mem
* - Be sure to provide :
*        - mem.blockread(page, block_number);
*        - mem.blockwrite(page, block_number);
*    These functions are used by USBMSD class to read or write data
*/

#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"
#include "SDcard.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;

/**
* USBMSD example
*
* @code
* #include "mbed.h"
* #include "USBMSD.h"
*
* USBMSD msd;
*
* int main() {
*    while(1);
* }
*
* @endcode
*/
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);
    


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];
    
    // memory (Atmel AT45 family)
    //
    // You can change this memory to use another one (SDcard, ...)
    // You need to provide : 
    //    - mem.blockread(page, block_number);
    //    - mem.blockwrite(page, block_number);
    SDcard mem;

    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