From 22fcb7d1b5a4681d40bfe6f29e597e1785f156bd Mon Sep 17 00:00:00 2001 From: NicoHood Date: Tue, 21 Jul 2015 18:53:47 +0200 Subject: [PATCH 1/4] test --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 48ba523ba3a..2eed7353ab0 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -Arduino +Arduino Test ======== * Arduino is an open-source physical computing platform based on a simple i/o From 53e960cfd92cf9156d48c08ea2ff7a47f0405a9c Mon Sep 17 00:00:00 2001 From: NicoHood Date: Tue, 21 Jul 2015 19:05:11 +0200 Subject: [PATCH 2/4] Applied fixes from HID-Project development --- README.md | 2 +- hardware/arduino/avr/cores/arduino/CDC.cpp | 59 +++++++++++++++- hardware/arduino/avr/cores/arduino/USBAPI.h | 30 +++++--- .../arduino/avr/cores/arduino/USBCore.cpp | 69 ++++++++++++++----- hardware/arduino/avr/cores/arduino/USBCore.h | 19 ++++- hardware/arduino/avr/libraries/HID/HID.cpp | 2 +- hardware/arduino/avr/libraries/HID/HID.h | 2 +- 7 files changed, 149 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 2eed7353ab0..48ba523ba3a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -Arduino Test +Arduino ======== * Arduino is an open-source physical computing platform based on a simple i/o diff --git a/hardware/arduino/avr/cores/arduino/CDC.cpp b/hardware/arduino/avr/cores/arduino/CDC.cpp index d694a2d2ca8..9d91b2ae442 100644 --- a/hardware/arduino/avr/cores/arduino/CDC.cpp +++ b/hardware/arduino/avr/cores/arduino/CDC.cpp @@ -49,8 +49,8 @@ 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) + 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 CDC_GetInterface(u8* interfaceNum) @@ -78,11 +78,13 @@ bool CDC_Setup(USBSetup& setup) if (CDC_SET_LINE_CODING == r) { USB_RecvControl((void*)&_usbLineInfo,7); + CDC_LineEncodingEvent(); } if (CDC_SET_CONTROL_LINE_STATE == r) { _usbLineInfo.lineState = setup.wValueL; + CDC_LineStateEvent(); } if (CDC_SET_LINE_CODING == r || CDC_SET_CONTROL_LINE_STATE == r) @@ -92,11 +94,19 @@ bool CDC_Setup(USBSetup& setup) // with a relatively long period so it can finish housekeeping tasks // like servicing endpoints before the sketch ends +#define MAGIC_KEY 0x7777 + // We check DTR state to determine if host port is open (bit 0 of lineState). if (1200 == _usbLineInfo.dwDTERate && (_usbLineInfo.lineState & 0x01) == 0) { +#if defined(__AVR_ATmega32U4__) *(uint16_t *)(RAMEND-1) = *(uint16_t *)0x0800; - *(uint16_t *)0x0800 = 0x7777; + *(uint16_t *)0x0800 = MAGIC_KEY; +#else + // for future boards save the key in the inproblematic RAMEND + // which is reserved for the main() return value (which will never return) + *(uint16_t *)(RAMEND-1) = MAGIC_KEY; +#endif wdt_enable(WDTO_120MS); } else @@ -108,7 +118,11 @@ bool CDC_Setup(USBSetup& setup) wdt_disable(); wdt_reset(); +#if defined(__AVR_ATmega32U4__) *(uint16_t *)0x0800 = *(uint16_t *)(RAMEND-1); +#else + *(uint16_t *)(RAMEND-1) = 0x0000; +#endif } } return true; @@ -116,6 +130,15 @@ bool CDC_Setup(USBSetup& setup) return false; } +void WEAK CDC_LineEncodingEvent(void) +{ + // has to be implemented by the user +} + +void WEAK CDC_LineStateEvent(void) +{ + // has to be implemented by the user +} void Serial_::begin(unsigned long /* baud_count */) { @@ -190,6 +213,36 @@ size_t Serial_::write(const uint8_t *buffer, size_t size) return 0; } +uint32_t Serial_::baud(void) +{ + return _usbLineInfo.dwDTERate; +} + +uint8_t Serial_::stopbits(void) +{ + return _usbLineInfo.bCharFormat; +} + +uint8_t Serial_::paritytype(void) +{ + return _usbLineInfo.bParityType; +} + +uint8_t Serial_::numbits(void) +{ + return _usbLineInfo.bDataBits; +} + +bool Serial_::dtr(void) +{ + return (_usbLineInfo.lineState & CDC_CONTROL_LINE_OUT_DTR) ? true : false; +} + +bool Serial_::rts(void) +{ + return (_usbLineInfo.lineState & CDC_CONTROL_LINE_OUT_RTS) ? true : false; +} + // This operator is a convenient way for a sketch to check whether the // port has actually been configured and opened by the host (as opposed // to just being connected to the host). It can be used, for example, in diff --git a/hardware/arduino/avr/cores/arduino/USBAPI.h b/hardware/arduino/avr/cores/arduino/USBAPI.h index 4abd9616f33..2b9b983905b 100644 --- a/hardware/arduino/avr/cores/arduino/USBAPI.h +++ b/hardware/arduino/avr/cores/arduino/USBAPI.h @@ -26,12 +26,18 @@ #include #include -typedef unsigned char u8; +typedef unsigned char u8; //TODO remove typedef unsigned short u16; typedef unsigned long u32; #include "Arduino.h" +// this definitions is usefull if you want to reduce the EP_SIZE to 16 +// at the moment only 64 and 16 as EP_SIZE for all EPs are supported except the control endpoint +#ifndef USB_EP_SIZE +#define USB_EP_SIZE 64 +#endif + #if defined(USBCON) #include "USBDesc.h" @@ -41,13 +47,13 @@ typedef unsigned long u32; //================================================================================ // USB -#define EP_TYPE_CONTROL 0x00 -#define EP_TYPE_BULK_IN 0x81 -#define EP_TYPE_BULK_OUT 0x80 -#define EP_TYPE_INTERRUPT_IN 0xC1 -#define EP_TYPE_INTERRUPT_OUT 0xC0 -#define EP_TYPE_ISOCHRONOUS_IN 0x41 -#define EP_TYPE_ISOCHRONOUS_OUT 0x40 +#define EP_TYPE_CONTROL (0x00) +#define EP_TYPE_BULK_IN ((1< Date: Tue, 21 Jul 2015 20:10:54 +0200 Subject: [PATCH 3/4] Fixed descriptor length https://github.com/NicoHood/HID/issues/42 --- hardware/arduino/avr/libraries/HID/HID.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hardware/arduino/avr/libraries/HID/HID.h b/hardware/arduino/avr/libraries/HID/HID.h index 29b71c804db..72a5a945eba 100644 --- a/hardware/arduino/avr/libraries/HID/HID.h +++ b/hardware/arduino/avr/libraries/HID/HID.h @@ -88,7 +88,7 @@ typedef struct #define HID_TX HID_ENDPOINT_INT #define D_HIDREPORT(_descriptorLength) \ - { 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength, 0 } + { 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength, _descriptorLength >> 8 } #define WEAK __attribute__ ((weak)) From cd65aaf420d9fab9def55a16b25d511cb8b0f89b Mon Sep 17 00:00:00 2001 From: NicoHood Date: Tue, 4 Aug 2015 16:48:51 +0200 Subject: [PATCH 4/4] Made Magic Key position more flexible This way a key can be specified in the pins_arduino.h file --- hardware/arduino/avr/cores/arduino/CDC.cpp | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/CDC.cpp b/hardware/arduino/avr/cores/arduino/CDC.cpp index 9d91b2ae442..280696ff809 100644 --- a/hardware/arduino/avr/cores/arduino/CDC.cpp +++ b/hardware/arduino/avr/cores/arduino/CDC.cpp @@ -94,18 +94,24 @@ bool CDC_Setup(USBSetup& setup) // with a relatively long period so it can finish housekeeping tasks // like servicing endpoints before the sketch ends +#ifndef MAGIC_KEY #define MAGIC_KEY 0x7777 +#endif +#ifndef MAGIC_KEY_POS +#define MAGIC_KEY_POS (uint16_t *)0x0800 +#endif +// NO_MAGIC_KEY_BACKUP can be used if you want to store the key in the safer RAMEND directly, so no backup is needed // We check DTR state to determine if host port is open (bit 0 of lineState). if (1200 == _usbLineInfo.dwDTERate && (_usbLineInfo.lineState & 0x01) == 0) { -#if defined(__AVR_ATmega32U4__) - *(uint16_t *)(RAMEND-1) = *(uint16_t *)0x0800; - *(uint16_t *)0x0800 = MAGIC_KEY; +#if !defined(NO_MAGIC_KEY_BACKUP) + *(uint16_t *)(RAMEND-1) = *MAGIC_KEY_POS; + *MAGIC_KEY_POS = MAGIC_KEY; #else // for future boards save the key in the inproblematic RAMEND // which is reserved for the main() return value (which will never return) - *(uint16_t *)(RAMEND-1) = MAGIC_KEY; + *MAGIC_KEY_POS = MAGIC_KEY; #endif wdt_enable(WDTO_120MS); } @@ -118,10 +124,10 @@ bool CDC_Setup(USBSetup& setup) wdt_disable(); wdt_reset(); -#if defined(__AVR_ATmega32U4__) - *(uint16_t *)0x0800 = *(uint16_t *)(RAMEND-1); +#if !defined(NO_MAGIC_KEY_BACKUP) + *MAGIC_KEY_POS = *(uint16_t *)(RAMEND-1); #else - *(uint16_t *)(RAMEND-1) = 0x0000; + *MAGIC_KEY_POS = 0x0000; #endif } }