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 "USBHAL.h"
Dance 0:f1ecca559ec3 21 #include "USBHID.h"
Dance 0:f1ecca559ec3 22
Dance 0:f1ecca559ec3 23
Dance 0:f1ecca559ec3 24 USBHID::USBHID(uint8_t output_report_length, uint8_t input_report_length, uint16_t vendor_id, uint16_t product_id, uint16_t product_release, bool connect): USBDevice(vendor_id, product_id, product_release)
Dance 0:f1ecca559ec3 25 {
Dance 0:f1ecca559ec3 26 output_length = output_report_length;
Dance 0:f1ecca559ec3 27 input_length = input_report_length;
Dance 0:f1ecca559ec3 28 if(connect) {
Dance 0:f1ecca559ec3 29 USBDevice::connect();
Dance 0:f1ecca559ec3 30 }
Dance 0:f1ecca559ec3 31 }
Dance 0:f1ecca559ec3 32
Dance 0:f1ecca559ec3 33
Dance 0:f1ecca559ec3 34 bool USBHID::send(HID_REPORT *report)
Dance 0:f1ecca559ec3 35 {
Dance 0:f1ecca559ec3 36 return write(EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE);
Dance 0:f1ecca559ec3 37 }
Dance 0:f1ecca559ec3 38
Dance 0:f1ecca559ec3 39 bool USBHID::sendNB(HID_REPORT *report)
Dance 0:f1ecca559ec3 40 {
Dance 0:f1ecca559ec3 41 return writeNB(EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE);
Dance 0:f1ecca559ec3 42 }
Dance 0:f1ecca559ec3 43
Dance 0:f1ecca559ec3 44
Dance 0:f1ecca559ec3 45 bool USBHID::read(HID_REPORT *report)
Dance 0:f1ecca559ec3 46 {
Dance 0:f1ecca559ec3 47 uint32_t bytesRead = 0;
Dance 0:f1ecca559ec3 48 bool result;
Dance 0:f1ecca559ec3 49 result = USBDevice::readEP(EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);
Dance 0:f1ecca559ec3 50 if(!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))
Dance 0:f1ecca559ec3 51 return false;
Dance 0:f1ecca559ec3 52 report->length = bytesRead;
Dance 0:f1ecca559ec3 53 return result;
Dance 0:f1ecca559ec3 54 }
Dance 0:f1ecca559ec3 55
Dance 0:f1ecca559ec3 56
Dance 0:f1ecca559ec3 57 bool USBHID::readNB(HID_REPORT *report)
Dance 0:f1ecca559ec3 58 {
Dance 0:f1ecca559ec3 59 uint32_t bytesRead = 0;
Dance 0:f1ecca559ec3 60 bool result;
Dance 0:f1ecca559ec3 61 result = USBDevice::readEP_NB(EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);
Dance 0:f1ecca559ec3 62 // if readEP_NB did not succeed, does not issue a readStart
Dance 0:f1ecca559ec3 63 if (!result)
Dance 0:f1ecca559ec3 64 return false;
Dance 0:f1ecca559ec3 65 report->length = bytesRead;
Dance 0:f1ecca559ec3 66 if(!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))
Dance 0:f1ecca559ec3 67 return false;
Dance 0:f1ecca559ec3 68 return result;
Dance 0:f1ecca559ec3 69 }
Dance 0:f1ecca559ec3 70
Dance 0:f1ecca559ec3 71
Dance 0:f1ecca559ec3 72 uint16_t USBHID::reportDescLength() {
Dance 0:f1ecca559ec3 73 reportDesc();
Dance 0:f1ecca559ec3 74 return reportLength;
Dance 0:f1ecca559ec3 75 }
Dance 0:f1ecca559ec3 76
Dance 0:f1ecca559ec3 77
Dance 0:f1ecca559ec3 78
Dance 0:f1ecca559ec3 79 //
Dance 0:f1ecca559ec3 80 // Route callbacks from lower layers to class(es)
Dance 0:f1ecca559ec3 81 //
Dance 0:f1ecca559ec3 82
Dance 0:f1ecca559ec3 83
Dance 0:f1ecca559ec3 84 // Called in ISR context
Dance 0:f1ecca559ec3 85 // Called by USBDevice on Endpoint0 request
Dance 0:f1ecca559ec3 86 // This is used to handle extensions to standard requests
Dance 0:f1ecca559ec3 87 // and class specific requests
Dance 0:f1ecca559ec3 88 // Return true if class handles this request
Dance 0:f1ecca559ec3 89 bool USBHID::USBCallback_request() {
Dance 0:f1ecca559ec3 90 bool success = false;
Dance 0:f1ecca559ec3 91 CONTROL_TRANSFER * transfer = getTransferPtr();
Dance 0:f1ecca559ec3 92 uint8_t *hidDescriptor;
Dance 0:f1ecca559ec3 93
Dance 0:f1ecca559ec3 94 // Process additional standard requests
Dance 0:f1ecca559ec3 95
Dance 0:f1ecca559ec3 96 if ((transfer->setup.bmRequestType.Type == STANDARD_TYPE))
Dance 0:f1ecca559ec3 97 {
Dance 0:f1ecca559ec3 98 switch (transfer->setup.bRequest)
Dance 0:f1ecca559ec3 99 {
Dance 0:f1ecca559ec3 100 case GET_DESCRIPTOR:
Dance 0:f1ecca559ec3 101 switch (DESCRIPTOR_TYPE(transfer->setup.wValue))
Dance 0:f1ecca559ec3 102 {
Dance 0:f1ecca559ec3 103 case REPORT_DESCRIPTOR:
Dance 0:f1ecca559ec3 104 if ((reportDesc() != NULL) \
Dance 0:f1ecca559ec3 105 && (reportDescLength() != 0))
Dance 0:f1ecca559ec3 106 {
Dance 0:f1ecca559ec3 107 transfer->remaining = reportDescLength();
Dance 0:f1ecca559ec3 108 transfer->ptr = reportDesc();
Dance 0:f1ecca559ec3 109 transfer->direction = DEVICE_TO_HOST;
Dance 0:f1ecca559ec3 110 success = true;
Dance 0:f1ecca559ec3 111 }
Dance 0:f1ecca559ec3 112 break;
Dance 0:f1ecca559ec3 113 case HID_DESCRIPTOR:
Dance 0:f1ecca559ec3 114 // Find the HID descriptor, after the configuration descriptor
Dance 0:f1ecca559ec3 115 hidDescriptor = findDescriptor(HID_DESCRIPTOR);
Dance 0:f1ecca559ec3 116 if (hidDescriptor != NULL)
Dance 0:f1ecca559ec3 117 {
Dance 0:f1ecca559ec3 118 transfer->remaining = HID_DESCRIPTOR_LENGTH;
Dance 0:f1ecca559ec3 119 transfer->ptr = hidDescriptor;
Dance 0:f1ecca559ec3 120 transfer->direction = DEVICE_TO_HOST;
Dance 0:f1ecca559ec3 121 success = true;
Dance 0:f1ecca559ec3 122 }
Dance 0:f1ecca559ec3 123 break;
Dance 0:f1ecca559ec3 124
Dance 0:f1ecca559ec3 125 default:
Dance 0:f1ecca559ec3 126 break;
Dance 0:f1ecca559ec3 127 }
Dance 0:f1ecca559ec3 128 break;
Dance 0:f1ecca559ec3 129 default:
Dance 0:f1ecca559ec3 130 break;
Dance 0:f1ecca559ec3 131 }
Dance 0:f1ecca559ec3 132 }
Dance 0:f1ecca559ec3 133
Dance 0:f1ecca559ec3 134 // Process class-specific requests
Dance 0:f1ecca559ec3 135
Dance 0:f1ecca559ec3 136 if (transfer->setup.bmRequestType.Type == CLASS_TYPE)
Dance 0:f1ecca559ec3 137 {
Dance 0:f1ecca559ec3 138 switch (transfer->setup.bRequest)
Dance 0:f1ecca559ec3 139 {
Dance 0:f1ecca559ec3 140 case SET_REPORT:
Dance 0:f1ecca559ec3 141 // First byte will be used for report ID
Dance 0:f1ecca559ec3 142 outputReport.data[0] = transfer->setup.wValue & 0xff;
Dance 0:f1ecca559ec3 143 outputReport.length = transfer->setup.wLength + 1;
Dance 0:f1ecca559ec3 144
Dance 0:f1ecca559ec3 145 transfer->remaining = sizeof(outputReport.data) - 1;
Dance 0:f1ecca559ec3 146 transfer->ptr = &outputReport.data[1];
Dance 0:f1ecca559ec3 147 transfer->direction = HOST_TO_DEVICE;
Dance 0:f1ecca559ec3 148 transfer->notify = true;
Dance 0:f1ecca559ec3 149 success = true;
Dance 0:f1ecca559ec3 150 default:
Dance 0:f1ecca559ec3 151 break;
Dance 0:f1ecca559ec3 152 }
Dance 0:f1ecca559ec3 153 }
Dance 0:f1ecca559ec3 154
Dance 0:f1ecca559ec3 155 return success;
Dance 0:f1ecca559ec3 156 }
Dance 0:f1ecca559ec3 157
Dance 0:f1ecca559ec3 158
Dance 0:f1ecca559ec3 159 #define DEFAULT_CONFIGURATION (1)
Dance 0:f1ecca559ec3 160
Dance 0:f1ecca559ec3 161
Dance 0:f1ecca559ec3 162 // Called in ISR context
Dance 0:f1ecca559ec3 163 // Set configuration. Return false if the
Dance 0:f1ecca559ec3 164 // configuration is not supported
Dance 0:f1ecca559ec3 165 bool USBHID::USBCallback_setConfiguration(uint8_t configuration) {
Dance 0:f1ecca559ec3 166 if (configuration != DEFAULT_CONFIGURATION) {
Dance 0:f1ecca559ec3 167 return false;
Dance 0:f1ecca559ec3 168 }
Dance 0:f1ecca559ec3 169
Dance 0:f1ecca559ec3 170 // Configure endpoints > 0
Dance 0:f1ecca559ec3 171 addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);
Dance 0:f1ecca559ec3 172 addEndpoint(EPINT_OUT, MAX_PACKET_SIZE_EPINT);
Dance 0:f1ecca559ec3 173
Dance 0:f1ecca559ec3 174 // We activate the endpoint to be able to recceive data
Dance 0:f1ecca559ec3 175 readStart(EPINT_OUT, MAX_PACKET_SIZE_EPINT);
Dance 0:f1ecca559ec3 176 return true;
Dance 0:f1ecca559ec3 177 }
Dance 0:f1ecca559ec3 178
Dance 0:f1ecca559ec3 179
Dance 0:f1ecca559ec3 180 uint8_t * USBHID::stringIinterfaceDesc() {
Dance 0:f1ecca559ec3 181 static uint8_t stringIinterfaceDescriptor[] = {
Dance 0:f1ecca559ec3 182 0x08, //bLength
Dance 0:f1ecca559ec3 183 STRING_DESCRIPTOR, //bDescriptorType 0x03
Dance 0:f1ecca559ec3 184 'H',0,'I',0,'D',0, //bString iInterface - HID
Dance 0:f1ecca559ec3 185 };
Dance 0:f1ecca559ec3 186 return stringIinterfaceDescriptor;
Dance 0:f1ecca559ec3 187 }
Dance 0:f1ecca559ec3 188
Dance 0:f1ecca559ec3 189 uint8_t * USBHID::stringIproductDesc() {
Dance 0:f1ecca559ec3 190 static uint8_t stringIproductDescriptor[] = {
Dance 0:f1ecca559ec3 191 0x16, //bLength
Dance 0:f1ecca559ec3 192 STRING_DESCRIPTOR, //bDescriptorType 0x03
Dance 0:f1ecca559ec3 193 'H',0,'I',0,'D',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 //bString iProduct - HID device
Dance 0:f1ecca559ec3 194 };
Dance 0:f1ecca559ec3 195 return stringIproductDescriptor;
Dance 0:f1ecca559ec3 196 }
Dance 0:f1ecca559ec3 197
Dance 0:f1ecca559ec3 198
Dance 0:f1ecca559ec3 199
Dance 0:f1ecca559ec3 200 uint8_t * USBHID::reportDesc() {
Dance 0:f1ecca559ec3 201 static uint8_t reportDescriptor[] = {
Dance 0:f1ecca559ec3 202 0x06, LSB(0xFFAB), MSB(0xFFAB),
Dance 0:f1ecca559ec3 203 0x0A, LSB(0x0200), MSB(0x0200),
Dance 0:f1ecca559ec3 204 0xA1, 0x01, // Collection 0x01
Dance 0:f1ecca559ec3 205 0x75, 0x08, // report size = 8 bits
Dance 0:f1ecca559ec3 206 0x15, 0x00, // logical minimum = 0
Dance 0:f1ecca559ec3 207 0x26, 0xFF, 0x00, // logical maximum = 255
Dance 0:f1ecca559ec3 208 0x95, input_length, // report count
Dance 0:f1ecca559ec3 209 0x09, 0x01, // usage
Dance 0:f1ecca559ec3 210 0x81, 0x02, // Input (array)
Dance 0:f1ecca559ec3 211 0x95, output_length,// report count
Dance 0:f1ecca559ec3 212 0x09, 0x02, // usage
Dance 0:f1ecca559ec3 213 0x91, 0x02, // Output (array)
Dance 0:f1ecca559ec3 214 0xC0 // end collection
Dance 0:f1ecca559ec3 215
Dance 0:f1ecca559ec3 216 };
Dance 0:f1ecca559ec3 217 reportLength = sizeof(reportDescriptor);
Dance 0:f1ecca559ec3 218 return reportDescriptor;
Dance 0:f1ecca559ec3 219 }
Dance 0:f1ecca559ec3 220
Dance 0:f1ecca559ec3 221 #define DEFAULT_CONFIGURATION (1)
Dance 0:f1ecca559ec3 222 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
Dance 0:f1ecca559ec3 223 + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
Dance 0:f1ecca559ec3 224 + (1 * HID_DESCRIPTOR_LENGTH) \
Dance 0:f1ecca559ec3 225 + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
Dance 0:f1ecca559ec3 226
Dance 0:f1ecca559ec3 227 uint8_t * USBHID::configurationDesc() {
Dance 0:f1ecca559ec3 228 static uint8_t configurationDescriptor[] = {
Dance 0:f1ecca559ec3 229 CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
Dance 0:f1ecca559ec3 230 CONFIGURATION_DESCRIPTOR, // bDescriptorType
Dance 0:f1ecca559ec3 231 LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB)
Dance 0:f1ecca559ec3 232 MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB)
Dance 0:f1ecca559ec3 233 0x01, // bNumInterfaces
Dance 0:f1ecca559ec3 234 DEFAULT_CONFIGURATION, // bConfigurationValue
Dance 0:f1ecca559ec3 235 0x00, // iConfiguration
Dance 0:f1ecca559ec3 236 C_RESERVED | C_SELF_POWERED, // bmAttributes
Dance 0:f1ecca559ec3 237 C_POWER(0), // bMaxPower
Dance 0:f1ecca559ec3 238
Dance 0:f1ecca559ec3 239 INTERFACE_DESCRIPTOR_LENGTH, // bLength
Dance 0:f1ecca559ec3 240 INTERFACE_DESCRIPTOR, // bDescriptorType
Dance 0:f1ecca559ec3 241 0x00, // bInterfaceNumber
Dance 0:f1ecca559ec3 242 0x00, // bAlternateSetting
Dance 0:f1ecca559ec3 243 0x02, // bNumEndpoints
Dance 0:f1ecca559ec3 244 HID_CLASS, // bInterfaceClass
Dance 0:f1ecca559ec3 245 HID_SUBCLASS_NONE, // bInterfaceSubClass
Dance 0:f1ecca559ec3 246 HID_PROTOCOL_NONE, // bInterfaceProtocol
Dance 0:f1ecca559ec3 247 0x00, // iInterface
Dance 0:f1ecca559ec3 248
Dance 0:f1ecca559ec3 249 HID_DESCRIPTOR_LENGTH, // bLength
Dance 0:f1ecca559ec3 250 HID_DESCRIPTOR, // bDescriptorType
Dance 0:f1ecca559ec3 251 LSB(HID_VERSION_1_11), // bcdHID (LSB)
Dance 0:f1ecca559ec3 252 MSB(HID_VERSION_1_11), // bcdHID (MSB)
Dance 0:f1ecca559ec3 253 0x00, // bCountryCode
Dance 0:f1ecca559ec3 254 0x01, // bNumDescriptors
Dance 0:f1ecca559ec3 255 REPORT_DESCRIPTOR, // bDescriptorType
Dance 0:f1ecca559ec3 256 (uint8_t)(LSB(this->reportDescLength())), // wDescriptorLength (LSB)
Dance 0:f1ecca559ec3 257 (uint8_t)(MSB(this->reportDescLength())), // wDescriptorLength (MSB)
Dance 0:f1ecca559ec3 258
Dance 0:f1ecca559ec3 259 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
Dance 0:f1ecca559ec3 260 ENDPOINT_DESCRIPTOR, // bDescriptorType
Dance 0:f1ecca559ec3 261 PHY_TO_DESC(EPINT_IN), // bEndpointAddress
Dance 0:f1ecca559ec3 262 E_INTERRUPT, // bmAttributes
Dance 0:f1ecca559ec3 263 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
Dance 0:f1ecca559ec3 264 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
Dance 0:f1ecca559ec3 265 1, // bInterval (milliseconds)
Dance 0:f1ecca559ec3 266
Dance 0:f1ecca559ec3 267 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
Dance 0:f1ecca559ec3 268 ENDPOINT_DESCRIPTOR, // bDescriptorType
Dance 0:f1ecca559ec3 269 PHY_TO_DESC(EPINT_OUT), // bEndpointAddress
Dance 0:f1ecca559ec3 270 E_INTERRUPT, // bmAttributes
Dance 0:f1ecca559ec3 271 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
Dance 0:f1ecca559ec3 272 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
Dance 0:f1ecca559ec3 273 1, // bInterval (milliseconds)
Dance 0:f1ecca559ec3 274 };
Dance 0:f1ecca559ec3 275 return configurationDescriptor;
Dance 0:f1ecca559ec3 276 }