Slingshot Controller

Dependencies:   ADXL345 DebounceIn USBDevice mbed

Committer:
Brandon
Date:
Wed Oct 17 16:33:04 2012 +0000
Revision:
1:2721dc2acc2c
Parent:
0:cf17ea89fd09
Updated comments, added names, cleaned old code.

Who changed what in which revision?

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