BA / SerialCom

Fork of OmniWheels by Gustav Atmel

Committer:
gustavatmel
Date:
Tue May 01 15:47:08 2018 +0000
Revision:
1:9c5af431a1f1
sdf

Who changed what in which revision?

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