diff --git a/CDC.cpp b/CDC.cpp index c4e4d1a..79a790b 100644 --- a/CDC.cpp +++ b/CDC.cpp @@ -50,8 +50,9 @@ const CDCDescriptor _cdcInterface = // CDC data interface D_INTERFACE(CDC_DATA_INTERFACE, 2, CDC_DATA_INTERFACE_CLASS, 0, 0), - D_ENDPOINT(USB_ENDPOINT_OUT(CDC_ENDPOINT_OUT), USB_ENDPOINT_TYPE_BULK, 0x40, 0), - D_ENDPOINT(USB_ENDPOINT_IN(CDC_ENDPOINT_IN), USB_ENDPOINT_TYPE_BULK, 0x40, 0) + // edit by NicoHood + D_ENDPOINT(USB_ENDPOINT_OUT(CDC_ENDPOINT_OUT), USB_ENDPOINT_TYPE_BULK, USB_EP_SIZE, 0), + D_ENDPOINT(USB_ENDPOINT_IN(CDC_ENDPOINT_IN), USB_ENDPOINT_TYPE_BULK, USB_EP_SIZE, 0) }; int WEAK CDC_GetInterface(u8* interfaceNum) @@ -96,8 +97,28 @@ bool WEAK CDC_Setup(Setup& setup) // We check DTR state to determine if host port is open (bit 0 of lineState). if (1200 == _usbLineInfo.dwDTERate && (_usbLineInfo.lineState & 0x01) == 0) { + // edit by NicoHood + // change ram pointer to fit the 16u2's ram size and only use an 8bit value +#if defined(__AVR_ATmega32U4__) *(uint16_t *)0x0800 = 0x7777; wdt_enable(WDTO_120MS); +#else + // workaround for this issue: + // https://github.com/arduino/Arduino/issues/2474 + // I didn't change this for the 32u4 to simply not touch their code + // the correct way would be to add a compiler flag like this: + // -Wl,--section-start=.blkey=0x280 + //volatile uint8_t MagicBootKey __attribute__((section(".blkey"))); + cli(); + + //MagicBootKey = 0x77; + *(uint8_t *)0x0280 = 0x77; + + wdt_enable(WDTO_120MS); + + // wait for reset + for (;;); +#endif } else { @@ -106,9 +127,18 @@ bool WEAK CDC_Setup(Setup& setup) // To avoid spurious resets we set the watchdog to 250ms and eventually // cancel if DTR goes back high. + // edit by NicoHood +#if defined(__AVR_ATmega32U4__) wdt_disable(); wdt_reset(); *(uint16_t *)0x0800 = 0x0; +#else + // not used because of the workaround above + //wdt_disable(); + //wdt_reset(); + //MagicBootKey = 0x00; + //*(uint8_t *)0x0280 = 0x00; +#endif } } return true; diff --git a/HID.cpp b/HID.cpp index 6923607..b578c4b 100644 --- a/HID.cpp +++ b/HID.cpp @@ -131,7 +131,8 @@ const HIDDescriptor _hidInterface = { D_INTERFACE(HID_INTERFACE, 1, 3, 0, 0), D_HIDREPORT(sizeof(_hidReportDescriptor)), - D_ENDPOINT(USB_ENDPOINT_IN(HID_ENDPOINT_INT), USB_ENDPOINT_TYPE_INTERRUPT, 0x40, 0x01) + // edit by NicoHood + D_ENDPOINT(USB_ENDPOINT_IN(HID_ENDPOINT_INT), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x01) }; //================================================================================ diff --git a/USBCore.cpp b/USBCore.cpp index a44cce4..82aa372 100644 --- a/USBCore.cpp +++ b/USBCore.cpp @@ -271,7 +271,8 @@ u8 USB_SendSpace(u8 ep) LockEP lock(ep); if (!ReadWriteAllowed()) return 0; - return 64 - FifoByteCount(); + // edit by NicoHood + return USB_EP_SIZE - FifoByteCount(); } // Blocking Send of data to an endpoint @@ -344,6 +345,8 @@ const u8 _initEndpoints[] = #define EP_SINGLE_64 0x32 // EP0 #define EP_DOUBLE_64 0x36 // Other endpoints +// edit by NicoHood +#define EP_SINGLE_16 0x12 static void InitEP(u8 index, u8 type, u8 size) @@ -362,7 +365,14 @@ void InitEndpoints() UENUM = i; UECONX = (1 << EPEN); UECFG0X = pgm_read_byte(_initEndpoints + i); + // edit by NicoHood +#if USB_EP_SIZE == 16 + UECFG1X = EP_SINGLE_16; +#elif USB_EP_SIZE == 64 UECFG1X = EP_DOUBLE_64; +#else +#error Unsupported value for USB_EP_SIZE +#endif } UERST = 0x7E; // And reset them UERST = 0; @@ -666,16 +676,55 @@ USBDevice_::USBDevice_() { } +// edit by NicoHood +// added from teensy definition by paul stoffregen +#if defined(__AVR_AT90USB82__) || defined(__AVR_AT90USB162__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega8U2__) +#define HW_CONFIG() +#define PLL_CONFIG() (PLLCSR = ((1<