Transistor Gijutsu, October 2014, Special Features Chapter 8,Software of the thermistor thermometer of 0.001 ° resolution, トランジスタ技術2014年10月号 特集第8章のソフトウェア 0.001℃分解能で気配もキャッチ「超敏感肌温度計」

Dependencies:   USBDevice mbed

Information

tg_201410s8_AD7714 トランジスタ技術 2014年 10月号 第8章のソフトウェア

Program for Section 8 in October. 2014 issue of the Transistor Gijutsu
(Japanese electronics magazine)

概要

このプログラムは、サーミスタの抵抗値変化をAD7714(24bitADC)で測定し、抵抗値を温度値に変換することで、0.001℃程度の分解能で温度変化を測定します。

ファイル

このソフトウエアは、次のファイルから構成されています。

  • AD7714.cpp - AD7714の内部レジスタを設定
  • Thermistor.cpp - サーミスタの抵抗値から温度値に変換
  • ExpAvr.cpp - 指数平均によるソフトウエアLPF
  • main.cpp - main()関数

詳細については、10月号の記事および上記ファイル中のコメントを参照してください。

Committer:
Dance
Date:
Fri Aug 29 08:38:36 2014 +0000
Revision:
0:de885a6da962
Transistor Gijutsu, October 2014, Special Features Chapter 8; ????????2014?10??????8????????

Who changed what in which revision?

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