Skip to content

Commit d61bc3c

Browse files
committed
major changes, move tinyusb source from core to this library
make it more portable and easier to integrate with other cores
1 parent 6d9cbc9 commit d61bc3c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+25546
-2
lines changed

src/Adafruit_TinyUSB.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@
2323
*/
2424

2525
#include "Adafruit_TinyUSB.h"
26-
#include "Arduino.h"
26+
27+
#ifdef USE_TINYUSB
2728

2829
//--------------------------------------------------------------------+
2930
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
3031
//--------------------------------------------------------------------+
3132

32-
//------------- IMPLEMENTATION -------------//
33+
void Adafruit_TinyUSB_Device_init(uint8_t rhport)
34+
{
35+
USBDevice.begin(rhport);
36+
}
37+
38+
#endif

src/Adafruit_TinyUSB.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,26 @@
2525
#ifndef ADAFRUIT_TINYUSB_H_
2626
#define ADAFRUIT_TINYUSB_H_
2727

28+
//#ifndef USE_TINYUSB
29+
//#error TinyUSB is not selected, please select it in Tools->Menu->USB Stack
30+
//#endif
31+
32+
#ifdef USE_TINYUSB
33+
34+
#include "tusb.h"
35+
#include "Adafruit_USBD_Device.h"
36+
#include "Adafruit_USBD_CDC.h"
2837
#include "Adafruit_USBD_HID.h"
2938
#include "Adafruit_USBD_MIDI.h"
3039
#include "Adafruit_USBD_MSC.h"
3140
#include "Adafruit_USBD_WebUSB.h"
3241

42+
// Called by main.cpp to initialize usb device typically with CDC device for Serial
43+
void Adafruit_TinyUSB_Device_init(uint8_t rhport);
44+
45+
// Invoked when host disconnects cdc at baud 1200, usually touch feature to go into DFU mode
46+
void Adafruit_TinyUSB_touch1200(void);
47+
48+
#endif
49+
3350
#endif /* ADAFRUIT_TINYUSB_H_ */

