Pinscape Controller version 1 fork. This is a fork to allow for ongoing bug fixes to the original controller version, from before the major changes for the expansion board project.

Dependencies:   FastIO FastPWM SimpleDMA mbed

Fork of Pinscape_Controller by Mike R

Committer:
mjr
Date:
Mon Feb 15 23:03:55 2016 +0000
Revision:
68:edfecf67a931
Parent:
52:63f0a9b45f0c
Finalize USB library updates to fix compatibility problems introduced in USBHAL_KL25Z overhaul.

Who changed what in which revision?

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