SD card interface

Committer:
lharoon
Date:
Mon Oct 08 11:14:07 2012 +0000
Revision:
0:22612ae617a0
1st edition

Who changed what in which revision?

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