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 "USBMSD.h"
Dance 0:f1ecca559ec3 21
Dance 0:f1ecca559ec3 22 #define DISK_OK 0x00
Dance 0:f1ecca559ec3 23 #define NO_INIT 0x01
Dance 0:f1ecca559ec3 24 #define NO_DISK 0x02
Dance 0:f1ecca559ec3 25 #define WRITE_PROTECT 0x04
Dance 0:f1ecca559ec3 26
Dance 0:f1ecca559ec3 27 #define CBW_Signature 0x43425355
Dance 0:f1ecca559ec3 28 #define CSW_Signature 0x53425355
Dance 0:f1ecca559ec3 29
Dance 0:f1ecca559ec3 30 // SCSI Commands
Dance 0:f1ecca559ec3 31 #define TEST_UNIT_READY 0x00
Dance 0:f1ecca559ec3 32 #define REQUEST_SENSE 0x03
Dance 0:f1ecca559ec3 33 #define FORMAT_UNIT 0x04
Dance 0:f1ecca559ec3 34 #define INQUIRY 0x12
Dance 0:f1ecca559ec3 35 #define MODE_SELECT6 0x15
Dance 0:f1ecca559ec3 36 #define MODE_SENSE6 0x1A
Dance 0:f1ecca559ec3 37 #define START_STOP_UNIT 0x1B
Dance 0:f1ecca559ec3 38 #define MEDIA_REMOVAL 0x1E
Dance 0:f1ecca559ec3 39 #define READ_FORMAT_CAPACITIES 0x23
Dance 0:f1ecca559ec3 40 #define READ_CAPACITY 0x25
Dance 0:f1ecca559ec3 41 #define READ10 0x28
Dance 0:f1ecca559ec3 42 #define WRITE10 0x2A
Dance 0:f1ecca559ec3 43 #define VERIFY10 0x2F
Dance 0:f1ecca559ec3 44 #define READ12 0xA8
Dance 0:f1ecca559ec3 45 #define WRITE12 0xAA
Dance 0:f1ecca559ec3 46 #define MODE_SELECT10 0x55
Dance 0:f1ecca559ec3 47 #define MODE_SENSE10 0x5A
Dance 0:f1ecca559ec3 48
Dance 0:f1ecca559ec3 49 // MSC class specific requests
Dance 0:f1ecca559ec3 50 #define MSC_REQUEST_RESET 0xFF
Dance 0:f1ecca559ec3 51 #define MSC_REQUEST_GET_MAX_LUN 0xFE
Dance 0:f1ecca559ec3 52
Dance 0:f1ecca559ec3 53 #define DEFAULT_CONFIGURATION (1)
Dance 0:f1ecca559ec3 54
Dance 0:f1ecca559ec3 55 // max packet size
Dance 0:f1ecca559ec3 56 #define MAX_PACKET MAX_PACKET_SIZE_EPBULK
Dance 0:f1ecca559ec3 57
Dance 0:f1ecca559ec3 58 // CSW Status
Dance 0:f1ecca559ec3 59 enum Status {
Dance 0:f1ecca559ec3 60 CSW_PASSED,
Dance 0:f1ecca559ec3 61 CSW_FAILED,
Dance 0:f1ecca559ec3 62 CSW_ERROR,
Dance 0:f1ecca559ec3 63 };
Dance 0:f1ecca559ec3 64
Dance 0:f1ecca559ec3 65
Dance 0:f1ecca559ec3 66 USBMSD::USBMSD(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {
Dance 0:f1ecca559ec3 67 stage = READ_CBW;
Dance 0:f1ecca559ec3 68 memset((void *)&cbw, 0, sizeof(CBW));
Dance 0:f1ecca559ec3 69 memset((void *)&csw, 0, sizeof(CSW));
Dance 0:f1ecca559ec3 70 page = NULL;
Dance 0:f1ecca559ec3 71 }
Dance 0:f1ecca559ec3 72
Dance 0:f1ecca559ec3 73 USBMSD::~USBMSD() {
Dance 0:f1ecca559ec3 74 disconnect();
Dance 0:f1ecca559ec3 75 }
Dance 0:f1ecca559ec3 76
Dance 0:f1ecca559ec3 77
Dance 0:f1ecca559ec3 78 // Called in ISR context to process a class specific request
Dance 0:f1ecca559ec3 79 bool USBMSD::USBCallback_request(void) {
Dance 0:f1ecca559ec3 80
Dance 0:f1ecca559ec3 81 bool success = false;
Dance 0:f1ecca559ec3 82 CONTROL_TRANSFER * transfer = getTransferPtr();
Dance 0:f1ecca559ec3 83 static uint8_t maxLUN[1] = {0};
Dance 0:f1ecca559ec3 84
Dance 0:f1ecca559ec3 85 if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
Dance 0:f1ecca559ec3 86 switch (transfer->setup.bRequest) {
Dance 0:f1ecca559ec3 87 case MSC_REQUEST_RESET:
Dance 0:f1ecca559ec3 88 reset();
Dance 0:f1ecca559ec3 89 success = true;
Dance 0:f1ecca559ec3 90 break;
Dance 0:f1ecca559ec3 91 case MSC_REQUEST_GET_MAX_LUN:
Dance 0:f1ecca559ec3 92 transfer->remaining = 1;
Dance 0:f1ecca559ec3 93 transfer->ptr = maxLUN;
Dance 0:f1ecca559ec3 94 transfer->direction = DEVICE_TO_HOST;
Dance 0:f1ecca559ec3 95 success = true;
Dance 0:f1ecca559ec3 96 break;
Dance 0:f1ecca559ec3 97 default:
Dance 0:f1ecca559ec3 98 break;
Dance 0:f1ecca559ec3 99 }
Dance 0:f1ecca559ec3 100 }
Dance 0:f1ecca559ec3 101
Dance 0:f1ecca559ec3 102 return success;
Dance 0:f1ecca559ec3 103 }
Dance 0:f1ecca559ec3 104
Dance 0:f1ecca559ec3 105
Dance 0:f1ecca559ec3 106 bool USBMSD::connect(bool blocking) {
Dance 0:f1ecca559ec3 107 //disk initialization
Dance 0:f1ecca559ec3 108 if (disk_status() & NO_INIT) {
Dance 0:f1ecca559ec3 109 if (disk_initialize()) {
Dance 0:f1ecca559ec3 110 return false;
Dance 0:f1ecca559ec3 111 }
Dance 0:f1ecca559ec3 112 }
Dance 0:f1ecca559ec3 113
Dance 0:f1ecca559ec3 114 // get number of blocks
Dance 0:f1ecca559ec3 115 BlockCount = disk_sectors();
Dance 0:f1ecca559ec3 116
Dance 0:f1ecca559ec3 117 // get memory size
Dance 0:f1ecca559ec3 118 MemorySize = disk_size();
Dance 0:f1ecca559ec3 119
Dance 0:f1ecca559ec3 120 if (BlockCount > 0) {
Dance 0:f1ecca559ec3 121 BlockSize = MemorySize / BlockCount;
Dance 0:f1ecca559ec3 122 if (BlockSize != 0) {
Dance 0:f1ecca559ec3 123 free(page);
Dance 0:f1ecca559ec3 124 page = (uint8_t *)malloc(BlockSize * sizeof(uint8_t));
Dance 0:f1ecca559ec3 125 if (page == NULL)
Dance 0:f1ecca559ec3 126 return false;
Dance 0:f1ecca559ec3 127 }
Dance 0:f1ecca559ec3 128 } else {
Dance 0:f1ecca559ec3 129 return false;
Dance 0:f1ecca559ec3 130 }
Dance 0:f1ecca559ec3 131
Dance 0:f1ecca559ec3 132 //connect the device
Dance 0:f1ecca559ec3 133 USBDevice::connect(blocking);
Dance 0:f1ecca559ec3 134 return true;
Dance 0:f1ecca559ec3 135 }
Dance 0:f1ecca559ec3 136
Dance 0:f1ecca559ec3 137 void USBMSD::disconnect() {
Dance 0:f1ecca559ec3 138 //De-allocate MSD page size:
Dance 0:f1ecca559ec3 139 free(page);
Dance 0:f1ecca559ec3 140 page = NULL;
Dance 0:f1ecca559ec3 141 USBDevice::disconnect();
Dance 0:f1ecca559ec3 142 }
Dance 0:f1ecca559ec3 143
Dance 0:f1ecca559ec3 144 void USBMSD::reset() {
Dance 0:f1ecca559ec3 145 stage = READ_CBW;
Dance 0:f1ecca559ec3 146 }
Dance 0:f1ecca559ec3 147
Dance 0:f1ecca559ec3 148
Dance 0:f1ecca559ec3 149 // Called in ISR context called when a data is received
Dance 0:f1ecca559ec3 150 bool USBMSD::EP2_OUT_callback() {
Dance 0:f1ecca559ec3 151 uint32_t size = 0;
Dance 0:f1ecca559ec3 152 uint8_t buf[MAX_PACKET_SIZE_EPBULK];
Dance 0:f1ecca559ec3 153 readEP(EPBULK_OUT, buf, &size, MAX_PACKET_SIZE_EPBULK);
Dance 0:f1ecca559ec3 154 switch (stage) {
Dance 0:f1ecca559ec3 155 // the device has to decode the CBW received
Dance 0:f1ecca559ec3 156 case READ_CBW:
Dance 0:f1ecca559ec3 157 CBWDecode(buf, size);
Dance 0:f1ecca559ec3 158 break;
Dance 0:f1ecca559ec3 159
Dance 0:f1ecca559ec3 160 // the device has to receive data from the host
Dance 0:f1ecca559ec3 161 case PROCESS_CBW:
Dance 0:f1ecca559ec3 162 switch (cbw.CB[0]) {
Dance 0:f1ecca559ec3 163 case WRITE10:
Dance 0:f1ecca559ec3 164 case WRITE12:
Dance 0:f1ecca559ec3 165 memoryWrite(buf, size);
Dance 0:f1ecca559ec3 166 break;
Dance 0:f1ecca559ec3 167 case VERIFY10:
Dance 0:f1ecca559ec3 168 memoryVerify(buf, size);
Dance 0:f1ecca559ec3 169 break;
Dance 0:f1ecca559ec3 170 }
Dance 0:f1ecca559ec3 171 break;
Dance 0:f1ecca559ec3 172
Dance 0:f1ecca559ec3 173 // an error has occured: stall endpoint and send CSW
Dance 0:f1ecca559ec3 174 default:
Dance 0:f1ecca559ec3 175 stallEndpoint(EPBULK_OUT);
Dance 0:f1ecca559ec3 176 csw.Status = CSW_ERROR;
Dance 0:f1ecca559ec3 177 sendCSW();
Dance 0:f1ecca559ec3 178 break;
Dance 0:f1ecca559ec3 179 }
Dance 0:f1ecca559ec3 180
Dance 0:f1ecca559ec3 181 //reactivate readings on the OUT bulk endpoint
Dance 0:f1ecca559ec3 182 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
Dance 0:f1ecca559ec3 183 return true;
Dance 0:f1ecca559ec3 184 }
Dance 0:f1ecca559ec3 185
Dance 0:f1ecca559ec3 186 // Called in ISR context when a data has been transferred
Dance 0:f1ecca559ec3 187 bool USBMSD::EP2_IN_callback() {
Dance 0:f1ecca559ec3 188 switch (stage) {
Dance 0:f1ecca559ec3 189
Dance 0:f1ecca559ec3 190 // the device has to send data to the host
Dance 0:f1ecca559ec3 191 case PROCESS_CBW:
Dance 0:f1ecca559ec3 192 switch (cbw.CB[0]) {
Dance 0:f1ecca559ec3 193 case READ10:
Dance 0:f1ecca559ec3 194 case READ12:
Dance 0:f1ecca559ec3 195 memoryRead();
Dance 0:f1ecca559ec3 196 break;
Dance 0:f1ecca559ec3 197 }
Dance 0:f1ecca559ec3 198 break;
Dance 0:f1ecca559ec3 199
Dance 0:f1ecca559ec3 200 //the device has to send a CSW
Dance 0:f1ecca559ec3 201 case SEND_CSW:
Dance 0:f1ecca559ec3 202 sendCSW();
Dance 0:f1ecca559ec3 203 break;
Dance 0:f1ecca559ec3 204
Dance 0:f1ecca559ec3 205 // the host has received the CSW -> we wait a CBW
Dance 0:f1ecca559ec3 206 case WAIT_CSW:
Dance 0:f1ecca559ec3 207 stage = READ_CBW;
Dance 0:f1ecca559ec3 208 break;
Dance 0:f1ecca559ec3 209
Dance 0:f1ecca559ec3 210 // an error has occured
Dance 0:f1ecca559ec3 211 default:
Dance 0:f1ecca559ec3 212 stallEndpoint(EPBULK_IN);
Dance 0:f1ecca559ec3 213 sendCSW();
Dance 0:f1ecca559ec3 214 break;
Dance 0:f1ecca559ec3 215 }
Dance 0:f1ecca559ec3 216 return true;
Dance 0:f1ecca559ec3 217 }
Dance 0:f1ecca559ec3 218
Dance 0:f1ecca559ec3 219
Dance 0:f1ecca559ec3 220 void USBMSD::memoryWrite (uint8_t * buf, uint16_t size) {
Dance 0:f1ecca559ec3 221
Dance 0:f1ecca559ec3 222 if ((addr + size) > MemorySize) {
Dance 0:f1ecca559ec3 223 size = MemorySize - addr;
Dance 0:f1ecca559ec3 224 stage = ERROR;
Dance 0:f1ecca559ec3 225 stallEndpoint(EPBULK_OUT);
Dance 0:f1ecca559ec3 226 }
Dance 0:f1ecca559ec3 227
Dance 0:f1ecca559ec3 228 // we fill an array in RAM of 1 block before writing it in memory
Dance 0:f1ecca559ec3 229 for (int i = 0; i < size; i++)
Dance 0:f1ecca559ec3 230 page[addr%BlockSize + i] = buf[i];
Dance 0:f1ecca559ec3 231
Dance 0:f1ecca559ec3 232 // if the array is filled, write it in memory
Dance 0:f1ecca559ec3 233 if (!((addr + size)%BlockSize)) {
Dance 0:f1ecca559ec3 234 if (!(disk_status() & WRITE_PROTECT)) {
Dance 0:f1ecca559ec3 235 disk_write(page, addr/BlockSize);
Dance 0:f1ecca559ec3 236 }
Dance 0:f1ecca559ec3 237 }
Dance 0:f1ecca559ec3 238
Dance 0:f1ecca559ec3 239 addr += size;
Dance 0:f1ecca559ec3 240 length -= size;
Dance 0:f1ecca559ec3 241 csw.DataResidue -= size;
Dance 0:f1ecca559ec3 242
Dance 0:f1ecca559ec3 243 if ((!length) || (stage != PROCESS_CBW)) {
Dance 0:f1ecca559ec3 244 csw.Status = (stage == ERROR) ? CSW_FAILED : CSW_PASSED;
Dance 0:f1ecca559ec3 245 sendCSW();
Dance 0:f1ecca559ec3 246 }
Dance 0:f1ecca559ec3 247 }
Dance 0:f1ecca559ec3 248
Dance 0:f1ecca559ec3 249 void USBMSD::memoryVerify (uint8_t * buf, uint16_t size) {
Dance 0:f1ecca559ec3 250 uint32_t n;
Dance 0:f1ecca559ec3 251
Dance 0:f1ecca559ec3 252 if ((addr + size) > MemorySize) {
Dance 0:f1ecca559ec3 253 size = MemorySize - addr;
Dance 0:f1ecca559ec3 254 stage = ERROR;
Dance 0:f1ecca559ec3 255 stallEndpoint(EPBULK_OUT);
Dance 0:f1ecca559ec3 256 }
Dance 0:f1ecca559ec3 257
Dance 0:f1ecca559ec3 258 // beginning of a new block -> load a whole block in RAM
Dance 0:f1ecca559ec3 259 if (!(addr%BlockSize))
Dance 0:f1ecca559ec3 260 disk_read(page, addr/BlockSize);
Dance 0:f1ecca559ec3 261
Dance 0:f1ecca559ec3 262 // info are in RAM -> no need to re-read memory
Dance 0:f1ecca559ec3 263 for (n = 0; n < size; n++) {
Dance 0:f1ecca559ec3 264 if (page[addr%BlockSize + n] != buf[n]) {
Dance 0:f1ecca559ec3 265 memOK = false;
Dance 0:f1ecca559ec3 266 break;
Dance 0:f1ecca559ec3 267 }
Dance 0:f1ecca559ec3 268 }
Dance 0:f1ecca559ec3 269
Dance 0:f1ecca559ec3 270 addr += size;
Dance 0:f1ecca559ec3 271 length -= size;
Dance 0:f1ecca559ec3 272 csw.DataResidue -= size;
Dance 0:f1ecca559ec3 273
Dance 0:f1ecca559ec3 274 if ( !length || (stage != PROCESS_CBW)) {
Dance 0:f1ecca559ec3 275 csw.Status = (memOK && (stage == PROCESS_CBW)) ? CSW_PASSED : CSW_FAILED;
Dance 0:f1ecca559ec3 276 sendCSW();
Dance 0:f1ecca559ec3 277 }
Dance 0:f1ecca559ec3 278 }
Dance 0:f1ecca559ec3 279
Dance 0:f1ecca559ec3 280
Dance 0:f1ecca559ec3 281 bool USBMSD::inquiryRequest (void) {
Dance 0:f1ecca559ec3 282 uint8_t inquiry[] = { 0x00, 0x80, 0x00, 0x01,
Dance 0:f1ecca559ec3 283 36 - 4, 0x80, 0x00, 0x00,
Dance 0:f1ecca559ec3 284 'M', 'B', 'E', 'D', '.', 'O', 'R', 'G',
Dance 0:f1ecca559ec3 285 'M', 'B', 'E', 'D', ' ', 'U', 'S', 'B', ' ', 'D', 'I', 'S', 'K', ' ', ' ', ' ',
Dance 0:f1ecca559ec3 286 '1', '.', '0', ' ',
Dance 0:f1ecca559ec3 287 };
Dance 0:f1ecca559ec3 288 if (!write(inquiry, sizeof(inquiry))) {
Dance 0:f1ecca559ec3 289 return false;
Dance 0:f1ecca559ec3 290 }
Dance 0:f1ecca559ec3 291 return true;
Dance 0:f1ecca559ec3 292 }
Dance 0:f1ecca559ec3 293
Dance 0:f1ecca559ec3 294
Dance 0:f1ecca559ec3 295 bool USBMSD::readFormatCapacity() {
Dance 0:f1ecca559ec3 296 uint8_t capacity[] = { 0x00, 0x00, 0x00, 0x08,
Dance 0:f1ecca559ec3 297 (uint8_t)((BlockCount >> 24) & 0xff),
Dance 0:f1ecca559ec3 298 (uint8_t)((BlockCount >> 16) & 0xff),
Dance 0:f1ecca559ec3 299 (uint8_t)((BlockCount >> 8) & 0xff),
Dance 0:f1ecca559ec3 300 (uint8_t)((BlockCount >> 0) & 0xff),
Dance 0:f1ecca559ec3 301
Dance 0:f1ecca559ec3 302 0x02,
Dance 0:f1ecca559ec3 303 (uint8_t)((BlockSize >> 16) & 0xff),
Dance 0:f1ecca559ec3 304 (uint8_t)((BlockSize >> 8) & 0xff),
Dance 0:f1ecca559ec3 305 (uint8_t)((BlockSize >> 0) & 0xff),
Dance 0:f1ecca559ec3 306 };
Dance 0:f1ecca559ec3 307 if (!write(capacity, sizeof(capacity))) {
Dance 0:f1ecca559ec3 308 return false;
Dance 0:f1ecca559ec3 309 }
Dance 0:f1ecca559ec3 310 return true;
Dance 0:f1ecca559ec3 311 }
Dance 0:f1ecca559ec3 312
Dance 0:f1ecca559ec3 313
Dance 0:f1ecca559ec3 314 bool USBMSD::readCapacity (void) {
Dance 0:f1ecca559ec3 315 uint8_t capacity[] = {
Dance 0:f1ecca559ec3 316 (uint8_t)(((BlockCount - 1) >> 24) & 0xff),
Dance 0:f1ecca559ec3 317 (uint8_t)(((BlockCount - 1) >> 16) & 0xff),
Dance 0:f1ecca559ec3 318 (uint8_t)(((BlockCount - 1) >> 8) & 0xff),
Dance 0:f1ecca559ec3 319 (uint8_t)(((BlockCount - 1) >> 0) & 0xff),
Dance 0:f1ecca559ec3 320
Dance 0:f1ecca559ec3 321 (uint8_t)((BlockSize >> 24) & 0xff),
Dance 0:f1ecca559ec3 322 (uint8_t)((BlockSize >> 16) & 0xff),
Dance 0:f1ecca559ec3 323 (uint8_t)((BlockSize >> 8) & 0xff),
Dance 0:f1ecca559ec3 324 (uint8_t)((BlockSize >> 0) & 0xff),
Dance 0:f1ecca559ec3 325 };
Dance 0:f1ecca559ec3 326 if (!write(capacity, sizeof(capacity))) {
Dance 0:f1ecca559ec3 327 return false;
Dance 0:f1ecca559ec3 328 }
Dance 0:f1ecca559ec3 329 return true;
Dance 0:f1ecca559ec3 330 }
Dance 0:f1ecca559ec3 331
Dance 0:f1ecca559ec3 332 bool USBMSD::write (uint8_t * buf, uint16_t size) {
Dance 0:f1ecca559ec3 333
Dance 0:f1ecca559ec3 334 if (size >= cbw.DataLength) {
Dance 0:f1ecca559ec3 335 size = cbw.DataLength;
Dance 0:f1ecca559ec3 336 }
Dance 0:f1ecca559ec3 337 stage = SEND_CSW;
Dance 0:f1ecca559ec3 338
Dance 0:f1ecca559ec3 339 if (!writeNB(EPBULK_IN, buf, size, MAX_PACKET_SIZE_EPBULK)) {
Dance 0:f1ecca559ec3 340 return false;
Dance 0:f1ecca559ec3 341 }
Dance 0:f1ecca559ec3 342
Dance 0:f1ecca559ec3 343 csw.DataResidue -= size;
Dance 0:f1ecca559ec3 344 csw.Status = CSW_PASSED;
Dance 0:f1ecca559ec3 345 return true;
Dance 0:f1ecca559ec3 346 }
Dance 0:f1ecca559ec3 347
Dance 0:f1ecca559ec3 348
Dance 0:f1ecca559ec3 349 bool USBMSD::modeSense6 (void) {
Dance 0:f1ecca559ec3 350 uint8_t sense6[] = { 0x03, 0x00, 0x00, 0x00 };
Dance 0:f1ecca559ec3 351 if (!write(sense6, sizeof(sense6))) {
Dance 0:f1ecca559ec3 352 return false;
Dance 0:f1ecca559ec3 353 }
Dance 0:f1ecca559ec3 354 return true;
Dance 0:f1ecca559ec3 355 }
Dance 0:f1ecca559ec3 356
Dance 0:f1ecca559ec3 357 void USBMSD::sendCSW() {
Dance 0:f1ecca559ec3 358 csw.Signature = CSW_Signature;
Dance 0:f1ecca559ec3 359 writeNB(EPBULK_IN, (uint8_t *)&csw, sizeof(CSW), MAX_PACKET_SIZE_EPBULK);
Dance 0:f1ecca559ec3 360 stage = WAIT_CSW;
Dance 0:f1ecca559ec3 361 }
Dance 0:f1ecca559ec3 362
Dance 0:f1ecca559ec3 363 bool USBMSD::requestSense (void) {
Dance 0:f1ecca559ec3 364 uint8_t request_sense[] = {
Dance 0:f1ecca559ec3 365 0x70,
Dance 0:f1ecca559ec3 366 0x00,
Dance 0:f1ecca559ec3 367 0x05, // Sense Key: illegal request
Dance 0:f1ecca559ec3 368 0x00,
Dance 0:f1ecca559ec3 369 0x00,
Dance 0:f1ecca559ec3 370 0x00,
Dance 0:f1ecca559ec3 371 0x00,
Dance 0:f1ecca559ec3 372 0x0A,
Dance 0:f1ecca559ec3 373 0x00,
Dance 0:f1ecca559ec3 374 0x00,
Dance 0:f1ecca559ec3 375 0x00,
Dance 0:f1ecca559ec3 376 0x00,
Dance 0:f1ecca559ec3 377 0x30,
Dance 0:f1ecca559ec3 378 0x01,
Dance 0:f1ecca559ec3 379 0x00,
Dance 0:f1ecca559ec3 380 0x00,
Dance 0:f1ecca559ec3 381 0x00,
Dance 0:f1ecca559ec3 382 0x00,
Dance 0:f1ecca559ec3 383 };
Dance 0:f1ecca559ec3 384
Dance 0:f1ecca559ec3 385 if (!write(request_sense, sizeof(request_sense))) {
Dance 0:f1ecca559ec3 386 return false;
Dance 0:f1ecca559ec3 387 }
Dance 0:f1ecca559ec3 388
Dance 0:f1ecca559ec3 389 return true;
Dance 0:f1ecca559ec3 390 }
Dance 0:f1ecca559ec3 391
Dance 0:f1ecca559ec3 392 void USBMSD::fail() {
Dance 0:f1ecca559ec3 393 csw.Status = CSW_FAILED;
Dance 0:f1ecca559ec3 394 sendCSW();
Dance 0:f1ecca559ec3 395 }
Dance 0:f1ecca559ec3 396
Dance 0:f1ecca559ec3 397
Dance 0:f1ecca559ec3 398 void USBMSD::CBWDecode(uint8_t * buf, uint16_t size) {
Dance 0:f1ecca559ec3 399 if (size == sizeof(cbw)) {
Dance 0:f1ecca559ec3 400 memcpy((uint8_t *)&cbw, buf, size);
Dance 0:f1ecca559ec3 401 if (cbw.Signature == CBW_Signature) {
Dance 0:f1ecca559ec3 402 csw.Tag = cbw.Tag;
Dance 0:f1ecca559ec3 403 csw.DataResidue = cbw.DataLength;
Dance 0:f1ecca559ec3 404 if ((cbw.CBLength < 1) || (cbw.CBLength > 16) ) {
Dance 0:f1ecca559ec3 405 fail();
Dance 0:f1ecca559ec3 406 } else {
Dance 0:f1ecca559ec3 407 switch (cbw.CB[0]) {
Dance 0:f1ecca559ec3 408 case TEST_UNIT_READY:
Dance 0:f1ecca559ec3 409 testUnitReady();
Dance 0:f1ecca559ec3 410 break;
Dance 0:f1ecca559ec3 411 case REQUEST_SENSE:
Dance 0:f1ecca559ec3 412 requestSense();
Dance 0:f1ecca559ec3 413 break;
Dance 0:f1ecca559ec3 414 case INQUIRY:
Dance 0:f1ecca559ec3 415 inquiryRequest();
Dance 0:f1ecca559ec3 416 break;
Dance 0:f1ecca559ec3 417 case MODE_SENSE6:
Dance 0:f1ecca559ec3 418 modeSense6();
Dance 0:f1ecca559ec3 419 break;
Dance 0:f1ecca559ec3 420 case READ_FORMAT_CAPACITIES:
Dance 0:f1ecca559ec3 421 readFormatCapacity();
Dance 0:f1ecca559ec3 422 break;
Dance 0:f1ecca559ec3 423 case READ_CAPACITY:
Dance 0:f1ecca559ec3 424 readCapacity();
Dance 0:f1ecca559ec3 425 break;
Dance 0:f1ecca559ec3 426 case READ10:
Dance 0:f1ecca559ec3 427 case READ12:
Dance 0:f1ecca559ec3 428 if (infoTransfer()) {
Dance 0:f1ecca559ec3 429 if ((cbw.Flags & 0x80)) {
Dance 0:f1ecca559ec3 430 stage = PROCESS_CBW;
Dance 0:f1ecca559ec3 431 memoryRead();
Dance 0:f1ecca559ec3 432 } else {
Dance 0:f1ecca559ec3 433 stallEndpoint(EPBULK_OUT);
Dance 0:f1ecca559ec3 434 csw.Status = CSW_ERROR;
Dance 0:f1ecca559ec3 435 sendCSW();
Dance 0:f1ecca559ec3 436 }
Dance 0:f1ecca559ec3 437 }
Dance 0:f1ecca559ec3 438 break;
Dance 0:f1ecca559ec3 439 case WRITE10:
Dance 0:f1ecca559ec3 440 case WRITE12:
Dance 0:f1ecca559ec3 441 if (infoTransfer()) {
Dance 0:f1ecca559ec3 442 if (!(cbw.Flags & 0x80)) {
Dance 0:f1ecca559ec3 443 stage = PROCESS_CBW;
Dance 0:f1ecca559ec3 444 } else {
Dance 0:f1ecca559ec3 445 stallEndpoint(EPBULK_IN);
Dance 0:f1ecca559ec3 446 csw.Status = CSW_ERROR;
Dance 0:f1ecca559ec3 447 sendCSW();
Dance 0:f1ecca559ec3 448 }
Dance 0:f1ecca559ec3 449 }
Dance 0:f1ecca559ec3 450 break;
Dance 0:f1ecca559ec3 451 case VERIFY10:
Dance 0:f1ecca559ec3 452 if (!(cbw.CB[1] & 0x02)) {
Dance 0:f1ecca559ec3 453 csw.Status = CSW_PASSED;
Dance 0:f1ecca559ec3 454 sendCSW();
Dance 0:f1ecca559ec3 455 break;
Dance 0:f1ecca559ec3 456 }
Dance 0:f1ecca559ec3 457 if (infoTransfer()) {
Dance 0:f1ecca559ec3 458 if (!(cbw.Flags & 0x80)) {
Dance 0:f1ecca559ec3 459 stage = PROCESS_CBW;
Dance 0:f1ecca559ec3 460 memOK = true;
Dance 0:f1ecca559ec3 461 } else {
Dance 0:f1ecca559ec3 462 stallEndpoint(EPBULK_IN);
Dance 0:f1ecca559ec3 463 csw.Status = CSW_ERROR;
Dance 0:f1ecca559ec3 464 sendCSW();
Dance 0:f1ecca559ec3 465 }
Dance 0:f1ecca559ec3 466 }
Dance 0:f1ecca559ec3 467 break;
Dance 0:f1ecca559ec3 468 case MEDIA_REMOVAL:
Dance 0:f1ecca559ec3 469 csw.Status = CSW_PASSED;
Dance 0:f1ecca559ec3 470 sendCSW();
Dance 0:f1ecca559ec3 471 break;
Dance 0:f1ecca559ec3 472 default:
Dance 0:f1ecca559ec3 473 fail();
Dance 0:f1ecca559ec3 474 break;
Dance 0:f1ecca559ec3 475 }
Dance 0:f1ecca559ec3 476 }
Dance 0:f1ecca559ec3 477 }
Dance 0:f1ecca559ec3 478 }
Dance 0:f1ecca559ec3 479 }
Dance 0:f1ecca559ec3 480
Dance 0:f1ecca559ec3 481 void USBMSD::testUnitReady (void) {
Dance 0:f1ecca559ec3 482
Dance 0:f1ecca559ec3 483 if (cbw.DataLength != 0) {
Dance 0:f1ecca559ec3 484 if ((cbw.Flags & 0x80) != 0) {
Dance 0:f1ecca559ec3 485 stallEndpoint(EPBULK_IN);
Dance 0:f1ecca559ec3 486 } else {
Dance 0:f1ecca559ec3 487 stallEndpoint(EPBULK_OUT);
Dance 0:f1ecca559ec3 488 }
Dance 0:f1ecca559ec3 489 }
Dance 0:f1ecca559ec3 490
Dance 0:f1ecca559ec3 491 csw.Status = CSW_PASSED;
Dance 0:f1ecca559ec3 492 sendCSW();
Dance 0:f1ecca559ec3 493 }
Dance 0:f1ecca559ec3 494
Dance 0:f1ecca559ec3 495
Dance 0:f1ecca559ec3 496 void USBMSD::memoryRead (void) {
Dance 0:f1ecca559ec3 497 uint32_t n;
Dance 0:f1ecca559ec3 498
Dance 0:f1ecca559ec3 499 n = (length > MAX_PACKET) ? MAX_PACKET : length;
Dance 0:f1ecca559ec3 500
Dance 0:f1ecca559ec3 501 if ((addr + n) > MemorySize) {
Dance 0:f1ecca559ec3 502 n = MemorySize - addr;
Dance 0:f1ecca559ec3 503 stage = ERROR;
Dance 0:f1ecca559ec3 504 }
Dance 0:f1ecca559ec3 505
Dance 0:f1ecca559ec3 506 // we read an entire block
Dance 0:f1ecca559ec3 507 if (!(addr%BlockSize))
Dance 0:f1ecca559ec3 508 disk_read(page, addr/BlockSize);
Dance 0:f1ecca559ec3 509
Dance 0:f1ecca559ec3 510 // write data which are in RAM
Dance 0:f1ecca559ec3 511 writeNB(EPBULK_IN, &page[addr%BlockSize], n, MAX_PACKET_SIZE_EPBULK);
Dance 0:f1ecca559ec3 512
Dance 0:f1ecca559ec3 513 addr += n;
Dance 0:f1ecca559ec3 514 length -= n;
Dance 0:f1ecca559ec3 515
Dance 0:f1ecca559ec3 516 csw.DataResidue -= n;
Dance 0:f1ecca559ec3 517
Dance 0:f1ecca559ec3 518 if ( !length || (stage != PROCESS_CBW)) {
Dance 0:f1ecca559ec3 519 csw.Status = (stage == PROCESS_CBW) ? CSW_PASSED : CSW_FAILED;
Dance 0:f1ecca559ec3 520 stage = (stage == PROCESS_CBW) ? SEND_CSW : stage;
Dance 0:f1ecca559ec3 521 }
Dance 0:f1ecca559ec3 522 }
Dance 0:f1ecca559ec3 523
Dance 0:f1ecca559ec3 524
Dance 0:f1ecca559ec3 525 bool USBMSD::infoTransfer (void) {
Dance 0:f1ecca559ec3 526 uint32_t n;
Dance 0:f1ecca559ec3 527
Dance 0:f1ecca559ec3 528 // Logical Block Address of First Block
Dance 0:f1ecca559ec3 529 n = (cbw.CB[2] << 24) | (cbw.CB[3] << 16) | (cbw.CB[4] << 8) | (cbw.CB[5] << 0);
Dance 0:f1ecca559ec3 530
Dance 0:f1ecca559ec3 531 addr = n * BlockSize;
Dance 0:f1ecca559ec3 532
Dance 0:f1ecca559ec3 533 // Number of Blocks to transfer
Dance 0:f1ecca559ec3 534 switch (cbw.CB[0]) {
Dance 0:f1ecca559ec3 535 case READ10:
Dance 0:f1ecca559ec3 536 case WRITE10:
Dance 0:f1ecca559ec3 537 case VERIFY10:
Dance 0:f1ecca559ec3 538 n = (cbw.CB[7] << 8) | (cbw.CB[8] << 0);
Dance 0:f1ecca559ec3 539 break;
Dance 0:f1ecca559ec3 540
Dance 0:f1ecca559ec3 541 case READ12:
Dance 0:f1ecca559ec3 542 case WRITE12:
Dance 0:f1ecca559ec3 543 n = (cbw.CB[6] << 24) | (cbw.CB[7] << 16) | (cbw.CB[8] << 8) | (cbw.CB[9] << 0);
Dance 0:f1ecca559ec3 544 break;
Dance 0:f1ecca559ec3 545 }
Dance 0:f1ecca559ec3 546
Dance 0:f1ecca559ec3 547 length = n * BlockSize;
Dance 0:f1ecca559ec3 548
Dance 0:f1ecca559ec3 549 if (!cbw.DataLength) { // host requests no data
Dance 0:f1ecca559ec3 550 csw.Status = CSW_FAILED;
Dance 0:f1ecca559ec3 551 sendCSW();
Dance 0:f1ecca559ec3 552 return false;
Dance 0:f1ecca559ec3 553 }
Dance 0:f1ecca559ec3 554
Dance 0:f1ecca559ec3 555 if (cbw.DataLength != length) {
Dance 0:f1ecca559ec3 556 if ((cbw.Flags & 0x80) != 0) {
Dance 0:f1ecca559ec3 557 stallEndpoint(EPBULK_IN);
Dance 0:f1ecca559ec3 558 } else {
Dance 0:f1ecca559ec3 559 stallEndpoint(EPBULK_OUT);
Dance 0:f1ecca559ec3 560 }
Dance 0:f1ecca559ec3 561
Dance 0:f1ecca559ec3 562 csw.Status = CSW_FAILED;
Dance 0:f1ecca559ec3 563 sendCSW();
Dance 0:f1ecca559ec3 564 return false;
Dance 0:f1ecca559ec3 565 }
Dance 0:f1ecca559ec3 566
Dance 0:f1ecca559ec3 567 return true;
Dance 0:f1ecca559ec3 568 }
Dance 0:f1ecca559ec3 569
Dance 0:f1ecca559ec3 570
Dance 0:f1ecca559ec3 571
Dance 0:f1ecca559ec3 572
Dance 0:f1ecca559ec3 573
Dance 0:f1ecca559ec3 574 // Called in ISR context
Dance 0:f1ecca559ec3 575 // Set configuration. Return false if the
Dance 0:f1ecca559ec3 576 // configuration is not supported.
Dance 0:f1ecca559ec3 577 bool USBMSD::USBCallback_setConfiguration(uint8_t configuration) {
Dance 0:f1ecca559ec3 578 if (configuration != DEFAULT_CONFIGURATION) {
Dance 0:f1ecca559ec3 579 return false;
Dance 0:f1ecca559ec3 580 }
Dance 0:f1ecca559ec3 581
Dance 0:f1ecca559ec3 582 // Configure endpoints > 0
Dance 0:f1ecca559ec3 583 addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
Dance 0:f1ecca559ec3 584 addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
Dance 0:f1ecca559ec3 585
Dance 0:f1ecca559ec3 586 //activate readings
Dance 0:f1ecca559ec3 587 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
Dance 0:f1ecca559ec3 588 return true;
Dance 0:f1ecca559ec3 589 }
Dance 0:f1ecca559ec3 590
Dance 0:f1ecca559ec3 591
Dance 0:f1ecca559ec3 592 uint8_t * USBMSD::stringIinterfaceDesc() {
Dance 0:f1ecca559ec3 593 static uint8_t stringIinterfaceDescriptor[] = {
Dance 0:f1ecca559ec3 594 0x08, //bLength
Dance 0:f1ecca559ec3 595 STRING_DESCRIPTOR, //bDescriptorType 0x03
Dance 0:f1ecca559ec3 596 'M',0,'S',0,'D',0 //bString iInterface - MSD
Dance 0:f1ecca559ec3 597 };
Dance 0:f1ecca559ec3 598 return stringIinterfaceDescriptor;
Dance 0:f1ecca559ec3 599 }
Dance 0:f1ecca559ec3 600
Dance 0:f1ecca559ec3 601 uint8_t * USBMSD::stringIproductDesc() {
Dance 0:f1ecca559ec3 602 static uint8_t stringIproductDescriptor[] = {
Dance 0:f1ecca559ec3 603 0x12, //bLength
Dance 0:f1ecca559ec3 604 STRING_DESCRIPTOR, //bDescriptorType 0x03
Dance 0:f1ecca559ec3 605 'M',0,'b',0,'e',0,'d',0,' ',0,'M',0,'S',0,'D',0 //bString iProduct - Mbed Audio
Dance 0:f1ecca559ec3 606 };
Dance 0:f1ecca559ec3 607 return stringIproductDescriptor;
Dance 0:f1ecca559ec3 608 }
Dance 0:f1ecca559ec3 609
Dance 0:f1ecca559ec3 610
Dance 0:f1ecca559ec3 611 uint8_t * USBMSD::configurationDesc() {
Dance 0:f1ecca559ec3 612 static uint8_t configDescriptor[] = {
Dance 0:f1ecca559ec3 613
Dance 0:f1ecca559ec3 614 // Configuration 1
Dance 0:f1ecca559ec3 615 9, // bLength
Dance 0:f1ecca559ec3 616 2, // bDescriptorType
Dance 0:f1ecca559ec3 617 LSB(9 + 9 + 7 + 7), // wTotalLength
Dance 0:f1ecca559ec3 618 MSB(9 + 9 + 7 + 7),
Dance 0:f1ecca559ec3 619 0x01, // bNumInterfaces
Dance 0:f1ecca559ec3 620 0x01, // bConfigurationValue: 0x01 is used to select this configuration
Dance 0:f1ecca559ec3 621 0x00, // iConfiguration: no string to describe this configuration
Dance 0:f1ecca559ec3 622 0xC0, // bmAttributes
Dance 0:f1ecca559ec3 623 100, // bMaxPower, device power consumption is 100 mA
Dance 0:f1ecca559ec3 624
Dance 0:f1ecca559ec3 625 // Interface 0, Alternate Setting 0, MSC Class
Dance 0:f1ecca559ec3 626 9, // bLength
Dance 0:f1ecca559ec3 627 4, // bDescriptorType
Dance 0:f1ecca559ec3 628 0x00, // bInterfaceNumber
Dance 0:f1ecca559ec3 629 0x00, // bAlternateSetting
Dance 0:f1ecca559ec3 630 0x02, // bNumEndpoints
Dance 0:f1ecca559ec3 631 0x08, // bInterfaceClass
Dance 0:f1ecca559ec3 632 0x06, // bInterfaceSubClass
Dance 0:f1ecca559ec3 633 0x50, // bInterfaceProtocol
Dance 0:f1ecca559ec3 634 0x04, // iInterface
Dance 0:f1ecca559ec3 635
Dance 0:f1ecca559ec3 636 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
Dance 0:f1ecca559ec3 637 7, // bLength
Dance 0:f1ecca559ec3 638 5, // bDescriptorType
Dance 0:f1ecca559ec3 639 PHY_TO_DESC(EPBULK_IN), // bEndpointAddress
Dance 0:f1ecca559ec3 640 0x02, // bmAttributes (0x02=bulk)
Dance 0:f1ecca559ec3 641 LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)
Dance 0:f1ecca559ec3 642 MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)
Dance 0:f1ecca559ec3 643 0, // bInterval
Dance 0:f1ecca559ec3 644
Dance 0:f1ecca559ec3 645 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
Dance 0:f1ecca559ec3 646 7, // bLength
Dance 0:f1ecca559ec3 647 5, // bDescriptorType
Dance 0:f1ecca559ec3 648 PHY_TO_DESC(EPBULK_OUT), // bEndpointAddress
Dance 0:f1ecca559ec3 649 0x02, // bmAttributes (0x02=bulk)
Dance 0:f1ecca559ec3 650 LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)
Dance 0:f1ecca559ec3 651 MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)
Dance 0:f1ecca559ec3 652 0 // bInterval
Dance 0:f1ecca559ec3 653 };
Dance 0:f1ecca559ec3 654 return configDescriptor;
Dance 0:f1ecca559ec3 655 }