From e21371d6d01ec724c26c38dbcd6b1eab7c00eeea Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 21 Nov 2014 19:15:40 +0300 Subject: [PATCH] Tidy up backend code for esp8266 (malloc, free, yield) --- cores/esp8266/WString.cpp | 6 ++--- cores/esp8266/abi.cpp | 10 -------- cores/esp8266/hooks.c | 31 ---------------------- cores/esp8266/main.cpp | 54 ++++++++++++++++++++++----------------- cores/esp8266/new.cpp | 15 ++++------- cores/esp8266/wiring.c | 8 +++--- platform.txt | 2 +- 7 files changed, 43 insertions(+), 83 deletions(-) delete mode 100644 cores/esp8266/hooks.c diff --git a/cores/esp8266/WString.cpp b/cores/esp8266/WString.cpp index 1e4019e38..6fa19aeb2 100644 --- a/cores/esp8266/WString.cpp +++ b/cores/esp8266/WString.cpp @@ -121,7 +121,7 @@ String::String(double value, unsigned char decimalPlaces) String::~String() { - os_free(buffer); + free(buffer); } /*********************************************/ @@ -137,7 +137,7 @@ inline void String::init(void) void String::invalidate(void) { - if (buffer) os_free(buffer); + if (buffer) free(buffer); buffer = NULL; capacity = len = 0; } @@ -189,7 +189,7 @@ void String::move(String &rhs) rhs.len = 0; return; } else { - os_free(buffer); + free(buffer); } } buffer = rhs.buffer; diff --git a/cores/esp8266/abi.cpp b/cores/esp8266/abi.cpp index a13026bfe..8d719b8e6 100644 --- a/cores/esp8266/abi.cpp +++ b/cores/esp8266/abi.cpp @@ -17,20 +17,10 @@ */ #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(); diff --git a/cores/esp8266/hooks.c b/cores/esp8266/hooks.c deleted file mode 100644 index 641eabc73..000000000 --- a/cores/esp8266/hooks.c +++ /dev/null @@ -1,31 +0,0 @@ -/* - Copyright (c) 2012 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 -*/ - -/** - * Empty yield() hook. - * - * This function is intended to be used by library writers to build - * libraries or sketches that supports cooperative threads. - * - * Its defined as a weak symbol and it can be redefined to implement a - * real cooperative scheduler. - */ -static void __empty() { - // Empty -} -void yield(void) __attribute__ ((weak, alias("__empty"))); diff --git a/cores/esp8266/main.cpp b/cores/esp8266/main.cpp index 05d8b8d9b..c77762656 100644 --- a/cores/esp8266/main.cpp +++ b/cores/esp8266/main.cpp @@ -17,6 +17,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +//This may be used to change user task stack size: +//#define CONT_STACKSIZE 4096 + #include extern "C" { #include "ets_sys.h" @@ -26,42 +29,51 @@ extern "C" { #include "user_interface.h" #include "cont.h" } +#define LOOP_TASK_PRIORITY 0 +#define LOOP_QUEUE_SIZE 1 -//Declared weak in Arduino.h to allow user redefinitions. int atexit(void (*func)()) { return 0; } -// Weak empty variant initialization function. -// May be redefined by variant files. void initVariant() __attribute__((weak)); void initVariant() { } extern void loop(); extern void setup(); -#define LOOP_TASK_PRIORITY 0 -#define LOOP_QUEUE_SIZE 1 +extern void (*__init_array_start)(void); +extern void (*__init_array_end)(void); -cont_t g_cont; +static cont_t g_cont; +static os_event_t g_loop_queue[LOOP_QUEUE_SIZE]; -os_event_t loop_queue[LOOP_QUEUE_SIZE]; +extern "C" void esp_yield() +{ + cont_yield(&g_cont); +} -bool g_setup_done = false; - -extern "C" void loop_schedule() +extern "C" void esp_schedule() { system_os_post(LOOP_TASK_PRIORITY, 0, 0); } +extern "C" void __yield() +{ + esp_schedule(); + esp_yield(); +} +extern "C" void yield(void) __attribute__ ((weak, alias("__yield"))); + + static void loop_wrapper() { - if (!g_setup_done) + static bool setup_done = false; + if (!setup_done) { - g_setup_done = true; setup(); + setup_done = true; } - loop(); - loop_schedule(); + esp_schedule(); } static void loop_task(os_event_t *events) @@ -73,9 +85,6 @@ static void loop_task(os_event_t *events) } } -extern void (*__init_array_start)(void); -extern void (*__init_array_end)(void); - static void do_global_ctors(void) { void (**p)(void); @@ -85,16 +94,13 @@ static void do_global_ctors(void) 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); + do_global_ctors(); + esp_schedule(); } extern "C" { void user_init(void) { - do_global_ctors(); uart_div_modify(0, UART_CLK_FREQ / (115200)); init(); @@ -105,10 +111,10 @@ void user_init(void) system_os_task( loop_task, LOOP_TASK_PRIORITY, - loop_queue, + g_loop_queue, LOOP_QUEUE_SIZE); - system_init_done_cb(&init_done); + system_init_done_cb(&esp_schedule); } } diff --git a/cores/esp8266/new.cpp b/cores/esp8266/new.cpp index 9c52a7e0e..cf6f89c17 100644 --- a/cores/esp8266/new.cpp +++ b/cores/esp8266/new.cpp @@ -16,26 +16,21 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -extern "C" { -#include "osapi.h" -#include "ets_sys.h" -#include "mem.h" -} - +#include void *operator new(size_t size) { - return os_malloc(size); + return malloc(size); } void *operator new[](size_t size) { - return os_malloc(size); + return malloc(size); } void operator delete(void * ptr) { - os_free(ptr); + free(ptr); } void operator delete[](void * ptr) { - os_free(ptr); + free(ptr); } diff --git a/cores/esp8266/wiring.c b/cores/esp8266/wiring.c index 57d9779ca..575b8ab8b 100644 --- a/cores/esp8266/wiring.c +++ b/cores/esp8266/wiring.c @@ -28,8 +28,8 @@ #include "user_interface.h" #include "cont.h" -extern cont_t g_cont; -extern void loop_schedule(); +extern void esp_schedule(); +extern void esp_yield(); static os_timer_t delay_timer; #define ONCE 0 @@ -43,14 +43,14 @@ unsigned long millis() void delay_end(void* arg) { - loop_schedule(); + esp_schedule(); } void delay(unsigned long ms) { os_timer_setfn(&delay_timer, (os_timer_func_t*) &delay_end, 0); os_timer_arm(&delay_timer, ms, ONCE); - cont_yield(&g_cont); + esp_yield(); os_timer_disarm(&delay_timer); } diff --git a/platform.txt b/platform.txt index 8f066751f..70c9f4a0f 100644 --- a/platform.txt +++ b/platform.txt @@ -15,7 +15,7 @@ compiler.sdk.path={compiler.tools.path}/sdk/ compiler.cpreprocessor.flags=-D__ets__ -DICACHE_FLASH -I{compiler.sdk.path}/include compiler.c.cmd=xtensa-lx106-elf-gcc -compiler.c.flags=-c -Os -Wpointer-arith -Wno-implicit-function-declaration -Wl,-EL -fno-inline-functions -nostdlib -mlongcalls -MMD +compiler.c.flags=-c -Os -Wpointer-arith -Wno-implicit-function-declaration -Wl,-EL -fno-inline-functions -nostdlib -mlongcalls -MMD -std=c99 compiler.c.elf.ldscript=eagle.app.v6.ld compiler.c.elf.flags=-nostdlib -Wl,--no-check-sections -u call_user_start -Wl,-static -L{compiler.sdk.path}/lib -L{compiler.sdk.path}/ld -T{compiler.c.elf.ldscript}