nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nitsshukla
Date:
Fri Nov 04 12:06:04 2016 +0000
Revision:
7:3a65ef12ba31
Parent:
1:55a6170b404f
kghj;

Who changed what in which revision?

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