USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Sun Dec 11 15:42:23 2011 +0000
Revision:
19:148a0b8d23bc
Parent:
18:08b207d10056
Child:
20:50bf1cd2e2fe
add disk_status to check if init and write protection

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 19:148a0b8d23bc 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 #define DEFAULT_CONFIGURATION (1)
samux 17:364ef42e502d 54
samux 17:364ef42e502d 55
samux 17:364ef42e502d 56 // MSC Bulk-only Stage
samux 17:364ef42e502d 57 enum Stage {
samux 17:364ef42e502d 58 READ_CBW, // wait a CBW
samux 17:364ef42e502d 59 ERROR, // error
samux 17:364ef42e502d 60 PROCESS_CBW, // process a CBW request
samux 17:364ef42e502d 61 SEND_CSW, // send a CSW
samux 17:364ef42e502d 62 WAIT_CSW, // wait that a CSW has been effectively sent
samux 17:364ef42e502d 63 };
samux 17:364ef42e502d 64
samux 17:364ef42e502d 65 // Bulk-only CBW
samux 17:364ef42e502d 66 typedef __packed struct {
samux 17:364ef42e502d 67 uint32_t Signature;
samux 17:364ef42e502d 68 uint32_t Tag;
samux 17:364ef42e502d 69 uint32_t DataLength;
samux 17:364ef42e502d 70 uint8_t Flags;
samux 17:364ef42e502d 71 uint8_t LUN;
samux 17:364ef42e502d 72 uint8_t CBLength;
samux 17:364ef42e502d 73 uint8_t CB[16];
samux 17:364ef42e502d 74 } CBW;
samux 17:364ef42e502d 75
samux 17:364ef42e502d 76 // Bulk-only CSW
samux 17:364ef42e502d 77 typedef __packed struct {
samux 17:364ef42e502d 78 uint32_t Signature;
samux 17:364ef42e502d 79 uint32_t Tag;
samux 17:364ef42e502d 80 uint32_t DataResidue;
samux 17:364ef42e502d 81 uint8_t Status;
samux 17:364ef42e502d 82 } CSW;
samux 17:364ef42e502d 83
samux 17:364ef42e502d 84
samux 17:364ef42e502d 85 class USBMSD: public USBDevice {
samux 17:364ef42e502d 86 public:
samux 17:364ef42e502d 87
samux 17:364ef42e502d 88 /**
samux 17:364ef42e502d 89 * Constructor
samux 17:364ef42e502d 90 *
samux 17:364ef42e502d 91 * @param vendor_id Your vendor_id
samux 17:364ef42e502d 92 * @param product_id Your product_id
samux 17:364ef42e502d 93 * @param product_release Your preoduct_release
samux 17:364ef42e502d 94 */
samux 17:364ef42e502d 95 USBMSD(uint16_t vendor_id = 0x0703, uint16_t product_id = 0x0104, uint16_t product_release = 0x0001);
samux 17:364ef42e502d 96
samux 17:364ef42e502d 97 /*
samux 17:364ef42e502d 98 * read a block on a storage chip
samux 17:364ef42e502d 99 *
samux 17:364ef42e502d 100 * @param data pointer where will be stored read data
samux 17:364ef42e502d 101 * @param block block number
samux 17:364ef42e502d 102 * @returns 0 if successful
samux 17:364ef42e502d 103 */
samux 17:364ef42e502d 104 virtual int disk_read(char * data, int block) = 0;
samux 17:364ef42e502d 105
samux 17:364ef42e502d 106 /*
samux 17:364ef42e502d 107 * write a block on a storage chip
samux 17:364ef42e502d 108 *
samux 17:364ef42e502d 109 * @param data data to write
samux 17:364ef42e502d 110 * @param block block number
samux 17:364ef42e502d 111 * @returns 0 if successful
samux 17:364ef42e502d 112 */
samux 17:364ef42e502d 113 virtual int disk_write(const char * data, int block) = 0;
samux 17:364ef42e502d 114
samux 17:364ef42e502d 115 /*
samux 17:364ef42e502d 116 * Disk initilization
samux 17:364ef42e502d 117 */
samux 17:364ef42e502d 118 virtual int disk_initialize() = 0;
samux 17:364ef42e502d 119
samux 17:364ef42e502d 120 /*
samux 17:364ef42e502d 121 * Return the number of blocks
samux 17:364ef42e502d 122 *
samux 17:364ef42e502d 123 * @returns number of blocks
samux 17:364ef42e502d 124 */
samux 17:364ef42e502d 125 virtual int disk_sectors() = 0;
samux 17:364ef42e502d 126
samux 17:364ef42e502d 127 /*
samux 17:364ef42e502d 128 * Return memory size
samux 17:364ef42e502d 129 *
samux 17:364ef42e502d 130 * @returns memory size
samux 17:364ef42e502d 131 */
samux 17:364ef42e502d 132 virtual int disk_size() = 0;
samux 19:148a0b8d23bc 133
samux 19:148a0b8d23bc 134
samux 19:148a0b8d23bc 135 /*
samux 19:148a0b8d23bc 136 * To check the status of the storage chip
samux 19:148a0b8d23bc 137 *
samux 19:148a0b8d23bc 138 * @returns status: 0: OK, 1: disk not initialized, 2: no medium in the drive, 4: write protected
samux 19:148a0b8d23bc 139 */
samux 19:148a0b8d23bc 140 virtual int disk_status() = 0;
samux 17:364ef42e502d 141
samux 17:364ef42e502d 142 /*
samux 17:364ef42e502d 143 * Connect the USB MSD device. Establish disk initialization before really connect the device.
samux 17:364ef42e502d 144 *
samux 17:364ef42e502d 145 * @returns
samux 17:364ef42e502d 146 */
samux 17:364ef42e502d 147 bool connect();
samux 17:364ef42e502d 148
samux 17:364ef42e502d 149
samux 17:364ef42e502d 150 protected:
samux 17:364ef42e502d 151
samux 17:364ef42e502d 152
samux 17:364ef42e502d 153 /*
samux 17:364ef42e502d 154 * Get number of logical unit - 1 (here 0)
samux 17:364ef42e502d 155 *
samux 17:364ef42e502d 156 * @returns Pointer containing the number of logical unit - 1
samux 17:364ef42e502d 157 */
samux 17:364ef42e502d 158 uint8_t * getMaxLUN();
samux 17:364ef42e502d 159
samux 17:364ef42e502d 160 /*
samux 17:364ef42e502d 161 * Get string product descriptor
samux 17:364ef42e502d 162 *
samux 17:364ef42e502d 163 * @returns pointer to the string product descriptor
samux 17:364ef42e502d 164 */
samux 17:364ef42e502d 165 virtual uint8_t * stringIproductDesc();
samux 17:364ef42e502d 166
samux 17:364ef42e502d 167 /*
samux 17:364ef42e502d 168 * Get string interface descriptor
samux 17:364ef42e502d 169 *
samux 17:364ef42e502d 170 * @returns pointer to the string interface descriptor
samux 17:364ef42e502d 171 */
samux 17:364ef42e502d 172 virtual uint8_t * stringIinterfaceDesc();
samux 17:364ef42e502d 173
samux 17:364ef42e502d 174 /*
samux 17:364ef42e502d 175 * Get configuration descriptor
samux 17:364ef42e502d 176 *
samux 17:364ef42e502d 177 * @returns pointer to the configuration descriptor
samux 17:364ef42e502d 178 */
samux 17:364ef42e502d 179 virtual uint8_t * configurationDesc();
samux 17:364ef42e502d 180
samux 17:364ef42e502d 181 /*
samux 17:364ef42e502d 182 * Callback called when a packet is received
samux 17:364ef42e502d 183 */
samux 17:364ef42e502d 184 virtual bool EP2_OUT_callback();
samux 17:364ef42e502d 185
samux 17:364ef42e502d 186 /*
samux 17:364ef42e502d 187 * Callback called when a packet has been sent
samux 17:364ef42e502d 188 */
samux 17:364ef42e502d 189 virtual bool EP2_IN_callback();
samux 17:364ef42e502d 190
samux 17:364ef42e502d 191 /*
samux 17:364ef42e502d 192 * Set configuration of device. Add endpoints
samux 17:364ef42e502d 193 */
samux 17:364ef42e502d 194 virtual bool USBCallback_setConfiguration(uint8_t configuration);
samux 17:364ef42e502d 195
samux 17:364ef42e502d 196 /*
samux 17:364ef42e502d 197 * Callback called to process class specific requests
samux 17:364ef42e502d 198 */
samux 17:364ef42e502d 199 virtual bool USBCallback_request();
samux 17:364ef42e502d 200
samux 17:364ef42e502d 201
samux 17:364ef42e502d 202 private:
samux 17:364ef42e502d 203 //state of the bulk-only state machine
samux 17:364ef42e502d 204 Stage stage;
samux 17:364ef42e502d 205
samux 17:364ef42e502d 206 // current CBW
samux 17:364ef42e502d 207 CBW cbw;
samux 17:364ef42e502d 208
samux 17:364ef42e502d 209 // CSW which will be sent
samux 17:364ef42e502d 210 CSW csw;
samux 17:364ef42e502d 211
samux 17:364ef42e502d 212 // addr where will be read or written data
samux 17:364ef42e502d 213 uint32_t addr;
samux 17:364ef42e502d 214
samux 17:364ef42e502d 215 // length of a reading or writing
samux 17:364ef42e502d 216 uint32_t length;
samux 17:364ef42e502d 217
samux 17:364ef42e502d 218 // memory OK (after a memoryVerify)
samux 17:364ef42e502d 219 bool memOK;
samux 17:364ef42e502d 220
samux 17:364ef42e502d 221 // cache in RAM before writing in memory. Useful also to read a block.
samux 17:364ef42e502d 222 uint8_t * page;
samux 17:364ef42e502d 223
samux 17:364ef42e502d 224 int BlockSize;
samux 17:364ef42e502d 225 int MemorySize;
samux 17:364ef42e502d 226 int BlockCount;
samux 17:364ef42e502d 227
samux 17:364ef42e502d 228 void CBWDecode(uint8_t * buf, uint16_t size);
samux 17:364ef42e502d 229 void sendCSW (void);
samux 17:364ef42e502d 230 bool inquiryRequest (void);
samux 17:364ef42e502d 231 bool write (uint8_t * buf, uint16_t size);
samux 17:364ef42e502d 232 bool readFormatCapacity();
samux 17:364ef42e502d 233 bool readCapacity (void);
samux 17:364ef42e502d 234 bool infoTransfer (void);
samux 17:364ef42e502d 235 void memoryRead (void);
samux 17:364ef42e502d 236 bool modeSense6 (void);
samux 17:364ef42e502d 237 void testUnitReady (void);
samux 17:364ef42e502d 238 bool requestSense (void);
samux 17:364ef42e502d 239 void memoryVerify (uint8_t * buf, uint16_t size);
samux 17:364ef42e502d 240 void memoryWrite (uint8_t * buf, uint16_t size);
samux 17:364ef42e502d 241 void reset();
samux 17:364ef42e502d 242 void fail();
samux 17:364ef42e502d 243 };
samux 17:364ef42e502d 244
samux 17:364ef42e502d 245 #endif