From e199fc349c2932e35be46cfd683af43a655edccb Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 21 Nov 2014 18:14:45 +0300 Subject: [PATCH] Implement global init for esp8266 --- cores/esp8266/abi.cpp | 14 ++++++++-- cores/esp8266/main.cpp | 58 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/cores/esp8266/abi.cpp b/cores/esp8266/abi.cpp index 294ed3c30..a13026bfe 100644 --- a/cores/esp8266/abi.cpp +++ b/cores/esp8266/abi.cpp @@ -17,19 +17,29 @@ */ #include +extern "C" +{ +#include "ets_sys.h" +} extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__)); extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__)); +extern "C" void abort() +{ + os_printf(" x_x "); + while(true){} +} + void __cxa_pure_virtual(void) { // We might want to write some diagnostics to uart in this case //std::terminate(); - while(true) {} + abort(); } void __cxa_deleted_virtual(void) { // We might want to write some diagnostics to uart in this case //std::terminate(); - while(true) {} + abort(); } diff --git a/cores/esp8266/main.cpp b/cores/esp8266/main.cpp index 56dadc936..05d8b8d9b 100644 --- a/cores/esp8266/main.cpp +++ b/cores/esp8266/main.cpp @@ -24,6 +24,7 @@ extern "C" { #include "osapi.h" #include "mem.h" #include "user_interface.h" +#include "cont.h" } //Declared weak in Arduino.h to allow user redefinitions. @@ -40,29 +41,74 @@ extern void setup(); #define LOOP_TASK_PRIORITY 0 #define LOOP_QUEUE_SIZE 1 +cont_t g_cont; + os_event_t loop_queue[LOOP_QUEUE_SIZE]; -void loop_task(os_event_t *events) +bool g_setup_done = false; + +extern "C" void loop_schedule() { - loop(); system_os_post(LOOP_TASK_PRIORITY, 0, 0); } +static void loop_wrapper() +{ + if (!g_setup_done) + { + g_setup_done = true; + setup(); + } + + loop(); + loop_schedule(); +} + +static void loop_task(os_event_t *events) +{ + cont_run(&g_cont, &loop_wrapper); + if (cont_check(&g_cont) != 0) + { + abort(); + } +} + +extern void (*__init_array_start)(void); +extern void (*__init_array_end)(void); + +static void do_global_ctors(void) +{ + void (**p)(void); + for (p = &__init_array_start; p != &__init_array_end; ++p) + (*p)(); +} + +void init_done() +{ + loop_schedule(); + + int i = ((char*)__init_array_end) - (char*)__init_array_start; + os_printf("\r\nInit array size: %d\r\n", i); +} + extern "C" { void user_init(void) { + do_global_ctors(); + uart_div_modify(0, UART_CLK_FREQ / (115200)); + init(); initVariant(); + cont_init(&g_cont); + system_os_task( loop_task, LOOP_TASK_PRIORITY, loop_queue, LOOP_QUEUE_SIZE); - setup(); - loop_task(0); + + system_init_done_cb(&init_done); } } - -