Base station firmware

Committer:
Perijah
Date:
Tue May 24 13:16:55 2016 +0000
Revision:
0:ecc3925d3f8c
Base station firmware

Who changed what in which revision?

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