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 #include "stdint.h"
Dance 0:f1ecca559ec3 20 #include "USBCDC.h"
Dance 0:f1ecca559ec3 21
Dance 0:f1ecca559ec3 22 static uint8_t cdc_line_coding[7]= {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08};
Dance 0:f1ecca559ec3 23
Dance 0:f1ecca559ec3 24 #define DEFAULT_CONFIGURATION (1)
Dance 0:f1ecca559ec3 25
Dance 0:f1ecca559ec3 26 #define CDC_SET_LINE_CODING 0x20
Dance 0:f1ecca559ec3 27 #define CDC_GET_LINE_CODING 0x21
Dance 0:f1ecca559ec3 28 #define CDC_SET_CONTROL_LINE_STATE 0x22
Dance 0:f1ecca559ec3 29
Dance 0:f1ecca559ec3 30 #define MAX_CDC_REPORT_SIZE MAX_PACKET_SIZE_EPBULK
Dance 0:f1ecca559ec3 31
Dance 0:f1ecca559ec3 32 USBCDC::USBCDC(uint16_t vendor_id, uint16_t product_id, uint16_t product_release, bool connect_blocking): USBDevice(vendor_id, product_id, product_release) {
Dance 0:f1ecca559ec3 33 terminal_connected = false;
Dance 0:f1ecca559ec3 34 USBDevice::connect(connect_blocking);
Dance 0:f1ecca559ec3 35 }
Dance 0:f1ecca559ec3 36
Dance 0:f1ecca559ec3 37 bool USBCDC::USBCallback_request(void) {
Dance 0:f1ecca559ec3 38 /* Called in ISR context */
Dance 0:f1ecca559ec3 39
Dance 0:f1ecca559ec3 40 bool success = false;
Dance 0:f1ecca559ec3 41 CONTROL_TRANSFER * transfer = getTransferPtr();
Dance 0:f1ecca559ec3 42
Dance 0:f1ecca559ec3 43 /* Process class-specific requests */
Dance 0:f1ecca559ec3 44
Dance 0:f1ecca559ec3 45 if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
Dance 0:f1ecca559ec3 46 switch (transfer->setup.bRequest) {
Dance 0:f1ecca559ec3 47 case CDC_GET_LINE_CODING:
Dance 0:f1ecca559ec3 48 transfer->remaining = 7;
Dance 0:f1ecca559ec3 49 transfer->ptr = cdc_line_coding;
Dance 0:f1ecca559ec3 50 transfer->direction = DEVICE_TO_HOST;
Dance 0:f1ecca559ec3 51 success = true;
Dance 0:f1ecca559ec3 52 break;
Dance 0:f1ecca559ec3 53 case CDC_SET_LINE_CODING:
Dance 0:f1ecca559ec3 54 transfer->remaining = 7;
Dance 0:f1ecca559ec3 55 transfer->notify = true;
Dance 0:f1ecca559ec3 56 success = true;
Dance 0:f1ecca559ec3 57 terminal_connected = true;
Dance 0:f1ecca559ec3 58 break;
Dance 0:f1ecca559ec3 59 case CDC_SET_CONTROL_LINE_STATE:
Dance 0:f1ecca559ec3 60 terminal_connected = false;
Dance 0:f1ecca559ec3 61 success = true;
Dance 0:f1ecca559ec3 62 break;
Dance 0:f1ecca559ec3 63 default:
Dance 0:f1ecca559ec3 64 break;
Dance 0:f1ecca559ec3 65 }
Dance 0:f1ecca559ec3 66 }
Dance 0:f1ecca559ec3 67
Dance 0:f1ecca559ec3 68 return success;
Dance 0:f1ecca559ec3 69 }
Dance 0:f1ecca559ec3 70
Dance 0:f1ecca559ec3 71 void USBCDC::USBCallback_requestCompleted(uint8_t *buf, uint32_t length) {
Dance 0:f1ecca559ec3 72 // Request of setting line coding has 7 bytes
Dance 0:f1ecca559ec3 73 if (length != 7) {
Dance 0:f1ecca559ec3 74 return;
Dance 0:f1ecca559ec3 75 }
Dance 0:f1ecca559ec3 76
Dance 0:f1ecca559ec3 77 CONTROL_TRANSFER * transfer = getTransferPtr();
Dance 0:f1ecca559ec3 78
Dance 0:f1ecca559ec3 79 /* Process class-specific requests */
Dance 0:f1ecca559ec3 80 if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
Dance 0:f1ecca559ec3 81 if (transfer->setup.bRequest == CDC_SET_LINE_CODING) {
Dance 0:f1ecca559ec3 82 if (memcmp(cdc_line_coding, buf, 7)) {
Dance 0:f1ecca559ec3 83 memcpy(cdc_line_coding, buf, 7);
Dance 0:f1ecca559ec3 84
Dance 0:f1ecca559ec3 85 int baud = buf[0] + (buf[1] << 8)
Dance 0:f1ecca559ec3 86 + (buf[2] << 16) + (buf[3] << 24);
Dance 0:f1ecca559ec3 87 int stop = buf[4];
Dance 0:f1ecca559ec3 88 int bits = buf[6];
Dance 0:f1ecca559ec3 89 int parity = buf[5];
Dance 0:f1ecca559ec3 90
Dance 0:f1ecca559ec3 91 lineCodingChanged(baud, bits, parity, stop);
Dance 0:f1ecca559ec3 92 }
Dance 0:f1ecca559ec3 93 }
Dance 0:f1ecca559ec3 94 }
Dance 0:f1ecca559ec3 95 }
Dance 0:f1ecca559ec3 96
Dance 0:f1ecca559ec3 97 // Called in ISR context
Dance 0:f1ecca559ec3 98 // Set configuration. Return false if the
Dance 0:f1ecca559ec3 99 // configuration is not supported.
Dance 0:f1ecca559ec3 100 bool USBCDC::USBCallback_setConfiguration(uint8_t configuration) {
Dance 0:f1ecca559ec3 101 if (configuration != DEFAULT_CONFIGURATION) {
Dance 0:f1ecca559ec3 102 return false;
Dance 0:f1ecca559ec3 103 }
Dance 0:f1ecca559ec3 104
Dance 0:f1ecca559ec3 105 // Configure endpoints > 0
Dance 0:f1ecca559ec3 106 addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);
Dance 0:f1ecca559ec3 107 addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
Dance 0:f1ecca559ec3 108 addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
Dance 0:f1ecca559ec3 109
Dance 0:f1ecca559ec3 110 // We activate the endpoint to be able to recceive data
Dance 0:f1ecca559ec3 111 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
Dance 0:f1ecca559ec3 112 return true;
Dance 0:f1ecca559ec3 113 }
Dance 0:f1ecca559ec3 114
Dance 0:f1ecca559ec3 115 bool USBCDC::send(uint8_t * buffer, uint32_t size) {
Dance 0:f1ecca559ec3 116 return USBDevice::write(EPBULK_IN, buffer, size, MAX_CDC_REPORT_SIZE);
Dance 0:f1ecca559ec3 117 }
Dance 0:f1ecca559ec3 118
Dance 0:f1ecca559ec3 119 bool USBCDC::readEP(uint8_t * buffer, uint32_t * size) {
Dance 0:f1ecca559ec3 120 if (!USBDevice::readEP(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))
Dance 0:f1ecca559ec3 121 return false;
Dance 0:f1ecca559ec3 122 if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))
Dance 0:f1ecca559ec3 123 return false;
Dance 0:f1ecca559ec3 124 return true;
Dance 0:f1ecca559ec3 125 }
Dance 0:f1ecca559ec3 126
Dance 0:f1ecca559ec3 127 bool USBCDC::readEP_NB(uint8_t * buffer, uint32_t * size) {
Dance 0:f1ecca559ec3 128 if (!USBDevice::readEP_NB(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))
Dance 0:f1ecca559ec3 129 return false;
Dance 0:f1ecca559ec3 130 if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))
Dance 0:f1ecca559ec3 131 return false;
Dance 0:f1ecca559ec3 132 return true;
Dance 0:f1ecca559ec3 133 }
Dance 0:f1ecca559ec3 134
Dance 0:f1ecca559ec3 135
Dance 0:f1ecca559ec3 136 uint8_t * USBCDC::deviceDesc() {
Dance 0:f1ecca559ec3 137 static uint8_t deviceDescriptor[] = {
Dance 0:f1ecca559ec3 138 18, // bLength
Dance 0:f1ecca559ec3 139 1, // bDescriptorType
Dance 0:f1ecca559ec3 140 0x10, 0x01, // bcdUSB
Dance 0:f1ecca559ec3 141 2, // bDeviceClass
Dance 0:f1ecca559ec3 142 0, // bDeviceSubClass
Dance 0:f1ecca559ec3 143 0, // bDeviceProtocol
Dance 0:f1ecca559ec3 144 MAX_PACKET_SIZE_EP0, // bMaxPacketSize0
Dance 0:f1ecca559ec3 145 (uint8_t)(LSB(VENDOR_ID)), (uint8_t)(MSB(VENDOR_ID)), // idVendor
Dance 0:f1ecca559ec3 146 (uint8_t)(LSB(PRODUCT_ID)), (uint8_t)(MSB(PRODUCT_ID)),// idProduct
Dance 0:f1ecca559ec3 147 0x00, 0x01, // bcdDevice
Dance 0:f1ecca559ec3 148 1, // iManufacturer
Dance 0:f1ecca559ec3 149 2, // iProduct
Dance 0:f1ecca559ec3 150 3, // iSerialNumber
Dance 0:f1ecca559ec3 151 1 // bNumConfigurations
Dance 0:f1ecca559ec3 152 };
Dance 0:f1ecca559ec3 153 return deviceDescriptor;
Dance 0:f1ecca559ec3 154 }
Dance 0:f1ecca559ec3 155
Dance 0:f1ecca559ec3 156 uint8_t * USBCDC::stringIinterfaceDesc() {
Dance 0:f1ecca559ec3 157 static uint8_t stringIinterfaceDescriptor[] = {
Dance 0:f1ecca559ec3 158 0x08,
Dance 0:f1ecca559ec3 159 STRING_DESCRIPTOR,
Dance 0:f1ecca559ec3 160 'C',0,'D',0,'C',0,
Dance 0:f1ecca559ec3 161 };
Dance 0:f1ecca559ec3 162 return stringIinterfaceDescriptor;
Dance 0:f1ecca559ec3 163 }
Dance 0:f1ecca559ec3 164
Dance 0:f1ecca559ec3 165 uint8_t * USBCDC::stringIproductDesc() {
Dance 0:f1ecca559ec3 166 static uint8_t stringIproductDescriptor[] = {
Dance 0:f1ecca559ec3 167 0x16,
Dance 0:f1ecca559ec3 168 STRING_DESCRIPTOR,
Dance 0:f1ecca559ec3 169 'C',0,'D',0,'C',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0
Dance 0:f1ecca559ec3 170 };
Dance 0:f1ecca559ec3 171 return stringIproductDescriptor;
Dance 0:f1ecca559ec3 172 }
Dance 0:f1ecca559ec3 173
Dance 0:f1ecca559ec3 174
Dance 0:f1ecca559ec3 175 #define CONFIG1_DESC_SIZE (9+8+9+5+5+4+5+7+9+7+7)
Dance 0:f1ecca559ec3 176
Dance 0:f1ecca559ec3 177 uint8_t * USBCDC::configurationDesc() {
Dance 0:f1ecca559ec3 178 static uint8_t configDescriptor[] = {
Dance 0:f1ecca559ec3 179 // configuration descriptor
Dance 0:f1ecca559ec3 180 9, // bLength
Dance 0:f1ecca559ec3 181 2, // bDescriptorType
Dance 0:f1ecca559ec3 182 LSB(CONFIG1_DESC_SIZE), // wTotalLength
Dance 0:f1ecca559ec3 183 MSB(CONFIG1_DESC_SIZE),
Dance 0:f1ecca559ec3 184 2, // bNumInterfaces
Dance 0:f1ecca559ec3 185 1, // bConfigurationValue
Dance 0:f1ecca559ec3 186 0, // iConfiguration
Dance 0:f1ecca559ec3 187 0x80, // bmAttributes
Dance 0:f1ecca559ec3 188 50, // bMaxPower
Dance 0:f1ecca559ec3 189
Dance 0:f1ecca559ec3 190 // IAD to associate the two CDC interfaces
Dance 0:f1ecca559ec3 191 0x08, // bLength
Dance 0:f1ecca559ec3 192 0x0b, // bDescriptorType
Dance 0:f1ecca559ec3 193 0x00, // bFirstInterface
Dance 0:f1ecca559ec3 194 0x02, // bInterfaceCount
Dance 0:f1ecca559ec3 195 0x02, // bFunctionClass
Dance 0:f1ecca559ec3 196 0x02, // bFunctionSubClass
Dance 0:f1ecca559ec3 197 0, // bFunctionProtocol
Dance 0:f1ecca559ec3 198 0, // iFunction
Dance 0:f1ecca559ec3 199
Dance 0:f1ecca559ec3 200 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
Dance 0:f1ecca559ec3 201 9, // bLength
Dance 0:f1ecca559ec3 202 4, // bDescriptorType
Dance 0:f1ecca559ec3 203 0, // bInterfaceNumber
Dance 0:f1ecca559ec3 204 0, // bAlternateSetting
Dance 0:f1ecca559ec3 205 1, // bNumEndpoints
Dance 0:f1ecca559ec3 206 0x02, // bInterfaceClass
Dance 0:f1ecca559ec3 207 0x02, // bInterfaceSubClass
Dance 0:f1ecca559ec3 208 0x01, // bInterfaceProtocol
Dance 0:f1ecca559ec3 209 0, // iInterface
Dance 0:f1ecca559ec3 210
Dance 0:f1ecca559ec3 211 // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
Dance 0:f1ecca559ec3 212 5, // bFunctionLength
Dance 0:f1ecca559ec3 213 0x24, // bDescriptorType
Dance 0:f1ecca559ec3 214 0x00, // bDescriptorSubtype
Dance 0:f1ecca559ec3 215 0x10, 0x01, // bcdCDC
Dance 0:f1ecca559ec3 216
Dance 0:f1ecca559ec3 217 // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
Dance 0:f1ecca559ec3 218 5, // bFunctionLength
Dance 0:f1ecca559ec3 219 0x24, // bDescriptorType
Dance 0:f1ecca559ec3 220 0x01, // bDescriptorSubtype
Dance 0:f1ecca559ec3 221 0x03, // bmCapabilities
Dance 0:f1ecca559ec3 222 1, // bDataInterface
Dance 0:f1ecca559ec3 223
Dance 0:f1ecca559ec3 224 // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
Dance 0:f1ecca559ec3 225 4, // bFunctionLength
Dance 0:f1ecca559ec3 226 0x24, // bDescriptorType
Dance 0:f1ecca559ec3 227 0x02, // bDescriptorSubtype
Dance 0:f1ecca559ec3 228 0x06, // bmCapabilities
Dance 0:f1ecca559ec3 229
Dance 0:f1ecca559ec3 230 // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
Dance 0:f1ecca559ec3 231 5, // bFunctionLength
Dance 0:f1ecca559ec3 232 0x24, // bDescriptorType
Dance 0:f1ecca559ec3 233 0x06, // bDescriptorSubtype
Dance 0:f1ecca559ec3 234 0, // bMasterInterface
Dance 0:f1ecca559ec3 235 1, // bSlaveInterface0
Dance 0:f1ecca559ec3 236
Dance 0:f1ecca559ec3 237 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
Dance 0:f1ecca559ec3 238 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
Dance 0:f1ecca559ec3 239 ENDPOINT_DESCRIPTOR, // bDescriptorType
Dance 0:f1ecca559ec3 240 PHY_TO_DESC(EPINT_IN), // bEndpointAddress
Dance 0:f1ecca559ec3 241 E_INTERRUPT, // bmAttributes (0x03=intr)
Dance 0:f1ecca559ec3 242 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
Dance 0:f1ecca559ec3 243 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
Dance 0:f1ecca559ec3 244 16, // bInterval
Dance 0:f1ecca559ec3 245
Dance 0:f1ecca559ec3 246
Dance 0:f1ecca559ec3 247
Dance 0:f1ecca559ec3 248
Dance 0:f1ecca559ec3 249 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
Dance 0:f1ecca559ec3 250 9, // bLength
Dance 0:f1ecca559ec3 251 4, // bDescriptorType
Dance 0:f1ecca559ec3 252 1, // bInterfaceNumber
Dance 0:f1ecca559ec3 253 0, // bAlternateSetting
Dance 0:f1ecca559ec3 254 2, // bNumEndpoints
Dance 0:f1ecca559ec3 255 0x0A, // bInterfaceClass
Dance 0:f1ecca559ec3 256 0x00, // bInterfaceSubClass
Dance 0:f1ecca559ec3 257 0x00, // bInterfaceProtocol
Dance 0:f1ecca559ec3 258 0, // iInterface
Dance 0:f1ecca559ec3 259
Dance 0:f1ecca559ec3 260 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
Dance 0:f1ecca559ec3 261 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
Dance 0:f1ecca559ec3 262 ENDPOINT_DESCRIPTOR, // bDescriptorType
Dance 0:f1ecca559ec3 263 PHY_TO_DESC(EPBULK_IN), // bEndpointAddress
Dance 0:f1ecca559ec3 264 E_BULK, // bmAttributes (0x02=bulk)
Dance 0:f1ecca559ec3 265 LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)
Dance 0:f1ecca559ec3 266 MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)
Dance 0:f1ecca559ec3 267 0, // bInterval
Dance 0:f1ecca559ec3 268
Dance 0:f1ecca559ec3 269 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
Dance 0:f1ecca559ec3 270 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
Dance 0:f1ecca559ec3 271 ENDPOINT_DESCRIPTOR, // bDescriptorType
Dance 0:f1ecca559ec3 272 PHY_TO_DESC(EPBULK_OUT), // bEndpointAddress
Dance 0:f1ecca559ec3 273 E_BULK, // bmAttributes (0x02=bulk)
Dance 0:f1ecca559ec3 274 LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)
Dance 0:f1ecca559ec3 275 MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)
Dance 0:f1ecca559ec3 276 0 // bInterval
Dance 0:f1ecca559ec3 277 };
Dance 0:f1ecca559ec3 278 return configDescriptor;
Dance 0:f1ecca559ec3 279 }