Skip to content

Commit e199fc3

Browse files
committed
Implement global init for esp8266
1 parent c55b5a8 commit e199fc3

2 files changed

Lines changed: 64 additions & 8 deletions

File tree

‎cores/esp8266/abi.cpp‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,29 @@
1717
*/
1818

1919
#include <stdlib.h>
20+
extern "C"
21+
{
22+
#include "ets_sys.h"
23+
}
2024

2125
extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__));
2226
extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__));
2327

28+
extern "C" void abort()
29+
{
30+
os_printf(" x_x ");
31+
while(true){}
32+
}
33+
2434
void __cxa_pure_virtual(void) {
2535
// We might want to write some diagnostics to uart in this case
2636
//std::terminate();
27-
while(true) {}
37+
abort();
2838
}
2939

3040
void __cxa_deleted_virtual(void) {
3141
// We might want to write some diagnostics to uart in this case
3242
//std::terminate();
33-
while(true) {}
43+
abort();
3444
}
3545

‎cores/esp8266/main.cpp‎

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extern "C" {
2424
#include "osapi.h"
2525
#include "mem.h"
2626
#include "user_interface.h"
27+
#include "cont.h"
2728
}
2829

2930
//Declared weak in Arduino.h to allow user redefinitions.
@@ -40,29 +41,74 @@ extern void setup();
4041
#define LOOP_TASK_PRIORITY 0
4142
#define LOOP_QUEUE_SIZE 1
4243

44+
cont_t g_cont;
45+
4346
os_event_t loop_queue[LOOP_QUEUE_SIZE];
4447

45-
void loop_task(os_event_t *events)
48+
bool g_setup_done = false;
49+
50+
extern "C" void loop_schedule()
4651
{
47-
loop();
4852
system_os_post(LOOP_TASK_PRIORITY, 0, 0);
4953
}
5054

55+
static void loop_wrapper()
56+
{
57+
if (!g_setup_done)
58+
{
59+
g_setup_done = true;
60+
setup();
61+
}
62+
63+
loop();
64+
loop_schedule();
65+
}
66+
67+
static void loop_task(os_event_t *events)
68+
{
69+
cont_run(&g_cont, &loop_wrapper);
70+
if (cont_check(&g_cont) != 0)
71+
{
72+
abort();
73+
}
74+
}
75+
76+
extern void (*__init_array_start)(void);
77+
extern void (*__init_array_end)(void);
78+
79+
static void do_global_ctors(void)
80+
{
81+
void (**p)(void);
82+
for (p = &__init_array_start; p != &__init_array_end; ++p)
83+
(*p)();
84+
}
85+
86+
void init_done()
87+
{
88+
loop_schedule();
89+
90+
int i = ((char*)__init_array_end) - (char*)__init_array_start;
91+
os_printf("\r\nInit array size: %d\r\n", i);
92+
}
93+
5194
extern "C" {
5295
void user_init(void)
5396
{
97+
do_global_ctors();
98+
uart_div_modify(0, UART_CLK_FREQ / (115200));
99+
54100
init();
55101

56102
initVariant();
57103

104+
cont_init(&g_cont);
105+
58106
system_os_task( loop_task,
59107
LOOP_TASK_PRIORITY,
60108
loop_queue,
61109
LOOP_QUEUE_SIZE);
62-
setup();
63-
loop_task(0);
110+
111+
system_init_done_cb(&init_done);
64112
}
65113
}
66114

67-
68-

0 commit comments

Comments
 (0)