Lucas Lim / Mbed 2 deprecated HSP_Temperature_Barometer_CS3237

Dependencies:   mbed

Committer:
lucaslwl
Date:
Mon Aug 26 08:11:41 2019 +0000
Revision:
22:5c07298d3383
add library folder

Who changed what in which revision?

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