USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Sun Nov 13 12:30:43 2011 +0000
Revision:
5:8afbc15d6892
Parent:
4:980e6470dcce
Child:
6:126c4d980196
rename things and introduce an ERROR state

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 2:27a7e7f8d399 1 /* USBMSD.h */
samux 2:27a7e7f8d399 2 /* USB mass storage device example */
samux 2:27a7e7f8d399 3 /* Copyright (c) 2011 ARM Limited. All rights reserved. */
samux 2:27a7e7f8d399 4
samux 2:27a7e7f8d399 5 /*
samux 2:27a7e7f8d399 6 * Guide to adapt this class to your storage chip:
samux 2:27a7e7f8d399 7 *
samux 3:0ffb2eee9e06 8 * - Adapt the BlockSize symbol in USBMSD.h
samux 3:0ffb2eee9e06 9 * - Adapt the MemorySize symbol in USBMSD.cpp
samux 3:0ffb2eee9e06 10 * - Declare a class which inherits from USBMSD
samux 3:0ffb2eee9e06 11 * - Define two virtual functions:
samux 3:0ffb2eee9e06 12 * - blockRead(uint8_t * page, uint16_t block_number);
samux 3:0ffb2eee9e06 13 * - blockWrite(uint8_t * page, uint16_t block_number);
samux 2:27a7e7f8d399 14 * These functions are used by USBMSD class to read or write data
samux 3:0ffb2eee9e06 15 * - Instanciate your object
samux 2:27a7e7f8d399 16 */
samux 2:27a7e7f8d399 17
samux 2:27a7e7f8d399 18 #ifndef USBMSD_H
samux 2:27a7e7f8d399 19 #define USBMSD_H
samux 2:27a7e7f8d399 20
samux 2:27a7e7f8d399 21 /* These headers are included for child class. */
samux 2:27a7e7f8d399 22 #include "USBEndpoints.h"
samux 2:27a7e7f8d399 23 #include "USBDescriptor.h"
samux 2:27a7e7f8d399 24 #include "USBDevice_Types.h"
samux 2:27a7e7f8d399 25
samux 2:27a7e7f8d399 26 #include "USBDevice.h"
samux 2:27a7e7f8d399 27
samux 2:27a7e7f8d399 28 #define DEFAULT_CONFIGURATION (1)
samux 2:27a7e7f8d399 29
samux 2:27a7e7f8d399 30 // Block Size
samux 2:27a7e7f8d399 31 #define BlockSize 512
samux 2:27a7e7f8d399 32
samux 2:27a7e7f8d399 33 // MSC Bulk-only Stage
samux 2:27a7e7f8d399 34 enum Stage {
samux 5:8afbc15d6892 35 READ_CBW, // wait a CBW
samux 5:8afbc15d6892 36 PROCESS_CBW, // process a CBW request
samux 5:8afbc15d6892 37 SEND_CSW, // send a CSW
samux 5:8afbc15d6892 38 ERROR, // error
samux 5:8afbc15d6892 39 WAIT_CSW, // wait that a CSW has been effectively sent
samux 2:27a7e7f8d399 40 };
samux 2:27a7e7f8d399 41
samux 2:27a7e7f8d399 42 // Bulk-only CBW
samux 2:27a7e7f8d399 43 typedef __packed struct {
samux 2:27a7e7f8d399 44 uint32_t Signature;
samux 2:27a7e7f8d399 45 uint32_t Tag;
samux 2:27a7e7f8d399 46 uint32_t DataLength;
samux 2:27a7e7f8d399 47 uint8_t Flags;
samux 2:27a7e7f8d399 48 uint8_t LUN;
samux 2:27a7e7f8d399 49 uint8_t CBLength;
samux 2:27a7e7f8d399 50 uint8_t CB[16];
samux 2:27a7e7f8d399 51 } CBW;
samux 2:27a7e7f8d399 52
samux 2:27a7e7f8d399 53 // Bulk-only CSW
samux 2:27a7e7f8d399 54 typedef __packed struct {
samux 2:27a7e7f8d399 55 uint32_t Signature;
samux 2:27a7e7f8d399 56 uint32_t Tag;
samux 2:27a7e7f8d399 57 uint32_t DataResidue;
samux 2:27a7e7f8d399 58 uint8_t Status;
samux 2:27a7e7f8d399 59 } CSW;
samux 2:27a7e7f8d399 60
samux 3:0ffb2eee9e06 61
samux 2:27a7e7f8d399 62 class USBMSD: public USBDevice {
samux 2:27a7e7f8d399 63 public:
samux 2:27a7e7f8d399 64
samux 2:27a7e7f8d399 65 /**
samux 2:27a7e7f8d399 66 * Constructor
samux 2:27a7e7f8d399 67 *
samux 2:27a7e7f8d399 68 * @param vendor_id Your vendor_id
samux 2:27a7e7f8d399 69 * @param product_id Your product_id
samux 2:27a7e7f8d399 70 * @param product_release Your preoduct_release
samux 2:27a7e7f8d399 71 */
samux 2:27a7e7f8d399 72 USBMSD(uint16_t vendor_id = 0x0703, uint16_t product_id = 0x0104, uint16_t product_release = 0x0001);
samux 2:27a7e7f8d399 73
samux 3:0ffb2eee9e06 74 /*
samux 3:0ffb2eee9e06 75 * read a block on a storage chip
samux 3:0ffb2eee9e06 76 *
samux 3:0ffb2eee9e06 77 * @param data pointer where will be stored read data
samux 3:0ffb2eee9e06 78 * @param block block number
samux 3:0ffb2eee9e06 79 * @returns 0 if successful
samux 3:0ffb2eee9e06 80 */
samux 3:0ffb2eee9e06 81 virtual int blockRead(uint8_t * data, uint16_t block){return 1;};
samux 3:0ffb2eee9e06 82
samux 3:0ffb2eee9e06 83 /*
samux 3:0ffb2eee9e06 84 * write a block on a storage chip
samux 3:0ffb2eee9e06 85 *
samux 3:0ffb2eee9e06 86 * @param data data to write
samux 3:0ffb2eee9e06 87 * @param block block number
samux 3:0ffb2eee9e06 88 * @returns 0 if successful
samux 3:0ffb2eee9e06 89 */
samux 3:0ffb2eee9e06 90 virtual int blockWrite(uint8_t * data, uint16_t block){return 1;};
samux 3:0ffb2eee9e06 91
samux 4:980e6470dcce 92
samux 3:0ffb2eee9e06 93 protected:
samux 2:27a7e7f8d399 94
samux 2:27a7e7f8d399 95
samux 2:27a7e7f8d399 96 /*
samux 2:27a7e7f8d399 97 * Get number of logical unit - 1 (here 0)
samux 2:27a7e7f8d399 98 *
samux 2:27a7e7f8d399 99 * @returns Pointer containing the number of logical unit - 1
samux 2:27a7e7f8d399 100 */
samux 2:27a7e7f8d399 101 uint8_t * getMaxLUN();
samux 2:27a7e7f8d399 102
samux 2:27a7e7f8d399 103 /*
samux 2:27a7e7f8d399 104 * Get string product descriptor
samux 2:27a7e7f8d399 105 *
samux 2:27a7e7f8d399 106 * @returns pointer to the string product descriptor
samux 2:27a7e7f8d399 107 */
samux 2:27a7e7f8d399 108 virtual uint8_t * stringIproductDesc();
samux 2:27a7e7f8d399 109
samux 2:27a7e7f8d399 110 /*
samux 2:27a7e7f8d399 111 * Get string interface descriptor
samux 2:27a7e7f8d399 112 *
samux 2:27a7e7f8d399 113 * @returns pointer to the string interface descriptor
samux 2:27a7e7f8d399 114 */
samux 2:27a7e7f8d399 115 virtual uint8_t * stringIinterfaceDesc();
samux 2:27a7e7f8d399 116
samux 2:27a7e7f8d399 117 /*
samux 2:27a7e7f8d399 118 * Get configuration descriptor
samux 2:27a7e7f8d399 119 *
samux 2:27a7e7f8d399 120 * @returns pointer to the configuration descriptor
samux 2:27a7e7f8d399 121 */
samux 2:27a7e7f8d399 122 virtual uint8_t * configurationDesc();
samux 2:27a7e7f8d399 123
samux 2:27a7e7f8d399 124 /*
samux 2:27a7e7f8d399 125 * Callback called when a packet is received
samux 2:27a7e7f8d399 126 */
samux 2:27a7e7f8d399 127 virtual bool EP2_OUT_callback();
samux 2:27a7e7f8d399 128
samux 2:27a7e7f8d399 129 /*
samux 2:27a7e7f8d399 130 * Callback called when a packet has been sent
samux 2:27a7e7f8d399 131 */
samux 2:27a7e7f8d399 132 virtual bool EP2_IN_callback();
samux 2:27a7e7f8d399 133
samux 2:27a7e7f8d399 134 /*
samux 2:27a7e7f8d399 135 * Set configuration of device. Add endpoints
samux 2:27a7e7f8d399 136 */
samux 2:27a7e7f8d399 137 virtual bool USBCallback_setConfiguration(uint8_t configuration);
samux 2:27a7e7f8d399 138
samux 2:27a7e7f8d399 139 /*
samux 2:27a7e7f8d399 140 * Callback called to process class specific requests
samux 2:27a7e7f8d399 141 */
samux 2:27a7e7f8d399 142 virtual bool USBCallback_request();
samux 2:27a7e7f8d399 143
samux 2:27a7e7f8d399 144
samux 2:27a7e7f8d399 145 private:
samux 2:27a7e7f8d399 146 //state of the bulk-only state machine
samux 2:27a7e7f8d399 147 Stage stage;
samux 2:27a7e7f8d399 148
samux 2:27a7e7f8d399 149 // current CBW
samux 2:27a7e7f8d399 150 CBW cbw;
samux 2:27a7e7f8d399 151
samux 2:27a7e7f8d399 152 // CSW which will be sent
samux 2:27a7e7f8d399 153 CSW csw;
samux 2:27a7e7f8d399 154
samux 2:27a7e7f8d399 155 // addr where will be read or written data
samux 2:27a7e7f8d399 156 uint32_t addr;
samux 2:27a7e7f8d399 157
samux 2:27a7e7f8d399 158 // length of a reading or writing
samux 2:27a7e7f8d399 159 uint32_t length;
samux 2:27a7e7f8d399 160
samux 2:27a7e7f8d399 161 // memory OK (after a memoryVerify)
samux 2:27a7e7f8d399 162 bool memOK;
samux 2:27a7e7f8d399 163
samux 2:27a7e7f8d399 164 // cache in RAM before writing in memory. Useful also to read a block.
samux 2:27a7e7f8d399 165 uint8_t page[BlockSize];
samux 2:27a7e7f8d399 166
samux 2:27a7e7f8d399 167 void CBWDecode(uint8_t * buf, uint16_t size);
samux 2:27a7e7f8d399 168 void sendCSW (void);
samux 2:27a7e7f8d399 169 bool inquiryRequest (void);
samux 2:27a7e7f8d399 170 bool write (uint8_t * buf, uint16_t size);
samux 2:27a7e7f8d399 171 bool readFormatCapacity();
samux 2:27a7e7f8d399 172 bool readCapacity (void);
samux 2:27a7e7f8d399 173 bool infoTransfer (void);
samux 2:27a7e7f8d399 174 void memoryRead (void);
samux 2:27a7e7f8d399 175 bool modeSense6 (void);
samux 2:27a7e7f8d399 176 void testUnitReady (void);
samux 2:27a7e7f8d399 177 bool requestSense (void);
samux 2:27a7e7f8d399 178 void memoryVerify (uint8_t * buf, uint16_t size);
samux 2:27a7e7f8d399 179 void memoryWrite (uint8_t * buf, uint16_t size);
samux 2:27a7e7f8d399 180 void reset();
samux 2:27a7e7f8d399 181 void fail();
samux 2:27a7e7f8d399 182 };
samux 2:27a7e7f8d399 183
samux 2:27a7e7f8d399 184 #endif