From 7b74cddf2d13a1eec642e15ba9304c1a01888586 Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Wed, 5 Jul 2023 22:58:03 -0700 Subject: [PATCH] More module decomposition. Needs much more on device testing. --- Makefile | 74 +++++++-- common/forth_namespace_tests.fs | 9 -- esp32/bindings.fs | 66 -------- esp32/builtins.h | 151 ++++-------------- esp32/optional/README-optional.txt | 1 + esp32/{ => optional/camera}/camera.fs | 0 esp32/optional/camera/camera.h | 35 ++++ esp32/{ => optional/camera}/camera_server.fs | 0 esp32/optional/oled/oled.fs | 37 +++++ esp32/optional/{ => oled}/oled.h | 17 +- .../{ => optional/serial-bluetooth}/bterm.fs | 0 .../serial-bluetooth/serial-bluetooth.fs | 23 +++ .../serial-bluetooth/serial-bluetooth.h | 51 ++++++ esp32/optional/spi-flash/spi-flash.fs | 45 ++++++ esp32/optional/spi-flash/spi-flash.h | 81 ++++++++++ esp32/optionals.fs | 16 ++ esp32/options.h | 25 +-- esp32/print-builtins.cpp | 12 +- esp32/sim_main.cpp | 8 +- 19 files changed, 420 insertions(+), 231 deletions(-) rename esp32/{ => optional/camera}/camera.fs (100%) create mode 100644 esp32/optional/camera/camera.h rename esp32/{ => optional/camera}/camera_server.fs (100%) create mode 100644 esp32/optional/oled/oled.fs rename esp32/optional/{ => oled}/oled.h (88%) rename esp32/{ => optional/serial-bluetooth}/bterm.fs (100%) create mode 100644 esp32/optional/serial-bluetooth/serial-bluetooth.fs create mode 100644 esp32/optional/serial-bluetooth/serial-bluetooth.h create mode 100644 esp32/optional/spi-flash/spi-flash.fs create mode 100644 esp32/optional/spi-flash/spi-flash.h diff --git a/Makefile b/Makefile index 0e4966e..c113a88 100644 --- a/Makefile +++ b/Makefile @@ -278,8 +278,7 @@ ESP32_BOOT = $(COMMON_PHASE1) \ esp32/platform.fs \ posix/httpd.fs posix/web_interface.fs esp32/web_interface.fs \ esp32/registers.fs esp32/timers.fs \ - esp32/bterm.fs posix/telnetd.fs \ - esp32/camera.fs esp32/camera_server.fs \ + posix/telnetd.fs \ esp32/optionals.fs \ esp32/autoboot.fs common/fini.fs $(GEN)/esp32_boot.h: tools/source_to_string.js $(ESP32_BOOT) | $(GEN) @@ -298,19 +297,40 @@ $(GEN)/esp32_riscv-assembler.h: \ $< riscv_assembler_source $(VERSION) $(REVISION) \ esp32/optional/assemblers/riscv-assembler.fs >$@ +$(GEN)/esp32_camera.h: \ + tools/source_to_string.js esp32/optional/camera/camera.fs | $(GEN) + $< camera_source $(VERSION) $(REVISION) \ + esp32/optional/camera/camera.fs \ + esp32/optional/camera/camera_server.fs >$@ + +$(GEN)/esp32_oled.h: \ + tools/source_to_string.js esp32/optional/oled/oled.fs | $(GEN) + $< camera_source $(VERSION) $(REVISION) \ + esp32/optional/oled/oled.fs >$@ + +$(GEN)/esp32_spi-flash.h: \ + tools/source_to_string.js esp32/optional/spi-flash/spi-flash.fs | $(GEN) + $< spi_flash_source $(VERSION) $(REVISION) \ + esp32/optional/spi-flash/spi-flash.fs >$@ + +$(GEN)/esp32_serial-bluetooth.h: \ + tools/source_to_string.js esp32/optional/serial-bluetooth/bterm.fs | $(GEN) + $< bterm_source $(VERSION) $(REVISION) \ + esp32/optional/serial-bluetooth/bterm.fs >$@ + OPTIONAL_MODULES = \ + $(ESP32)/ESP32forth/assemblers.h \ + $(ESP32)/ESP32forth/camera.h \ $(ESP32)/ESP32forth/oled.h \ - $(ESP32)/ESP32forth/assemblers.h + $(ESP32)/ESP32forth/serial-bluetooth.h \ + $(ESP32)/ESP32forth/spi-flash.h add-optional: $(OPTIONAL_MODULES) drop-optional: rm -f $(OPTIONAL_MODULES) -$(ESP32)/ESP32forth/assemblers.h: $(ESP32)/ESP32forth/optional/assemblers.h - cp $< $@ - -$(ESP32)/ESP32forth/oled.h: $(ESP32)/ESP32forth/optional/oled.h +$(ESP32)/ESP32forth/%.h: $(ESP32)/ESP32forth/optional/%.h cp $< $@ $(GEN)/dump_web_opcodes: \ @@ -595,11 +615,40 @@ $(ESP32)/ESP32forth/optional/assemblers.h: \ riscv_assembler=@$(GEN)/esp32_riscv-assembler.h \ >$@ -$(ESP32)/ESP32forth/optional/oled.h: \ - esp32/optional/oled.h | $(ESP32)/ESP32forth/optional - cat esp32/optional/oled.h | tools/replace.js \ +$(ESP32)/ESP32forth/optional/camera.h: \ + esp32/optional/camera/camera.h \ + $(GEN)/esp32_camera.h | $(ESP32)/ESP32forth/optional + cat esp32/optional/camera/camera.h | tools/replace.js \ VERSION=$(VERSION) \ REVISION=$(REVISION) \ + camera=@$(GEN)/esp32_camera.h \ + >$@ + +$(ESP32)/ESP32forth/optional/oled.h: \ + esp32/optional/oled/oled.h \ + $(GEN)/esp32_oled.h | $(ESP32)/ESP32forth/optional + cat esp32/optional/oled/oled.h | tools/replace.js \ + VERSION=$(VERSION) \ + REVISION=$(REVISION) \ + oled=@$(GEN)/esp32_oled.h \ + >$@ + +$(ESP32)/ESP32forth/optional/serial-bluetooth.h: \ + esp32/optional/serial-bluetooth/serial-bluetooth.h \ + $(GEN)/esp32_serial-bluetooth.h | $(ESP32)/ESP32forth/optional + cat esp32/optional/serial-bluetooth/serial-bluetooth.h | tools/replace.js \ + VERSION=$(VERSION) \ + REVISION=$(REVISION) \ + serial_bluetooth=@$(GEN)/esp32_serial-bluetooth.h \ + >$@ + +$(ESP32)/ESP32forth/optional/spi-flash.h: \ + esp32/optional/spi-flash/spi-flash.h \ + $(GEN)/esp32_spi-flash.h | $(ESP32)/ESP32forth/optional + cat esp32/optional/spi-flash/spi-flash.h | tools/replace.js \ + VERSION=$(VERSION) \ + REVISION=$(REVISION) \ + spi_flash=@$(GEN)/esp32_spi-flash.h \ >$@ # ---- ESP32 ARDUINO BUILD AND FLASH ---- @@ -697,7 +746,10 @@ $(ESP32)/ESP32forth.zip: \ $(ESP32)/ESP32forth/README.txt \ $(ESP32)/ESP32forth/optional/README-optional.txt \ $(ESP32)/ESP32forth/optional/assemblers.h \ - $(ESP32)/ESP32forth/optional/oled.h + $(ESP32)/ESP32forth/optional/camera.h \ + $(ESP32)/ESP32forth/optional/oled.h \ + $(ESP32)/ESP32forth/optional/serial-bluetooth.h \ + $(ESP32)/ESP32forth/optional/spi-flash.h cd $(ESP32) && rm -f ESP32forth.zip && zip -r ESP32forth.zip ESP32forth # ---- Publish to Archive ---- diff --git a/common/forth_namespace_tests.fs b/common/forth_namespace_tests.fs index 733b684..8e17dde 100644 --- a/common/forth_namespace_tests.fs +++ b/common/forth_namespace_tests.fs @@ -622,8 +622,6 @@ e: test-esp32-forth-voclist out: editor out: streams out: tasks - out: oled - out: bluetooth out: rtos out: rmt out: interrupts @@ -631,7 +629,6 @@ e: test-esp32-forth-voclist out: Serial out: ledc out: SPIFFS - out: spi_flash out: SD_MMC out: SD out: WiFi @@ -685,8 +682,6 @@ e: check-esp32-builtins ;e e: check-esp32-bindings - out: oled - out: bluetooth out: rtos out: rmt out: interrupts @@ -694,7 +689,6 @@ e: check-esp32-bindings out: Serial out: ledc out: SPIFFS - out: spi_flash out: SD_MMC out: SD out: WiFi @@ -706,10 +700,7 @@ e: check-esp32-bindings e: test-esp32-forth-namespace ' forth list-from out: FORTH - out: camera-server - out: camera out: telnetd - out: bterm out: timers out: registers out: webui diff --git a/esp32/bindings.fs b/esp32/bindings.fs index 980fc12..0817d5d 100644 --- a/esp32/bindings.fs +++ b/esp32/bindings.fs @@ -43,38 +43,6 @@ vocabulary SD_MMC SD_MMC definitions transfer SD_MMC-builtins forth definitions -vocabulary spi_flash spi_flash definitions -transfer spi_flash-builtins -DEFINED? spi_flash_init [IF] -0 constant SPI_PARTITION_TYPE_APP -1 constant SPI_PARTITION_TYPE_DATA -$ff constant SPI_PARTITION_SUBTYPE_ANY - -also structures -struct esp_partition_t - ( Work around changing struct layout ) - esp_partition_t_size 40 >= [IF] - ptr field p>gap - [THEN] - ptr field p>type - ptr field p>subtype - ptr field p>address - ptr field p>size - ptr field p>label - -: p. ( part -- ) - base @ >r >r decimal - ." TYPE: " r@ p>type @ . ." SUBTYPE: " r@ p>subtype @ . - ." ADDR: " r@ hex p>address @ . ." SIZE: " r@ p>size @ . - ." LABEL: " r> p>label @ z>s type cr r> base ! ; -: list-partition-type ( type -- ) - SPI_PARTITION_SUBTYPE_ANY 0 esp_partition_find - begin dup esp_partition_get p. esp_partition_next dup 0= until drop ; -: list-partitions SPI_PARTITION_TYPE_APP list-partition-type - SPI_PARTITION_TYPE_DATA list-partition-type ; -[THEN] -only forth definitions - vocabulary SPIFFS SPIFFS definitions transfer SPIFFS-builtins forth definitions @@ -143,40 +111,6 @@ vocabulary rtos rtos definitions transfer rtos-builtins forth definitions -DEFINED? SerialBT.new [IF] - vocabulary bluetooth bluetooth definitions - transfer bluetooth-builtins - forth definitions -[ELSE] - internals definitions - transfer bluetooth-builtins - forth definitions -[THEN] - -vocabulary oled oled definitions -transfer oled-builtins -DEFINED? OledNew [IF] -128 constant WIDTH -64 constant HEIGHT --1 constant OledReset -0 constant BLACK -1 constant WHITE -1 constant SSD1306_EXTERNALVCC -2 constant SSD1306_SWITCHCAPVCC -: OledInit - OledAddr @ 0= if - WIDTH HEIGHT OledReset OledNew - SSD1306_SWITCHCAPVCC $3C OledBegin drop - then - OledCLS - 2 OledTextsize ( Draw 2x Scale Text ) - WHITE OledTextc ( Draw white text ) - 0 0 OledSetCursor ( Start at top-left corner ) - z" *Esp32forth*" OledPrintln OledDisplay -; -[THEN] -forth definitions - internals definitions ( Heap Capabilities ) 1 0 lshift constant MALLOC_CAP_EXEC diff --git a/esp32/builtins.h b/esp32/builtins.h index 8f88a89..287e733 100644 --- a/esp32/builtins.h +++ b/esp32/builtins.h @@ -29,27 +29,52 @@ # define USER_WORDS # endif -// Hook to pull in words from optional assemblers.h +// Hook to pull in words from optional assemblers. # if __has_include("assemblers.h") # include "assemblers.h" # else # define OPTIONAL_ASSEMBLERS_SUPPORT # endif -// Hook to pull in words from optional oled.h +// Hook to pull in words from optional Oled support. # if __has_include("oled.h") # include "oled.h" # else +# define OPTIONAL_OLED_VOCABULARY # define OPTIONAL_OLED_SUPPORT # endif +// Hook to pull in words from optional ESP32-CAM camera support. +# if __has_include("camera.h") +# include "camera.h" +# else +# define OPTIONAL_CAMERA_VOCABULARY +# define OPTIONAL_CAMERA_SUPPORT +# endif + +// Hook to pull in words from optional serial bluetooth support. +# if __has_include("serial-bluetooth.h") +# include "bluetooth-serial.h" +# else +# define OPTIONAL_SERIAL_BLUETOOTH_SUPPORT +# define OPTIONAL_BLUETOOTH_VOCABULARY +# endif + +// Hook to pull in words from optional SPI flash support. +# if __has_include("spi-flash.h") +# include "spi-flash.h" +# else +# define OPTIONAL_SPI_FLASH_SUPPORT +# define OPTIONAL_SPI_FLASH_VOCABULARY +# endif + static cell_t ResizeFile(cell_t fd, cell_t size); #endif #define PLATFORM_OPCODE_LIST \ USER_WORDS \ - OPTIONAL_ASSEMBLERS_SUPPORT \ + EXTERNAL_OPTIONAL_MODULE_SUPPORT \ REQUIRED_PLATFORM_SUPPORT \ REQUIRED_ESP_SUPPORT \ REQUIRED_MEMORY_SUPPORT \ @@ -66,17 +91,20 @@ static cell_t ResizeFile(cell_t fd, cell_t size); OPTIONAL_SD_SUPPORT \ OPTIONAL_SD_MMC_SUPPORT \ OPTIONAL_I2C_SUPPORT \ - OPTIONAL_SERIAL_BLUETOOTH_SUPPORT \ - OPTIONAL_CAMERA_SUPPORT \ OPTIONAL_SOCKETS_SUPPORT \ OPTIONAL_FREERTOS_SUPPORT \ OPTIONAL_INTERRUPTS_SUPPORT \ OPTIONAL_RMT_SUPPORT \ - OPTIONAL_OLED_SUPPORT \ - OPTIONAL_SPI_FLASH_SUPPORT \ CALLING_OPCODE_LIST \ FLOATING_POINT_LIST +#define EXTERNAL_OPTIONAL_MODULE_SUPPORT \ + OPTIONAL_ASSEMBLERS_SUPPORT \ + OPTIONAL_CAMERA_SUPPORT \ + OPTIONAL_SPI_FLASH_SUPPORT \ + OPTIONAL_SERIAL_BLUETOOTH_SUPPORT \ + OPTIONAL_OLED_SUPPORT \ + #define REQUIRED_MEMORY_SUPPORT \ YV(internals, MALLOC, SET malloc(n0)) \ YV(internals, SYSFREE, free(a0); DROP) \ @@ -211,68 +239,6 @@ static cell_t ResizeFile(cell_t fd, cell_t size); Y(dacWrite, dacWrite(n1, n0); DROPn(2)) #endif -#ifndef ENABLE_SPI_FLASH_SUPPORT -# define OPTIONAL_SPI_FLASH_SUPPORT -#else -# ifndef SIM_PRINT_ONLY -# include "esp_spi_flash.h" -# include "esp_partition.h" -# endif -# define OPTIONAL_SPI_FLASH_SUPPORT \ - YV(spi_flash, spi_flash_init, spi_flash_init()) \ - YV(spi_flash, spi_flash_get_chip_size, PUSH spi_flash_get_chip_size()) \ - YV(spi_flash, spi_flash_erase_sector, n0 = spi_flash_erase_sector(n0)) \ - YV(spi_flash, spi_flash_erase_range, n0 = spi_flash_erase_range(n1, n0); DROP) \ - YV(spi_flash, spi_flash_write, n0 = spi_flash_write(n2, a1, n0); NIPn(2)) \ - YV(spi_flash, spi_flash_write_encrypted, n0 = spi_flash_write_encrypted(n2, a1, n0); NIPn(2)) \ - YV(spi_flash, spi_flash_read, n0 = spi_flash_read(n2, a1, n0); NIPn(2)) \ - YV(spi_flash, spi_flash_read_encrypted, n0 = spi_flash_read_encrypted(n2, a1, n0); NIPn(2)) \ - YV(spi_flash, spi_flash_mmap, \ - n0 = spi_flash_mmap(n4, n3, (spi_flash_mmap_memory_t) n2, \ - (const void **) a1, (spi_flash_mmap_handle_t *) a0); NIPn(4)) \ - YV(spi_flash, spi_flash_mmap_pages, \ - n0 = spi_flash_mmap_pages((const int *) a4, n3, (spi_flash_mmap_memory_t) n2, \ - (const void **) a1, (spi_flash_mmap_handle_t *) a0); NIPn(4)) \ - YV(spi_flash, spi_flash_munmap, spi_flash_munmap((spi_flash_mmap_handle_t) a0); DROP) \ - YV(spi_flash, spi_flash_mmap_dump, spi_flash_mmap_dump()) \ - YV(spi_flash, spi_flash_mmap_get_free_pages, \ - n0 = spi_flash_mmap_get_free_pages((spi_flash_mmap_memory_t) n0)) \ - YV(spi_flash, spi_flash_cache2phys, n0 = spi_flash_cache2phys(a0)) \ - YV(spi_flash, spi_flash_phys2cache, \ - n0 = (cell_t) spi_flash_phys2cache(n1, (spi_flash_mmap_memory_t) n0); NIP) \ - YV(spi_flash, spi_flash_cache_enabled, PUSH spi_flash_cache_enabled()) \ - YV(spi_flash, esp_partition_find, \ - n0 = (cell_t) esp_partition_find((esp_partition_type_t) n2, \ - (esp_partition_subtype_t) n1, c0); NIPn(2)) \ - YV(spi_flash, esp_partition_find_first, \ - n0 = (cell_t) esp_partition_find_first((esp_partition_type_t) n2, \ - (esp_partition_subtype_t) n1, c0); NIPn(2)) \ - YV(spi_flash, esp_partition_t_size, PUSH sizeof(esp_partition_t)) \ - YV(spi_flash, esp_partition_get, \ - n0 = (cell_t) esp_partition_get((esp_partition_iterator_t) a0)) \ - YV(spi_flash, esp_partition_next, \ - n0 = (cell_t) esp_partition_next((esp_partition_iterator_t) a0)) \ - YV(spi_flash, esp_partition_iterator_release, \ - esp_partition_iterator_release((esp_partition_iterator_t) a0); DROP) \ - YV(spi_flash, esp_partition_verify, n0 = (cell_t) esp_partition_verify((esp_partition_t *) a0)) \ - YV(spi_flash, esp_partition_read, \ - n0 = esp_partition_read((const esp_partition_t *) a3, n2, a1, n0); NIPn(3)) \ - YV(spi_flash, esp_partition_write, \ - n0 = esp_partition_write((const esp_partition_t *) a3, n2, a1, n0); NIPn(3)) \ - YV(spi_flash, esp_partition_erase_range, \ - n0 = esp_partition_erase_range((const esp_partition_t *) a2, n1, n0); NIPn(2)) \ - YV(spi_flash, esp_partition_mmap, \ - n0 = esp_partition_mmap((const esp_partition_t *) a5, n4, n3, \ - (spi_flash_mmap_memory_t) n2, \ - (const void **) a1, \ - (spi_flash_mmap_handle_t *) a0); NIPn(5)) \ - YV(spi_flash, esp_partition_get_sha256, \ - n0 = esp_partition_get_sha256((const esp_partition_t *) a1, b0); NIP) \ - YV(spi_flash, esp_partition_check_identity, \ - n0 = esp_partition_check_identity((const esp_partition_t *) a1, \ - (const esp_partition_t *) a0); NIP) -#endif - #ifndef ENABLE_SPIFFS_SUPPORT // Provide a default failing SPIFFS.begin # define OPTIONAL_SPIFFS_SUPPORT \ @@ -443,22 +409,6 @@ static void TimerInitNull(cell_t group, cell_t timer); YV(rmt, rmt_write_sample, n0 = rmt_write_sample((rmt_channel_t) n3, b2, n1, n0); NIPn(3)) #endif -#ifndef ENABLE_CAMERA_SUPPORT -# define OPTIONAL_CAMERA_SUPPORT -#else -# ifndef SIM_PRINT_ONLY -# include "esp_camera.h" -# endif -# define OPTIONAL_CAMERA_SUPPORT \ - YV(camera, esp_camera_init, n0 = esp_camera_init((camera_config_t *) a0)) \ - YV(camera, esp_camera_deinit, PUSH esp_camera_deinit()) \ - YV(camera, esp_camera_fb_get, PUSH esp_camera_fb_get()) \ - YV(camera, esp_camera_fb_return, esp_camera_fb_return((camera_fb_t *) a0); DROP) \ - YV(camera, esp_camera_sensor_get, PUSH esp_camera_sensor_get()) \ - YV(camera, esp_camera_save_to_nvs, n0 = esp_camera_save_to_nvs(c0)) \ - YV(camera, esp_camera_load_from_nvs, n0 = esp_camera_load_from_nvs(c0)) -#endif - #ifndef ENABLE_SOCKETS_SUPPORT # define OPTIONAL_SOCKETS_SUPPORT #else @@ -548,37 +498,6 @@ static void TimerInitNull(cell_t group, cell_t timer); XV(Wire, "Wire.flush", WIRE_FLUSH, Wire.flush()) #endif -#ifndef ENABLE_SERIAL_BLUETOOTH_SUPPORT -# define OPTIONAL_SERIAL_BLUETOOTH_SUPPORT -#else -# ifndef SIM_PRINT_ONLY -# include "esp_bt_device.h" -# include "BluetoothSerial.h" -# define bt0 ((BluetoothSerial *) a0) -# endif -# define OPTIONAL_SERIAL_BLUETOOTH_SUPPORT \ - XV(bluetooth, "SerialBT.new", SERIALBT_NEW, PUSH new BluetoothSerial()) \ - XV(bluetooth, "SerialBT.delete", SERIALBT_DELETE, delete bt0; DROP) \ - XV(bluetooth, "SerialBT.begin", SERIALBT_BEGIN, n0 = bt0->begin(c2, n1); NIPn(2)) \ - XV(bluetooth, "SerialBT.end", SERIALBT_END, bt0->end(); DROP) \ - XV(bluetooth, "SerialBT.available", SERIALBT_AVAILABLE, n0 = bt0->available()) \ - XV(bluetooth, "SerialBT.readBytes", SERIALBT_READ_BYTES, n0 = bt0->readBytes(b2, n1); NIPn(2)) \ - XV(bluetooth, "SerialBT.write", SERIALBT_WRITE, n0 = bt0->write(b2, n1); NIPn(2)) \ - XV(bluetooth, "SerialBT.flush", SERIALBT_FLUSH, bt0->flush(); DROP) \ - XV(bluetooth, "SerialBT.hasClient", SERIALBT_HAS_CLIENT, n0 = bt0->hasClient()) \ - XV(bluetooth, "SerialBT.enableSSP", SERIALBT_ENABLE_SSP, bt0->enableSSP(); DROP) \ - XV(bluetooth, "SerialBT.setPin", SERIALBT_SET_PIN, n0 = bt0->setPin(c1); NIP) \ - XV(bluetooth, "SerialBT.unpairDevice", SERIALBT_UNPAIR_DEVICE, \ - n0 = bt0->unpairDevice(b1); NIP) \ - XV(bluetooth, "SerialBT.connect", SERIALBT_CONNECT, n0 = bt0->connect(c1); NIP) \ - XV(bluetooth, "SerialBT.connectAddr", SERIALBT_CONNECT_ADDR, n0 = bt0->connect(b1); NIP) \ - XV(bluetooth, "SerialBT.disconnect", SERIALBT_DISCONNECT, n0 = bt0->disconnect()) \ - XV(bluetooth, "SerialBT.connected", SERIALBT_CONNECTED, n0 = bt0->connected(n1); NIP) \ - XV(bluetooth, "SerialBT.isReady", SERIALBT_IS_READY, n0 = bt0->isReady(n2, n1); NIPn(2)) \ - /* Bluetooth */ \ - YV(bluetooth, esp_bt_dev_get_address, PUSH esp_bt_dev_get_address()) -#endif - #ifndef ENABLE_WIFI_SUPPORT # define OPTIONAL_WIFI_SUPPORT #else diff --git a/esp32/optional/README-optional.txt b/esp32/optional/README-optional.txt index 2d69e4e..c5aa71e 100644 --- a/esp32/optional/README-optional.txt +++ b/esp32/optional/README-optional.txt @@ -10,6 +10,7 @@ into the parent directory, next to the ESPforth.ino file. These are the current optional modules: * assemblers.h - Assemblers for ESP32 Xtensa and ESP32 RISC-V + * camera.h - Support for the ESP32-CAM camera * oled.h - Support for the SSD1306 Oled Initially ESP32forth focused on a minimal C kernel, with most functionality diff --git a/esp32/camera.fs b/esp32/optional/camera/camera.fs similarity index 100% rename from esp32/camera.fs rename to esp32/optional/camera/camera.fs diff --git a/esp32/optional/camera/camera.h b/esp32/optional/camera/camera.h new file mode 100644 index 0000000..5b6581a --- /dev/null +++ b/esp32/optional/camera/camera.h @@ -0,0 +1,35 @@ +// Copyright 2023 Bradley D. Nelson +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/* + * ESP32forth ESP32-CAM Camera v{{VERSION}} + * Revision: {{REVISION}} + */ + +#ifndef SIM_PRINT_ONLY +# include "esp_camera.h" +#endif +#define OPTIONAL_CAMERA_VOCABULARY V(camera) +#define OPTIONAL_CAMERA_SUPPORT \ + XV(internals, "camera-source", CAMERA_SOURCE, \ + PUSH camera_source; PUSH sizeof(camera_source) - 1) \ + YV(camera, esp_camera_init, n0 = esp_camera_init((camera_config_t *) a0)) \ + YV(camera, esp_camera_deinit, PUSH esp_camera_deinit()) \ + YV(camera, esp_camera_fb_get, PUSH esp_camera_fb_get()) \ + YV(camera, esp_camera_fb_return, esp_camera_fb_return((camera_fb_t *) a0); DROP) \ + YV(camera, esp_camera_sensor_get, PUSH esp_camera_sensor_get()) \ + YV(camera, esp_camera_save_to_nvs, n0 = esp_camera_save_to_nvs(c0)) \ + YV(camera, esp_camera_load_from_nvs, n0 = esp_camera_load_from_nvs(c0)) + +{{camera}} diff --git a/esp32/camera_server.fs b/esp32/optional/camera/camera_server.fs similarity index 100% rename from esp32/camera_server.fs rename to esp32/optional/camera/camera_server.fs diff --git a/esp32/optional/oled/oled.fs b/esp32/optional/oled/oled.fs new file mode 100644 index 0000000..0f35210 --- /dev/null +++ b/esp32/optional/oled/oled.fs @@ -0,0 +1,37 @@ +\ Copyright 2023 Bradley D. Nelson +\ +\ Licensed under the Apache License, Version 2.0 (the "License"); +\ you may not use this file except in compliance with the License. +\ You may obtain a copy of the License at +\ +\ http://www.apache.org/licenses/LICENSE-2.0 +\ +\ Unless required by applicable law or agreed to in writing, software +\ distributed under the License is distributed on an "AS IS" BASIS, +\ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +\ See the License for the specific language governing permissions and +\ limitations under the License. + +vocabulary oled oled definitions +transfer oled-builtins +DEFINED? OledNew [IF] +128 constant WIDTH +64 constant HEIGHT +-1 constant OledReset +0 constant BLACK +1 constant WHITE +1 constant SSD1306_EXTERNALVCC +2 constant SSD1306_SWITCHCAPVCC +: OledInit + OledAddr @ 0= if + WIDTH HEIGHT OledReset OledNew + SSD1306_SWITCHCAPVCC $3C OledBegin drop + then + OledCLS + 2 OledTextsize ( Draw 2x Scale Text ) + WHITE OledTextc ( Draw white text ) + 0 0 OledSetCursor ( Start at top-left corner ) + z" *Esp32forth*" OledPrintln OledDisplay +; +[THEN] +forth definitions diff --git a/esp32/optional/oled.h b/esp32/optional/oled/oled.h similarity index 88% rename from esp32/optional/oled.h rename to esp32/optional/oled/oled.h index 05a21ec..ad9d7b3 100644 --- a/esp32/optional/oled.h +++ b/esp32/optional/oled/oled.h @@ -13,7 +13,7 @@ // limitations under the License. /* - * ESP32forth Assemblers v{{VERSION}} + * ESP32forth Oled v{{VERSION}} * Revision: {{REVISION}} */ @@ -22,12 +22,15 @@ // Adafruit GFX Library // Adafruit BusIO -# ifndef SIM_PRINT_ONLY -# include -# include +#ifndef SIM_PRINT_ONLY +# include +# include static Adafruit_SSD1306 *oled_display = 0; -# endif -# define OPTIONAL_OLED_SUPPORT \ +#endif +#define OPTIONAL_OLED_VOCABULARY V(oled) +#define OPTIONAL_OLED_SUPPORT \ + XV(internals, "oled-source", OLED_SOURCE, \ + PUSH oled_source; PUSH sizeof(oled_source) - 1) \ YV(oled, OledAddr, PUSH &oled_display) \ YV(oled, OledNew, oled_display = new Adafruit_SSD1306(n2, n1, &Wire, n0); DROPn(3)) \ YV(oled, OledDelete, delete oled_display) \ @@ -51,3 +54,5 @@ static Adafruit_SSD1306 *oled_display = 0; YV(oled, OledRectF, oled_display->fillRect(n4, n3, n2, n1, n0); DROPn(3)) \ YV(oled, OledRectR, oled_display->drawRoundRect(n5, n4, n3, n2, n1, n0); DROPn(5)) \ YV(oled, OledRectRF, oled_display->fillRoundRect(n5, n4, n3, n2, n1, n0 ); DROPn(5)) + +{{oled}} diff --git a/esp32/bterm.fs b/esp32/optional/serial-bluetooth/bterm.fs similarity index 100% rename from esp32/bterm.fs rename to esp32/optional/serial-bluetooth/bterm.fs diff --git a/esp32/optional/serial-bluetooth/serial-bluetooth.fs b/esp32/optional/serial-bluetooth/serial-bluetooth.fs new file mode 100644 index 0000000..c032367 --- /dev/null +++ b/esp32/optional/serial-bluetooth/serial-bluetooth.fs @@ -0,0 +1,23 @@ +\ Copyright 2023 Bradley D. Nelson +\ +\ Licensed under the Apache License, Version 2.0 (the "License"); +\ you may not use this file except in compliance with the License. +\ You may obtain a copy of the License at +\ +\ http://www.apache.org/licenses/LICENSE-2.0 +\ +\ Unless required by applicable law or agreed to in writing, software +\ distributed under the License is distributed on an "AS IS" BASIS, +\ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +\ See the License for the specific language governing permissions and +\ limitations under the License. + +DEFINED? SerialBT.new [IF] + vocabulary bluetooth bluetooth definitions + transfer bluetooth-builtins + forth definitions +[ELSE] + internals definitions + transfer bluetooth-builtins + forth definitions +[THEN] diff --git a/esp32/optional/serial-bluetooth/serial-bluetooth.h b/esp32/optional/serial-bluetooth/serial-bluetooth.h new file mode 100644 index 0000000..c876cc3 --- /dev/null +++ b/esp32/optional/serial-bluetooth/serial-bluetooth.h @@ -0,0 +1,51 @@ +// Copyright 2022 Bradley D. Nelson +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/* + * ESP32forth Serial Bluetooth v{{VERSION}} + * Revision: {{REVISION}} + */ + +#ifndef SIM_PRINT_ONLY +# include "esp_bt_device.h" +# include "BluetoothSerial.h" +# define bt0 ((BluetoothSerial *) a0) +#endif +#define OPTIONAL_BLUETOOTH_VOCABULARY V(bluetooth) +#define OPTIONAL_SERIAL_BLUETOOTH_SUPPORT \ + XV(internals, "bterm-source", BTERM_SOURCE, \ + PUSH bterm_source; PUSH sizeof(bterm_source) - 1) \ + XV(bluetooth, "SerialBT.new", SERIALBT_NEW, PUSH new BluetoothSerial()) \ + XV(bluetooth, "SerialBT.delete", SERIALBT_DELETE, delete bt0; DROP) \ + XV(bluetooth, "SerialBT.begin", SERIALBT_BEGIN, n0 = bt0->begin(c2, n1); NIPn(2)) \ + XV(bluetooth, "SerialBT.end", SERIALBT_END, bt0->end(); DROP) \ + XV(bluetooth, "SerialBT.available", SERIALBT_AVAILABLE, n0 = bt0->available()) \ + XV(bluetooth, "SerialBT.readBytes", SERIALBT_READ_BYTES, n0 = bt0->readBytes(b2, n1); NIPn(2)) \ + XV(bluetooth, "SerialBT.write", SERIALBT_WRITE, n0 = bt0->write(b2, n1); NIPn(2)) \ + XV(bluetooth, "SerialBT.flush", SERIALBT_FLUSH, bt0->flush(); DROP) \ + XV(bluetooth, "SerialBT.hasClient", SERIALBT_HAS_CLIENT, n0 = bt0->hasClient()) \ + XV(bluetooth, "SerialBT.enableSSP", SERIALBT_ENABLE_SSP, bt0->enableSSP(); DROP) \ + XV(bluetooth, "SerialBT.setPin", SERIALBT_SET_PIN, n0 = bt0->setPin(c1); NIP) \ + XV(bluetooth, "SerialBT.unpairDevice", SERIALBT_UNPAIR_DEVICE, \ + n0 = bt0->unpairDevice(b1); NIP) \ + XV(bluetooth, "SerialBT.connect", SERIALBT_CONNECT, n0 = bt0->connect(c1); NIP) \ + XV(bluetooth, "SerialBT.connectAddr", SERIALBT_CONNECT_ADDR, n0 = bt0->connect(b1); NIP) \ + XV(bluetooth, "SerialBT.disconnect", SERIALBT_DISCONNECT, n0 = bt0->disconnect()) \ + XV(bluetooth, "SerialBT.connected", SERIALBT_CONNECTED, n0 = bt0->connected(n1); NIP) \ + XV(bluetooth, "SerialBT.isReady", SERIALBT_IS_READY, n0 = bt0->isReady(n2, n1); NIPn(2)) \ + /* Bluetooth */ \ + YV(bluetooth, esp_bt_dev_get_address, PUSH esp_bt_dev_get_address()) +#endif + +{{serial_bluetooth}} diff --git a/esp32/optional/spi-flash/spi-flash.fs b/esp32/optional/spi-flash/spi-flash.fs new file mode 100644 index 0000000..8ab61b8 --- /dev/null +++ b/esp32/optional/spi-flash/spi-flash.fs @@ -0,0 +1,45 @@ +\ Copyright 2023 Bradley D. Nelson +\ +\ Licensed under the Apache License, Version 2.0 (the "License"); +\ you may not use this file except in compliance with the License. +\ You may obtain a copy of the License at +\ +\ http://www.apache.org/licenses/LICENSE-2.0 +\ +\ Unless required by applicable law or agreed to in writing, software +\ distributed under the License is distributed on an "AS IS" BASIS, +\ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +\ See the License for the specific language governing permissions and +\ limitations under the License. + +vocabulary spi_flash spi_flash definitions +transfer spi_flash-builtins +DEFINED? spi_flash_init [IF] +0 constant SPI_PARTITION_TYPE_APP +1 constant SPI_PARTITION_TYPE_DATA +$ff constant SPI_PARTITION_SUBTYPE_ANY + +also structures +struct esp_partition_t + ( Work around changing struct layout ) + esp_partition_t_size 40 >= [IF] + ptr field p>gap + [THEN] + ptr field p>type + ptr field p>subtype + ptr field p>address + ptr field p>size + ptr field p>label + +: p. ( part -- ) + base @ >r >r decimal + ." TYPE: " r@ p>type @ . ." SUBTYPE: " r@ p>subtype @ . + ." ADDR: " r@ hex p>address @ . ." SIZE: " r@ p>size @ . + ." LABEL: " r> p>label @ z>s type cr r> base ! ; +: list-partition-type ( type -- ) + SPI_PARTITION_SUBTYPE_ANY 0 esp_partition_find + begin dup esp_partition_get p. esp_partition_next dup 0= until drop ; +: list-partitions SPI_PARTITION_TYPE_APP list-partition-type + SPI_PARTITION_TYPE_DATA list-partition-type ; +[THEN] +only forth definitions diff --git a/esp32/optional/spi-flash/spi-flash.h b/esp32/optional/spi-flash/spi-flash.h new file mode 100644 index 0000000..3412d0e --- /dev/null +++ b/esp32/optional/spi-flash/spi-flash.h @@ -0,0 +1,81 @@ +// Copyright 2023 Bradley D. Nelson +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/* + * ESP32forth SPI Flash v{{VERSION}} + * Revision: {{REVISION}} + */ + +#ifndef SIM_PRINT_ONLY +# include "esp_spi_flash.h" +# include "esp_partition.h" +#endif +#define OPTIONAL_SPI_FLASH_VOCABULARY V(spi_flash) +#define OPTIONAL_SPI_FLASH_SUPPORT \ + XV(internals, "spi_flash-source", SPI_FLASH_SOURCE, \ + PUSH spi_flash_source; PUSH sizeof(spi_flash_source) - 1) \ + YV(spi_flash, spi_flash_init, spi_flash_init()) \ + YV(spi_flash, spi_flash_get_chip_size, PUSH spi_flash_get_chip_size()) \ + YV(spi_flash, spi_flash_erase_sector, n0 = spi_flash_erase_sector(n0)) \ + YV(spi_flash, spi_flash_erase_range, n0 = spi_flash_erase_range(n1, n0); DROP) \ + YV(spi_flash, spi_flash_write, n0 = spi_flash_write(n2, a1, n0); NIPn(2)) \ + YV(spi_flash, spi_flash_write_encrypted, n0 = spi_flash_write_encrypted(n2, a1, n0); NIPn(2)) \ + YV(spi_flash, spi_flash_read, n0 = spi_flash_read(n2, a1, n0); NIPn(2)) \ + YV(spi_flash, spi_flash_read_encrypted, n0 = spi_flash_read_encrypted(n2, a1, n0); NIPn(2)) \ + YV(spi_flash, spi_flash_mmap, \ + n0 = spi_flash_mmap(n4, n3, (spi_flash_mmap_memory_t) n2, \ + (const void **) a1, (spi_flash_mmap_handle_t *) a0); NIPn(4)) \ + YV(spi_flash, spi_flash_mmap_pages, \ + n0 = spi_flash_mmap_pages((const int *) a4, n3, (spi_flash_mmap_memory_t) n2, \ + (const void **) a1, (spi_flash_mmap_handle_t *) a0); NIPn(4)) \ + YV(spi_flash, spi_flash_munmap, spi_flash_munmap((spi_flash_mmap_handle_t) a0); DROP) \ + YV(spi_flash, spi_flash_mmap_dump, spi_flash_mmap_dump()) \ + YV(spi_flash, spi_flash_mmap_get_free_pages, \ + n0 = spi_flash_mmap_get_free_pages((spi_flash_mmap_memory_t) n0)) \ + YV(spi_flash, spi_flash_cache2phys, n0 = spi_flash_cache2phys(a0)) \ + YV(spi_flash, spi_flash_phys2cache, \ + n0 = (cell_t) spi_flash_phys2cache(n1, (spi_flash_mmap_memory_t) n0); NIP) \ + YV(spi_flash, spi_flash_cache_enabled, PUSH spi_flash_cache_enabled()) \ + YV(spi_flash, esp_partition_find, \ + n0 = (cell_t) esp_partition_find((esp_partition_type_t) n2, \ + (esp_partition_subtype_t) n1, c0); NIPn(2)) \ + YV(spi_flash, esp_partition_find_first, \ + n0 = (cell_t) esp_partition_find_first((esp_partition_type_t) n2, \ + (esp_partition_subtype_t) n1, c0); NIPn(2)) \ + YV(spi_flash, esp_partition_t_size, PUSH sizeof(esp_partition_t)) \ + YV(spi_flash, esp_partition_get, \ + n0 = (cell_t) esp_partition_get((esp_partition_iterator_t) a0)) \ + YV(spi_flash, esp_partition_next, \ + n0 = (cell_t) esp_partition_next((esp_partition_iterator_t) a0)) \ + YV(spi_flash, esp_partition_iterator_release, \ + esp_partition_iterator_release((esp_partition_iterator_t) a0); DROP) \ + YV(spi_flash, esp_partition_verify, n0 = (cell_t) esp_partition_verify((esp_partition_t *) a0)) \ + YV(spi_flash, esp_partition_read, \ + n0 = esp_partition_read((const esp_partition_t *) a3, n2, a1, n0); NIPn(3)) \ + YV(spi_flash, esp_partition_write, \ + n0 = esp_partition_write((const esp_partition_t *) a3, n2, a1, n0); NIPn(3)) \ + YV(spi_flash, esp_partition_erase_range, \ + n0 = esp_partition_erase_range((const esp_partition_t *) a2, n1, n0); NIPn(2)) \ + YV(spi_flash, esp_partition_mmap, \ + n0 = esp_partition_mmap((const esp_partition_t *) a5, n4, n3, \ + (spi_flash_mmap_memory_t) n2, \ + (const void **) a1, \ + (spi_flash_mmap_handle_t *) a0); NIPn(5)) \ + YV(spi_flash, esp_partition_get_sha256, \ + n0 = esp_partition_get_sha256((const esp_partition_t *) a1, b0); NIP) \ + YV(spi_flash, esp_partition_check_identity, \ + n0 = esp_partition_check_identity((const esp_partition_t *) a1, \ + (const esp_partition_t *) a0); NIP) + +{{spi_flash}} diff --git a/esp32/optionals.fs b/esp32/optionals.fs index e532e31..2ffb45b 100644 --- a/esp32/optionals.fs +++ b/esp32/optionals.fs @@ -21,3 +21,19 @@ internals DEFINED? xtensa-assembler-source [IF] internals DEFINED? riscv-assembler-source [IF] riscv-assembler-source evaluate [THEN] forth + +internals DEFINED? camera-source [IF] + camera-source evaluate +[THEN] forth + +internals DEFINED? oled-source [IF] + oled-source evaluate +[THEN] forth + +internals DEFINED? serial-bluetooth-source [IF] + serial-bluetooth-source evaluate +[THEN] forth + +internals DEFINED? spi-flash-source [IF] + spi-flash-source evaluate +[THEN] forth diff --git a/esp32/options.h b/esp32/options.h index 47d4bb7..cf76b74 100644 --- a/esp32/options.h +++ b/esp32/options.h @@ -26,7 +26,6 @@ #define ENABLE_INTERRUPTS_SUPPORT #define ENABLE_LEDC_SUPPORT #define ENABLE_SD_SUPPORT -#define ENABLE_SPI_FLASH_SUPPORT #define ENABLE_ESP32_FORTH_FAULT_HANDLING // SD_MMC does not work on ESP32-S2 / ESP32-C3 @@ -57,22 +56,6 @@ #if !defined(CONFIG_IDF_TARGET_ESP32C3) #endif -// For now assume only boards with PSRAM should enable -// camera support and BluetoothSerial. -// ESP32-CAM always have PSRAM, but so do WROVER boards, -// so this isn't an ideal indicator. -// Also limiting to ESP32 classic only, as these can't be ESP32-CAM. -// Some boards (e.g. ESP32-S2-WROVER) don't seem to have -// built the serial library, so check if its enabled as well. -#if (defined(CONFIG_IDF_TARGET_ESP32) && defined(BOARD_HAS_PSRAM)) || defined(SIM_PRINT_ONLY) -# define ENABLE_CAMERA_SUPPORT -# if (defined(CONFIG_BT_ENABLED) && \ - defined(CONFIG_BLUEDROID_ENABLED)) || \ - defined(SIM_PRINT_ONLY) -# define ENABLE_SERIAL_BLUETOOTH_SUPPORT -# endif -#endif - #if !defined(USER_VOCABULARIES) # define USER_VOCABULARIES #endif @@ -116,6 +99,10 @@ #define VOCABULARY_LIST \ V(forth) V(internals) \ 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) \ + V(ledc) V(Wire) V(WiFi) V(sockets) \ + V(rmt) V(interrupts) V(timers) \ + OPTIONAL_CAMERA_VOCABULARY \ + OPTIONAL_BLUETOOTH_VOCABULARY \ + OPTIONAL_OLED_VOCABULARY \ + OPTIONAL_SPI_FLASH_VOCABULARY \ USER_VOCABULARIES diff --git a/esp32/print-builtins.cpp b/esp32/print-builtins.cpp index 6292597..a73b875 100644 --- a/esp32/print-builtins.cpp +++ b/esp32/print-builtins.cpp @@ -17,14 +17,24 @@ #include #define SIM_PRINT_ONLY -#define ENABLE_OLED_SUPPORT + #include "esp32/platform.h" #include "esp32/options.h" #define CALLING_OPCODE_LIST #define FLOATING_POINT_LIST #define USER_WORDS + #define OPTIONAL_ASSEMBLERS_SUPPORT #define OPTIONAL_OLED_SUPPORT +#define OPTIONAL_CAMERA_SUPPORT +#define OPTIONAL_SERIAL_BLUETOOTH_SUPPORT +#define OPTIONAL_SPI_FLASH_SUPPORT + +#define OPTIONAL_OLED_VOCABULARY +#define OPTIONAL_CAMERA_VOCABULARY +#define OPTIONAL_BLUETOOTH_VOCABULARY +#define OPTIONAL_SPI_FLASH_VOCABULARY + #include "builtins.h" #define XV(flags, name, op, code) Z(flags, name, op, code) diff --git a/esp32/sim_main.cpp b/esp32/sim_main.cpp index 8845a69..dc486d6 100644 --- a/esp32/sim_main.cpp +++ b/esp32/sim_main.cpp @@ -22,6 +22,11 @@ #define SIM_HEAP_SIZE (100 * 1024 + 1024 * 1024) +#define OPTIONAL_OLED_VOCABULARY +#define OPTIONAL_CAMERA_VOCABULARY +#define OPTIONAL_BLUETOOTH_VOCABULARY +#define OPTIONAL_SPI_FLASH_VOCABULARY + static cell_t *simulated(cell_t *sp, const char *op); #define PLATFORM_OPCODE_LIST \ @@ -157,9 +162,6 @@ static cell_t *simulated(cell_t *sp, const char *op) { } else if (op == STR_getMaxAllocHeap) { *++sp = 80 * 1024; return sp; - } else if (op == STR_esp_partition_t_size) { - *++sp = 64; - return sp; } else if (op == STR_IS_XTENSA) { *++sp = -1; return sp;