1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

Tidy up backend code for esp8266 (malloc, free, yield)

This commit is contained in:
Ivan Grokhotkov 2014-11-21 19:15:40 +03:00
parent e199fc349c
commit e21371d6d0
7 changed files with 43 additions and 83 deletions

View File

@ -121,7 +121,7 @@ String::String(double value, unsigned char decimalPlaces)
String::~String() String::~String()
{ {
os_free(buffer); free(buffer);
} }
/*********************************************/ /*********************************************/
@ -137,7 +137,7 @@ inline void String::init(void)
void String::invalidate(void) void String::invalidate(void)
{ {
if (buffer) os_free(buffer); if (buffer) free(buffer);
buffer = NULL; buffer = NULL;
capacity = len = 0; capacity = len = 0;
} }
@ -189,7 +189,7 @@ void String::move(String &rhs)
rhs.len = 0; rhs.len = 0;
return; return;
} else { } else {
os_free(buffer); free(buffer);
} }
} }
buffer = rhs.buffer; buffer = rhs.buffer;

View File

@ -17,20 +17,10 @@
*/ */
#include <stdlib.h> #include <stdlib.h>
extern "C"
{
#include "ets_sys.h"
}
extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__)); extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__));
extern "C" void __cxa_deleted_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) { void __cxa_pure_virtual(void) {
// We might want to write some diagnostics to uart in this case // We might want to write some diagnostics to uart in this case
//std::terminate(); //std::terminate();

View File

@ -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")));

View File

@ -17,6 +17,9 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 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 <Arduino.h> #include <Arduino.h>
extern "C" { extern "C" {
#include "ets_sys.h" #include "ets_sys.h"
@ -26,42 +29,51 @@ extern "C" {
#include "user_interface.h" #include "user_interface.h"
#include "cont.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; } int atexit(void (*func)()) { return 0; }
// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak)); void initVariant() __attribute__((weak));
void initVariant() { } void initVariant() { }
extern void loop(); extern void loop();
extern void setup(); extern void setup();
#define LOOP_TASK_PRIORITY 0 extern void (*__init_array_start)(void);
#define LOOP_QUEUE_SIZE 1 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 esp_schedule()
extern "C" void loop_schedule()
{ {
system_os_post(LOOP_TASK_PRIORITY, 0, 0); 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() static void loop_wrapper()
{ {
if (!g_setup_done) static bool setup_done = false;
if (!setup_done)
{ {
g_setup_done = true;
setup(); setup();
setup_done = true;
} }
loop(); loop();
loop_schedule(); esp_schedule();
} }
static void loop_task(os_event_t *events) 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) static void do_global_ctors(void)
{ {
void (**p)(void); void (**p)(void);
@ -85,16 +94,13 @@ static void do_global_ctors(void)
void init_done() void init_done()
{ {
loop_schedule(); do_global_ctors();
esp_schedule();
int i = ((char*)__init_array_end) - (char*)__init_array_start;
os_printf("\r\nInit array size: %d\r\n", i);
} }
extern "C" { extern "C" {
void user_init(void) void user_init(void)
{ {
do_global_ctors();
uart_div_modify(0, UART_CLK_FREQ / (115200)); uart_div_modify(0, UART_CLK_FREQ / (115200));
init(); init();
@ -105,10 +111,10 @@ void user_init(void)
system_os_task( loop_task, system_os_task( loop_task,
LOOP_TASK_PRIORITY, LOOP_TASK_PRIORITY,
loop_queue, g_loop_queue,
LOOP_QUEUE_SIZE); LOOP_QUEUE_SIZE);
system_init_done_cb(&init_done); system_init_done_cb(&esp_schedule);
} }
} }

View File

@ -16,26 +16,21 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
extern "C" { #include <stdlib.h>
#include "osapi.h"
#include "ets_sys.h"
#include "mem.h"
}
void *operator new(size_t size) { void *operator new(size_t size) {
return os_malloc(size); return malloc(size);
} }
void *operator new[](size_t size) { void *operator new[](size_t size) {
return os_malloc(size); return malloc(size);
} }
void operator delete(void * ptr) { void operator delete(void * ptr) {
os_free(ptr); free(ptr);
} }
void operator delete[](void * ptr) { void operator delete[](void * ptr) {
os_free(ptr); free(ptr);
} }

View File

@ -28,8 +28,8 @@
#include "user_interface.h" #include "user_interface.h"
#include "cont.h" #include "cont.h"
extern cont_t g_cont; extern void esp_schedule();
extern void loop_schedule(); extern void esp_yield();
static os_timer_t delay_timer; static os_timer_t delay_timer;
#define ONCE 0 #define ONCE 0
@ -43,14 +43,14 @@ unsigned long millis()
void delay_end(void* arg) void delay_end(void* arg)
{ {
loop_schedule(); esp_schedule();
} }
void delay(unsigned long ms) void delay(unsigned long ms)
{ {
os_timer_setfn(&delay_timer, (os_timer_func_t*) &delay_end, 0); os_timer_setfn(&delay_timer, (os_timer_func_t*) &delay_end, 0);
os_timer_arm(&delay_timer, ms, ONCE); os_timer_arm(&delay_timer, ms, ONCE);
cont_yield(&g_cont); esp_yield();
os_timer_disarm(&delay_timer); os_timer_disarm(&delay_timer);
} }

View File

@ -15,7 +15,7 @@ compiler.sdk.path={compiler.tools.path}/sdk/
compiler.cpreprocessor.flags=-D__ets__ -DICACHE_FLASH -I{compiler.sdk.path}/include compiler.cpreprocessor.flags=-D__ets__ -DICACHE_FLASH -I{compiler.sdk.path}/include
compiler.c.cmd=xtensa-lx106-elf-gcc 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.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} 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}