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 #ifndef USBAudio_H
Dance 0:f1ecca559ec3 20 #define USBAudio_H
Dance 0:f1ecca559ec3 21
Dance 0:f1ecca559ec3 22 /* These headers are included for child class. */
Dance 0:f1ecca559ec3 23 #include "USBEndpoints.h"
Dance 0:f1ecca559ec3 24 #include "USBDescriptor.h"
Dance 0:f1ecca559ec3 25 #include "USBDevice_Types.h"
Dance 0:f1ecca559ec3 26
Dance 0:f1ecca559ec3 27 #include "USBDevice.h"
Dance 0:f1ecca559ec3 28
Dance 0:f1ecca559ec3 29
Dance 0:f1ecca559ec3 30 /**
Dance 0:f1ecca559ec3 31 * USBAudio example
Dance 0:f1ecca559ec3 32 *
Dance 0:f1ecca559ec3 33 * @code
Dance 0:f1ecca559ec3 34 * #include "mbed.h"
Dance 0:f1ecca559ec3 35 * #include "USBAudio.h"
Dance 0:f1ecca559ec3 36 *
Dance 0:f1ecca559ec3 37 * Serial pc(USBTX, USBRX);
Dance 0:f1ecca559ec3 38 *
Dance 0:f1ecca559ec3 39 * // frequency: 48 kHz
Dance 0:f1ecca559ec3 40 * #define FREQ 48000
Dance 0:f1ecca559ec3 41 *
Dance 0:f1ecca559ec3 42 * // 1 channel: mono
Dance 0:f1ecca559ec3 43 * #define NB_CHA 1
Dance 0:f1ecca559ec3 44 *
Dance 0:f1ecca559ec3 45 * // length of an audio packet: each ms, we receive 48 * 16bits ->48 * 2 bytes. as there is one channel, the length will be 48 * 2 * 1
Dance 0:f1ecca559ec3 46 * #define AUDIO_LENGTH_PACKET 48 * 2 * 1
Dance 0:f1ecca559ec3 47 *
Dance 0:f1ecca559ec3 48 * // USBAudio
Dance 0:f1ecca559ec3 49 * USBAudio audio(FREQ, NB_CHA);
Dance 0:f1ecca559ec3 50 *
Dance 0:f1ecca559ec3 51 * int main() {
Dance 0:f1ecca559ec3 52 * int16_t buf[AUDIO_LENGTH_PACKET/2];
Dance 0:f1ecca559ec3 53 *
Dance 0:f1ecca559ec3 54 * while (1) {
Dance 0:f1ecca559ec3 55 * // read an audio packet
Dance 0:f1ecca559ec3 56 * audio.read((uint8_t *)buf);
Dance 0:f1ecca559ec3 57 *
Dance 0:f1ecca559ec3 58 *
Dance 0:f1ecca559ec3 59 * // print packet received
Dance 0:f1ecca559ec3 60 * pc.printf("recv: ");
Dance 0:f1ecca559ec3 61 * for(int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) {
Dance 0:f1ecca559ec3 62 * pc.printf("%d ", buf[i]);
Dance 0:f1ecca559ec3 63 * }
Dance 0:f1ecca559ec3 64 * pc.printf("\r\n");
Dance 0:f1ecca559ec3 65 * }
Dance 0:f1ecca559ec3 66 * }
Dance 0:f1ecca559ec3 67 * @endcode
Dance 0:f1ecca559ec3 68 */
Dance 0:f1ecca559ec3 69 class USBAudio: public USBDevice {
Dance 0:f1ecca559ec3 70 public:
Dance 0:f1ecca559ec3 71
Dance 0:f1ecca559ec3 72 /**
Dance 0:f1ecca559ec3 73 * Constructor
Dance 0:f1ecca559ec3 74 *
Dance 0:f1ecca559ec3 75 * @param frequency_in frequency in Hz (default: 48000)
Dance 0:f1ecca559ec3 76 * @param channel_nb_in channel number (1 or 2) (default: 1)
Dance 0:f1ecca559ec3 77 * @param frequency_out frequency in Hz (default: 8000)
Dance 0:f1ecca559ec3 78 * @param channel_nb_out_in channel number (1 or 2) (default: 1)
Dance 0:f1ecca559ec3 79 * @param vendor_id Your vendor_id
Dance 0:f1ecca559ec3 80 * @param product_id Your product_id
Dance 0:f1ecca559ec3 81 * @param product_release Your preoduct_release
Dance 0:f1ecca559ec3 82 */
Dance 0:f1ecca559ec3 83 USBAudio(uint32_t frequency_in = 48000, uint8_t channel_nb_in = 1, uint32_t frequency_out = 8000, uint8_t channel_nb_out = 1, uint16_t vendor_id = 0x7bb8, uint16_t product_id = 0x1111, uint16_t product_release = 0x0100);
Dance 0:f1ecca559ec3 84
Dance 0:f1ecca559ec3 85 /**
Dance 0:f1ecca559ec3 86 * Get current volume between 0.0 and 1.0
Dance 0:f1ecca559ec3 87 *
Dance 0:f1ecca559ec3 88 * @returns volume
Dance 0:f1ecca559ec3 89 */
Dance 0:f1ecca559ec3 90 float getVolume();
Dance 0:f1ecca559ec3 91
Dance 0:f1ecca559ec3 92 /**
Dance 0:f1ecca559ec3 93 * Read an audio packet. During a frame, only a single reading (you can't write and read an audio packet during the same frame)can be done using this method. Warning: Blocking
Dance 0:f1ecca559ec3 94 *
Dance 0:f1ecca559ec3 95 * @param buf pointer on a buffer which will be filled with an audio packet
Dance 0:f1ecca559ec3 96 *
Dance 0:f1ecca559ec3 97 * @returns true if successfull
Dance 0:f1ecca559ec3 98 */
Dance 0:f1ecca559ec3 99 bool read(uint8_t * buf);
Dance 0:f1ecca559ec3 100
Dance 0:f1ecca559ec3 101 /**
Dance 0:f1ecca559ec3 102 * Try to read an audio packet. During a frame, only a single reading (you can't write and read an audio packet during the same frame)can be done using this method. Warning: Non Blocking
Dance 0:f1ecca559ec3 103 *
Dance 0:f1ecca559ec3 104 * @param buf pointer on a buffer which will be filled if an audio packet is available
Dance 0:f1ecca559ec3 105 *
Dance 0:f1ecca559ec3 106 * @returns true if successfull
Dance 0:f1ecca559ec3 107 */
Dance 0:f1ecca559ec3 108 bool readNB(uint8_t * buf);
Dance 0:f1ecca559ec3 109
Dance 0:f1ecca559ec3 110 /**
Dance 0:f1ecca559ec3 111 * Write an audio packet. During a frame, only a single writing (you can't write and read an audio packet during the same frame)can be done using this method.
Dance 0:f1ecca559ec3 112 *
Dance 0:f1ecca559ec3 113 * @param buf pointer on the audio packet which will be sent
Dance 0:f1ecca559ec3 114 * @returns true if successful
Dance 0:f1ecca559ec3 115 */
Dance 0:f1ecca559ec3 116 bool write(uint8_t * buf);
Dance 0:f1ecca559ec3 117
Dance 0:f1ecca559ec3 118 /**
Dance 0:f1ecca559ec3 119 * Write and read an audio packet at the same time (on the same frame)
Dance 0:f1ecca559ec3 120 *
Dance 0:f1ecca559ec3 121 * @param buf_read pointer on a buffer which will be filled with an audio packet
Dance 0:f1ecca559ec3 122 * @param buf_write pointer on the audio packet which will be sent
Dance 0:f1ecca559ec3 123 * @returns true if successful
Dance 0:f1ecca559ec3 124 */
Dance 0:f1ecca559ec3 125 bool readWrite(uint8_t * buf_read, uint8_t * buf_write);
Dance 0:f1ecca559ec3 126
Dance 0:f1ecca559ec3 127
Dance 0:f1ecca559ec3 128 /** attach a handler to update the volume
Dance 0:f1ecca559ec3 129 *
Dance 0:f1ecca559ec3 130 * @param function Function to attach
Dance 0:f1ecca559ec3 131 *
Dance 0:f1ecca559ec3 132 */
Dance 0:f1ecca559ec3 133 void attach(void(*fptr)(void)) {
Dance 0:f1ecca559ec3 134 updateVol.attach(fptr);
Dance 0:f1ecca559ec3 135 }
Dance 0:f1ecca559ec3 136
Dance 0:f1ecca559ec3 137 /** Attach a nonstatic void/void member function to update the volume
Dance 0:f1ecca559ec3 138 *
Dance 0:f1ecca559ec3 139 * @param tptr Object pointer
Dance 0:f1ecca559ec3 140 * @param mptr Member function pointer
Dance 0:f1ecca559ec3 141 *
Dance 0:f1ecca559ec3 142 */
Dance 0:f1ecca559ec3 143 template<typename T>
Dance 0:f1ecca559ec3 144 void attach(T *tptr, void(T::*mptr)(void)) {
Dance 0:f1ecca559ec3 145 updateVol.attach(tptr, mptr);
Dance 0:f1ecca559ec3 146 }
Dance 0:f1ecca559ec3 147
Dance 0:f1ecca559ec3 148
Dance 0:f1ecca559ec3 149 protected:
Dance 0:f1ecca559ec3 150
Dance 0:f1ecca559ec3 151 /*
Dance 0:f1ecca559ec3 152 * Called by USBDevice layer. Set configuration of the device.
Dance 0:f1ecca559ec3 153 * For instance, you can add all endpoints that you need on this function.
Dance 0:f1ecca559ec3 154 *
Dance 0:f1ecca559ec3 155 * @param configuration Number of the configuration
Dance 0:f1ecca559ec3 156 * @returns true if class handles this request
Dance 0:f1ecca559ec3 157 */
Dance 0:f1ecca559ec3 158 virtual bool USBCallback_setConfiguration(uint8_t configuration);
Dance 0:f1ecca559ec3 159
Dance 0:f1ecca559ec3 160 /*
Dance 0:f1ecca559ec3 161 * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
Dance 0:f1ecca559ec3 162 * This is used to handle extensions to standard requests
Dance 0:f1ecca559ec3 163 * and class specific requests
Dance 0:f1ecca559ec3 164 *
Dance 0:f1ecca559ec3 165 * @returns true if class handles this request
Dance 0:f1ecca559ec3 166 */
Dance 0:f1ecca559ec3 167 virtual bool USBCallback_request();
Dance 0:f1ecca559ec3 168
Dance 0:f1ecca559ec3 169 /*
Dance 0:f1ecca559ec3 170 * Get string product descriptor
Dance 0:f1ecca559ec3 171 *
Dance 0:f1ecca559ec3 172 * @returns pointer to the string product descriptor
Dance 0:f1ecca559ec3 173 */
Dance 0:f1ecca559ec3 174 virtual uint8_t * stringIproductDesc();
Dance 0:f1ecca559ec3 175
Dance 0:f1ecca559ec3 176 /*
Dance 0:f1ecca559ec3 177 * Get string interface descriptor
Dance 0:f1ecca559ec3 178 *
Dance 0:f1ecca559ec3 179 * @returns pointer to the string interface descriptor
Dance 0:f1ecca559ec3 180 */
Dance 0:f1ecca559ec3 181 virtual uint8_t * stringIinterfaceDesc();
Dance 0:f1ecca559ec3 182
Dance 0:f1ecca559ec3 183 /*
Dance 0:f1ecca559ec3 184 * Get configuration descriptor
Dance 0:f1ecca559ec3 185 *
Dance 0:f1ecca559ec3 186 * @returns pointer to the configuration descriptor
Dance 0:f1ecca559ec3 187 */
Dance 0:f1ecca559ec3 188 virtual uint8_t * configurationDesc();
Dance 0:f1ecca559ec3 189
Dance 0:f1ecca559ec3 190 /*
Dance 0:f1ecca559ec3 191 * Called by USBDevice layer. Set interface/alternate of the device.
Dance 0:f1ecca559ec3 192 *
Dance 0:f1ecca559ec3 193 * @param interface Number of the interface to be configured
Dance 0:f1ecca559ec3 194 * @param alternate Number of the alternate to be configured
Dance 0:f1ecca559ec3 195 * @returns true if class handles this request
Dance 0:f1ecca559ec3 196 */
Dance 0:f1ecca559ec3 197 virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate);
Dance 0:f1ecca559ec3 198
Dance 0:f1ecca559ec3 199 /*
Dance 0:f1ecca559ec3 200 * Called by USBDevice on Endpoint0 request completion
Dance 0:f1ecca559ec3 201 * if the 'notify' flag has been set to true. Warning: Called in ISR context
Dance 0:f1ecca559ec3 202 *
Dance 0:f1ecca559ec3 203 * In this case it is used to indicate that a HID report has
Dance 0:f1ecca559ec3 204 * been received from the host on endpoint 0
Dance 0:f1ecca559ec3 205 *
Dance 0:f1ecca559ec3 206 * @param buf buffer received on endpoint 0
Dance 0:f1ecca559ec3 207 * @param length length of this buffer
Dance 0:f1ecca559ec3 208 */
Dance 0:f1ecca559ec3 209 virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length);
Dance 0:f1ecca559ec3 210
Dance 0:f1ecca559ec3 211 /*
Dance 0:f1ecca559ec3 212 * Callback called on each Start of Frame event
Dance 0:f1ecca559ec3 213 */
Dance 0:f1ecca559ec3 214 virtual void SOF(int frameNumber);
Dance 0:f1ecca559ec3 215
Dance 0:f1ecca559ec3 216 /*
Dance 0:f1ecca559ec3 217 * Callback called when a packet is received
Dance 0:f1ecca559ec3 218 */
Dance 0:f1ecca559ec3 219 virtual bool EP3_OUT_callback();
Dance 0:f1ecca559ec3 220
Dance 0:f1ecca559ec3 221 /*
Dance 0:f1ecca559ec3 222 * Callback called when a packet has been sent
Dance 0:f1ecca559ec3 223 */
Dance 0:f1ecca559ec3 224 virtual bool EP3_IN_callback();
Dance 0:f1ecca559ec3 225
Dance 0:f1ecca559ec3 226 private:
Dance 0:f1ecca559ec3 227
Dance 0:f1ecca559ec3 228 // stream available ?
Dance 0:f1ecca559ec3 229 volatile bool available;
Dance 0:f1ecca559ec3 230
Dance 0:f1ecca559ec3 231 // interrupt OUT has been received
Dance 0:f1ecca559ec3 232 volatile bool interruptOUT;
Dance 0:f1ecca559ec3 233
Dance 0:f1ecca559ec3 234 // interrupt IN has been received
Dance 0:f1ecca559ec3 235 volatile bool interruptIN;
Dance 0:f1ecca559ec3 236
Dance 0:f1ecca559ec3 237 // audio packet has been written
Dance 0:f1ecca559ec3 238 volatile bool writeIN;
Dance 0:f1ecca559ec3 239
Dance 0:f1ecca559ec3 240 // FREQ
Dance 0:f1ecca559ec3 241 uint32_t FREQ_OUT;
Dance 0:f1ecca559ec3 242 uint32_t FREQ_IN;
Dance 0:f1ecca559ec3 243
Dance 0:f1ecca559ec3 244 // size of the maximum packet for the isochronous endpoint
Dance 0:f1ecca559ec3 245 uint32_t PACKET_SIZE_ISO_IN;
Dance 0:f1ecca559ec3 246 uint32_t PACKET_SIZE_ISO_OUT;
Dance 0:f1ecca559ec3 247
Dance 0:f1ecca559ec3 248 // mono, stereo,...
Dance 0:f1ecca559ec3 249 uint8_t channel_nb_in;
Dance 0:f1ecca559ec3 250 uint8_t channel_nb_out;
Dance 0:f1ecca559ec3 251
Dance 0:f1ecca559ec3 252 // channel config: master, left, right
Dance 0:f1ecca559ec3 253 uint8_t channel_config_in;
Dance 0:f1ecca559ec3 254 uint8_t channel_config_out;
Dance 0:f1ecca559ec3 255
Dance 0:f1ecca559ec3 256 // mute state
Dance 0:f1ecca559ec3 257 uint8_t mute;
Dance 0:f1ecca559ec3 258
Dance 0:f1ecca559ec3 259 // Volume Current Value
Dance 0:f1ecca559ec3 260 uint16_t volCur;
Dance 0:f1ecca559ec3 261
Dance 0:f1ecca559ec3 262 // Volume Minimum Value
Dance 0:f1ecca559ec3 263 uint16_t volMin;
Dance 0:f1ecca559ec3 264
Dance 0:f1ecca559ec3 265 // Volume Maximum Value
Dance 0:f1ecca559ec3 266 uint16_t volMax;
Dance 0:f1ecca559ec3 267
Dance 0:f1ecca559ec3 268 // Volume Resolution
Dance 0:f1ecca559ec3 269 uint16_t volRes;
Dance 0:f1ecca559ec3 270
Dance 0:f1ecca559ec3 271 // Buffer containing one audio packet (to be read)
Dance 0:f1ecca559ec3 272 volatile uint8_t * buf_stream_in;
Dance 0:f1ecca559ec3 273
Dance 0:f1ecca559ec3 274 // Buffer containing one audio packet (to be written)
Dance 0:f1ecca559ec3 275 volatile uint8_t * buf_stream_out;
Dance 0:f1ecca559ec3 276
Dance 0:f1ecca559ec3 277 // callback to update volume
Dance 0:f1ecca559ec3 278 FunctionPointer updateVol;
Dance 0:f1ecca559ec3 279
Dance 0:f1ecca559ec3 280 // boolean showing that the SOF handler has been called. Useful for readNB.
Dance 0:f1ecca559ec3 281 volatile bool SOF_handler;
Dance 0:f1ecca559ec3 282
Dance 0:f1ecca559ec3 283 volatile float volume;
Dance 0:f1ecca559ec3 284
Dance 0:f1ecca559ec3 285 };
Dance 0:f1ecca559ec3 286
Dance 0:f1ecca559ec3 287 #endif