@@ -110,7 +110,7 @@ static inline void ClearOUT(void)
110
110
UEINTX = ~(1 <<RXOUTI);
111
111
}
112
112
113
- void Recv (volatile u8* data, u8 count)
113
+ static inline void Recv (volatile u8* data, u8 count)
114
114
{
115
115
while (count--)
116
116
*data++ = UEDATX;
@@ -253,7 +253,7 @@ u8 USB_SendSpace(u8 ep)
253
253
LockEP lock (ep);
254
254
if (!ReadWriteAllowed ())
255
255
return 0 ;
256
- return 64 - FifoByteCount ();
256
+ return USB_EP_SIZE - FifoByteCount ();
257
257
}
258
258
259
259
// Blocking Send of data to an endpoint
@@ -326,6 +326,7 @@ u8 _initEndpoints[] =
326
326
327
327
#define EP_SINGLE_64 0x32 // EP0
328
328
#define EP_DOUBLE_64 0x36 // Other endpoints
329
+ #define EP_SINGLE_16 0x12
329
330
330
331
static
331
332
void InitEP (u8 index, u8 type, u8 size)
@@ -344,7 +345,13 @@ void InitEndpoints()
344
345
UENUM = i;
345
346
UECONX = (1 <<EPEN);
346
347
UECFG0X = _initEndpoints[i];
348
+ #if USB_EP_SIZE == 16
349
+ UECFG1X = EP_SINGLE_16;
350
+ #elif USB_EP_SIZE == 64
347
351
UECFG1X = EP_DOUBLE_64;
352
+ #else
353
+ #error Unsupported value for USB_EP_SIZE
354
+ #endif
348
355
}
349
356
UERST = 0x7E ; // And reset them
350
357
UERST = 0 ;
@@ -620,13 +627,19 @@ void USB_Flush(u8 ep)
620
627
621
628
static inline void USB_ClockDisable ()
622
629
{
630
+ #if defined(OTGPADE)
623
631
USBCON = (USBCON & ~(1 <<OTGPADE)) | (1 <<FRZCLK); // freeze clock and disable VBUS Pad
632
+ #else // u2 Series
633
+ USBCON = (1 << FRZCLK); // freeze clock
634
+ #endif
624
635
PLLCSR &= ~(1 <<PLLE); // stop PLL
625
636
}
626
637
627
638
static inline void USB_ClockEnable ()
628
639
{
640
+ #if defined(UHWCON)
629
641
UHWCON |= (1 <<UVREGE); // power internal reg
642
+ #endif
630
643
USBCON = (1 <<USBE) | (1 <<FRZCLK); // clock frozen, usb enabled
631
644
632
645
// ATmega32U4
@@ -639,6 +652,16 @@ static inline void USB_ClockEnable()
639
652
#error "Clock rate of F_CPU not supported"
640
653
#endif
641
654
655
+ #elif defined(__AVR_AT90USB82__) || defined(__AVR_AT90USB162__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega8U2__)
656
+ // for the u2 Series the datasheet is confusing. On page 40 its called PINDIV and on page 290 its called PLLP0
657
+ #if F_CPU == 16000000UL
658
+ // Need 16 MHz xtal
659
+ PLLCSR |= (1 << PLLP0);
660
+ #elif F_CPU == 8000000UL
661
+ // Need 8 MHz xtal
662
+ PLLCSR &= ~(1 << PLLP0);
663
+ #endif
664
+
642
665
// AT90USB646, AT90USB647, AT90USB1286, AT90USB1287
643
666
#elif defined(PLLP2)
644
667
#if F_CPU == 16000000UL
@@ -670,10 +693,18 @@ static inline void USB_ClockEnable()
670
693
// strange behaviors when the board is reset using the serial
671
694
// port touch at 1200 bps. This delay fixes this behavior.
672
695
delay (1 );
696
+ #if defined(OTGPADE)
673
697
USBCON = (USBCON & ~(1 <<FRZCLK)) | (1 <<OTGPADE); // start USB clock, enable VBUS Pad
698
+ #else
699
+ USBCON &= ~(1 << FRZCLK); // start USB clock
700
+ #endif
674
701
675
702
#if defined(RSTCPU)
703
+ #if defined(LSM)
676
704
UDCON &= ~((1 <<RSTCPU) | (1 <<LSM) | (1 <<RMWKUP) | (1 <<DETACH)); // enable attach resistor, set full speed mode
705
+ #else // u2 Series
706
+ UDCON &= ~((1 << RSTCPU) | (1 << RMWKUP) | (1 << DETACH)); // enable attach resistor, set full speed mode
707
+ #endif
677
708
#else
678
709
// AT90USB64x and AT90USB128x don't have RSTCPU
679
710
UDCON &= ~((1 <<LSM) | (1 <<RMWKUP) | (1 <<DETACH)); // enable attach resistor, set full speed mode
0 commit comments