USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Mon Nov 14 17:50:02 2011 +0000
Revision:
8:534fd41d8cc7
Parent:
7:6494da2a5c60
Child:
9:9c343b9ee6d8
doesn\'t work...

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 2:27a7e7f8d399 19
samux 2:27a7e7f8d399 20 #define DEFAULT_CONFIGURATION (1)
samux 2:27a7e7f8d399 21
samux 2:27a7e7f8d399 22
samux 2:27a7e7f8d399 23 // MSC Bulk-only Stage
samux 2:27a7e7f8d399 24 enum Stage {
samux 5:8afbc15d6892 25 READ_CBW, // wait a CBW
samux 6:126c4d980196 26 ERROR, // error
samux 5:8afbc15d6892 27 PROCESS_CBW, // process a CBW request
samux 5:8afbc15d6892 28 SEND_CSW, // send a CSW
samux 5:8afbc15d6892 29 WAIT_CSW, // wait that a CSW has been effectively sent
samux 2:27a7e7f8d399 30 };
samux 2:27a7e7f8d399 31
samux 2:27a7e7f8d399 32 // Bulk-only CBW
samux 2:27a7e7f8d399 33 typedef __packed struct {
samux 2:27a7e7f8d399 34 uint32_t Signature;
samux 2:27a7e7f8d399 35 uint32_t Tag;
samux 2:27a7e7f8d399 36 uint32_t DataLength;
samux 2:27a7e7f8d399 37 uint8_t Flags;
samux 2:27a7e7f8d399 38 uint8_t LUN;
samux 2:27a7e7f8d399 39 uint8_t CBLength;
samux 2:27a7e7f8d399 40 uint8_t CB[16];
samux 2:27a7e7f8d399 41 } CBW;
samux 2:27a7e7f8d399 42
samux 2:27a7e7f8d399 43 // Bulk-only CSW
samux 2:27a7e7f8d399 44 typedef __packed struct {
samux 2:27a7e7f8d399 45 uint32_t Signature;
samux 2:27a7e7f8d399 46 uint32_t Tag;
samux 2:27a7e7f8d399 47 uint32_t DataResidue;
samux 2:27a7e7f8d399 48 uint8_t Status;
samux 2:27a7e7f8d399 49 } CSW;
samux 2:27a7e7f8d399 50
samux 3:0ffb2eee9e06 51
samux 2:27a7e7f8d399 52 class USBMSD: public USBDevice {
samux 2:27a7e7f8d399 53 public:
samux 2:27a7e7f8d399 54
samux 2:27a7e7f8d399 55 /**
samux 2:27a7e7f8d399 56 * Constructor
samux 2:27a7e7f8d399 57 *
samux 2:27a7e7f8d399 58 * @param vendor_id Your vendor_id
samux 2:27a7e7f8d399 59 * @param product_id Your product_id
samux 2:27a7e7f8d399 60 * @param product_release Your preoduct_release
samux 2:27a7e7f8d399 61 */
samux 2:27a7e7f8d399 62 USBMSD(uint16_t vendor_id = 0x0703, uint16_t product_id = 0x0104, uint16_t product_release = 0x0001);
samux 2:27a7e7f8d399 63
samux 3:0ffb2eee9e06 64 /*
samux 3:0ffb2eee9e06 65 * read a block on a storage chip
samux 3:0ffb2eee9e06 66 *
samux 3:0ffb2eee9e06 67 * @param data pointer where will be stored read data
samux 3:0ffb2eee9e06 68 * @param block block number
samux 3:0ffb2eee9e06 69 * @returns 0 if successful
samux 3:0ffb2eee9e06 70 */
samux 8:534fd41d8cc7 71 virtual int disk_read(char *buffer, int block_number){return 1;};
samux 3:0ffb2eee9e06 72
samux 3:0ffb2eee9e06 73 /*
samux 3:0ffb2eee9e06 74 * write a block on a storage chip
samux 3:0ffb2eee9e06 75 *
samux 3:0ffb2eee9e06 76 * @param data data to write
samux 3:0ffb2eee9e06 77 * @param block block number
samux 3:0ffb2eee9e06 78 * @returns 0 if successful
samux 3:0ffb2eee9e06 79 */
samux 8:534fd41d8cc7 80 virtual int disk_write(const char *buffer, int block_number){return 1;};
samux 3:0ffb2eee9e06 81
samux 6:126c4d980196 82 /*
samux 6:126c4d980196 83 * Disk initilization
samux 6:126c4d980196 84 */
samux 8:534fd41d8cc7 85 virtual int disk_initialize(){return -1;};
samux 8:534fd41d8cc7 86
samux 6:126c4d980196 87 /*
samux 8:534fd41d8cc7 88 * Return number of sectors
samux 6:126c4d980196 89 *
samux 8:534fd41d8cc7 90 * @returns number of sectors
samux 6:126c4d980196 91 */
samux 8:534fd41d8cc7 92 virtual int disk_sectors(){return 0;};
samux 6:126c4d980196 93
samux 8:534fd41d8cc7 94 virtual void set_status(int st){};
samux 6:126c4d980196 95
samux 6:126c4d980196 96 /*
samux 6:126c4d980196 97 * Connect the USB MSD device. Establish disk initialization before really connect the device.
samux 6:126c4d980196 98 *
samux 6:126c4d980196 99 * @returns
samux 6:126c4d980196 100 */
samux 6:126c4d980196 101 bool connect();
samux 6:126c4d980196 102
samux 4:980e6470dcce 103
samux 3:0ffb2eee9e06 104 protected:
samux 2:27a7e7f8d399 105
samux 2:27a7e7f8d399 106
samux 2:27a7e7f8d399 107 /*
samux 2:27a7e7f8d399 108 * Get number of logical unit - 1 (here 0)
samux 2:27a7e7f8d399 109 *
samux 2:27a7e7f8d399 110 * @returns Pointer containing the number of logical unit - 1
samux 2:27a7e7f8d399 111 */
samux 2:27a7e7f8d399 112 uint8_t * getMaxLUN();
samux 2:27a7e7f8d399 113
samux 2:27a7e7f8d399 114 /*
samux 2:27a7e7f8d399 115 * Get string product descriptor
samux 2:27a7e7f8d399 116 *
samux 2:27a7e7f8d399 117 * @returns pointer to the string product descriptor
samux 2:27a7e7f8d399 118 */
samux 2:27a7e7f8d399 119 virtual uint8_t * stringIproductDesc();
samux 2:27a7e7f8d399 120
samux 2:27a7e7f8d399 121 /*
samux 2:27a7e7f8d399 122 * Get string interface descriptor
samux 2:27a7e7f8d399 123 *
samux 2:27a7e7f8d399 124 * @returns pointer to the string interface descriptor
samux 2:27a7e7f8d399 125 */
samux 2:27a7e7f8d399 126 virtual uint8_t * stringIinterfaceDesc();
samux 2:27a7e7f8d399 127
samux 2:27a7e7f8d399 128 /*
samux 2:27a7e7f8d399 129 * Get configuration descriptor
samux 2:27a7e7f8d399 130 *
samux 2:27a7e7f8d399 131 * @returns pointer to the configuration descriptor
samux 2:27a7e7f8d399 132 */
samux 2:27a7e7f8d399 133 virtual uint8_t * configurationDesc();
samux 2:27a7e7f8d399 134
samux 2:27a7e7f8d399 135 /*
samux 2:27a7e7f8d399 136 * Callback called when a packet is received
samux 2:27a7e7f8d399 137 */
samux 2:27a7e7f8d399 138 virtual bool EP2_OUT_callback();
samux 2:27a7e7f8d399 139
samux 2:27a7e7f8d399 140 /*
samux 2:27a7e7f8d399 141 * Callback called when a packet has been sent
samux 2:27a7e7f8d399 142 */
samux 2:27a7e7f8d399 143 virtual bool EP2_IN_callback();
samux 2:27a7e7f8d399 144
samux 2:27a7e7f8d399 145 /*
samux 2:27a7e7f8d399 146 * Set configuration of device. Add endpoints
samux 2:27a7e7f8d399 147 */
samux 2:27a7e7f8d399 148 virtual bool USBCallback_setConfiguration(uint8_t configuration);
samux 2:27a7e7f8d399 149
samux 2:27a7e7f8d399 150 /*
samux 2:27a7e7f8d399 151 * Callback called to process class specific requests
samux 2:27a7e7f8d399 152 */
samux 2:27a7e7f8d399 153 virtual bool USBCallback_request();
samux 2:27a7e7f8d399 154
samux 2:27a7e7f8d399 155
samux 2:27a7e7f8d399 156 private:
samux 2:27a7e7f8d399 157 //state of the bulk-only state machine
samux 2:27a7e7f8d399 158 Stage stage;
samux 2:27a7e7f8d399 159
samux 2:27a7e7f8d399 160 // current CBW
samux 2:27a7e7f8d399 161 CBW cbw;
samux 2:27a7e7f8d399 162
samux 2:27a7e7f8d399 163 // CSW which will be sent
samux 2:27a7e7f8d399 164 CSW csw;
samux 2:27a7e7f8d399 165
samux 2:27a7e7f8d399 166 // addr where will be read or written data
samux 2:27a7e7f8d399 167 uint32_t addr;
samux 2:27a7e7f8d399 168
samux 2:27a7e7f8d399 169 // length of a reading or writing
samux 2:27a7e7f8d399 170 uint32_t length;
samux 2:27a7e7f8d399 171
samux 2:27a7e7f8d399 172 // memory OK (after a memoryVerify)
samux 2:27a7e7f8d399 173 bool memOK;
samux 2:27a7e7f8d399 174
samux 2:27a7e7f8d399 175 // cache in RAM before writing in memory. Useful also to read a block.
samux 6:126c4d980196 176 uint8_t * page;
samux 6:126c4d980196 177
samux 6:126c4d980196 178 uint16_t BlockSize;
samux 6:126c4d980196 179 uint32_t MemorySize;
samux 8:534fd41d8cc7 180 uint32_t BlockCount;
samux 2:27a7e7f8d399 181
samux 2:27a7e7f8d399 182 void CBWDecode(uint8_t * buf, uint16_t size);
samux 2:27a7e7f8d399 183 void sendCSW (void);
samux 2:27a7e7f8d399 184 bool inquiryRequest (void);
samux 2:27a7e7f8d399 185 bool write (uint8_t * buf, uint16_t size);
samux 2:27a7e7f8d399 186 bool readFormatCapacity();
samux 2:27a7e7f8d399 187 bool readCapacity (void);
samux 2:27a7e7f8d399 188 bool infoTransfer (void);
samux 2:27a7e7f8d399 189 void memoryRead (void);
samux 2:27a7e7f8d399 190 bool modeSense6 (void);
samux 2:27a7e7f8d399 191 void testUnitReady (void);
samux 2:27a7e7f8d399 192 bool requestSense (void);
samux 2:27a7e7f8d399 193 void memoryVerify (uint8_t * buf, uint16_t size);
samux 2:27a7e7f8d399 194 void memoryWrite (uint8_t * buf, uint16_t size);
samux 2:27a7e7f8d399 195 void reset();
samux 2:27a7e7f8d399 196 void fail();
samux 2:27a7e7f8d399 197 };
samux 2:27a7e7f8d399 198
samux 2:27a7e7f8d399 199 #endif