From 9a02bd8043c41f6ee57f136083ffc8bf8ec2bdc9 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Thu, 17 Sep 2020 19:29:31 +0200 Subject: Swap new and new.h header files Originally, the Arduino core used "new.h", rather than the standard "new", probably because the implementation was incomplete, and for the most commonly used new and delete operators, no include is needed at all (they are defined implicitly by the compiler). However, now Arduino does expose the "new" name, as an alias for the older "new.h". Given that the standard name is "new", it makes more sense to put the actual content in "new", and make "new.h" a compatibility header that includes "new" instead of the other way around. --- cores/arduino/new | 32 +++++++++++++++++++++++++++++--- cores/arduino/new.h | 34 +++------------------------------- 2 files changed, 32 insertions(+), 34 deletions(-) (limited to 'cores/arduino') diff --git a/cores/arduino/new b/cores/arduino/new index aa8ecb5..763f5cc 100644 --- a/cores/arduino/new +++ b/cores/arduino/new @@ -1,5 +1,31 @@ /* -this header is for compatibility with standard c++ header names -so that #include works as expected + Copyright (c) 2014 Arduino. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "new.h" + +#ifndef NEW_H +#define NEW_H + +#include + +void * operator new(size_t size); +void * operator new[](size_t size); +void * operator new(size_t size, void * ptr) noexcept; +void operator delete(void * ptr); +void operator delete[](void * ptr); + +#endif + diff --git a/cores/arduino/new.h b/cores/arduino/new.h index 763f5cc..d529853 100644 --- a/cores/arduino/new.h +++ b/cores/arduino/new.h @@ -1,31 +1,3 @@ -/* - Copyright (c) 2014 Arduino. All right reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - See the GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#ifndef NEW_H -#define NEW_H - -#include - -void * operator new(size_t size); -void * operator new[](size_t size); -void * operator new(size_t size, void * ptr) noexcept; -void operator delete(void * ptr); -void operator delete[](void * ptr); - -#endif - +// This file originally used a non-standard name for this Arduino core +// only, so still expose the old new.h name for compatibility. +#include "new" -- cgit v1.2.3-18-g5258 From 07b6bd188f2395551fbd5eee3c3ca7ca3769bbbc Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Thu, 17 Sep 2020 17:15:42 +0200 Subject: Clean up and complete `` header This makes this header complete up to including C++14, except two exception classes that cannot be defined without ``. The functions related to the "new_handler" are declared but not actually defined, to prevent overhead and complexity. They are still declared to allow implementing them in user code if needed. This makes the implementation of all operator new and delete functions comply with the C++11/C++14 specification in terms of which should be actually implemented and which should be delegate to other functions. There are still some areas where these implementations are not entirely standards-compliant, which will be fixed in subsequent commits. This fixes part of #287 and fixes #47. --- cores/arduino/new | 35 ++++++++++++++++++++++++++++--- cores/arduino/new.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 79 insertions(+), 13 deletions(-) (limited to 'cores/arduino') diff --git a/cores/arduino/new b/cores/arduino/new index 763f5cc..3599571 100644 --- a/cores/arduino/new +++ b/cores/arduino/new @@ -21,11 +21,40 @@ #include +namespace std { + struct nothrow_t {}; + extern const nothrow_t nothrow; + + // These are not actually implemented, to prevent overhead and + // complexity. They are still declared to allow implementing + // them in user code if needed. + typedef void (*new_handler)(); + new_handler set_new_handler(new_handler new_p) noexcept; + new_handler get_new_handler() noexcept; +} // namespace std + void * operator new(size_t size); void * operator new[](size_t size); -void * operator new(size_t size, void * ptr) noexcept; -void operator delete(void * ptr); -void operator delete[](void * ptr); + +void * operator new(size_t size, const std::nothrow_t tag) noexcept; +void * operator new[](size_t size, const std::nothrow_t& tag) noexcept; + +void * operator new(size_t size, void *place) noexcept; +void * operator new[](size_t size, void *place) noexcept; + +void operator delete(void * ptr) noexcept; +void operator delete[](void * ptr) noexcept; + +#if __cplusplus >= 201402L +void operator delete(void* ptr, size_t size) noexcept; +void operator delete[](void * ptr, size_t size) noexcept; +#endif // __cplusplus >= 201402L + +void operator delete(void* ptr, const std::nothrow_t& tag) noexcept; +void operator delete[](void* ptr, const std::nothrow_t& tag) noexcept; + +void operator delete(void* ptr, void* place) noexcept; +void operator delete[](void* ptr, void* place) noexcept; #endif diff --git a/cores/arduino/new.cpp b/cores/arduino/new.cpp index fc30cf8..a36fd21 100644 --- a/cores/arduino/new.cpp +++ b/cores/arduino/new.cpp @@ -16,26 +16,63 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include +#include "new.h" -void *operator new(size_t size) { - return malloc(size); +namespace std { + const nothrow_t nothrow; } -void *operator new[](size_t size) { +void * operator new(size_t size) { return malloc(size); } +void * operator new[](size_t size) { + return operator new(size); +} -void * operator new(size_t size, void * ptr) noexcept { - (void)size; - return ptr; +void * operator new(size_t size, const std::nothrow_t tag) noexcept { + return operator new(size); +} +void * operator new[](size_t size, const std::nothrow_t& tag) noexcept { + return operator new[](size); } -void operator delete(void * ptr) { - free(ptr); +void * operator new(size_t size, void *place) noexcept { + // Nothing to do + (void)size; // unused + return place; +} +void * operator new[](size_t size, void *place) noexcept { + return operator new(size, place); } -void operator delete[](void * ptr) { +void operator delete(void * ptr) noexcept { free(ptr); } +void operator delete[](void * ptr) noexcept { + operator delete(ptr); +} + +#if __cplusplus >= 201402L +void operator delete(void* ptr, size_t size) noexcept { + operator delete(ptr); +} +void operator delete[](void * ptr, size_t size) noexcept { + operator delete[](ptr); +} +#endif // __cplusplus >= 201402L +void operator delete(void* ptr, const std::nothrow_t& tag) noexcept { + operator delete(ptr); +} +void operator delete[](void* ptr, const std::nothrow_t& tag) noexcept { + operator delete[](ptr); +} + +void operator delete(void* ptr, void* place) noexcept { + (void)ptr; (void)place; // unused + // Nothing to do +} +void operator delete[](void* ptr, void* place) noexcept { + (void)ptr; (void)place; // unused + // Nothing to do +} -- cgit v1.2.3-18-g5258 From 4e469e0c83799ad6d3698e7cfa51ef8a5f2a2c76 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Thu, 17 Sep 2020 17:22:41 +0200 Subject: Allow overriding selected operator new and delete functions This makes these functions weak, so that a sketch or library can replace them. This does not apply to all of these operators, only for the ones that the C++ standard specifies as replaceable. --- cores/arduino/new | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'cores/arduino') diff --git a/cores/arduino/new b/cores/arduino/new index 3599571..fb60927 100644 --- a/cores/arduino/new +++ b/cores/arduino/new @@ -33,25 +33,25 @@ namespace std { new_handler get_new_handler() noexcept; } // namespace std -void * operator new(size_t size); -void * operator new[](size_t size); +[[gnu::weak]] void * operator new(size_t size); +[[gnu::weak]] void * operator new[](size_t size); -void * operator new(size_t size, const std::nothrow_t tag) noexcept; -void * operator new[](size_t size, const std::nothrow_t& tag) noexcept; +[[gnu::weak]] void * operator new(size_t size, const std::nothrow_t tag) noexcept; +[[gnu::weak]] void * operator new[](size_t size, const std::nothrow_t& tag) noexcept; void * operator new(size_t size, void *place) noexcept; void * operator new[](size_t size, void *place) noexcept; -void operator delete(void * ptr) noexcept; -void operator delete[](void * ptr) noexcept; +[[gnu::weak]] void operator delete(void * ptr) noexcept; +[[gnu::weak]] void operator delete[](void * ptr) noexcept; #if __cplusplus >= 201402L -void operator delete(void* ptr, size_t size) noexcept; -void operator delete[](void * ptr, size_t size) noexcept; +[[gnu::weak]] void operator delete(void* ptr, size_t size) noexcept; +[[gnu::weak]] void operator delete[](void * ptr, size_t size) noexcept; #endif // __cplusplus >= 201402L -void operator delete(void* ptr, const std::nothrow_t& tag) noexcept; -void operator delete[](void* ptr, const std::nothrow_t& tag) noexcept; +[[gnu::weak]] void operator delete(void* ptr, const std::nothrow_t& tag) noexcept; +[[gnu::weak]] void operator delete[](void* ptr, const std::nothrow_t& tag) noexcept; void operator delete(void* ptr, void* place) noexcept; void operator delete[](void* ptr, void* place) noexcept; -- cgit v1.2.3-18-g5258 From b8c6c850421d0d81bb5ea9c340c4fcd958937165 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Thu, 17 Sep 2020 17:37:53 +0200 Subject: Add weak `std::terminate()` implementation This allows calling it from other places later. The default implementation calls `abort()`, but making it weak allows user code to override this function (either directly, or by including a library like uclibc++ that implements `std::set_terminate()`). Note that this does not add a declaration for this function, since the standard dictates this to be in ``, but we cannot meaningfully or completely implement that header, so better leave it to be overridden by e.g. libraries like uclibc++. --- cores/arduino/abi.cpp | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'cores/arduino') diff --git a/cores/arduino/abi.cpp b/cores/arduino/abi.cpp index 8d719b8..b8f76cf 100644 --- a/cores/arduino/abi.cpp +++ b/cores/arduino/abi.cpp @@ -21,6 +21,12 @@ extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__)); extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__)); +namespace std { + [[gnu::weak, noreturn]] void terminate() { + abort(); + } +} + void __cxa_pure_virtual(void) { // We might want to write some diagnostics to uart in this case //std::terminate(); -- cgit v1.2.3-18-g5258 From 66d06b033c3f6eafde901418be3c089ffcc6ebfc Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Thu, 17 Sep 2020 17:40:02 +0200 Subject: Call std::terminate on pure or deleted virtual functions These are special functions that are presumably put into vtables for deleted or pure virtual functions. Previously, this would call `abort()` directly, but calling `std::terminate()` achieves the same effect, but allows user code to change the behavior (e.g. to print to serial, blink leds or whatever makes sense). --- cores/arduino/abi.cpp | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'cores/arduino') diff --git a/cores/arduino/abi.cpp b/cores/arduino/abi.cpp index b8f76cf..6e1b0f8 100644 --- a/cores/arduino/abi.cpp +++ b/cores/arduino/abi.cpp @@ -28,14 +28,9 @@ namespace std { } void __cxa_pure_virtual(void) { - // We might want to write some diagnostics to uart in this case - //std::terminate(); - abort(); + std::terminate(); } void __cxa_deleted_virtual(void) { - // We might want to write some diagnostics to uart in this case - //std::terminate(); - abort(); + std::terminate(); } - -- cgit v1.2.3-18-g5258 From 6e0fb1ee25efa07be5aef320501f3908f44e5b79 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Thu, 17 Sep 2020 17:55:27 +0200 Subject: Make zero-sized new standards-compliant This fixes part of #287. --- cores/arduino/new.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'cores/arduino') diff --git a/cores/arduino/new.cpp b/cores/arduino/new.cpp index a36fd21..1683594 100644 --- a/cores/arduino/new.cpp +++ b/cores/arduino/new.cpp @@ -23,6 +23,10 @@ namespace std { } void * operator new(size_t size) { + // Even zero-sized allocations should return a unique pointer, but + // malloc does not guarantee this + if (size == 0) + size = 1; return malloc(size); } void * operator new[](size_t size) { -- cgit v1.2.3-18-g5258 From 1a885ce890219213ac24c00d5aededa88c122fe1 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Thu, 17 Sep 2020 17:59:08 +0200 Subject: Optionally let new terminate on allocation failure This is currently disabled, keeping the old behavior of returning NULL on failure, but should probably be enabled in the future as code that does want to do a null check has had a chance to switch to the more portable nothrow versions. When enabled, allocation failure calls the weak `std::terminate()`, which calls `abort()` by default, but can be replaced by user code to do more specific handling. To enable this, a macro must be defined (in new.cpp or on the compiler commandline). This fixes part of #287. --- cores/arduino/new.cpp | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'cores/arduino') diff --git a/cores/arduino/new.cpp b/cores/arduino/new.cpp index 1683594..19d80b6 100644 --- a/cores/arduino/new.cpp +++ b/cores/arduino/new.cpp @@ -18,26 +18,61 @@ #include "new.h" +// The C++ spec dicates that allocation failure should cause the +// (non-nothrow version of the) operator new to throw an exception. +// Since we expect to have exceptions disabled, it would be more +// appropriate (and probably standards-compliant) to terminate instead. +// Historically failure causes null to be returned, but this define +// allows switching to more robust terminating behaviour (that might +// become the default at some point in the future). Note that any code +// that wants null to be returned can (and should) use the nothrow +// versions of the new statement anyway and is unaffected by this. +// #define NEW_TERMINATES_ON_FAILURE + namespace std { + // Defined in abi.cpp + void terminate(); + const nothrow_t nothrow; } -void * operator new(size_t size) { +static void * new_helper(size_t size) { // Even zero-sized allocations should return a unique pointer, but // malloc does not guarantee this if (size == 0) size = 1; return malloc(size); } + +void * operator new(size_t size) { + void *res = new_helper(size); +#if defined(NEW_TERMINATES_ON_FAILURE) + if (!res) + std::terminate(); +#endif + return res; +} void * operator new[](size_t size) { return operator new(size); } void * operator new(size_t size, const std::nothrow_t tag) noexcept { +#if defined(NEW_TERMINATES_ON_FAILURE) + // Cannot call throwing operator new as standard suggests, so call + // new_helper directly then + return new_helper(size); +#else return operator new(size); +#endif } void * operator new[](size_t size, const std::nothrow_t& tag) noexcept { +#if defined(NEW_TERMINATES_ON_FAILURE) + // Cannot call throwing operator new[] as standard suggests, so call + // malloc directly then + return new_helper(size); +#else return operator new[](size); +#endif } void * operator new(size_t size, void *place) noexcept { -- cgit v1.2.3-18-g5258 From 6d292502e138b5c73705f0df8095ded3f1df956f Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Thu, 17 Sep 2020 19:43:09 +0200 Subject: Use std::size_t in new/delete The standard dictates that `std::size_t` is used, rather than the plain `size_t` type. Even though these types are usually, if not always, exactly the same type, other code might assume that `std::size_t` is actually used and thus also available under that name after including ``. This fixes that by using the right type. One challenge is that it is usually declared in headers that we do not have available, so this just defines the `std::size_t` type in the `` header to work around that. --- cores/arduino/new | 22 ++++++++++++++-------- cores/arduino/new.cpp | 18 +++++++++--------- 2 files changed, 23 insertions(+), 17 deletions(-) (limited to 'cores/arduino') diff --git a/cores/arduino/new b/cores/arduino/new index fb60927..8cf2103 100644 --- a/cores/arduino/new +++ b/cores/arduino/new @@ -31,23 +31,29 @@ namespace std { typedef void (*new_handler)(); new_handler set_new_handler(new_handler new_p) noexcept; new_handler get_new_handler() noexcept; + + // This is normally declared in various headers that we do not have + // available, so just define it here. We could also use ::size_t + // below, but then anyone including can no longer assume + // std::size_t is available. + using size_t = ::size_t; } // namespace std -[[gnu::weak]] void * operator new(size_t size); -[[gnu::weak]] void * operator new[](size_t size); +[[gnu::weak]] void * operator new(std::size_t size); +[[gnu::weak]] void * operator new[](std::size_t size); -[[gnu::weak]] void * operator new(size_t size, const std::nothrow_t tag) noexcept; -[[gnu::weak]] void * operator new[](size_t size, const std::nothrow_t& tag) noexcept; +[[gnu::weak]] void * operator new(std::size_t size, const std::nothrow_t tag) noexcept; +[[gnu::weak]] void * operator new[](std::size_t size, const std::nothrow_t& tag) noexcept; -void * operator new(size_t size, void *place) noexcept; -void * operator new[](size_t size, void *place) noexcept; +void * operator new(std::size_t size, void *place) noexcept; +void * operator new[](std::size_t size, void *place) noexcept; [[gnu::weak]] void operator delete(void * ptr) noexcept; [[gnu::weak]] void operator delete[](void * ptr) noexcept; #if __cplusplus >= 201402L -[[gnu::weak]] void operator delete(void* ptr, size_t size) noexcept; -[[gnu::weak]] void operator delete[](void * ptr, size_t size) noexcept; +[[gnu::weak]] void operator delete(void* ptr, std::size_t size) noexcept; +[[gnu::weak]] void operator delete[](void * ptr, std::size_t size) noexcept; #endif // __cplusplus >= 201402L [[gnu::weak]] void operator delete(void* ptr, const std::nothrow_t& tag) noexcept; diff --git a/cores/arduino/new.cpp b/cores/arduino/new.cpp index 19d80b6..9047b2d 100644 --- a/cores/arduino/new.cpp +++ b/cores/arduino/new.cpp @@ -36,7 +36,7 @@ namespace std { const nothrow_t nothrow; } -static void * new_helper(size_t size) { +static void * new_helper(std::size_t size) { // Even zero-sized allocations should return a unique pointer, but // malloc does not guarantee this if (size == 0) @@ -44,7 +44,7 @@ static void * new_helper(size_t size) { return malloc(size); } -void * operator new(size_t size) { +void * operator new(std::size_t size) { void *res = new_helper(size); #if defined(NEW_TERMINATES_ON_FAILURE) if (!res) @@ -52,11 +52,11 @@ void * operator new(size_t size) { #endif return res; } -void * operator new[](size_t size) { +void * operator new[](std::size_t size) { return operator new(size); } -void * operator new(size_t size, const std::nothrow_t tag) noexcept { +void * operator new(std::size_t size, const std::nothrow_t tag) noexcept { #if defined(NEW_TERMINATES_ON_FAILURE) // Cannot call throwing operator new as standard suggests, so call // new_helper directly then @@ -65,7 +65,7 @@ void * operator new(size_t size, const std::nothrow_t tag) noexcept { return operator new(size); #endif } -void * operator new[](size_t size, const std::nothrow_t& tag) noexcept { +void * operator new[](std::size_t size, const std::nothrow_t& tag) noexcept { #if defined(NEW_TERMINATES_ON_FAILURE) // Cannot call throwing operator new[] as standard suggests, so call // malloc directly then @@ -75,12 +75,12 @@ void * operator new[](size_t size, const std::nothrow_t& tag) noexcept { #endif } -void * operator new(size_t size, void *place) noexcept { +void * operator new(std::size_t size, void *place) noexcept { // Nothing to do (void)size; // unused return place; } -void * operator new[](size_t size, void *place) noexcept { +void * operator new[](std::size_t size, void *place) noexcept { return operator new(size, place); } @@ -92,10 +92,10 @@ void operator delete[](void * ptr) noexcept { } #if __cplusplus >= 201402L -void operator delete(void* ptr, size_t size) noexcept { +void operator delete(void* ptr, std::size_t size) noexcept { operator delete(ptr); } -void operator delete[](void * ptr, size_t size) noexcept { +void operator delete[](void * ptr, std::size_t size) noexcept { operator delete[](ptr); } #endif // __cplusplus >= 201402L -- cgit v1.2.3-18-g5258 From 4fd3801e32fee9e25f04ae68a4cbfc95533d56df Mon Sep 17 00:00:00 2001 From: Vitaly Shmagun <65466721+Vitve4@users.noreply.github.com> Date: Thu, 28 May 2020 20:31:49 +0300 Subject: Improve reading ADC result 7.3.0-atmel3.6.1-arduino7 gcc fails to optimize separate reading from ADCL and ADCH. It produces additionally three eor commands or in some cases two mov commands in the assembly code (see discussion #344). These commands swap register contents before store them to data area. So they are completely unnecessary. Reading ADC result with ADC macro fixes it and gcc generates the right code.. --- cores/arduino/wiring_analog.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) (limited to 'cores/arduino') diff --git a/cores/arduino/wiring_analog.c b/cores/arduino/wiring_analog.c index e237d6d..0de64f7 100644 --- a/cores/arduino/wiring_analog.c +++ b/cores/arduino/wiring_analog.c @@ -37,7 +37,6 @@ void analogReference(uint8_t mode) int analogRead(uint8_t pin) { - uint8_t low, high; #if defined(analogPinToChannel) #if defined(__AVR_ATmega32U4__) @@ -74,27 +73,20 @@ int analogRead(uint8_t pin) // without a delay, we seem to read from the wrong channel //delay(1); -#if defined(ADCSRA) && defined(ADCL) +#if defined(ADCSRA) && defined(ADC) // start the conversion sbi(ADCSRA, ADSC); // ADSC is cleared when the conversion finishes while (bit_is_set(ADCSRA, ADSC)); - // we have to read ADCL first; doing so locks both ADCL - // and ADCH until ADCH is read. reading ADCL second would - // cause the results of each conversion to be discarded, - // as ADCL and ADCH would be locked when it completed. - low = ADCL; - high = ADCH; + // ADC macro takes care of reading ADC register. + // avr-gcc implements the proper reading order: ADCL is read first. + return ADC; #else // we dont have an ADC, return 0 - low = 0; - high = 0; + return 0; #endif - - // combine the two bytes - return (high << 8) | low; } // Right now, PWM output only works on the pins with -- cgit v1.2.3-18-g5258 From 8e823d276f939d79b2d323fad675fb8442a718c2 Mon Sep 17 00:00:00 2001 From: Daniel Gibson Date: Tue, 5 Jan 2021 13:48:43 +0100 Subject: Allow disabling CDC with -DCDC_DISABLED Sometimes Arduino-based USB devices don't work because some hardware (like KVM switches) gets confused by the CDC sub-devices. This change makes it relatively easy to disable CDC at compiletime. Disabling it of course means that the serial console won't work anymore, so you need to use the reset button when flashing. CDC_DISABLED is also used in ArduinoCore-samd for the same purpose. based on https://github.com/gdsports/usb-metamorph/tree/master/USBSerPassThruLine See also https://github.com/NicoHood/HID/issues/225 and https://github.com/arduino/Arduino/issues/6387 and https://forum.arduino.cc/index.php?topic=545288.msg3717028#msg3717028 --- cores/arduino/CDC.cpp | 8 ++++++++ cores/arduino/USBCore.cpp | 18 +++++++++++++++++- cores/arduino/USBDesc.h | 17 +++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) (limited to 'cores/arduino') diff --git a/cores/arduino/CDC.cpp b/cores/arduino/CDC.cpp index 4ff6b9b..7d5afaa 100644 --- a/cores/arduino/CDC.cpp +++ b/cores/arduino/CDC.cpp @@ -22,6 +22,13 @@ #if defined(USBCON) +#ifndef CDC_ENABLED + +#warning "! Disabled serial console via USB (CDC)!" +#warning "! With this change you'll have to use the Arduino's reset button/pin to flash (upload)!" + +#else // CDC not disabled + typedef struct { u32 dwDTERate; @@ -299,4 +306,5 @@ int32_t Serial_::readBreak() { Serial_ Serial; +#endif /* if defined(CDC_ENABLED) */ #endif /* if defined(USBCON) */ diff --git a/cores/arduino/USBCore.cpp b/cores/arduino/USBCore.cpp index dc6bc38..9335238 100644 --- a/cores/arduino/USBCore.cpp +++ b/cores/arduino/USBCore.cpp @@ -69,8 +69,18 @@ const u8 STRING_MANUFACTURER[] PROGMEM = USB_MANUFACTURER; #define DEVICE_CLASS 0x02 // DEVICE DESCRIPTOR + +#ifdef CDC_ENABLED const DeviceDescriptor USB_DeviceDescriptorIAD = D_DEVICE(0xEF,0x02,0x01,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,ISERIAL,1); +#else // CDC_DISABLED +// The default descriptor uses USB class OxEF, subclass 0x02 with protocol 1 +// which means "Interface Association Descriptor" - that's needed for the CDC, +// but doesn't make much sense as a default for custom devices when CDC is disabled. +// (0x00 means "Use class information in the Interface Descriptors" which should be generally ok) +const DeviceDescriptor USB_DeviceDescriptorIAD = + D_DEVICE(0x00,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,ISERIAL,1); +#endif //================================================================== //================================================================== @@ -328,10 +338,12 @@ int USB_Send(u8 ep, const void* d, int len) u8 _initEndpoints[USB_ENDPOINTS] = { 0, // Control Endpoint - + +#ifdef CDC_ENABLED EP_TYPE_INTERRUPT_IN, // CDC_ENDPOINT_ACM EP_TYPE_BULK_OUT, // CDC_ENDPOINT_OUT EP_TYPE_BULK_IN, // CDC_ENDPOINT_IN +#endif // Following endpoints are automatically initialized to 0 }; @@ -373,10 +385,12 @@ void InitEndpoints() static bool ClassInterfaceRequest(USBSetup& setup) { +#ifdef CDC_ENABLED u8 i = setup.wIndex; if (CDC_ACM_INTERFACE == i) return CDC_Setup(setup); +#endif #ifdef PLUGGABLE_USB_ENABLED return PluggableUSB().setup(setup); @@ -466,7 +480,9 @@ static u8 SendInterfaces() { u8 interfaces = 0; +#ifdef CDC_ENABLED CDC_GetInterface(&interfaces); +#endif #ifdef PLUGGABLE_USB_ENABLED PluggableUSB().getInterface(&interfaces); diff --git a/cores/arduino/USBDesc.h b/cores/arduino/USBDesc.h index c0dce07..b55ac20 100644 --- a/cores/arduino/USBDesc.h +++ b/cores/arduino/USBDesc.h @@ -26,8 +26,25 @@ #define ISERIAL_MAX_LEN 20 +// Uncomment the following line or pass -DCDC_DISABLED to the compiler +// to disable CDC (serial console via USB). +// That's useful if you want to create an USB device (like an USB Boot Keyboard) +// that works even with problematic devices (like KVM switches). +// Keep in mind that with this change you'll have to use the Arduino's +// reset button to be able to flash it. +//#define CDC_DISABLED + +#ifndef CDC_DISABLED +#define CDC_ENABLED +#endif + +#ifdef CDC_ENABLED #define CDC_INTERFACE_COUNT 2 #define CDC_ENPOINT_COUNT 3 +#else // CDC_DISABLED +#define CDC_INTERFACE_COUNT 0 +#define CDC_ENPOINT_COUNT 0 +#endif #define CDC_ACM_INTERFACE 0 // CDC ACM #define CDC_DATA_INTERFACE 1 // CDC Data -- cgit v1.2.3-18-g5258 From 8b327d7bede1c1245db99daeba4e168c92c11194 Mon Sep 17 00:00:00 2001 From: per1234 Date: Wed, 26 May 2021 04:35:59 -0700 Subject: Correct typos in comments and documentation --- cores/arduino/HardwareSerial.cpp | 2 +- cores/arduino/HardwareSerial.h | 2 +- cores/arduino/HardwareSerial_private.h | 2 +- cores/arduino/Print.h | 2 +- cores/arduino/Stream.h | 2 +- cores/arduino/USBAPI.h | 2 +- cores/arduino/WString.h | 4 ++-- cores/arduino/new.cpp | 2 +- cores/arduino/wiring.c | 26 +++++++++++++------------- cores/arduino/wiring_digital.c | 2 +- 10 files changed, 23 insertions(+), 23 deletions(-) (limited to 'cores/arduino') diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp index e99d503..561127f 100644 --- a/cores/arduino/HardwareSerial.cpp +++ b/cores/arduino/HardwareSerial.cpp @@ -219,7 +219,7 @@ void HardwareSerial::flush() _tx_udr_empty_irq(); } // If we get here, nothing is queued anymore (DRIE is disabled) and - // the hardware finished tranmission (TXC is set). + // the hardware finished transmission (TXC is set). } size_t HardwareSerial::write(uint8_t c) diff --git a/cores/arduino/HardwareSerial.h b/cores/arduino/HardwareSerial.h index 17000c2..6ff29d0 100644 --- a/cores/arduino/HardwareSerial.h +++ b/cores/arduino/HardwareSerial.h @@ -32,7 +32,7 @@ // using a ring buffer (I think), in which head is the index of the location // to which to write the next incoming character and tail is the index of the // location from which to read. -// NOTE: a "power of 2" buffer size is reccomended to dramatically +// NOTE: a "power of 2" buffer size is recommended to dramatically // optimize all the modulo operations for ring buffers. // WARNING: When buffer sizes are increased to > 256, the buffer index // variables are automatically increased in size, but the extra diff --git a/cores/arduino/HardwareSerial_private.h b/cores/arduino/HardwareSerial_private.h index 761a5e5..2e23cec 100644 --- a/cores/arduino/HardwareSerial_private.h +++ b/cores/arduino/HardwareSerial_private.h @@ -63,7 +63,7 @@ #endif #endif // !defined TXC0 -// Check at compiletime that it is really ok to use the bit positions of +// Check at compile time that it is really ok to use the bit positions of // UART0 for the other UARTs as well, in case these values ever get // changed for future hardware. #if defined(TXC1) && (TXC1 != TXC0 || RXEN1 != RXEN0 || RXCIE1 != RXCIE0 || \ diff --git a/cores/arduino/Print.h b/cores/arduino/Print.h index 058a2ab..0097cc1 100644 --- a/cores/arduino/Print.h +++ b/cores/arduino/Print.h @@ -59,7 +59,7 @@ class Print } // default to zero, meaning "a single write may block" - // should be overriden by subclasses with buffering + // should be overridden by subclasses with buffering virtual int availableForWrite() { return 0; } size_t print(const __FlashStringHelper *); diff --git a/cores/arduino/Stream.h b/cores/arduino/Stream.h index 8e950c7..21a247a 100644 --- a/cores/arduino/Stream.h +++ b/cores/arduino/Stream.h @@ -25,7 +25,7 @@ #include #include "Print.h" -// compatability macros for testing +// compatibility macros for testing /* #define getInt() parseInt() #define getInt(ignore) parseInt(ignore) diff --git a/cores/arduino/USBAPI.h b/cores/arduino/USBAPI.h index 701a14f..3ff1459 100644 --- a/cores/arduino/USBAPI.h +++ b/cores/arduino/USBAPI.h @@ -32,7 +32,7 @@ typedef unsigned long u32; #include "Arduino.h" -// This definitions is usefull if you want to reduce the EP_SIZE to 16 +// This definitions is useful 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 diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index 77709c3..2cf4cd7 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -95,7 +95,7 @@ public: // returns true on success, false on failure (in which case, the string // is left unchanged). if the argument is null or invalid, the - // concatenation is considered unsucessful. + // concatenation is considered unsuccessful. unsigned char concat(const String &str); unsigned char concat(const char *cstr); unsigned char concat(char c); @@ -152,7 +152,7 @@ public: unsigned char startsWith(const String &prefix, unsigned int offset) const; unsigned char endsWith(const String &suffix) const; - // character acccess + // character access char charAt(unsigned int index) const; void setCharAt(unsigned int index, char c); char operator [] (unsigned int index) const; diff --git a/cores/arduino/new.cpp b/cores/arduino/new.cpp index 9047b2d..7ca4931 100644 --- a/cores/arduino/new.cpp +++ b/cores/arduino/new.cpp @@ -18,7 +18,7 @@ #include "new.h" -// The C++ spec dicates that allocation failure should cause the +// The C++ spec dictates that allocation failure should cause the // (non-nothrow version of the) operator new to throw an exception. // Since we expect to have exceptions disabled, it would be more // appropriate (and probably standards-compliant) to terminate instead. diff --git a/cores/arduino/wiring.c b/cores/arduino/wiring.c index 9727135..8caf455 100644 --- a/cores/arduino/wiring.c +++ b/cores/arduino/wiring.c @@ -125,7 +125,7 @@ void delayMicroseconds(unsigned int us) // 2 microseconds) gives delays longer than desired. //delay_us(us); #if F_CPU >= 24000000L - // for the 24 MHz clock for the aventurous ones, trying to overclock + // for the 24 MHz clock for the adventurous ones trying to overclock // zero delay fix if (!us) return; // = 3 cycles, (4 when true) @@ -135,9 +135,9 @@ void delayMicroseconds(unsigned int us) // delay requested. us *= 6; // x6 us, = 7 cycles - // account for the time taken in the preceeding commands. + // account for the time taken in the preceding commands. // we just burned 22 (24) cycles above, remove 5, (5*4=20) - // us is at least 6 so we can substract 5 + // us is at least 6 so we can subtract 5 us -= 5; //=2 cycles #elif F_CPU >= 20000000L @@ -157,9 +157,9 @@ void delayMicroseconds(unsigned int us) // delay requested. us = (us << 2) + us; // x5 us, = 7 cycles - // account for the time taken in the preceeding commands. + // account for the time taken in the preceding commands. // we just burned 26 (28) cycles above, remove 7, (7*4=28) - // us is at least 10 so we can substract 7 + // us is at least 10 so we can subtract 7 us -= 7; // 2 cycles #elif F_CPU >= 16000000L @@ -174,9 +174,9 @@ void delayMicroseconds(unsigned int us) // delay requested. us <<= 2; // x4 us, = 4 cycles - // account for the time taken in the preceeding commands. + // account for the time taken in the preceding commands. // we just burned 19 (21) cycles above, remove 5, (5*4=20) - // us is at least 8 so we can substract 5 + // us is at least 8 so we can subtract 5 us -= 5; // = 2 cycles, #elif F_CPU >= 12000000L @@ -191,9 +191,9 @@ void delayMicroseconds(unsigned int us) // delay requested. us = (us << 1) + us; // x3 us, = 5 cycles - // account for the time taken in the preceeding commands. + // account for the time taken in the preceding commands. // we just burned 20 (22) cycles above, remove 5, (5*4=20) - // us is at least 6 so we can substract 5 + // us is at least 6 so we can subtract 5 us -= 5; //2 cycles #elif F_CPU >= 8000000L @@ -208,9 +208,9 @@ void delayMicroseconds(unsigned int us) // delay requested. us <<= 1; //x2 us, = 2 cycles - // account for the time taken in the preceeding commands. + // account for the time taken in the preceding commands. // we just burned 17 (19) cycles above, remove 4, (4*4=16) - // us is at least 6 so we can substract 4 + // us is at least 6 so we can subtract 4 us -= 4; // = 2 cycles #else @@ -218,9 +218,9 @@ void delayMicroseconds(unsigned int us) // the overhead of the function calls is 14 (16) cycles if (us <= 16) return; //= 3 cycles, (4 when true) - if (us <= 25) return; //= 3 cycles, (4 when true), (must be at least 25 if we want to substract 22) + if (us <= 25) return; //= 3 cycles, (4 when true), (must be at least 25 if we want to subtract 22) - // compensate for the time taken by the preceeding and next commands (about 22 cycles) + // compensate for the time taken by the preceding and next commands (about 22 cycles) us -= 22; // = 2 cycles // the following loop takes 4 microseconds (4 cycles) // per iteration, so execute it us/4 times diff --git a/cores/arduino/wiring_digital.c b/cores/arduino/wiring_digital.c index 27a62fc..432a150 100644 --- a/cores/arduino/wiring_digital.c +++ b/cores/arduino/wiring_digital.c @@ -67,7 +67,7 @@ void pinMode(uint8_t pin, uint8_t mode) // // Mark Sproul: // - Removed inline. Save 170 bytes on atmega1280 -// - changed to a switch statment; added 32 bytes but much easier to read and maintain. +// - changed to a switch statement; added 32 bytes but much easier to read and maintain. // - Added more #ifdefs, now compiles for atmega645 // //static inline void turnOffPWM(uint8_t timer) __attribute__ ((always_inline)); -- cgit v1.2.3-18-g5258