From c2f2329411d67b1fd6252dc3c436135dbd13eefc Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Sun, 6 Feb 2022 19:23:47 -0800 Subject: [PATCH] Added ESP words. --- ueforth/common/core.h | 5 +++-- ueforth/common/forth_namespace_tests.fs | 2 ++ ueforth/esp32/bindings.fs | 4 ++++ ueforth/esp32/builtins.h | 12 ++++++++++++ ueforth/esp32/config.h | 1 - ueforth/esp32/main.cpp | 5 +++-- ueforth/esp32/options.h | 2 +- ueforth/esp32/sim_main.cpp | 5 ++++- ueforth/posix/main.c | 2 +- ueforth/windows/main.c | 2 +- 10 files changed, 31 insertions(+), 9 deletions(-) diff --git a/ueforth/common/core.h b/ueforth/common/core.h index 3257d5e..ece7a6b 100644 --- a/ueforth/common/core.h +++ b/ueforth/common/core.h @@ -235,10 +235,11 @@ static cell_t *evaluate1(cell_t *sp, float **fp) { static cell_t *forth_run(cell_t *initrp); -static void forth_init(int argc, char *argv[], void *heap, +static void forth_init(int argc, char *argv[], + void *heap, cell_t heap_size, const char *src, cell_t src_len) { g_sys.heap_start = (cell_t *) heap; - g_sys.heap_size = HEAP_SIZE; + g_sys.heap_size = heap_size; g_sys.stack_cells = STACK_CELLS; g_sys.boot = src; g_sys.boot_size = src_len; diff --git a/ueforth/common/forth_namespace_tests.fs b/ueforth/common/forth_namespace_tests.fs index a9f29be..c5def76 100644 --- a/ueforth/common/forth_namespace_tests.fs +++ b/ueforth/common/forth_namespace_tests.fs @@ -547,6 +547,7 @@ e: test-esp32-forth-namespace out: SD out: WiFi out: Wire + out: ESP out: editor out: streams out: tasks @@ -610,6 +611,7 @@ e: test-esp32-forth-namespace out: SD out: WiFi out: Wire + out: ESP out: ok check-esp32-basics out: default-key? diff --git a/ueforth/esp32/bindings.fs b/ueforth/esp32/bindings.fs index bc65ce1..9c3d627 100644 --- a/ueforth/esp32/bindings.fs +++ b/ueforth/esp32/bindings.fs @@ -14,6 +14,10 @@ ( Migrate various words to separate vocabularies, and constants ) +vocabulary ESP ESP definitions +transfer ESP-builtins +forth definitions + vocabulary Wire Wire definitions transfer wire-builtins forth definitions diff --git a/ueforth/esp32/builtins.h b/ueforth/esp32/builtins.h index 7d0dffd..ffb751d 100644 --- a/ueforth/esp32/builtins.h +++ b/ueforth/esp32/builtins.h @@ -34,6 +34,7 @@ static cell_t ResizeFile(cell_t fd, cell_t size); #define PLATFORM_OPCODE_LIST \ USER_WORDS \ + REQUIRED_ESP_SUPPORT \ REQUIRED_MEMORY_SUPPORT \ REQUIRED_SERIAL_SUPPORT \ REQUIRED_ARDUINO_GPIO_SUPPORT \ @@ -66,6 +67,17 @@ static cell_t ResizeFile(cell_t fd, cell_t size); YV(internals, heap_caps_realloc, \ tos = (cell_t) heap_caps_realloc(a2, n1, n0); NIPn(2)) +#define REQUIRED_ESP_SUPPORT \ + YV(ESP, getHeapSize, PUSH ESP.getHeapSize()) \ + YV(ESP, getFreeHeap, PUSH ESP.getFreeHeap()) \ + YV(ESP, getMaxAllocHeap, PUSH ESP.getMaxAllocHeap()) \ + YV(ESP, getChipModel, PUSH ESP.getChipModel()) \ + YV(ESP, getChipCores, PUSH ESP.getChipCores()) \ + YV(ESP, getFlashChipSize, PUSH ESP.getFlashChipSize()) \ + YV(ESP, getCPUFreqMHz, PUSH ESP.getCPUFreqMHz()) \ + YV(ESP, getSketchSize, PUSH ESP.getSketchSize()) \ + YV(ESP, deepSleep, ESP.deepSleep(tos); DROP) + #define REQUIRED_SYSTEM_SUPPORT \ X("MS-TICKS", MS_TICKS, PUSH millis()) \ XV(internals, "RAW-YIELD", RAW_YIELD, yield()) \ diff --git a/ueforth/esp32/config.h b/ueforth/esp32/config.h index 4ea139b..bb6f8d9 100644 --- a/ueforth/esp32/config.h +++ b/ueforth/esp32/config.h @@ -12,6 +12,5 @@ // See the License for the specific language governing permissions and // limitations under the License. -#define HEAP_SIZE (100 * 1024) #define STACK_CELLS 512 #define INTERRUPT_STACK_CELLS 64 diff --git a/ueforth/esp32/main.cpp b/ueforth/esp32/main.cpp index c981c25..bde5894 100644 --- a/ueforth/esp32/main.cpp +++ b/ueforth/esp32/main.cpp @@ -13,8 +13,9 @@ // limitations under the License. void setup() { - cell_t *heap = (cell_t *) malloc(HEAP_SIZE); - forth_init(0, 0, heap, boot, sizeof(boot)); + cell_t hs = heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL); + cell_t *heap = (cell_t *) malloc(hs); + forth_init(0, 0, heap, hs, boot, sizeof(boot)); } void loop() { diff --git a/ueforth/esp32/options.h b/ueforth/esp32/options.h index e275428..5fd5f76 100644 --- a/ueforth/esp32/options.h +++ b/ueforth/esp32/options.h @@ -67,6 +67,6 @@ #define VOCABULARY_LIST \ V(forth) V(internals) \ - V(rtos) V(SPIFFS) V(serial) V(SD) V(SD_MMC) \ + V(rtos) V(SPIFFS) V(serial) V(SD) V(SD_MMC) V(ESP) \ V(ledc) V(Wire) V(WiFi) V(bluetooth) V(sockets) V(oled) \ V(rmt) V(interrupts) V(spi_flash) V(camera) V(timers) diff --git a/ueforth/esp32/sim_main.cpp b/ueforth/esp32/sim_main.cpp index 79ae5ef..e13d682 100644 --- a/ueforth/esp32/sim_main.cpp +++ b/ueforth/esp32/sim_main.cpp @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#define HEAP_SIZE (100 * 1024 + 1024 * 1024) +#define SIM_HEAP_SIZE (100 * 1024 + 1024 * 1024) #define STACK_CELLS 512 #include "esp32/options.h" @@ -33,6 +33,9 @@ static cell_t *simulated(cell_t *sp, const char *op); PLATFORM_SIMULATED_OPCODE_LIST #undef XV +#define MALLOC_CAP_INTERNAL SIM_HEAP_SIZE +#define heap_caps_get_largest_free_block(x) (x) + #include "common/core.h" #include "common/interp.h" #include "gen/esp32_boot.h" diff --git a/ueforth/posix/main.c b/ueforth/posix/main.c index c3b12f2..9800788 100644 --- a/ueforth/posix/main.c +++ b/ueforth/posix/main.c @@ -40,7 +40,7 @@ int main(int argc, char *argv[]) { void *heap = mmap( (void *) 0x8000000, HEAP_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - forth_init(argc, argv, heap, boot, sizeof(boot)); + forth_init(argc, argv, heap, HEAP_SIZE, boot, sizeof(boot)); for (;;) { g_sys.rp = forth_run(g_sys.rp); } return 1; } diff --git a/ueforth/windows/main.c b/ueforth/windows/main.c index 39b1995..e995420 100644 --- a/ueforth/windows/main.c +++ b/ueforth/windows/main.c @@ -59,7 +59,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmd, int show) { void *heap = VirtualAlloc( (void *) 0x8000000, HEAP_SIZE, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE); - forth_init(0, 0, heap, boot, sizeof(boot)); + forth_init(0, 0, heap, HEAP_SIZE, boot, sizeof(boot)); for (;;) { g_sys.rp = forth_run(g_sys.rp); } }