From de39be5392f3686f8f2422eddb79352a0f3455b8 Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Sun, 21 Jul 2024 17:41:19 -0700 Subject: [PATCH] Add arduino-esp32 3.x compatibility. --- common/forth_namespace_tests.fs | 3 --- esp32/builtins.h | 24 ++++++++++++++++++++---- esp32/options.h | 8 +++++++- esp32/platform.fs | 3 --- site/ESP32forth.html | 14 ++++++++++++++ 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/common/forth_namespace_tests.fs b/common/forth_namespace_tests.fs index efd6312..33eb44f 100644 --- a/common/forth_namespace_tests.fs +++ b/common/forth_namespace_tests.fs @@ -656,9 +656,6 @@ e: check-esp32-platform out: INPUT out: HIGH out: LOW - out: tone - out: freq - out: duty out: adc out: pin out: default-key? diff --git a/esp32/builtins.h b/esp32/builtins.h index 74b37a6..69496d1 100644 --- a/esp32/builtins.h +++ b/esp32/builtins.h @@ -99,7 +99,8 @@ static cell_t ResizeFile(cell_t fd, cell_t size); REQUIRED_ARDUINO_GPIO_SUPPORT \ REQUIRED_SYSTEM_SUPPORT \ REQUIRED_FILES_SUPPORT \ - OPTIONAL_LEDC_SUPPORT \ + OPTIONAL_LEDC_V2_SUPPORT \ + OPTIONAL_LEDC_V3_SUPPORT \ OPTIONAL_DAC_SUPPORT \ OPTIONAL_SPIFFS_SUPPORT \ OPTIONAL_WIFI_SUPPORT \ @@ -231,10 +232,10 @@ static cell_t ResizeFile(cell_t fd, cell_t size); YV(internals, READDIR, \ struct dirent *ent = readdir((DIR *) n0); SET (ent ? ent->d_name: 0)) -#ifndef ENABLE_LEDC_SUPPORT -# define OPTIONAL_LEDC_SUPPORT +#ifndef ENABLE_LEDC_V2_SUPPORT +# define OPTIONAL_LEDC_V2_SUPPORT #else -# define OPTIONAL_LEDC_SUPPORT \ +# define OPTIONAL_LEDC_V2_SUPPORT \ YV(ledc, ledcSetup, \ n0 = (cell_t) (1000000 * ledcSetup(n2, n1 / 1000.0, n0)); NIPn(2)) \ YV(ledc, ledcAttachPin, ledcAttachPin(n1, n0); DROPn(2)) \ @@ -248,6 +249,21 @@ static cell_t ResizeFile(cell_t fd, cell_t size); tos = (cell_t) (1000000 * ledcWriteNote(n2, (note_t) n1, n0)); NIPn(2)) #endif +#ifndef ENABLE_LEDC_V3_SUPPORT +# define OPTIONAL_LEDC_V3_SUPPORT +#else +# define OPTIONAL_LEDC_V3_SUPPORT \ + YV(ledc, ledcAttach, n0 = ledcAttach(n2, n1, n0); NIPn(2)) \ + YV(ledc, ledcAttachChannel, n0 = ledcAttachChannel(n3, n2, n1, n0); NIPn(3)) \ + YV(ledc, ledcDetach, n0 = ledcDetach(n0)) \ + YV(ledc, ledcRead, n0 = ledcRead(n0)) \ + YV(ledc, ledcReadFreq, n0 = ledcReadFreq(n0)) \ + YV(ledc, ledcWrite, ledcWrite(n1, n0); DROPn(2)) \ + YV(ledc, ledcWriteTone, n0 = ledcWriteTone(n1, n0); NIP) \ + YV(ledc, ledcWriteNote, n0 = ledcWriteNote(n2, (note_t) n1, n0); NIPn(2)) \ + YV(ledc, ledcChangeFrequency, n0 = ledcChangeFrequency(n2, n1, n0); NIPn(2)) +#endif + #ifndef ENABLE_DAC_SUPPORT # define OPTIONAL_DAC_SUPPORT # else diff --git a/esp32/options.h b/esp32/options.h index 6250c6e..1b16f6e 100644 --- a/esp32/options.h +++ b/esp32/options.h @@ -22,10 +22,16 @@ #define ENABLE_I2C_SUPPORT #define ENABLE_SOCKETS_SUPPORT #define ENABLE_FREERTOS_SUPPORT -#define ENABLE_LEDC_SUPPORT #define ENABLE_SD_SUPPORT #define ENABLE_ESP32_FORTH_FAULT_HANDLING +// LEDC changed interface in v3.x+ +#if ESP_ARDUINO_VERSION_MAJOR >= 3 +# define ENABLE_LEDC_V3_SUPPORT +#else +# define ENABLE_LEDC_V2_SUPPORT +#endif + // SD_MMC does not work on ESP32-S2 / ESP32-C3 #if !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32C3) # define ENABLE_SD_MMC_SUPPORT diff --git a/esp32/platform.fs b/esp32/platform.fs index 1c155eb..3a0f4b8 100644 --- a/esp32/platform.fs +++ b/esp32/platform.fs @@ -40,9 +40,6 @@ also ledc also serial also SPIFFS ( Map Arduino / ESP32 things to shorter names. ) : pin ( n pin# -- ) swap digitalWrite ; : adc ( n -- n ) analogRead ; -: duty ( n n -- ) 255 min 8191 255 */ ledcWrite ; -: freq ( n n -- ) 1000 * 13 ledcSetup drop ; -: tone ( n n -- ) 1000 * ledcWriteTone drop ; ( Basic Ardiuno Constants ) 0 constant LOW diff --git a/site/ESP32forth.html b/site/ESP32forth.html index 507ef53..d231313 100644 --- a/site/ESP32forth.html +++ b/site/ESP32forth.html @@ -407,6 +407,20 @@ dacWrite ( pin 0-255 -- ) Write to DAC (pin 25, 26)
ledc
These words are inside the ledc vocabulary.
+(arduino-esp32 3.x+)
+ledcAttach ( pin freq resolution -- f )
+ledcAttachChannel ( pin freq resolution channel -- f )
+ledcDetach ( pin -- f )
+ledcRead ( channel -- n )
+ledcReadFreq ( channel -- freq )
+ledcWrite ( channel duty -- )
+ledcWriteTone ( channel freq -- freq )
+ledcWriteNote ( channel note octave -- freq )
+ledcChangeFrequency ( pin freq resolution -- freq )
+
+If built with arduino-esp32 2.x, this is the interface: +
+(arduino-esp32 2.x+)
 ledcSetup ( channel freq resolution -- freq )
 ledcAttachPin ( pin channel -- )
 ledcDetachPin ( pin -- )