usb device

Committer:
ppo
Date:
Sat May 14 17:24:10 2022 +0000
Revision:
0:c1e89c49eae5
commit

Who changed what in which revision?

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