|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2018, hathach for Adafruit |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +#ifdef USE_TINYUSB |
| 26 | + |
| 27 | +#include "Arduino.h" |
| 28 | +#include "Adafruit_TinyUSB_Core.h" |
| 29 | +#include <Reset.h> // Needed for auto-reset with 1200bps port touch |
| 30 | + |
| 31 | +//--------------------------------------------------------------------+ |
| 32 | +// MACRO TYPEDEF CONSTANT ENUM DECLARATION |
| 33 | +//--------------------------------------------------------------------+ |
| 34 | + |
| 35 | +// Init usb hardware when starting up. Softdevice is not enabled yet |
| 36 | +static void usb_hardware_init(void) |
| 37 | +{ |
| 38 | +#ifdef PIN_LED_TXL |
| 39 | +// txLEDPulse = 0; |
| 40 | + pinMode(PIN_LED_TXL, OUTPUT); |
| 41 | + digitalWrite(PIN_LED_TXL, HIGH); |
| 42 | +#endif |
| 43 | + |
| 44 | +#ifdef PIN_LED_RXL |
| 45 | +// rxLEDPulse = 0; |
| 46 | + pinMode(PIN_LED_RXL, OUTPUT); |
| 47 | + digitalWrite(PIN_LED_RXL, HIGH); |
| 48 | +#endif |
| 49 | + |
| 50 | + /* Enable USB clock */ |
| 51 | +#if defined(__SAMD51__) |
| 52 | + MCLK->APBBMASK.reg |= MCLK_APBBMASK_USB; |
| 53 | + MCLK->AHBMASK.reg |= MCLK_AHBMASK_USB; |
| 54 | + |
| 55 | + // Set up the USB DP/DN pins |
| 56 | + PORT->Group[0].PINCFG[PIN_PA24H_USB_DM].bit.PMUXEN = 1; |
| 57 | + PORT->Group[0].PMUX[PIN_PA24H_USB_DM/2].reg &= ~(0xF << (4 * (PIN_PA24H_USB_DM & 0x01u))); |
| 58 | + PORT->Group[0].PMUX[PIN_PA24H_USB_DM/2].reg |= MUX_PA24H_USB_DM << (4 * (PIN_PA24H_USB_DM & 0x01u)); |
| 59 | + PORT->Group[0].PINCFG[PIN_PA25H_USB_DP].bit.PMUXEN = 1; |
| 60 | + PORT->Group[0].PMUX[PIN_PA25H_USB_DP/2].reg &= ~(0xF << (4 * (PIN_PA25H_USB_DP & 0x01u))); |
| 61 | + PORT->Group[0].PMUX[PIN_PA25H_USB_DP/2].reg |= MUX_PA25H_USB_DP << (4 * (PIN_PA25H_USB_DP & 0x01u)); |
| 62 | + |
| 63 | + |
| 64 | + GCLK->PCHCTRL[USB_GCLK_ID].reg = GCLK_PCHCTRL_GEN_GCLK1_Val | (1 << GCLK_PCHCTRL_CHEN_Pos); |
| 65 | +#else |
| 66 | + PM->APBBMASK.reg |= PM_APBBMASK_USB; |
| 67 | + |
| 68 | + // Set up the USB DP/DN pins |
| 69 | + PORT->Group[0].PINCFG[PIN_PA24G_USB_DM].bit.PMUXEN = 1; |
| 70 | + PORT->Group[0].PMUX[PIN_PA24G_USB_DM/2].reg &= ~(0xF << (4 * (PIN_PA24G_USB_DM & 0x01u))); |
| 71 | + PORT->Group[0].PMUX[PIN_PA24G_USB_DM/2].reg |= MUX_PA24G_USB_DM << (4 * (PIN_PA24G_USB_DM & 0x01u)); |
| 72 | + PORT->Group[0].PINCFG[PIN_PA25G_USB_DP].bit.PMUXEN = 1; |
| 73 | + PORT->Group[0].PMUX[PIN_PA25G_USB_DP/2].reg &= ~(0xF << (4 * (PIN_PA25G_USB_DP & 0x01u))); |
| 74 | + PORT->Group[0].PMUX[PIN_PA25G_USB_DP/2].reg |= MUX_PA25G_USB_DP << (4 * (PIN_PA25G_USB_DP & 0x01u)); |
| 75 | + |
| 76 | + // Put Generic Clock Generator 0 as source for Generic Clock Multiplexer 6 (USB reference) |
| 77 | + GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID(6) | // Generic Clock Multiplexer 6 |
| 78 | + GCLK_CLKCTRL_GEN_GCLK0 | // Generic Clock Generator 0 is source |
| 79 | + GCLK_CLKCTRL_CLKEN; |
| 80 | + while (GCLK->STATUS.bit.SYNCBUSY) |
| 81 | + ; |
| 82 | +#endif |
| 83 | +} |
| 84 | + |
| 85 | +uint8_t load_serial_number(uint16_t* serial_str) |
| 86 | +{ |
| 87 | + enum { SERIAL_BYTE_LEN = 16 }; |
| 88 | + |
| 89 | +#ifdef __SAMD51__ |
| 90 | + uint32_t* id_addresses[4] = {(uint32_t *) 0x008061FC, (uint32_t *) 0x00806010, |
| 91 | + (uint32_t *) 0x00806014, (uint32_t *) 0x00806018}; |
| 92 | +#else // samd21 |
| 93 | + uint32_t* id_addresses[4] = {(uint32_t *) 0x0080A00C, (uint32_t *) 0x0080A040, |
| 94 | + (uint32_t *) 0x0080A044, (uint32_t *) 0x0080A048}; |
| 95 | + |
| 96 | +#endif |
| 97 | + |
| 98 | + uint8_t raw_id[SERIAL_BYTE_LEN]; |
| 99 | + |
| 100 | + for (int i=0; i<4; i++) { |
| 101 | + for (int k=0; k<4; k++) { |
| 102 | + raw_id[4 * i + (3 - k)] = (*(id_addresses[i]) >> k * 8) & 0xff; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + static const char nibble_to_hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; |
| 107 | + |
| 108 | + for (unsigned int i = 0; i < sizeof(raw_id); i++) { |
| 109 | + for (int j = 0; j < 2; j++) { |
| 110 | + uint8_t nibble = (raw_id[i] >> (j * 4)) & 0xf; |
| 111 | + // Strings are UTF-16-LE encoded. |
| 112 | + serial_str[i * 2 + (1 - j)] = nibble_to_hex[nibble]; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return sizeof(raw_id)*2; |
| 117 | +} |
| 118 | + |
| 119 | +void Adafruit_TinyUSB_Core_init(void) |
| 120 | +{ |
| 121 | + USBDevice.addInterface( (Adafruit_USBD_Interface&) Serial); |
| 122 | + USBDevice.setID(USB_VID, USB_PID); |
| 123 | + USBDevice.begin(); |
| 124 | + |
| 125 | + usb_hardware_init(); |
| 126 | + |
| 127 | + // Init tinyusb stack |
| 128 | + tusb_init(); |
| 129 | +} |
| 130 | + |
| 131 | +void Adafruit_TinyUSB_Core_touch1200(void) |
| 132 | +{ |
| 133 | + initiateReset(250); |
| 134 | +} |
| 135 | + |
| 136 | + |
| 137 | +extern "C" |
| 138 | +{ |
| 139 | + |
| 140 | +void yield(void) |
| 141 | +{ |
| 142 | + tud_task(); |
| 143 | + tud_cdc_write_flush(); |
| 144 | +} |
| 145 | + |
| 146 | +} |
| 147 | + |
| 148 | +#endif // USE_TINYUSB |
0 commit comments