Sensor reporting over USB CDC

Dependencies:   MAG3110 MMA8451Q SLCD- TSI USBDevice mbed

Committer:
wue
Date:
Wed Apr 16 12:20:12 2014 +0000
Revision:
0:7b58cdacf811
Sensor reporting over USB CDC

Who changed what in which revision?

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