From d69cbfe4ff6a16693f0b3c828be0027933eed3b4 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Sat, 12 Nov 2016 00:53:11 +0000 Subject: [PATCH] Make init() run before C++ static initializers Before this change, the contents of `init()` run after `.init6`, which is when static initializers run. However, this is not desirable: * init() does not need any C++ classes to be active * C++ static initialization sometimes _does_ require that the hardware be pre-configured! This makes it possible to make calls like `Serial.begin()` inside constructors of global variables. --- cores/arduino/wiring.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cores/arduino/wiring.c b/cores/arduino/wiring.c index 972713581..670a067c6 100644 --- a/cores/arduino/wiring.c +++ b/cores/arduino/wiring.c @@ -238,7 +238,10 @@ void delayMicroseconds(unsigned int us) // return = 4 cycles } -void init() +//http://www.atmel.com/webdoc/AVRLibcReferenceManual/mem_sections_1sec_dot_init.html +// init5 is after .data, but before C++ initialization - perfect for hardware setup +void _do_setup(void) __attribute__ ((naked, used, section (".init5"))); +void _do_setup() { // this needs to be called before setup() or some functions won't // work there @@ -390,3 +393,6 @@ void init() UCSR0B = 0; #endif } + +// for backwards compatibility +void init() {}