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

121 lines
2.5 KiB
C++

/*
main.cpp - Main loop for Arduino sketches
Copyright (c) 2005-2013 Arduino Team. 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
*/
//This may be used to change user task stack size:
//#define CONT_STACKSIZE 4096
#include <Arduino.h>
extern "C" {
#include "ets_sys.h"
#include "os_type.h"
#include "osapi.h"
#include "mem.h"
#include "user_interface.h"
#include "cont.h"
}
#define LOOP_TASK_PRIORITY 0
#define LOOP_QUEUE_SIZE 1
int atexit(void (*func)()) { return 0; }
void initVariant() __attribute__((weak));
void initVariant() { }
extern void loop();
extern void setup();
extern void (*__init_array_start)(void);
extern void (*__init_array_end)(void);
static cont_t g_cont;
static os_event_t g_loop_queue[LOOP_QUEUE_SIZE];
extern "C" void esp_yield()
{
cont_yield(&g_cont);
}
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()
{
static bool setup_done = false;
if (!setup_done)
{
setup();
setup_done = true;
}
loop();
esp_schedule();
}
static void loop_task(os_event_t *events)
{
cont_run(&g_cont, &loop_wrapper);
if (cont_check(&g_cont) != 0)
{
abort();
}
}
static void do_global_ctors(void)
{
void (**p)(void);
for (p = &__init_array_start; p != &__init_array_end; ++p)
(*p)();
}
void init_done()
{
do_global_ctors();
esp_schedule();
}
extern "C" {
void user_init(void)
{
uart_div_modify(0, UART_CLK_FREQ / (115200));
init();
initVariant();
cont_init(&g_cont);
system_os_task( loop_task,
LOOP_TASK_PRIORITY,
g_loop_queue,
LOOP_QUEUE_SIZE);
system_init_done_cb(&init_done);
}
}