Skip to content

Commit 9bcb761

Browse files
Merge branch 'master' into new_timer1_irq
2 parents 330f9cd + c12c9e2 commit 9bcb761

10 files changed

+146
-83
lines changed

cores/esp8266/cont_util.c

+3
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020

2121
#include "cont.h"
2222
#include <stddef.h>
23+
#include <string.h>
2324
#include "ets_sys.h"
2425

2526

2627
#define CONT_STACKGUARD 0xfeefeffe
2728

2829
void cont_init(cont_t* cont) {
30+
memset(cont, 0, sizeof(cont_t));
31+
2932
cont->stack_guard1 = CONT_STACKGUARD;
3033
cont->stack_guard2 = CONT_STACKGUARD;
3134
cont->stack_end = cont->stack + (sizeof(cont->stack) / 4);

cores/esp8266/core_esp8266_main.cpp

+46-27
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,30 @@ extern "C" {
3737

3838
#define LOOP_TASK_PRIORITY 1
3939
#define LOOP_QUEUE_SIZE 1
40-
4140
#define OPTIMISTIC_YIELD_TIME_US 16000
4241

42+
extern "C" void call_user_start();
43+
extern void loop();
44+
extern void setup();
45+
extern void (*__init_array_start)(void);
46+
extern void (*__init_array_end)(void);
47+
48+
/* Not static, used in Esp.cpp */
4349
struct rst_info resetInfo;
4450

51+
/* Not static, used in core_esp8266_postmortem.c.
52+
* Placed into noinit section because we assign value to this variable
53+
* before .bss is zero-filled, and need to preserve the value.
54+
*/
55+
cont_t* g_pcont __attribute__((section(".noinit")));
56+
57+
/* Event queue used by the main (arduino) task */
58+
static os_event_t s_loop_queue[LOOP_QUEUE_SIZE];
59+
60+
/* Used to implement optimistic_yield */
61+
static uint32_t s_micros_at_task_start;
62+
63+
4564
extern "C" {
4665
extern const uint32_t __attribute__((section(".ver_number"))) core_version = ARDUINO_ESP8266_GIT_VER;
4766
const char* core_release =
@@ -52,19 +71,10 @@ const char* core_release =
5271
#endif
5372
} // extern "C"
5473

55-
int atexit(void (*func)()) {
56-
(void) func;
57-
return 0;
58-
}
59-
60-
extern "C" void ets_update_cpu_frequency(int freqmhz);
6174
void initVariant() __attribute__((weak));
6275
void initVariant() {
6376
}
6477

65-
extern void loop();
66-
extern void setup();
67-
6878
void preloop_update_frequency() __attribute__((weak));
6979
void preloop_update_frequency() {
7080
#if defined(F_CPU) && (F_CPU == 160000000L)
@@ -73,17 +83,10 @@ void preloop_update_frequency() {
7383
#endif
7484
}
7585

76-
extern void (*__init_array_start)(void);
77-
extern void (*__init_array_end)(void);
78-
79-
cont_t g_cont __attribute__ ((aligned (16)));
80-
static os_event_t g_loop_queue[LOOP_QUEUE_SIZE];
81-
82-
static uint32_t g_micros_at_task_start;
8386

8487
extern "C" void esp_yield() {
85-
if (cont_can_yield(&g_cont)) {
86-
cont_yield(&g_cont);
88+
if (cont_can_yield(g_pcont)) {
89+
cont_yield(g_pcont);
8790
}
8891
}
8992

@@ -92,7 +95,7 @@ extern "C" void esp_schedule() {
9295
}
9396

9497
extern "C" void __yield() {
95-
if (cont_can_yield(&g_cont)) {
98+
if (cont_can_yield(g_pcont)) {
9699
esp_schedule();
97100
esp_yield();
98101
}
@@ -104,8 +107,8 @@ extern "C" void __yield() {
104107
extern "C" void yield(void) __attribute__ ((weak, alias("__yield")));
105108

106109
extern "C" void optimistic_yield(uint32_t interval_us) {
107-
if (cont_can_yield(&g_cont) &&
108-
(system_get_time() - g_micros_at_task_start) > interval_us)
110+
if (cont_can_yield(g_pcont) &&
111+
(system_get_time() - s_micros_at_task_start) > interval_us)
109112
{
110113
yield();
111114
}
@@ -125,9 +128,9 @@ static void loop_wrapper() {
125128

126129
static void loop_task(os_event_t *events) {
127130
(void) events;
128-
g_micros_at_task_start = system_get_time();
129-
cont_run(&g_cont, &loop_wrapper);
130-
if (cont_check(&g_cont) != 0) {
131+
s_micros_at_task_start = system_get_time();
132+
cont_run(g_pcont, &loop_wrapper);
133+
if (cont_check(g_pcont) != 0) {
131134
panic();
132135
}
133136
}
@@ -145,6 +148,22 @@ void init_done() {
145148
esp_schedule();
146149
}
147150

151+
/* This is the entry point of the application.
152+
* It gets called on the default stack, which grows down from the top
153+
* of DRAM area.
154+
* .bss has not been zeroed out yet, but .data and .rodata are in place.
155+
* Cache is not enabled, so only ROM and IRAM functions can be called.
156+
* Peripherals (except for SPI0 and UART0) are not initialized.
157+
* This function does not return.
158+
*/
159+
extern "C" void ICACHE_RAM_ATTR app_entry(void)
160+
{
161+
/* Allocate continuation context on this stack, and save pointer to it. */
162+
cont_t s_cont __attribute__((aligned(16)));
163+
g_pcont = &s_cont;
164+
/* Call the entry point of the SDK code. */
165+
call_user_start();
166+
}
148167

149168
extern "C" void user_init(void) {
150169
struct rst_info *rtc_info_ptr = system_get_rst_info();
@@ -156,10 +175,10 @@ extern "C" void user_init(void) {
156175

157176
initVariant();
158177

159-
cont_init(&g_cont);
178+
cont_init(g_pcont);
160179

161180
ets_task(loop_task,
162-
LOOP_TASK_PRIORITY, g_loop_queue,
181+
LOOP_TASK_PRIORITY, s_loop_queue,
163182
LOOP_QUEUE_SIZE);
164183

165184
system_init_done_cb(&init_done);

cores/esp8266/core_esp8266_postmortem.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
extern void __real_system_restart_local();
3434

35-
extern cont_t g_cont;
35+
extern cont_t* g_pcont;
3636

3737
// These will be pointers to PROGMEM const strings
3838
static const char* s_panic_file = 0;
@@ -131,8 +131,8 @@ void __wrap_system_restart_local() {
131131
ets_printf_P("\nSoft WDT reset\n");
132132
}
133133

134-
uint32_t cont_stack_start = (uint32_t) &(g_cont.stack);
135-
uint32_t cont_stack_end = (uint32_t) g_cont.stack_end;
134+
uint32_t cont_stack_start = (uint32_t) &(g_pcont->stack);
135+
uint32_t cont_stack_end = (uint32_t) g_pcont->stack_end;
136136
uint32_t stack_end;
137137

138138
// amount of stack taken by interrupt or exception handler

cores/esp8266/core_esp8266_wiring_digital.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ extern void ICACHE_RAM_ATTR __attachInterruptArg(uint8_t pin, voidFuncPtr userFu
160160
GPC(pin) &= ~(0xF << GPCI);//INT mode disabled
161161
GPIEC = (1 << pin); //Clear Interrupt for this pin
162162
GPC(pin) |= ((mode & 0xF) << GPCI);//INT mode "mode"
163+
ETS_GPIO_INTR_ATTACH(interrupt_handler, &interrupt_reg);
163164
ETS_GPIO_INTR_ENABLE();
164165
}
165166
}
@@ -179,7 +180,8 @@ extern void ICACHE_RAM_ATTR __detachInterrupt(uint8_t pin) {
179180
handler->mode = 0;
180181
handler->fn = 0;
181182
handler->arg = 0;
182-
ETS_GPIO_INTR_ENABLE();
183+
if (interrupt_reg)
184+
ETS_GPIO_INTR_ENABLE();
183185
}
184186
}
185187

@@ -196,9 +198,6 @@ void initPins() {
196198
for (int i = 12; i <= 16; ++i) {
197199
pinMode(i, INPUT);
198200
}
199-
200-
ETS_GPIO_INTR_ATTACH(interrupt_handler, &interrupt_reg);
201-
ETS_GPIO_INTR_ENABLE();
202201
}
203202

204203
extern void pinMode(uint8_t pin, uint8_t mode) __attribute__ ((weak, alias("__pinMode")));

cores/esp8266/libc_replacements.c

+3-35
Original file line numberDiff line numberDiff line change
@@ -122,39 +122,7 @@ void _exit(int status) {
122122
abort();
123123
}
124124

125-
#if 0
126-
127-
int ICACHE_RAM_ATTR printf(const char* format, ...) {
128-
va_list arglist;
129-
va_start(arglist, format);
130-
int ret = ets_vprintf(ets_putc, format, arglist);
131-
va_end(arglist);
132-
return ret;
133-
}
134-
135-
int ICACHE_RAM_ATTR sprintf(char* buffer, const char* format, ...) {
136-
int ret;
137-
va_list arglist;
138-
va_start(arglist, format);
139-
ret = ets_vsprintf(buffer, format, arglist);
140-
va_end(arglist);
141-
return ret;
142-
}
143-
144-
int ICACHE_RAM_ATTR snprintf(char* buffer, size_t size, const char* format, ...) {
145-
int ret;
146-
va_list arglist;
147-
va_start(arglist, format);
148-
ret = ets_vsnprintf(buffer, size, format, arglist);
149-
va_end(arglist);
150-
return ret;
151-
}
152-
153-
int ICACHE_RAM_ATTR vprintf(const char * format, va_list arg) {
154-
return ets_vprintf(ets_putc, format, arg);
155-
}
156-
157-
int ICACHE_RAM_ATTR vsnprintf(char * buffer, size_t size, const char * format, va_list arg) {
158-
return ets_vsnprintf(buffer, size, format, arg);
125+
int atexit(void (*func)()) {
126+
(void) func;
127+
return 0;
159128
}
160-
#endif

platform.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ compiler.c.flags=-c {compiler.warning_flags} -Os -g -Wpointer-arith -Wno-implici
3737
compiler.S.cmd=xtensa-lx106-elf-gcc
3838
compiler.S.flags=-c -g -x assembler-with-cpp -MMD -mlongcalls
3939

40-
compiler.c.elf.flags=-g {compiler.warning_flags} -Os -nostdlib -Wl,--no-check-sections -u call_user_start {build.float} -Wl,-static "-L{compiler.sdk.path}/lib" "-L{compiler.sdk.path}/ld" "-L{compiler.libc.path}/lib" "-T{build.flash_ld}" -Wl,--gc-sections -Wl,-wrap,system_restart_local -Wl,-wrap,spi_flash_read
40+
compiler.c.elf.flags=-g {compiler.warning_flags} -Os -nostdlib -Wl,--no-check-sections -u app_entry {build.float} -Wl,-static "-L{compiler.sdk.path}/lib" "-L{compiler.sdk.path}/ld" "-L{compiler.libc.path}/lib" "-T{build.flash_ld}" -Wl,--gc-sections -Wl,-wrap,system_restart_local -Wl,-wrap,spi_flash_read
4141

4242
compiler.c.elf.cmd=xtensa-lx106-elf-gcc
4343
compiler.c.elf.libs=-lhal -lphy -lpp -lnet80211 {build.lwip_lib} -lwpa -lcrypto -lmain -lwps -laxtls -lespnow -lsmartconfig -lairkiss -lwpa2 -lstdc++ -lm -lc -lgcc

tools/platformio-build.py

+75-11
Original file line numberDiff line numberDiff line change
@@ -50,40 +50,104 @@ def scons_patched_match_splitext(path, suffixes=None):
5050
assert isdir(FRAMEWORK_DIR)
5151

5252

53-
env.Prepend(
53+
env.Append(
54+
CCFLAGS=[
55+
"-Wall"
56+
],
57+
5458
CPPDEFINES=[
55-
("ARDUINO", 10600),
59+
("ARDUINO", 10805),
60+
("ARDUINO_BOARD", '\\"PLATFORMIO_%s\\"'
61+
% env.BoardConfig().id.upper()),
5662
"LWIP_OPEN_SRC"
5763
],
64+
5865
CPPPATH=[
5966
join(FRAMEWORK_DIR, "tools", "sdk", "include"),
60-
join(FRAMEWORK_DIR, "tools", "sdk", "lwip", "include"),
6167
join(FRAMEWORK_DIR, "tools", "sdk", "libc",
6268
"xtensa-lx106-elf", "include"),
6369
join(FRAMEWORK_DIR, "cores", env.BoardConfig().get("build.core"))
6470
],
71+
6572
LIBPATH=[
73+
join("$BUILD_DIR", "ld"), # eagle.app.v6.common.ld
6674
join(FRAMEWORK_DIR, "tools", "sdk", "lib"),
6775
join(FRAMEWORK_DIR, "tools", "sdk", "ld"),
6876
join(FRAMEWORK_DIR, "tools", "sdk", "libc", "xtensa-lx106-elf", "lib")
6977
],
78+
7079
LIBS=[
71-
"wpa2", "smartconfig", "espnow", "pp", "main", "wpa", "lwip_gcc",
72-
"net80211", "wps", "crypto", "phy", "hal", "axtls", "gcc",
73-
"m", "c", "stdc++"
74-
]
75-
)
80+
"hal", "phy", "pp", "net80211", "wpa", "crypto", "main",
81+
"wps", "axtls", "espnow", "smartconfig", "airkiss", "wpa2",
82+
"stdc++", "m", "c", "gcc"
83+
],
7684

77-
env.Append(
7885
LIBSOURCE_DIRS=[
7986
join(FRAMEWORK_DIR, "libraries")
80-
],
87+
]
88+
)
89+
90+
env.Replace(
8191
LINKFLAGS=[
92+
"-Os",
93+
"-nostdlib",
94+
"-Wl,--no-check-sections",
95+
"-Wl,-static",
96+
"-Wl,--gc-sections",
8297
"-Wl,-wrap,system_restart_local",
83-
"-Wl,-wrap,spi_flash_read"
98+
"-Wl,-wrap,spi_flash_read",
99+
"-u", "app_entry",
100+
"-u", "_printf_float",
101+
"-u", "_scanf_float"
84102
]
85103
)
86104

105+
flatten_cppdefines = env.Flatten(env['CPPDEFINES'])
106+
107+
#
108+
# lwIP
109+
#
110+
if "PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY" in flatten_cppdefines:
111+
env.Append(
112+
CPPDEFINES=[("TCP_MSS", 536)],
113+
CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")],
114+
LIBS=["lwip2"]
115+
)
116+
elif "PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH" in flatten_cppdefines:
117+
env.Append(
118+
CPPDEFINES=[("TCP_MSS", 1460)],
119+
CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")],
120+
LIBS=["lwip2_1460"]
121+
)
122+
else:
123+
env.Append(
124+
CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip", "include")],
125+
LIBS=["lwip_gcc"]
126+
)
127+
128+
#
129+
# VTables
130+
#
131+
132+
current_vtables = None
133+
for d in flatten_cppdefines:
134+
if str(d).startswith("VTABLES_IN_"):
135+
current_vtables = d
136+
if not current_vtables:
137+
current_vtables = "VTABLES_IN_FLASH"
138+
env.Append(CPPDEFINES=[current_vtables])
139+
assert current_vtables
140+
141+
# Build the eagle.app.v6.common.ld linker file
142+
app_ld = env.Command(
143+
join("$BUILD_DIR", "ld", "eagle.app.v6.common.ld"),
144+
join(FRAMEWORK_DIR, "tools", "sdk", "ld", "eagle.app.v6.common.ld.h"),
145+
env.VerboseAction(
146+
"$CC -CC -E -P -D%s $SOURCE -o $TARGET" % current_vtables,
147+
"Generating LD script $TARGET"))
148+
env.Depends("$BUILD_DIR/$PROGNAME$PROGSUFFIX", app_ld)
149+
150+
87151
#
88152
# Target: Build Core Library
89153
#

tools/sdk/include/ets_sys.h

+1
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ int ets_vprintf(int (*print_function)(int), const char * format, va_list arg) __
215215
int ets_putc(int);
216216
bool ets_task(ETSTask task, uint8 prio, ETSEvent *queue, uint8 qlen);
217217
bool ets_post(uint8 prio, ETSSignal sig, ETSParam par);
218+
void ets_update_cpu_frequency(uint32_t ticks_per_us);
218219

219220

220221
#ifdef __cplusplus

0 commit comments

Comments
 (0)