Skip to content

Commit

Permalink
ESP32 - no more SDK include dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchBradley committed Jan 4, 2017
1 parent 1df1902 commit b3c5689
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 48 deletions.
4 changes: 1 addition & 3 deletions build/esp32/Makefile
Expand Up @@ -4,21 +4,19 @@ default: forth.elf

TOPDIR=../..

# CONFIG += -DBITS32 -DT16
CONFIG += -DBITS32

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

Expand Down
2 changes: 1 addition & 1 deletion build/esp32/sdk_build/main/component.mk
Expand Up @@ -3,4 +3,4 @@

# COMPONENT_OBJS := ../../../app.o main.o

COMPONENT_OBJS := ../../../app.o
COMPONENT_OBJS := ../../../app.o interface.o
60 changes: 60 additions & 0 deletions 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);
}
32 changes: 5 additions & 27 deletions src/app/esp32/consio.c
Expand Up @@ -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); }
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 0 additions & 6 deletions src/app/esp32/targets.mk
Expand Up @@ -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

Expand Down
17 changes: 12 additions & 5 deletions src/app/esp32/textend.c
Expand Up @@ -2,7 +2,6 @@
// See "ccalls" below.

#include "forth.h"
#include "freertos/FreeRTOS.h"

extern cell *callback_up;

Expand All @@ -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 }
Expand Down
7 changes: 1 addition & 6 deletions 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();
Expand Down

0 comments on commit b3c5689

Please sign in to comment.