Skip to content

Commit 7f74c21

Browse files
committed
update usb stuff
1 parent 56bde31 commit 7f74c21

File tree

8 files changed

+89
-35
lines changed

8 files changed

+89
-35
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* SAMD21_USBDevice.cpp
3+
*
4+
* Created on: Feb 21, 2018
5+
* Author: deanm
6+
*/
7+
8+
9+
#include "SAMD21_USBDevice.h"
10+
11+
void USBDevice_SAMD21G18x::reset() {
12+
usb.CTRLA.bit.SWRST = 1;
13+
memset(EP, 0, sizeof(EP));
14+
while (usb.SYNCBUSY.bit.SWRST || usb.SYNCBUSY.bit.ENABLE) {}
15+
usb.DESCADD.reg = (uint32_t)(&EP);
16+
}
17+
18+
void USBDevice_SAMD21G18x::calibrate() {
19+
// Load Pad Calibration data from non-volatile memory
20+
uint32_t *pad_transn_p = (uint32_t *) USB_FUSES_TRANSN_ADDR;
21+
uint32_t *pad_transp_p = (uint32_t *) USB_FUSES_TRANSP_ADDR;
22+
uint32_t *pad_trim_p = (uint32_t *) USB_FUSES_TRIM_ADDR;
23+
24+
uint32_t pad_transn = (*pad_transn_p & USB_FUSES_TRANSN_Msk) >> USB_FUSES_TRANSN_Pos;
25+
uint32_t pad_transp = (*pad_transp_p & USB_FUSES_TRANSP_Msk) >> USB_FUSES_TRANSP_Pos;
26+
uint32_t pad_trim = (*pad_trim_p & USB_FUSES_TRIM_Msk ) >> USB_FUSES_TRIM_Pos;
27+
28+
if (pad_transn == 0x1F) // maximum value (31)
29+
pad_transn = 5;
30+
if (pad_transp == 0x1F) // maximum value (31)
31+
pad_transp = 29;
32+
if (pad_trim == 0x7) // maximum value (7)
33+
pad_trim = 3;
34+
35+
usb.PADCAL.bit.TRANSN = pad_transn;
36+
usb.PADCAL.bit.TRANSP = pad_transp;
37+
usb.PADCAL.bit.TRIM = pad_trim;
38+
}

cores/arduino/USB/SAMD21_USBDevice.h

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -177,35 +177,6 @@ class USBDevice_SAMD21G18x {
177177
__attribute__((__aligned__(4))) UsbDeviceDescriptor EP[USB_EPT_NUM];
178178
};
179179

180-
void USBDevice_SAMD21G18x::reset() {
181-
usb.CTRLA.bit.SWRST = 1;
182-
memset(EP, 0, sizeof(EP));
183-
while (usb.SYNCBUSY.bit.SWRST || usb.SYNCBUSY.bit.ENABLE) {}
184-
usb.DESCADD.reg = (uint32_t)(&EP);
185-
}
186-
187-
void USBDevice_SAMD21G18x::calibrate() {
188-
// Load Pad Calibration data from non-volatile memory
189-
uint32_t *pad_transn_p = (uint32_t *) USB_FUSES_TRANSN_ADDR;
190-
uint32_t *pad_transp_p = (uint32_t *) USB_FUSES_TRANSP_ADDR;
191-
uint32_t *pad_trim_p = (uint32_t *) USB_FUSES_TRIM_ADDR;
192-
193-
uint32_t pad_transn = (*pad_transn_p & USB_FUSES_TRANSN_Msk) >> USB_FUSES_TRANSN_Pos;
194-
uint32_t pad_transp = (*pad_transp_p & USB_FUSES_TRANSP_Msk) >> USB_FUSES_TRANSP_Pos;
195-
uint32_t pad_trim = (*pad_trim_p & USB_FUSES_TRIM_Msk ) >> USB_FUSES_TRIM_Pos;
196-
197-
if (pad_transn == 0x1F) // maximum value (31)
198-
pad_transn = 5;
199-
if (pad_transp == 0x1F) // maximum value (31)
200-
pad_transp = 29;
201-
if (pad_trim == 0x7) // maximum value (7)
202-
pad_trim = 3;
203-
204-
usb.PADCAL.bit.TRANSN = pad_transn;
205-
usb.PADCAL.bit.TRANSP = pad_transp;
206-
usb.PADCAL.bit.TRIM = pad_trim;
207-
}
208-
209180
/*
210181
* Synchronization primitives.
211182
* TODO: Move into a separate header file and make an API out of it
@@ -231,7 +202,6 @@ class __Guard {
231202

232203
#define synchronized for (__Guard __guard; __guard.enter(); )
233204

234-
235205
/*
236206
* USB EP generic handlers.
237207
*/
@@ -241,6 +211,8 @@ class EPHandler {
241211
virtual void handleEndpoint() = 0;
242212
virtual uint32_t recv(void *_data, uint32_t len) = 0;
243213
virtual uint32_t available() const = 0;
214+
215+
virtual void init() = 0;
244216
};
245217

