|
| 1 | +/* |
| 2 | + * daplink_v2.ino |
| 3 | + * DAPLink with CMSIS-DAP v2 implementation for Seeed SAMD devices. |
| 4 | + * Author: Kenta IDA |
| 5 | + * This sketch is based on simple_daplink.ino sample sketch in the Seeed_Arduino_DAPLink library. |
| 6 | + * The original license is below. |
| 7 | + */ |
| 8 | +/* |
| 9 | + * Seeed_Arduino_DAPLink.ino |
| 10 | + * |
| 11 | + * Copyright (c) 2020 seeed technology co., ltd. |
| 12 | + * Author : weihong.cai (weihong.cai@seeed.cc) |
| 13 | + * Create Time : Aug 2020 |
| 14 | + * Change Log : |
| 15 | + * |
| 16 | + * The MIT License (MIT) |
| 17 | + * |
| 18 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 19 | + * of this software and associated documentation files (the "Software"), to deal |
| 20 | + * in the Software without restriction, including without limitation the rights |
| 21 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 22 | + * copies of the Software, and to permit persons to whom the Software istm |
| 23 | + * furnished to do so, subject to the following conditions: |
| 24 | + * |
| 25 | + * The above copyright notice and this permission notice shall be included in |
| 26 | + * all copies or substantial portions of the Software. |
| 27 | + * |
| 28 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 29 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 30 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 31 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 32 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 33 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS INcommInterface |
| 34 | + * THE SOFTWARE. |
| 35 | + * |
| 36 | + * Get start: |
| 37 | + * 1. For a device to be correctly detected as a CMSIS-DAP adapter, it must contain the string "CMSIS-DAP" in |
| 38 | + * its USB product name. |
| 39 | + * |
| 40 | + * 2. Download the Adafruit TinyUSB Library from: https://github.com/adafruit/Adafruit_TinyUSB_Arduino. |
| 41 | + * And add it to your local arduino library. |
| 42 | + * 3. Arduino IDE tool-->Select the board: Seeeduino Wio Terminal. |
| 43 | + * 4. Arduino IDE tool-->Select the USB Stack: TinyUSB. |
| 44 | + * 5. Arduino IDE tool-->Select the COM port. |
| 45 | + * |
| 46 | + * WARNING: |
| 47 | + * This demo is just a test-demo. At present, it only enumerate a HID Device to communicate with host(PC). |
| 48 | + * But it can't work as a DAPLink adapter. I think that it need to deal with the problem of data interaction |
| 49 | + * between host and device. If there is any progress, please tell to me. |
| 50 | + * |
| 51 | + */ |
| 52 | + |
| 53 | +#include "Adafruit_TinyUSB.h" |
| 54 | +#include "DAP_config.h" |
| 55 | +#include "DAP.h" |
| 56 | +#include <stdint.h> |
| 57 | +#include <cassert> |
| 58 | + |
| 59 | +uint32_t baud; |
| 60 | +uint32_t old_baud; |
| 61 | + |
| 62 | +#define SerialTTL Serial1 |
| 63 | + |
| 64 | +#define EPOUT 0x00 |
| 65 | +#define EPIN 0x80 |
| 66 | +#define EPSIZE 64 |
| 67 | +class CMSISDAPV2 : public Adafruit_USBD_Interface |
| 68 | +{ |
| 69 | +public: |
| 70 | + CMSISDAPV2() { |
| 71 | + this->setStringDescriptor("CMSIS-DAP interface"); |
| 72 | + } |
| 73 | + virtual uint16_t getDescriptor(uint8_t itfnum, uint8_t* buf, uint16_t bufsize) override { |
| 74 | + assert(buf != nullptr); |
| 75 | + if( bufsize == 0 ) return 0; |
| 76 | + |
| 77 | + uint8_t const intf_desc[] = { |
| 78 | + TUD_VENDOR_DESCRIPTOR(itfnum, 0, EPOUT, EPIN, EPSIZE) |
| 79 | + }; |
| 80 | + const uint16_t intf_desc_len = sizeof(intf_desc); |
| 81 | + |
| 82 | + if( bufsize < intf_desc_len ) return 0; |
| 83 | + |
| 84 | + memcpy(buf, intf_desc, intf_desc_len); |
| 85 | + return intf_desc_len; |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +static CMSISDAPV2 cmsisdap_v2; |
| 90 | + |
| 91 | +extern "C" void tud_vendor_rx_cb(uint8_t itf) |
| 92 | +{ |
| 93 | + auto data_available = tud_vendor_available(); |
| 94 | + if( data_available > 0 ) { |
| 95 | + uint8_t buffer[EPSIZE]; |
| 96 | + uint8_t response[EPSIZE]; |
| 97 | + auto bytes_received = tud_vendor_read(buffer, sizeof(buffer)); |
| 98 | + auto bytes_to_write = DAP_ExecuteCommand(buffer, response); |
| 99 | + if( bytes_to_write > 0 ) { |
| 100 | + tud_vendor_write(response, bytes_to_write); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +void setup() { |
| 106 | + USBDevice.setProductDescriptor("CMSIS-DAP"); |
| 107 | + USBDevice.addInterface(cmsisdap_v2); |
| 108 | + |
| 109 | + pinMode(LED_BUILTIN, OUTPUT); |
| 110 | + |
| 111 | + baud = old_baud = 115200; |
| 112 | + Serial.begin(baud); |
| 113 | + SerialTTL.begin(baud); |
| 114 | + |
| 115 | + // wait until device mounted |
| 116 | + while( !USBDevice.mounted() ) delay(1); |
| 117 | + |
| 118 | + DAP_Setup(); |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | +void loop() { |
| 123 | + // put your main code here, to run repeatedly: |
| 124 | + baud = Serial.baud(); |
| 125 | + if (baud != old_baud) { |
| 126 | + SerialTTL.begin(baud); |
| 127 | + while (!SerialTTL); |
| 128 | + old_baud = baud; |
| 129 | + } |
| 130 | + |
| 131 | + if (Serial.available() > 0) |
| 132 | + { |
| 133 | + char c = Serial.read(); |
| 134 | + SerialTTL.write(c); |
| 135 | + } |
| 136 | + |
| 137 | + if (SerialTTL.available() > 0) { |
| 138 | + char c = SerialTTL.read(); |
| 139 | + Serial.write(c); |
| 140 | + } |
| 141 | +} |
0 commit comments