From b3c56890599353149275ece80977d9fbcad844c8 Mon Sep 17 00:00:00 2001 From: Mitch Bradley Date: Wed, 4 Jan 2017 07:25:18 -1000 Subject: [PATCH] ESP32 - no more SDK include dependencies --- build/esp32/Makefile | 4 +- build/esp32/sdk_build/main/component.mk | 2 +- build/esp32/sdk_build/main/interface.c | 60 +++++++++++++++++++++++++ src/app/esp32/consio.c | 32 +++---------- src/app/esp32/targets.mk | 6 --- src/app/esp32/textend.c | 17 ++++--- src/app/esp32/tmain.c | 7 +-- 7 files changed, 80 insertions(+), 48 deletions(-) create mode 100644 build/esp32/sdk_build/main/interface.c diff --git a/build/esp32/Makefile b/build/esp32/Makefile index 7a9b0ab6..242bf9f8 100644 --- a/build/esp32/Makefile +++ b/build/esp32/Makefile @@ -4,7 +4,6 @@ default: forth.elf TOPDIR=../.. -# CONFIG += -DBITS32 -DT16 CONFIG += -DBITS32 CFLAGS += -m32 @@ -12,13 +11,12 @@ CFLAGS += -m32 CC := gcc # Change these to reflect the locations of external stuff on your system, -# either here or on the command line, e.g. COMPORT=COM27 make download +# either here or on the command line XTGCCPATH ?= /c/msys32/opt/xtensa-esp32-elf/bin/ CROSS ?= $(XTGCCPATH)xtensa-esp32-elf- # ESP32 SDK IDF_PATH ?= $(TOPDIR)/../esp-idf -IDF_TEMPLATE_PATH ?= $(TOPDIR)/../esp-idf-template COMPORT ?= COM27 diff --git a/build/esp32/sdk_build/main/component.mk b/build/esp32/sdk_build/main/component.mk index c96f957a..543f00bc 100644 --- a/build/esp32/sdk_build/main/component.mk +++ b/build/esp32/sdk_build/main/component.mk @@ -3,4 +3,4 @@ # COMPONENT_OBJS := ../../../app.o main.o -COMPONENT_OBJS := ../../../app.o +COMPONENT_OBJS := ../../../app.o interface.o diff --git a/build/esp32/sdk_build/main/interface.c b/build/esp32/sdk_build/main/interface.c new file mode 100644 index 00000000..e3ebfc7e --- /dev/null +++ b/build/esp32/sdk_build/main/interface.c @@ -0,0 +1,60 @@ +// Interfaces between ESP32/FreeRTOS and Forth + +#include "freertos/FreeRTOS.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include "driver/uart.h" + +extern void forth(void); + +// This is the routine that is run by main_task() from cpu_start.c, +// i.e. the "call in" from FreeRTOS to Forth. +void app_main(void) +{ + nvs_flash_init(); + forth(); +} + +// The following routines are used by Forth to invoke functions +// defined by the SDK. The call signatures should be based on +// simple data types, typically "int" which is the same as Forth's +// "cell" on this processor. Doing so eliminates include dependencies +// between Forth and the SDK, i.e. we don't need to include forth.h +// herein, and we don't need to include lots of SDK .h files in the +// Forth tree. + +// init_uart() sets up UART0 for the Forth console, so key and emit +// can use uart_read_bytes() and uart_write_bytes(). The reason we do +// that instead of just calling getchar() and putchar() is because we +// want a non-blocking key?, and there is no easy way to do so with +// getchar(). + +#define BUF_SIZE (1024) +void init_uart(void) +{ + int uart_num = UART_NUM_0; + + uart_config_t uart_config = { + .baud_rate = 115200, + .data_bits = UART_DATA_8_BITS, + .parity = UART_PARITY_DISABLE, + .stop_bits = UART_STOP_BITS_1, + .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, + .rx_flow_ctrl_thresh = 122, + }; + uart_param_config(uart_num, &uart_config); + + // No need to set the pins as the defaults are correct for UART0 + + // Install driver with a receive buffer but no transmit buffer + // and no event queue. + uart_driver_install(uart_num, BUF_SIZE * 2, 0, 0, NULL, 0); +} + +// Routines for the ccalls[] table in textend.c. Add new ones +// as necessary. + +void ms(int msecs) +{ + vTaskDelay(msecs/ portTICK_PERIOD_MS); +} diff --git a/src/app/esp32/consio.c b/src/app/esp32/consio.c index 7bb65a0a..b7d3497a 100644 --- a/src/app/esp32/consio.c +++ b/src/app/esp32/consio.c @@ -5,7 +5,10 @@ #include "forth.h" #include "compiler.h" #include "stdlib.h" -#include "driver/uart.h" + +extern void uart_write_bytes(int, char *, int); +extern int uart_read_bytes(int, char *, int, int); +extern void init_uart(void); int isinteractive() { return (1); } int isstandalone() { return (1); } @@ -45,35 +48,10 @@ int key(cell *up) return (cell)the_key; } -static const char *TAG = "forth"; -#define BUF_SIZE (1024) -void uart_on(void) -{ - int uart_num = UART_NUM_0; - uart_config_t uart_config = { - .baud_rate = 115200, - .data_bits = UART_DATA_8_BITS, - .parity = UART_PARITY_DISABLE, - .stop_bits = UART_STOP_BITS_1, - .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, - .rx_flow_ctrl_thresh = 122, - }; - //Set UART parameters - uart_param_config(uart_num, &uart_config); - //Set UART log level -// esp_log_level_set(TAG, ESP_LOG_INFO); - //Set UART pins,(-1: default pin, no change.) - //For UART0, we can just use the default pins. - //uart_set_pin(uart_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); - //Install UART driver( We don't need an event queue here) - //We don't even use a buffer for sending data. - uart_driver_install(uart_num, BUF_SIZE * 2, 0, 0, NULL, 0); -} - void init_io(int argc, char **argv, cell *up) { key_is_avail = 0; - uart_on(); + init_uart(); } int caccept(char *addr, cell count, cell *up) diff --git a/src/app/esp32/targets.mk b/src/app/esp32/targets.mk index 110178d7..2b6c49f0 100644 --- a/src/app/esp32/targets.mk +++ b/src/app/esp32/targets.mk @@ -26,12 +26,6 @@ VPATH += $(SRC)/lib VPATH += $(APPPATH) INCS += -I$(APPPATH) -INCS += -I$(IDF_TEMPLATE_PATH)/build/include -INCS += -I$(IDF_PATH)/components/freertos/include -INCS += -I$(IDF_PATH)/components/esp32/include -INCS += -I$(IDF_PATH)/components/driver/include -INCS += -I$(IDF_PATH)/components/nvs_flash/include - include $(SRC)/common.mk include $(SRC)/cforth/targets.mk diff --git a/src/app/esp32/textend.c b/src/app/esp32/textend.c index 9f572f58..4f632f6d 100644 --- a/src/app/esp32/textend.c +++ b/src/app/esp32/textend.c @@ -2,7 +2,6 @@ // See "ccalls" below. #include "forth.h" -#include "freertos/FreeRTOS.h" extern cell *callback_up; @@ -18,12 +17,20 @@ cell build_date_adr(void) return (cell)build_date; } -void ms(cell msecs) -{ - vTaskDelay(msecs/ portTICK_PERIOD_MS); -} +// Many of the routines cited below are defined either directly +// in the ESP32 SDK or in sdk_build/main/interface.c . It is best +// to avoid putting the definition of ESP-specific routines in +// this file, because doing so typically requires including a +// lot of .h files here, and that introduces a dependency on the +// SDK configurator, which greatly complicates the Makefile +// dependencies for the CForth portion of the build. We avoid that +// by just referring to the names of the routines we want to include +// in the ccalls[] table, with fake call signatures "void xxx(void)". +// sdk_build/main/interface.c is compiled after the SDK configurator +// has run, so it can include whatever it needs. extern void software_reset(void); +extern void ms(void); cell ((* const ccalls[])()) = { C(build_date_adr) //c 'build-date { -- a.value } diff --git a/src/app/esp32/tmain.c b/src/app/esp32/tmain.c index a30b0555..76843d75 100644 --- a/src/app/esp32/tmain.c +++ b/src/app/esp32/tmain.c @@ -1,15 +1,10 @@ // Top-level routine for starting Forth #include "forth.h" -#include "esp_system.h" -#include "nvs_flash.h" - // Defines startup routine for nodemcu-firmware -void app_main(void) +void forth(void) { - nvs_flash_init(); - cell *up; init_io(0, (char **)0, (cell *)up); // Perform platform-specific initialization up = (void *)init_forth();