246218
class DoubleBufferedEPOutHandler : public EPHandler {
@@ -268,6 +240,7 @@ class DoubleBufferedEPOutHandler : public EPHandler {
268240
free((void*)data0);
269241
free((void*)data1);
270242
}
243+
void init() {};
271244

272245
virtual uint32_t recv(void *_data, uint32_t len)
273246
{
@@ -412,4 +385,3 @@ class DoubleBufferedEPOutHandler : public EPHandler {
412385

413386
volatile bool notify;
414387
};
415-

cores/arduino/USB/USBAPI.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,17 @@
3131

3232
#include "Stream.h"
3333
#include "RingBuffer.h"
34+
#ifdef __SAMR21G18A__
35+
#include "SAMR21_USBDevice.h"
36+
#else
37+
#include "SAMD21_USBDevice.h"
38+
#endif
3439

3540
//================================================================================
3641
// USB
3742

43+
class EPHandler;
44+
3845
// Low level API
3946
typedef struct {
4047
union {
@@ -85,6 +92,7 @@ class USBDeviceClass {
8592
// Generic EndPoint API
8693
void initEndpoints(void);
8794
void initEP(uint32_t ep, uint32_t type);
95+
void setHandler(uint32_t ep, EPHandler *handler);
8896
void handleEndpoint(uint8_t ep);
8997

9098
uint32_t send(uint32_t ep, const void *data, uint32_t len);

cores/arduino/USB/USBCore.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,12 @@ void USBDeviceClass::initEP(uint32_t ep, uint32_t config)
508508
}
509509
epHandlers[ep] = new DoubleBufferedEPOutHandler(usbd, ep, 256);
510510
}
511+
else if (config == (USB_ENDPOINT_TYPE_INTERRUPT | USB_ENDPOINT_OUT(0)))
512+
{
513+
if(epHandlers[ep]){
514+
epHandlers[ep]->init();
515+
}
516+
}
511517
else if (config == (USB_ENDPOINT_TYPE_BULK | USB_ENDPOINT_IN(0)))
512518
{
513519
usbd.epBank1SetSize(ep, 64);
@@ -539,6 +545,10 @@ void USBDeviceClass::initEP(uint32_t ep, uint32_t config)
539545
}
540546
}
541547

548+
void USBDeviceClass::setHandler(uint32_t ep, EPHandler *handler) {
549+
epHandlers[ep] = handler;
550+
}
551+
542552
void USBDeviceClass::flush(uint32_t ep)
543553
{
544554
if (available(ep)) {
@@ -734,8 +744,8 @@ uint32_t USBDeviceClass::send(uint32_t ep, const void *data, uint32_t len)
734744

735745
LastTransmitTimedOut[ep] = 0;
736746

737-
if (len >= EPX_SIZE) {
738-
length = EPX_SIZE - 1;
747+
if (len > EPX_SIZE) {
748+
length = EPX_SIZE;
739749
} else {
740750
length = len;
741751
}
@@ -996,6 +1006,7 @@ void USBDeviceClass::ISRHandler()
9961006
// Check if endpoint has a pending interrupt
9971007
if ((ept_int & (1 << i)) != 0)
9981008
{
1009+
9991010
// Endpoint Transfer Complete (0/1) Interrupt
10001011
if (usbd.epBank0IsTransferComplete(i) ||
10011012
usbd.epBank1IsTransferComplete(i))

cores/arduino/startup.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ void SystemInit( void )
222222
}
223223

224224
MCLK->CPUDIV.reg = MCLK_CPUDIV_DIV_DIV1;
225+
226+
/* Use the LDO regulator by default */
227+
SUPC->VREG.bit.SEL = 0;
225228

226229
//*************** END SAMD51 *************************//
227230

cores/arduino/wiring.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ void init( void )
177177
DAC->CTRLB.reg = DAC_CTRLB_REFSEL_AVCC | // Using the 3.3V reference
178178
DAC_CTRLB_EOEN ; // External Output Enable (Vout)
179179
#endif
180-
#endif
180+
181+
182+
#endif //SAMD51
181183
}
182184

183185
#ifdef __cplusplus

variants/metro_m4/variant.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,18 @@ const PinDescription g_APinDescription[]=
169169
{ PORTA, 3, PIO_ANALOG, PIN_ATTR_ANALOG, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE }, // DAC/VREFP
170170

171171
// ----------------------
172-
// 40 - Alternate use of A0 and A1 (DAC output)
172+
// 40 - 41 Alternate use of A0 and A1 (DAC output)
173173
{ PORTA, 2, PIO_ANALOG, PIN_ATTR_ANALOG, DAC_Channel0, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_2 }, // DAC/VOUT[0]
174174
{ PORTA, 5, PIO_ANALOG, PIN_ATTR_ANALOG, DAC_Channel1, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_5 }, // DAC/VOUT[1]
175+
176+
// ----------------------
177+
// 42 - 47 QSPI (SCK, CS, IO0, IO1, IO2, IO3)
178+
{ PORTB, 10, PIO_COM, PIN_ATTR_NONE, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE },
179+
{ PORTB, 11, PIO_COM, PIN_ATTR_NONE, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE },
180+
{ PORTA, 8, PIO_COM, PIN_ATTR_NONE, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE },
181+
{ PORTA, 9, PIO_COM, PIN_ATTR_NONE, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE },
182+
{ PORTA, 10, PIO_COM, PIN_ATTR_NONE, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE },
183+
{ PORTA, 11, PIO_COM, PIN_ATTR_NONE, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE },
175184
} ;
176185

177186
const void* g_apTCInstances[TCC_INST_NUM+TC_INST_NUM]={ TCC0, TCC1, TCC2, TCC3, TCC4, TC5 } ;

variants/metro_m4/variant.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,17 @@ static const uint8_t SCL = PIN_WIRE_SCL;
200200
#define PIN_I2S_FS (12u)
201201
#define PIN_I2S_MCK (2u)
202202

203+
//QSPI Pins
204+
#define PIN_QSPI_SCK (42u)
205+
#define PIN_QSPI_CS (43u)
206+
#define PIN_QSPI_IO0 (44u)
207+
#define PIN_QSPI_IO1 (45u)
208+
#define PIN_QSPI_IO2 (46u)
209+
#define PIN_QSPI_IO3 (47u)
210+
211+
//TODO: meaningful value for this
212+
#define VARIANT_QSPI_BAUD_DEFAULT 5000000
213+
203214
#ifdef __cplusplus
204215
}
205216
#endif

0 commit comments

Comments
 (0)