diff --git a/hardware/arduino/avr/cores/arduino/CDC.cpp b/hardware/arduino/avr/cores/arduino/CDC.cpp index d694a2d2ca8..280696ff809 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,25 @@ 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) { - *(uint16_t *)(RAMEND-1) = *(uint16_t *)0x0800; - *(uint16_t *)0x0800 = 0x7777; +#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) + *MAGIC_KEY_POS = MAGIC_KEY; +#endif wdt_enable(WDTO_120MS); } else @@ -108,7 +124,11 @@ bool CDC_Setup(USBSetup& setup) wdt_disable(); wdt_reset(); - *(uint16_t *)0x0800 = *(uint16_t *)(RAMEND-1); +#if !defined(NO_MAGIC_KEY_BACKUP) + *MAGIC_KEY_POS = *(uint16_t *)(RAMEND-1); +#else + *MAGIC_KEY_POS = 0x0000; +#endif } } return true; @@ -116,6 +136,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 +219,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<> 8 } #define WEAK __attribute__ ((weak)) #endif -#endif \ No newline at end of file +#endif