src/Adafruit_TinyUSB_SAMD.cpp

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019, 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+
#if defined ARDUINO_ARCH_SAMD & defined USE_TINYUSB
26+
27+
#include "Arduino.h"
28+
#include "tusb.h"
29+
#include "Adafruit_USBD_Device.h"
30+
#include "Adafruit_USBD_CDC.h"
31+
32+
#include <Reset.h> // Needed for auto-reset with 1200bps port touch
33+
34+
//--------------------------------------------------------------------+
35+
// Forward USB interrupt events to TinyUSB IRQ Handler
36+
//--------------------------------------------------------------------+
37+
extern "C"
38+
{
39+
40+
#if defined(__SAMD51__)
41+
42+
void USB_0_Handler (void) { tud_int_handler(0); }
43+
void USB_1_Handler (void) { tud_int_handler(0); }
44+
void USB_2_Handler (void) { tud_int_handler(0); }
45+
void USB_3_Handler (void) { tud_int_handler(0); }
46+
47+
#else
48+
49+
void USB_Handler(void) { tud_int_handler(0); }
50+
51+
#endif
52+
} // extern C
53+
54+
55+
56+
//--------------------------------------------------------------------+
57+
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
58+
//--------------------------------------------------------------------+
59+
60+
#if CFG_TUSB_DEBUG
61+
extern "C" int serial1_printf(const char *__restrict format, ...)
62+
{
63+
char buf[PRINTF_BUF];
64+
va_list ap;
65+
va_start(ap, format);
66+
vsnprintf(buf, sizeof(buf), format, ap);
67+
Serial1.write(buf);
68+
va_end(ap);
69+
70+
}
71+
#endif
72+
73+
//--------------------------------------------------------------------+
74+
// Core Init & Touch1200
75+
//--------------------------------------------------------------------+
76+
77+
void Adafruit_TinyUSB_touch1200(void)
78+
{
79+
initiateReset(250);
80+
}
81+
82+
// run TinyUSB background task when yield()
83+
extern "C" void yield(void)
84+
{
85+
tud_task();
86+
tud_cdc_write_flush();
87+
}
88+
89+
//--------------------------------------------------------------------+
90+
// Adafruit_USBD_Device platform dependent
91+
//--------------------------------------------------------------------+
92+
void Adafruit_USBD_Device::portInitHardware(uint8_t rhport)
93+
{
94+
/* Enable USB clock */
95+
#if defined(__SAMD51__)
96+
MCLK->APBBMASK.reg |= MCLK_APBBMASK_USB;
97+
MCLK->AHBMASK.reg |= MCLK_AHBMASK_USB;
98+
99+
// Set up the USB DP/DN pins
100+
PORT->Group[0].PINCFG[PIN_PA24H_USB_DM].bit.PMUXEN = 1;
101+
PORT->Group[0].PMUX[PIN_PA24H_USB_DM/2].reg &= ~(0xF << (4 * (PIN_PA24H_USB_DM & 0x01u)));
102+
PORT->Group[0].PMUX[PIN_PA24H_USB_DM/2].reg |= MUX_PA24H_USB_DM << (4 * (PIN_PA24H_USB_DM & 0x01u));
103+
PORT->Group[0].PINCFG[PIN_PA25H_USB_DP].bit.PMUXEN = 1;
104+
PORT->Group[0].PMUX[PIN_PA25H_USB_DP/2].reg &= ~(0xF << (4 * (PIN_PA25H_USB_DP & 0x01u)));
105+
PORT->Group[0].PMUX[PIN_PA25H_USB_DP/2].reg |= MUX_PA25H_USB_DP << (4 * (PIN_PA25H_USB_DP & 0x01u));
106+
107+
108+
GCLK->PCHCTRL[USB_GCLK_ID].reg = GCLK_PCHCTRL_GEN_GCLK1_Val | (1 << GCLK_PCHCTRL_CHEN_Pos);
109+
110+
NVIC_SetPriority(USB_0_IRQn, 0UL);
111+
NVIC_SetPriority(USB_1_IRQn, 0UL);
112+
NVIC_SetPriority(USB_2_IRQn, 0UL);
113+
NVIC_SetPriority(USB_3_IRQn, 0UL);
114+
#else
115+
PM->APBBMASK.reg |= PM_APBBMASK_USB;
116+
117+
// Set up the USB DP/DN pins
118+
PORT->Group[0].PINCFG[PIN_PA24G_USB_DM].bit.PMUXEN = 1;
119+
PORT->Group[0].PMUX[PIN_PA24G_USB_DM/2].reg &= ~(0xF << (4 * (PIN_PA24G_USB_DM & 0x01u)));
120+
PORT->Group[0].PMUX[PIN_PA24G_USB_DM/2].reg |= MUX_PA24G_USB_DM << (4 * (PIN_PA24G_USB_DM & 0x01u));
121+
PORT->Group[0].PINCFG[PIN_PA25G_USB_DP].bit.PMUXEN = 1;
122+
PORT->Group[0].PMUX[PIN_PA25G_USB_DP/2].reg &= ~(0xF << (4 * (PIN_PA25G_USB_DP & 0x01u)));
123+
PORT->Group[0].PMUX[PIN_PA25G_USB_DP/2].reg |= MUX_PA25G_USB_DP << (4 * (PIN_PA25G_USB_DP & 0x01u));
124+
125+
// Put Generic Clock Generator 0 as source for Generic Clock Multiplexer 6 (USB reference)
126+
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID(6) | // Generic Clock Multiplexer 6
127+
GCLK_CLKCTRL_GEN_GCLK0 | // Generic Clock Generator 0 is source
128+
GCLK_CLKCTRL_CLKEN;
129+
while (GCLK->STATUS.bit.SYNCBUSY)
130+
;
131+
132+
NVIC_SetPriority((IRQn_Type) USB_IRQn, 0UL);
133+
#endif
134+
}
135+
136+
uint8_t Adafruit_USBD_Device::getSerialDescriptor(uint16_t* serial_str)
137+
{
138+
enum { SERIAL_BYTE_LEN = 16 };
139+
140+
#ifdef __SAMD51__
141+
uint32_t* id_addresses[4] = {(uint32_t *) 0x008061FC, (uint32_t *) 0x00806010,
142+
(uint32_t *) 0x00806014, (uint32_t *) 0x00806018};
143+
#else // samd21
144+
uint32_t* id_addresses[4] = {(uint32_t *) 0x0080A00C, (uint32_t *) 0x0080A040,
145+
(uint32_t *) 0x0080A044, (uint32_t *) 0x0080A048};
146+
#endif
147+
148+
uint8_t raw_id[SERIAL_BYTE_LEN];
149+
150+
for (int i=0; i<4; i++) {
151+
for (int k=0; k<4; k++) {
152+
raw_id[4 * i + (3 - k)] = (*(id_addresses[i]) >> k * 8) & 0xff;
153+
}
154+
}
155+
156+
const char nibble_to_hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
157+
158+
for (unsigned int i = 0; i < sizeof(raw_id); i++) {
159+
for (int j = 0; j < 2; j++) {
160+
uint8_t nibble = (raw_id[i] >> (j * 4)) & 0xf;
161+
// Strings are UTF-16-LE encoded.
162+
serial_str[i * 2 + (1 - j)] = nibble_to_hex[nibble];
163+
}
164+
}
165+
166+
return sizeof(raw_id)*2;
167+
}
168+
169+
#endif // USE_TINYUSB

0 commit comments

Comments
 (0)