USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Wed Nov 16 11:06:27 2011 +0000
Revision:
10:cf8fd2b6ca23
Parent:
9:9c343b9ee6d8
Child:
11:a26e7b7a1221
YES!!!!!!!!!!!!!!!!!! msd with sd card and hid generic device works

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 2:27a7e7f8d399 8 */
samux 2:27a7e7f8d399 9
samux 2:27a7e7f8d399 10 #ifndef USBMSD_H
samux 2:27a7e7f8d399 11 #define USBMSD_H
samux 2:27a7e7f8d399 12
samux 2:27a7e7f8d399 13 /* These headers are included for child class. */
samux 2:27a7e7f8d399 14 #include "USBEndpoints.h"
samux 2:27a7e7f8d399 15 #include "USBDescriptor.h"
samux 2:27a7e7f8d399 16 #include "USBDevice_Types.h"
samux 2:27a7e7f8d399 17
samux 2:27a7e7f8d399 18 #include "USBDevice.h"
samux 10:cf8fd2b6ca23 19 #include "USBHID.h"
samux 2:27a7e7f8d399 20
samux 2:27a7e7f8d399 21 #define DEFAULT_CONFIGURATION (1)
samux 2:27a7e7f8d399 22
samux 2:27a7e7f8d399 23
samux 2:27a7e7f8d399 24 // MSC Bulk-only Stage
samux 2:27a7e7f8d399 25 enum Stage {
samux 5:8afbc15d6892 26 READ_CBW, // wait a CBW
samux 6:126c4d980196 27 ERROR, // error
samux 5:8afbc15d6892 28 PROCESS_CBW, // process a CBW request
samux 5:8afbc15d6892 29 SEND_CSW, // send a CSW
samux 5:8afbc15d6892 30 WAIT_CSW, // wait that a CSW has been effectively sent
samux 2:27a7e7f8d399 31 };
samux 2:27a7e7f8d399 32
samux 2:27a7e7f8d399 33 // Bulk-only CBW
samux 2:27a7e7f8d399 34 typedef __packed struct {
samux 2:27a7e7f8d399 35 uint32_t Signature;
samux 2:27a7e7f8d399 36 uint32_t Tag;
samux 2:27a7e7f8d399 37 uint32_t DataLength;
samux 2:27a7e7f8d399 38 uint8_t Flags;
samux 2:27a7e7f8d399 39 uint8_t LUN;
samux 2:27a7e7f8d399 40 uint8_t CBLength;
samux 2:27a7e7f8d399 41 uint8_t CB[16];
samux 2:27a7e7f8d399 42 } CBW;
samux 2:27a7e7f8d399 43
samux 2:27a7e7f8d399 44 // Bulk-only CSW
samux 2:27a7e7f8d399 45 typedef __packed struct {
samux 2:27a7e7f8d399 46 uint32_t Signature;
samux 2:27a7e7f8d399 47 uint32_t Tag;
samux 2:27a7e7f8d399 48 uint32_t DataResidue;
samux 2:27a7e7f8d399 49 uint8_t Status;
samux 2:27a7e7f8d399 50 } CSW;
samux 2:27a7e7f8d399 51
samux 3:0ffb2eee9e06 52
samux 2:27a7e7f8d399 53 class USBMSD: public USBDevice {
samux 2:27a7e7f8d399 54 public:
samux 2:27a7e7f8d399 55
samux 2:27a7e7f8d399 56 /**
samux 2:27a7e7f8d399 57 * Constructor
samux 2:27a7e7f8d399 58 *
samux 2:27a7e7f8d399 59 * @param vendor_id Your vendor_id
samux 2:27a7e7f8d399 60 * @param product_id Your product_id
samux 2:27a7e7f8d399 61 * @param product_release Your preoduct_release
samux 2:27a7e7f8d399 62 */
samux 10:cf8fd2b6ca23 63 USBMSD(uint16_t vendor_id = 0x0843, uint16_t product_id = 0x0154, uint16_t product_release = 0x0001);
samux 10:cf8fd2b6ca23 64
samux 10:cf8fd2b6ca23 65 /**
samux 10:cf8fd2b6ca23 66 * Send a Report
samux 10:cf8fd2b6ca23 67 *
samux 10:cf8fd2b6ca23 68 * @param report Report which will be sent (a report is defined by all data and the length)
samux 10:cf8fd2b6ca23 69 * @returns true if successful
samux 10:cf8fd2b6ca23 70 */
samux 10:cf8fd2b6ca23 71 bool send(HID_REPORT *report);
samux 10:cf8fd2b6ca23 72
samux 10:cf8fd2b6ca23 73 /**
samux 10:cf8fd2b6ca23 74 * Read a report: blocking
samux 10:cf8fd2b6ca23 75 *
samux 10:cf8fd2b6ca23 76 * @param report pointer to the report to fill
samux 10:cf8fd2b6ca23 77 * @returns true if successful
samux 10:cf8fd2b6ca23 78 */
samux 10:cf8fd2b6ca23 79 bool read(HID_REPORT * report);
samux 10:cf8fd2b6ca23 80
samux 10:cf8fd2b6ca23 81 /**
samux 10:cf8fd2b6ca23 82 * Read a report: non blocking
samux 10:cf8fd2b6ca23 83 *
samux 10:cf8fd2b6ca23 84 * @param report pointer to the report to fill
samux 10:cf8fd2b6ca23 85 * @returns true if successful
samux 10:cf8fd2b6ca23 86 */
samux 10:cf8fd2b6ca23 87 bool readNB(HID_REPORT * report);
samux 2:27a7e7f8d399 88
samux 3:0ffb2eee9e06 89 /*
samux 3:0ffb2eee9e06 90 * read a block on a storage chip
samux 3:0ffb2eee9e06 91 *
samux 3:0ffb2eee9e06 92 * @param data pointer where will be stored read data
samux 3:0ffb2eee9e06 93 * @param block block number
samux 3:0ffb2eee9e06 94 * @returns 0 if successful
samux 3:0ffb2eee9e06 95 */
samux 9:9c343b9ee6d8 96 virtual int disk_read(char * data, int block){return 1;};
samux 3:0ffb2eee9e06 97
samux 3:0ffb2eee9e06 98 /*
samux 3:0ffb2eee9e06 99 * write a block on a storage chip
samux 3:0ffb2eee9e06 100 *
samux 3:0ffb2eee9e06 101 * @param data data to write
samux 3:0ffb2eee9e06 102 * @param block block number
samux 3:0ffb2eee9e06 103 * @returns 0 if successful
samux 3:0ffb2eee9e06 104 */
samux 9:9c343b9ee6d8 105 virtual int disk_write(const char * data, int block){return 1;};
samux 3:0ffb2eee9e06 106
samux 6:126c4d980196 107 /*
samux 6:126c4d980196 108 * Disk initilization
samux 6:126c4d980196 109 */
samux 8:534fd41d8cc7 110 virtual int disk_initialize(){return -1;};
samux 9:9c343b9ee6d8 111
samux 6:126c4d980196 112 /*
samux 9:9c343b9ee6d8 113 * Return the number of blocks
samux 6:126c4d980196 114 *
samux 9:9c343b9ee6d8 115 * @returns number of blocks
samux 6:126c4d980196 116 */
samux 8:534fd41d8cc7 117 virtual int disk_sectors(){return 0;};
samux 6:126c4d980196 118
samux 9:9c343b9ee6d8 119 /*
samux 9:9c343b9ee6d8 120 * Return memory size
samux 9:9c343b9ee6d8 121 *
samux 9:9c343b9ee6d8 122 * @returns memory size
samux 9:9c343b9ee6d8 123 */
samux 9:9c343b9ee6d8 124 virtual uint32_t memorySize(){return 0;};
samux 6:126c4d980196 125
samux 6:126c4d980196 126 /*
samux 10:cf8fd2b6ca23 127 * Get the Report descriptor
samux 10:cf8fd2b6ca23 128 *
samux 10:cf8fd2b6ca23 129 * @returns pointer to the report descriptor
samux 10:cf8fd2b6ca23 130 */
samux 10:cf8fd2b6ca23 131 virtual uint8_t * reportDesc();
samux 10:cf8fd2b6ca23 132
samux 10:cf8fd2b6ca23 133 /*
samux 10:cf8fd2b6ca23 134 * Get the length of the report descriptor
samux 10:cf8fd2b6ca23 135 *
samux 10:cf8fd2b6ca23 136 * @returns the length of the report descriptor
samux 10:cf8fd2b6ca23 137 */
samux 10:cf8fd2b6ca23 138 virtual uint16_t reportDescLength();
samux 10:cf8fd2b6ca23 139
samux 10:cf8fd2b6ca23 140 /*
samux 6:126c4d980196 141 * Connect the USB MSD device. Establish disk initialization before really connect the device.
samux 6:126c4d980196 142 *
samux 6:126c4d980196 143 * @returns
samux 6:126c4d980196 144 */
samux 6:126c4d980196 145 bool connect();
samux 6:126c4d980196 146
samux 4:980e6470dcce 147
samux 3:0ffb2eee9e06 148 protected:
samux 10:cf8fd2b6ca23 149 uint16_t reportLength;
samux 2:27a7e7f8d399 150
samux 2:27a7e7f8d399 151
samux 2:27a7e7f8d399 152 /*
samux 2:27a7e7f8d399 153 * Get number of logical unit - 1 (here 0)
samux 2:27a7e7f8d399 154 *
samux 2:27a7e7f8d399 155 * @returns Pointer containing the number of logical unit - 1
samux 2:27a7e7f8d399 156 */
samux 2:27a7e7f8d399 157 uint8_t * getMaxLUN();
samux 2:27a7e7f8d399 158
samux 2:27a7e7f8d399 159 /*
samux 2:27a7e7f8d399 160 * Get string product descriptor
samux 2:27a7e7f8d399 161 *
samux 2:27a7e7f8d399 162 * @returns pointer to the string product descriptor
samux 2:27a7e7f8d399 163 */
samux 2:27a7e7f8d399 164 virtual uint8_t * stringIproductDesc();
samux 2:27a7e7f8d399 165
samux 2:27a7e7f8d399 166 /*
samux 2:27a7e7f8d399 167 * Get string interface descriptor
samux 2:27a7e7f8d399 168 *
samux 2:27a7e7f8d399 169 * @returns pointer to the string interface descriptor
samux 2:27a7e7f8d399 170 */
samux 2:27a7e7f8d399 171 virtual uint8_t * stringIinterfaceDesc();
samux 2:27a7e7f8d399 172
samux 2:27a7e7f8d399 173 /*
samux 2:27a7e7f8d399 174 * Get configuration descriptor
samux 2:27a7e7f8d399 175 *
samux 2:27a7e7f8d399 176 * @returns pointer to the configuration descriptor
samux 2:27a7e7f8d399 177 */
samux 2:27a7e7f8d399 178 virtual uint8_t * configurationDesc();
samux 2:27a7e7f8d399 179
samux 2:27a7e7f8d399 180 /*
samux 2:27a7e7f8d399 181 * Callback called when a packet is received
samux 2:27a7e7f8d399 182 */
samux 2:27a7e7f8d399 183 virtual bool EP2_OUT_callback();
samux 2:27a7e7f8d399 184
samux 2:27a7e7f8d399 185 /*
samux 2:27a7e7f8d399 186 * Callback called when a packet has been sent
samux 2:27a7e7f8d399 187 */
samux 2:27a7e7f8d399 188 virtual bool EP2_IN_callback();
samux 2:27a7e7f8d399 189
samux 2:27a7e7f8d399 190 /*
samux 2:27a7e7f8d399 191 * Set configuration of device. Add endpoints
samux 2:27a7e7f8d399 192 */
samux 2:27a7e7f8d399 193 virtual bool USBCallback_setConfiguration(uint8_t configuration);
samux 2:27a7e7f8d399 194
samux 2:27a7e7f8d399 195 /*
samux 2:27a7e7f8d399 196 * Callback called to process class specific requests
samux 2:27a7e7f8d399 197 */
samux 2:27a7e7f8d399 198 virtual bool USBCallback_request();
samux 2:27a7e7f8d399 199
samux 2:27a7e7f8d399 200
samux 2:27a7e7f8d399 201 private:
samux 2:27a7e7f8d399 202 //state of the bulk-only state machine
samux 2:27a7e7f8d399 203 Stage stage;
samux 2:27a7e7f8d399 204
samux 2:27a7e7f8d399 205 // current CBW
samux 2:27a7e7f8d399 206 CBW cbw;
samux 2:27a7e7f8d399 207
samux 2:27a7e7f8d399 208 // CSW which will be sent
samux 2:27a7e7f8d399 209 CSW csw;
samux 2:27a7e7f8d399 210
samux 2:27a7e7f8d399 211 // addr where will be read or written data
samux 2:27a7e7f8d399 212 uint32_t addr;
samux 2:27a7e7f8d399 213
samux 2:27a7e7f8d399 214 // length of a reading or writing
samux 2:27a7e7f8d399 215 uint32_t length;
samux 2:27a7e7f8d399 216
samux 2:27a7e7f8d399 217 // memory OK (after a memoryVerify)
samux 2:27a7e7f8d399 218 bool memOK;
samux 2:27a7e7f8d399 219
samux 2:27a7e7f8d399 220 // cache in RAM before writing in memory. Useful also to read a block.
samux 6:126c4d980196 221 uint8_t * page;
samux 6:126c4d980196 222
samux 6:126c4d980196 223 uint16_t BlockSize;
samux 6:126c4d980196 224 uint32_t MemorySize;
samux 9:9c343b9ee6d8 225 uint16_t BlockCount;
samux 2:27a7e7f8d399 226
samux 2:27a7e7f8d399 227 void CBWDecode(uint8_t * buf, uint16_t size);
samux 2:27a7e7f8d399 228 void sendCSW (void);
samux 2:27a7e7f8d399 229 bool inquiryRequest (void);
samux 2:27a7e7f8d399 230 bool write (uint8_t * buf, uint16_t size);
samux 2:27a7e7f8d399 231 bool readFormatCapacity();
samux 2:27a7e7f8d399 232 bool readCapacity (void);
samux 2:27a7e7f8d399 233 bool infoTransfer (void);
samux 2:27a7e7f8d399 234 void memoryRead (void);
samux 2:27a7e7f8d399 235 bool modeSense6 (void);
samux 2:27a7e7f8d399 236 void testUnitReady (void);
samux 2:27a7e7f8d399 237 bool requestSense (void);
samux 2:27a7e7f8d399 238 void memoryVerify (uint8_t * buf, uint16_t size);
samux 2:27a7e7f8d399 239 void memoryWrite (uint8_t * buf, uint16_t size);
samux 2:27a7e7f8d399 240 void reset();
samux 2:27a7e7f8d399 241 void fail();
samux 10:cf8fd2b6ca23 242
samux 10:cf8fd2b6ca23 243 HID_REPORT outputReport;
samux 2:27a7e7f8d399 244 };
samux 2:27a7e7f8d399 245
samux 2:27a7e7f8d399 246 #endif