USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Tue Dec 06 14:07:51 2011 +0000
Revision:
15:f848b71c4440
Parent:
14:757226626acb
forget a pure virtual function: disk_read

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 14:757226626acb 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
samux 14:757226626acb 2 *
samux 14:757226626acb 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 14:757226626acb 4 * and associated documentation files (the "Software"), to deal in the Software without
samux 14:757226626acb 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
samux 14:757226626acb 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
samux 14:757226626acb 7 * Software is furnished to do so, subject to the following conditions:
samux 14:757226626acb 8 *
samux 14:757226626acb 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 14:757226626acb 10 * substantial portions of the Software.
samux 14:757226626acb 11 *
samux 14:757226626acb 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 14:757226626acb 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 14:757226626acb 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 14:757226626acb 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 14:757226626acb 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 14:757226626acb 17 */
samux 14:757226626acb 18
samux 14:757226626acb 19
samux 15:f848b71c4440 20
samux 14:757226626acb 21 /* Introduction
samux 14:757226626acb 22 * ------------
samux 14:757226626acb 23 * The USBMSD implements the MSD protocol. It permits to access a memory chip (flash, sdcard,...)
samux 14:757226626acb 24 * from a computer over USB. But this class doesn't work standalone, you need to subclass this class
samux 14:757226626acb 25 * and define virtual functions which are called in USBMSD.
samux 14:757226626acb 26 *
samux 14:757226626acb 27 * How to use this class with your chip ?
samux 14:757226626acb 28 * --------------------------------------
samux 14:757226626acb 29 * You have to inherit and define some pure virtual functions (mandatory step):
samux 14:757226626acb 30 * - virtual int disk_read(char * data, int block): function to read a block
samux 14:757226626acb 31 * - virtual int disk_write(const char * data, int block): function to write a block
samux 14:757226626acb 32 * - virtual int disk_initialize(): function to initialize the memory
samux 14:757226626acb 33 * - virtual int disk_sectors(): return the number of blocks
samux 14:757226626acb 34 * - virtual int disk_size(): return the memory size
samux 14:757226626acb 35 *
samux 14:757226626acb 36 * All functions names are compatible with the fat filesystem library. So you can imagine using your own class with either
samux 14:757226626acb 37 * USBMSD or a fat filesystem library.
samux 14:757226626acb 38 *
samux 14:757226626acb 39 * Once these functions have been defined, you can call connect() (at the end of the constructor of your class for instance)
samux 14:757226626acb 40 * of USBMSD to connect your mass storage device ;)
samux 14:757226626acb 41 */
samux 14:757226626acb 42
samux 14:757226626acb 43 #ifndef USBMSD_H
samux 14:757226626acb 44 #define USBMSD_H
samux 14:757226626acb 45
samux 14:757226626acb 46 /* These headers are included for child class. */
samux 14:757226626acb 47 #include "USBEndpoints.h"
samux 14:757226626acb 48 #include "USBDescriptor.h"
samux 14:757226626acb 49 #include "USBDevice_Types.h"
samux 14:757226626acb 50
samux 14:757226626acb 51 #include "USBDevice.h"
samux 14:757226626acb 52
samux 14:757226626acb 53 class USBMSD: public USBDevice {
samux 14:757226626acb 54 public:
samux 14:757226626acb 55
samux 14:757226626acb 56 /**
samux 14:757226626acb 57 * Constructor
samux 14:757226626acb 58 *
samux 14:757226626acb 59 * @param vendor_id Your vendor_id
samux 14:757226626acb 60 * @param product_id Your product_id
samux 14:757226626acb 61 * @param product_release Your preoduct_release
samux 14:757226626acb 62 */
samux 14:757226626acb 63 USBMSD(uint16_t vendor_id = 0x0703, uint16_t product_id = 0x0104, uint16_t product_release = 0x0001);
samux 14:757226626acb 64
samux 14:757226626acb 65 /**
samux 14:757226626acb 66 * Connect the USB MSD device. Establish disk initialization, read memory capacity before really connecting the device.
samux 14:757226626acb 67 *
samux 14:757226626acb 68 * @returns
samux 14:757226626acb 69 */
samux 14:757226626acb 70 bool connect();
samux 14:757226626acb 71
samux 14:757226626acb 72 protected:
samux 14:757226626acb 73
samux 14:757226626acb 74 /*
samux 14:757226626acb 75 * read a block on a storage chip
samux 14:757226626acb 76 *
samux 14:757226626acb 77 * @param data pointer where will be stored read data
samux 14:757226626acb 78 * @param block block number
samux 14:757226626acb 79 * @returns 0 if successful
samux 14:757226626acb 80 */
samux 15:f848b71c4440 81 virtual int disk_read(char * data, int block) = 0;
samux 14:757226626acb 82
samux 14:757226626acb 83 /*
samux 14:757226626acb 84 * write a block on a storage chip
samux 14:757226626acb 85 *
samux 14:757226626acb 86 * @param data data to write
samux 14:757226626acb 87 * @param block block number
samux 14:757226626acb 88 * @returns 0 if successful
samux 14:757226626acb 89 */
samux 14:757226626acb 90 virtual int disk_write(const char * data, int block) = 0;
samux 14:757226626acb 91
samux 14:757226626acb 92 /*
samux 14:757226626acb 93 * Disk initilization
samux 14:757226626acb 94 */
samux 14:757226626acb 95 virtual int disk_initialize() = 0;
samux 14:757226626acb 96
samux 14:757226626acb 97 /*
samux 14:757226626acb 98 * Return the number of blocks
samux 14:757226626acb 99 *
samux 14:757226626acb 100 * @returns number of blocks
samux 14:757226626acb 101 */
samux 14:757226626acb 102 virtual int disk_sectors() = 0;
samux 14:757226626acb 103
samux 14:757226626acb 104 /*
samux 14:757226626acb 105 * Return memory size
samux 14:757226626acb 106 *
samux 14:757226626acb 107 * @returns memory size
samux 14:757226626acb 108 */
samux 14:757226626acb 109 virtual int disk_size() = 0;
samux 14:757226626acb 110
samux 14:757226626acb 111 /*
samux 14:757226626acb 112 * Get string product descriptor
samux 14:757226626acb 113 *
samux 14:757226626acb 114 * @returns pointer to the string product descriptor
samux 14:757226626acb 115 */
samux 14:757226626acb 116 virtual uint8_t * stringIproductDesc();
samux 14:757226626acb 117
samux 14:757226626acb 118 /*
samux 14:757226626acb 119 * Get string interface descriptor
samux 14:757226626acb 120 *
samux 14:757226626acb 121 * @returns pointer to the string interface descriptor
samux 14:757226626acb 122 */
samux 14:757226626acb 123 virtual uint8_t * stringIinterfaceDesc();
samux 14:757226626acb 124
samux 14:757226626acb 125 /*
samux 14:757226626acb 126 * Get configuration descriptor
samux 14:757226626acb 127 *
samux 14:757226626acb 128 * @returns pointer to the configuration descriptor
samux 14:757226626acb 129 */
samux 14:757226626acb 130 virtual uint8_t * configurationDesc();
samux 14:757226626acb 131
samux 14:757226626acb 132 /*
samux 14:757226626acb 133 * Callback called when a packet is received
samux 14:757226626acb 134 */
samux 14:757226626acb 135 virtual bool EP2_OUT_callback();
samux 14:757226626acb 136
samux 14:757226626acb 137 /*
samux 14:757226626acb 138 * Callback called when a packet has been sent
samux 14:757226626acb 139 */
samux 14:757226626acb 140 virtual bool EP2_IN_callback();
samux 14:757226626acb 141
samux 14:757226626acb 142 /*
samux 14:757226626acb 143 * Set configuration of device. Add endpoints
samux 14:757226626acb 144 */
samux 14:757226626acb 145 virtual bool USBCallback_setConfiguration(uint8_t configuration);
samux 14:757226626acb 146
samux 14:757226626acb 147 /*
samux 14:757226626acb 148 * Callback called to process class specific requests
samux 14:757226626acb 149 */
samux 14:757226626acb 150 virtual bool USBCallback_request();
samux 14:757226626acb 151
samux 14:757226626acb 152
samux 14:757226626acb 153 private:
samux 14:757226626acb 154
samux 14:757226626acb 155 // MSC Bulk-only Stage
samux 14:757226626acb 156 enum Stage {
samux 14:757226626acb 157 READ_CBW, // wait a CBW
samux 14:757226626acb 158 ERROR, // error
samux 14:757226626acb 159 PROCESS_CBW, // process a CBW request
samux 14:757226626acb 160 SEND_CSW, // send a CSW
samux 14:757226626acb 161 WAIT_CSW, // wait that a CSW has been effectively sent
samux 14:757226626acb 162 };
samux 14:757226626acb 163
samux 14:757226626acb 164 // Bulk-only CBW
samux 14:757226626acb 165 typedef __packed struct {
samux 14:757226626acb 166 uint32_t Signature;
samux 14:757226626acb 167 uint32_t Tag;
samux 14:757226626acb 168 uint32_t DataLength;
samux 14:757226626acb 169 uint8_t Flags;
samux 14:757226626acb 170 uint8_t LUN;
samux 14:757226626acb 171 uint8_t CBLength;
samux 14:757226626acb 172 uint8_t CB[16];
samux 14:757226626acb 173 } CBW;
samux 14:757226626acb 174
samux 14:757226626acb 175 // Bulk-only CSW
samux 14:757226626acb 176 typedef __packed struct {
samux 14:757226626acb 177 uint32_t Signature;
samux 14:757226626acb 178 uint32_t Tag;
samux 14:757226626acb 179 uint32_t DataResidue;
samux 14:757226626acb 180 uint8_t Status;
samux 14:757226626acb 181 } CSW;
samux 14:757226626acb 182
samux 14:757226626acb 183
samux 14:757226626acb 184 //state of the bulk-only state machine
samux 14:757226626acb 185 Stage stage;
samux 14:757226626acb 186
samux 14:757226626acb 187 // current CBW
samux 14:757226626acb 188 CBW cbw;
samux 14:757226626acb 189
samux 14:757226626acb 190 // CSW which will be sent
samux 14:757226626acb 191 CSW csw;
samux 14:757226626acb 192
samux 14:757226626acb 193 // addr where will be read or written data
samux 14:757226626acb 194 uint32_t addr;
samux 14:757226626acb 195
samux 14:757226626acb 196 // length of a reading or writing
samux 14:757226626acb 197 int32_t length;
samux 14:757226626acb 198
samux 14:757226626acb 199 // memory OK (after a memoryVerify)
samux 14:757226626acb 200 bool memOK;
samux 14:757226626acb 201
samux 14:757226626acb 202 // cache in RAM before writing in memory. Useful also to read a block.
samux 14:757226626acb 203 uint8_t * page;
samux 14:757226626acb 204
samux 14:757226626acb 205 // size of a single block
samux 14:757226626acb 206 uint32_t BlockSize;
samux 14:757226626acb 207
samux 14:757226626acb 208 // memory size
samux 14:757226626acb 209 uint32_t MemorySize;
samux 14:757226626acb 210
samux 14:757226626acb 211 //number of block
samux 14:757226626acb 212 uint32_t BlockCount;
samux 14:757226626acb 213
samux 14:757226626acb 214 bool lastRead;
samux 14:757226626acb 215
samux 14:757226626acb 216 void CBWDecode(uint8_t * buf, uint16_t size);
samux 14:757226626acb 217 void sendCSW (void);
samux 14:757226626acb 218 bool inquiryRequest (void);
samux 14:757226626acb 219 bool write (uint8_t * buf, uint16_t size);
samux 14:757226626acb 220 bool readFormatCapacity();
samux 14:757226626acb 221 bool readCapacity (void);
samux 14:757226626acb 222 bool infoTransfer (void);
samux 14:757226626acb 223 void memoryRead (void);
samux 14:757226626acb 224 bool modeSense6 (void);
samux 14:757226626acb 225 void testUnitReady (void);
samux 14:757226626acb 226 bool requestSense (void);
samux 14:757226626acb 227 void memoryVerify (uint8_t * buf, uint16_t size);
samux 14:757226626acb 228 void memoryWrite (uint8_t * buf, uint16_t size);
samux 14:757226626acb 229 void reset();
samux 14:757226626acb 230 void fail();
samux 14:757226626acb 231 };
samux 14:757226626acb 232
samux 14:757226626acb 233 #endif