mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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