From 87b403f10d59e9cda2c7e13d5efc5d7a250deb4f Mon Sep 17 00:00:00 2001 From: Christian Baars Date: Sun, 30 Oct 2022 18:40:32 +0100 Subject: [PATCH 1/3] support RISCV ULP for ESP32S2 and ESP32S3 --- .../xdrv_52_3_berry_ulp.ino | 58 +++++++++++++++---- 1 file changed, 46 insertions(+), 12 deletions(-) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_ulp.ino b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_ulp.ino index 4dbc24b3c..9d154a8da 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_ulp.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_ulp.ino @@ -1,5 +1,5 @@ /* - xdrv_52_3_berry_ulp.ino - Berry scripting language, ULP support for ESP32 + xdrv_52_3_berry_ulp.ino - Berry scripting language, ULP support for ESP32, ESP32S2, ESP32S3 Copyright (C) 2021 Stephan Hadinger & Christian Baars, Berry language by Guan Wenliang https://github.com/Skiars/berry @@ -21,9 +21,21 @@ #ifdef USE_BERRY_ULP #include -#if defined(USE_BERRY_ULP) && defined(CONFIG_IDF_TARGET_ESP32) +#if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3) +#if defined(CONFIG_IDF_TARGET_ESP32) #include "esp32/ulp.h" +#endif // esp32 +#if defined(CONFIG_IDF_TARGET_ESP32S2) +#include "esp32s2/ulp.h" +#include "esp32s2/ulp_riscv.h" +#include "esp32s2/ulp_riscv_adc.h" +#endif // s2 +#if defined(CONFIG_IDF_TARGET_ESP32S3) +#include "esp32s3/ulp.h" +#include "esp32s3/ulp_riscv.h" +#include "esp32s3/ulp_riscv_adc.h" +#endif //s3 #include "driver/rtc_io.h" #include "driver/gpio.h" #include "driver/adc.h" @@ -36,7 +48,11 @@ extern "C" { // // `ULP.run() -> nil` void be_ULP_run(int32_t entry) { +#if defined(CONFIG_IDF_TARGET_ESP32) ulp_run(entry); // entry point should be at the beginning of program +#else // S2 or S3 + ulp_riscv_run(); +#endif } // `ULP.wake_period(period_index:int, period_us:int) -> nil` @@ -52,7 +68,11 @@ extern "C" { // `ULP.get_mem(position:int) -> int` int32_t be_ULP_get_mem(int32_t pos) { - return RTC_SLOW_MEM[pos] & 0xFFFF; // only low 16 bits are used +#if defined(CONFIG_IDF_TARGET_ESP32) + return RTC_SLOW_MEM[pos] & 0xFFFF; // only low 16 bits are used +#else + return RTC_SLOW_MEM[pos]; // full 32bit for RISCV ULP +#endif } // `ULP.gpio_init(pin:int, mode:int) -> rtc_pin:int` @@ -70,25 +90,40 @@ extern "C" { // `ULP.adc_config(channel:int, attenuation:int, width:int) -> nil` // // enums: channel 0-7, attenuation 0-3, width 0-3 - void be_ULP_adc_config(struct bvm *vm, adc1_channel_t channel, adc_atten_t attenuation, adc_bits_width_t width) { - esp_err_t err = adc1_config_channel_atten(channel, attenuation); - err += adc1_config_width(width); + void be_ULP_adc_config(struct bvm *vm, int32_t channel, int32_t attenuation, int32_t width) { +#if defined(CONFIG_IDF_TARGET_ESP32) + esp_err_t err = adc1_config_channel_atten((adc1_channel_t)channel, (adc_atten_t)attenuation); + err += adc1_config_width((adc_bits_width_t)width); if (err != ESP_OK) { be_raisef(vm, "ulp_adc_config_error", "ULP: invalid code err=%i", err); } else { adc1_ulp_enable(); } +#else // S2 or S3 + ulp_riscv_adc_cfg_t cfg = { + .channel = (adc_channel_t)channel, + .atten = (adc_atten_t)attenuation, + .width = (adc_bits_width_t)width + }; + esp_err_t err = ulp_riscv_adc_init(&cfg); + if (err != ESP_OK) { + be_raisef(vm, "ulp_adc_config_error", "ULP: invalid code err=%i", err); + } +#endif } /** * @brief Load a Berry byte buffer containing a ULP program as raw byte data * * @param vm as `ULP.load(code:bytes) -> nil` - * @return void + * @return void for ESP32 or binary type as int32_t on RISCV capable SOC's */ void be_ULP_load(struct bvm *vm, const uint8_t *buf, size_t size) { - // AddLog(LOG_LEVEL_INFO, "ULP: load addr=%p size=%i %*_H", buf, size/4, size, buf); - esp_err_t err = ulp_load_binary(0, buf, size / 4); +#if defined(CONFIG_IDF_TARGET_ESP32) + esp_err_t err = ulp_load_binary(0, buf, size / 4); // FSM type only, specific header, size in long words +#else // S2 or S3 + esp_err_t err = ulp_riscv_load_binary(buf, size); // there are no header bytes, just load and hope for a valid binary - size in bytes +#endif // defined(CONFIG_IDF_TARGET_ESP32) if (err != ESP_OK) { be_raisef(vm, "ulp_load_error", "ULP: invalid code err=%i", err); } @@ -109,10 +144,9 @@ extern "C" { esp_deep_sleep_start(); } - } //extern "C" -#endif //CONFIG_IDF_TARGET_ESP32 +#endif //CONFIG_IDF_TARGET_ESP32 .. S2 .. S3 #endif // USE_BERRY_ULP -#endif // USE_BERRY \ No newline at end of file +#endif // USE_BERRY From ed6c21ad806f9cdb43e25d719b75906402b9feaa Mon Sep 17 00:00:00 2001 From: Christian Baars Date: Sun, 30 Oct 2022 18:42:47 +0100 Subject: [PATCH 2/3] allow ULP not only for ESP32 --- lib/libesp32/berry/default/be_modtab.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/libesp32/berry/default/be_modtab.c b/lib/libesp32/berry/default/be_modtab.c index c02e2ca6d..c4a2147d1 100644 --- a/lib/libesp32/berry/default/be_modtab.c +++ b/lib/libesp32/berry/default/be_modtab.c @@ -166,7 +166,7 @@ BERRY_LOCAL const bntvmodule* const be_module_table[] = { #ifdef USE_ALEXA_AVS &be_native_module(crypto), #endif -#if defined(USE_BERRY_ULP) && defined(CONFIG_IDF_TARGET_ESP32) +#if defined(USE_BERRY_ULP) &be_native_module(ULP), #endif // USE_BERRY_ULP #if defined(USE_MI_ESP32) && !defined(USE_BLE_ESP32) From 0b063f62dfa505f67dfb12ca68cc1ac409710803 Mon Sep 17 00:00:00 2001 From: Christian Baars Date: Sun, 30 Oct 2022 18:43:37 +0100 Subject: [PATCH 3/3] allow ULP on ESP32, S2 and S3 --- lib/libesp32/berry_tasmota/src/be_ULP_lib.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/libesp32/berry_tasmota/src/be_ULP_lib.c b/lib/libesp32/berry_tasmota/src/be_ULP_lib.c index 7edf43206..a5fe0e417 100644 --- a/lib/libesp32/berry_tasmota/src/be_ULP_lib.c +++ b/lib/libesp32/berry_tasmota/src/be_ULP_lib.c @@ -6,9 +6,9 @@ #include "be_constobj.h" #include "be_mapping.h" -#if defined(USE_BERRY_ULP) && defined(CONFIG_IDF_TARGET_ESP32) +#if defined(USE_BERRY_ULP) && (defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3)) -#include "esp32/ulp.h" +// #include "esp32/ulp.h" #include "driver/rtc_io.h" #include "driver/gpio.h" #include "driver/adc.h" @@ -37,8 +37,6 @@ BE_FUNC_CTYPE_DECLARE(be_ULP_sleep, "", "[i]"); // optional int arg extern void be_ULP_load(struct bvm *vm, const uint8_t *buf, size_t size); BE_FUNC_CTYPE_DECLARE(be_ULP_load, "", "@(bytes)~"); // pass: 1/ vm, 2/ bytes point, 3/ bytes size -#include "be_fixed_ULP.h" - /* @const_object_info_begin module ULP (scope: global) { run, ctype_func(be_ULP_run) @@ -51,5 +49,6 @@ module ULP (scope: global) { adc_config, ctype_func(be_ULP_adc_config) } @const_object_info_end */ +#include "be_fixed_ULP.h" -#endif // USE_BERRY_ULP \ No newline at end of file +#endif // USE_BERRY_ULP