Transistor Gijutsu, October 2014, Special Features Chapter 9, Software of the Function Generator トランジスタ技術2014年10月号 特集第9章のソフトウェア わがまま波形発生器のソフトウェア

Dependencies:   USBDevice mbed

Information

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

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

概要

このプログラムは、ソフトウエアDDSにより、任意の波形を出力(2ch)します。 特徴は次のとおりです。

  • PWM出力をDAコンバータとして利用します。
  • 周波数や波形、バースト条件などを個別に設定できる独立した出力を2チャネル持っています。
  • 周波数分解能0.023mHz
  • 周波数範囲0.023mHz~10kHz
  • 各チャネルにそれぞれ、波形の先頭で出力されるトリガ出力があります。
  • 出力波形を関数で定義できます。
  • 休止波数、出力波数、を設定することでバースト波形が出力できます。

ファイル

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

  • DDS.cpp - DDSによる波形発生
  • main.cpp - main()関数

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

Committer:
Dance
Date:
Fri Aug 29 08:33:17 2014 +0000
Revision:
0:f1ecca559ec3
Transistor Gijutsu, October 2014, Special Features Chapter 9; ????????2014?10??????9????????

Who changed what in which revision?

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