USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Sun Dec 11 15:56:08 2011 +0000
Revision:
20:50bf1cd2e2fe
Parent:
19:148a0b8d23bc
big cleaning up

Who changed what in which revision?

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