diff --git a/libraries/MySensors/core/MyMainDefault.cpp b/libraries/MySensors/core/MyMainDefault.cpp new file mode 100644 index 0000000..f4f0ee8 --- /dev/null +++ b/libraries/MySensors/core/MyMainDefault.cpp @@ -0,0 +1,16 @@ +// Initialize library and handle sketch functions like we want to +int main(void) { + init(); + #if defined(USBCON) + USBDevice.attach(); + #endif + _begin(); // Startup MySensors library + if (setup) setup(); // Call sketch setup + + for(;;) { + _process(); // Process incoming data + if (loop) loop(); // Call sketch loop + if (serialEventRun) serialEventRun(); + } + return 0; +} diff --git a/libraries/MySensors/core/MyMainESP8266.cpp b/libraries/MySensors/core/MyMainESP8266.cpp new file mode 100644 index 0000000..158ca74 --- /dev/null +++ b/libraries/MySensors/core/MyMainESP8266.cpp @@ -0,0 +1,134 @@ + +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 + +#define OPTIMISTIC_YIELD_TIME_US 16000 + +struct rst_info resetInfo; + +int atexit(void (*func)()) { + return 0; +} + +extern "C" void ets_update_cpu_frequency(int freqmhz); +void initVariant() __attribute__((weak)); +void initVariant() { +} + + + +void preloop_update_frequency() __attribute__((weak)); +void preloop_update_frequency() { +#if defined(F_CPU) && (F_CPU == 160000000L) + REG_SET_BIT(0x3ff00014, BIT(0)); + ets_update_cpu_frequency(160); +#endif +} + +extern void (*__init_array_start)(void); +extern void (*__init_array_end)(void); + +cont_t g_cont __attribute__ ((aligned (16))); +static os_event_t g_loop_queue[LOOP_QUEUE_SIZE]; + +static uint32_t g_micros_at_task_start; + + +extern "C" void abort() { + do { + *((int*)0) = 0; + } while(true); +} + +extern "C" void esp_yield() { + if (cont_can_yield(&g_cont)) { + cont_yield(&g_cont); + } +} + +extern "C" void esp_schedule() { + system_os_post(LOOP_TASK_PRIORITY, 0, 0); +} + +extern "C" void __yield() { + if (cont_can_yield(&g_cont)) { + esp_schedule(); + esp_yield(); + } + else { + abort(); + } +} + +extern "C" void yield(void) __attribute__ ((weak, alias("__yield"))); + +extern "C" void optimistic_yield(uint32_t interval_us) { + if (cont_can_yield(&g_cont) && + (system_get_time() - g_micros_at_task_start) > interval_us) + { + yield(); + } +} + +static void loop_wrapper() { + static bool setup_done = false; + if(!setup_done) { + _begin(); // Startup MySensors library + setup(); + setup_done = true; + } + preloop_update_frequency(); + _process(); // Process incoming data + loop(); + esp_schedule(); +} + +static void loop_task(os_event_t *events) { + g_micros_at_task_start = system_get_time(); + cont_run(&g_cont, &loop_wrapper); + if(cont_check(&g_cont) != 0) { + ets_printf("\r\nsketch stack overflow detected\r\n"); + abort(); + } +} + +static void do_global_ctors(void) { + void (**p)(void); + for(p = &__init_array_start; p != &__init_array_end; ++p) + (*p)(); +} + +void init_done() { + system_set_os_print(1); + do_global_ctors(); + esp_schedule(); +} + + +extern "C" void user_init(void) { + struct rst_info *rtc_info_ptr = system_get_rst_info(); + memcpy((void *) &resetInfo, (void *) rtc_info_ptr, sizeof(resetInfo)); + + 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); +}