From 4b370ec4d45f9cc4937882c47ad44dd6c3ff8f19 Mon Sep 17 00:00:00 2001 From: mjaspers2mtu <44484497+mjaspers2mtu@users.noreply.github.com> Date: Wed, 11 Jun 2025 10:26:02 +0200 Subject: [PATCH 1/3] Add LED Fade Example An example that uses ULP self-modifying code to create a PWM-like LED dimming effect on GPIO 15. --- examples/fade_s2.py | 82 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 examples/fade_s2.py diff --git a/examples/fade_s2.py b/examples/fade_s2.py new file mode 100644 index 0000000..8b9ca71 --- /dev/null +++ b/examples/fade_s2.py @@ -0,0 +1,82 @@ +# +# This file is part of the micropython-esp32-ulp project, +# https://github.com/micropython/micropython-esp32-ulp +# +# SPDX-FileCopyrightText: 2018-2023, the micropython-esp32-ulp authors, see AUTHORS file. +# SPDX-License-Identifier: MIT + +""" +Example for: ESP32-S2 Wemos mini development board V1.0.0 with led on pin 15 + +This example creates a PWM-like dimming effect using self-modifying ULP code. +The ULP program rewrites the `WAIT` instructions to control on/off LED durations, +simulating a variable duty cycle. + +Note: +The `WAIT` instruction uses an immediate operand (fixed value) for delay cycles. However, we can change the lower half of memory +to modify these values at runtime, simulating variable wait times via registers. +""" + +from esp32 import ULP +from machine import mem32 +from esp32_ulp import src_to_binary +from time import sleep + +source = """\ +# constants from: +# https://github.com/espressif/esp-idf/blob/v5.0.2/components/soc/esp32s2/include/soc/reg_base.h +#define DR_REG_RTCIO_BASE 0x3f408400 + +# constants from: +# https://github.com/espressif/esp-idf/blob/v5.0.2/components/soc/esp32s2/include/soc/rtc_io_reg.h +#define RTC_IO_XTAL_32P_PAD_REG (DR_REG_RTCIO_BASE + 0xC0) +#define RTC_IO_X32P_MUX_SEL_M (BIT(19)) +#define RTC_GPIO_OUT_REG (DR_REG_RTCIO_BASE + 0x0) +#define RTC_GPIO_ENABLE_REG (DR_REG_RTCIO_BASE + 0xc) +#define RTC_GPIO_ENABLE_S 10 +#define RTC_GPIO_OUT_DATA_S 10 + +# constants from: +# https://github.com/espressif/esp-idf/blob/v5.0.2/components/soc/esp32s2/include/soc/rtc_io_channel.h +#define RTCIO_GPIO15_CHANNEL 15 + +.global entry +program_init: + # connect GPIO to ULP (0: GPIO connected to digital GPIO module, 1: GPIO connected to analog RTC module) + WRITE_RTC_REG(RTC_IO_XTAL_32P_PAD_REG, RTC_IO_X32P_MUX_SEL_M, 1, 1); + + # enable GPIO as output, not input (this also enables a pull-down by default) + WRITE_RTC_REG(RTC_GPIO_ENABLE_REG, RTC_GPIO_ENABLE_S + RTCIO_GPIO15_CHANNEL, 1, 1) + +set_waits: add r0, r0, 0xFF # Increase r0 (delay time) + move r3, wait_off + st r0, r3, 0 # Overwrite wait_off with new delay value + + move r2, 0xFFFF + sub r1, r2, r0 # Calculate complementary delay time + move r3, wait_on + st r1, r3, 0 # Overwrite wait_on with new value + + WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S + RTCIO_GPIO15_CHANNEL, 1, 0) # turn off led +wait_off: wait 0 # Placeholder; value overwritten dynamically + WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S + RTCIO_GPIO15_CHANNEL, 1, 1) # turn on led +wait_on: wait 0 # Placeholder; value overwritten dynamically + +jump set_waits # Loop program + +""" + +binary = src_to_binary(source, cpu="esp32s2") # cpu is esp32 or esp32s2 + +load_addr, entry_addr = 0, 0 + +ULP_MEM_BASE = 0x50000000 + +ulp = ULP() +ulp.load_binary(load_addr, binary) + +ulp.run(entry_addr) + +while True: + print(hex(mem32[ULP_MEM_BASE + 40])) # show that the WAIT cycles are changing + sleep(0.5) From 277067c9c98d75f80843d37430375cf5d8126c9b Mon Sep 17 00:00:00 2001 From: mjaspers2mtu <44484497+mjaspers2mtu@users.noreply.github.com> Date: Sun, 15 Jun 2025 16:34:46 +0200 Subject: [PATCH 2/3] Add example for TSENS instruction Example code for reading the temperature of the chip using the ULP of the ESP32-S2 --- examples/tsens_s2.py | 80 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 examples/tsens_s2.py diff --git a/examples/tsens_s2.py b/examples/tsens_s2.py new file mode 100644 index 0000000..58594e6 --- /dev/null +++ b/examples/tsens_s2.py @@ -0,0 +1,80 @@ +# +# This file is part of the micropython-esp32-ulp project, +# https://github.com/micropython/micropython-esp32-ulp +# +# SPDX-FileCopyrightText: 2018-2023, the micropython-esp32-ulp authors, see AUTHORS file. +# SPDX-License-Identifier: MIT + + +""" +Example for: ESP32-S2 + +Example showing how to use the TSENS instruction from the ULP +and access temperature data from the main CPU. + +Note that the temperature sensor clock needs to be enabled for the TSENS instruction to complete. + +""" + +from esp32 import ULP +from machine import mem32 +from esp32_ulp import src_to_binary +from time import sleep + +source = """\ +# constants from: +# https://github.com/espressif/esp-idf/blob/v5.0.2/components/soc/esp32s2/include/soc/reg_base.h +#define DR_REG_SENS_BASE 0x3f408800 + +# constants from: +# https://github.com/espressif/esp-idf/blob/v5.0.2/components/soc/esp32s2/include/soc/sens_reg.h +#define SENS_SAR_TSENS_CTRL2_REG (DR_REG_SENS_BASE + 0x0054) +#define SENS_TSENS_CLKGATE_EN_M (BIT(15)) + +.set token, 0xACED + +.text +magic: .long 0 +temperature_data: .long 0 + +.global entry +entry: + move r3, magic + ld r0, r3, 0 + jumpr start, token, eq #check if we have already initialized + +init: + # Set SENS_TSENS_CLKGATE_EN to enable temperature sensor clock. + WRITE_RTC_REG(SENS_SAR_TSENS_CTRL2_REG, SENS_TSENS_CLKGATE_EN_M, 1, 1) + + # Store temperature_data memory location in r2 + move r2, temperature_data + + # store that we're done with initialisation + move r0, token + st r0, r3, 0 + +start: + tsens r0, 1000 # make measurement for 1000 clock cycles + st r0, r2, 0 # store the temperature in memory to be read by main CPU + halt # go back to sleep until next wakeup period +""" + +binary = src_to_binary(source, cpu="esp32s2") # cpu is esp32 or esp32s2 + +load_addr, entry_addr = 0, 8 + +ULP_MEM_BASE = 0x50000000 +ULP_DATA_MASK = 0xffff # ULP data is only in lower 16 bits + +ulp = ULP() +ulp.set_wakeup_period(0, 500000) # use timer0, wakeup after 500000usec (0.5s) +ulp.load_binary(load_addr, binary) + +ulp.run(entry_addr) + +while True: + magic_token = hex(mem32[ULP_MEM_BASE + load_addr] & ULP_DATA_MASK) + current_temperature = 0.4386*(mem32[ULP_MEM_BASE + load_addr + 4] & ULP_DATA_MASK)-20.52 + print(magic_token, current_temperature) + sleep(0.5) \ No newline at end of file From e6d5d96a8c1fedd352b2fb5b83ca110c43b4b14c Mon Sep 17 00:00:00 2001 From: mjaspers2mtu <44484497+mjaspers2mtu@users.noreply.github.com> Date: Sun, 6 Jul 2025 16:41:14 +0200 Subject: [PATCH 3/3] Update fade_s2.py made the default LED pin 4, with the option for the user to change led_pin. --- examples/fade_s2.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/examples/fade_s2.py b/examples/fade_s2.py index 8b9ca71..5f8d4ca 100644 --- a/examples/fade_s2.py +++ b/examples/fade_s2.py @@ -6,7 +6,7 @@ # SPDX-License-Identifier: MIT """ -Example for: ESP32-S2 Wemos mini development board V1.0.0 with led on pin 15 +Example for: ESP32-S2 This example creates a PWM-like dimming effect using self-modifying ULP code. The ULP program rewrites the `WAIT` instructions to control on/off LED durations, @@ -23,32 +23,34 @@ from time import sleep source = """\ +# Pin with LED: (0 to 21) +.set led_pin, 4 + # constants from: # https://github.com/espressif/esp-idf/blob/v5.0.2/components/soc/esp32s2/include/soc/reg_base.h #define DR_REG_RTCIO_BASE 0x3f408400 +# constants from: +# Espressif Technical Reference Manual (TRM) Chapter 5.15 Register 5.63: +#define RTCIO_TOUCH_PADn_REG (DR_REG_RTCIO_BASE + 0x84 + 4 * led_pin) +#define RTCIO_TOUCH_PADn_MUX_SEL_M (BIT(19)) + # constants from: # https://github.com/espressif/esp-idf/blob/v5.0.2/components/soc/esp32s2/include/soc/rtc_io_reg.h -#define RTC_IO_XTAL_32P_PAD_REG (DR_REG_RTCIO_BASE + 0xC0) -#define RTC_IO_X32P_MUX_SEL_M (BIT(19)) #define RTC_GPIO_OUT_REG (DR_REG_RTCIO_BASE + 0x0) #define RTC_GPIO_ENABLE_REG (DR_REG_RTCIO_BASE + 0xc) #define RTC_GPIO_ENABLE_S 10 #define RTC_GPIO_OUT_DATA_S 10 -# constants from: -# https://github.com/espressif/esp-idf/blob/v5.0.2/components/soc/esp32s2/include/soc/rtc_io_channel.h -#define RTCIO_GPIO15_CHANNEL 15 - .global entry program_init: - # connect GPIO to ULP (0: GPIO connected to digital GPIO module, 1: GPIO connected to analog RTC module) - WRITE_RTC_REG(RTC_IO_XTAL_32P_PAD_REG, RTC_IO_X32P_MUX_SEL_M, 1, 1); + # connect GPIO to ULP (0: GPIO connected to digital GPIO module, 1: GPIO connected to analog RTC module) + WRITE_RTC_REG(RTCIO_TOUCH_PADn_REG, RTCIO_TOUCH_PADn_MUX_SEL_M, 1, 1); - # enable GPIO as output, not input (this also enables a pull-down by default) - WRITE_RTC_REG(RTC_GPIO_ENABLE_REG, RTC_GPIO_ENABLE_S + RTCIO_GPIO15_CHANNEL, 1, 1) + # enable GPIO as output, not input (this also enables a pull-down by default) + WRITE_RTC_REG(RTC_GPIO_ENABLE_REG, RTC_GPIO_ENABLE_S + led_pin, 1, 1) -set_waits: add r0, r0, 0xFF # Increase r0 (delay time) +set_waits: add r0, r0, 200 # Increase r0 (delay time) move r3, wait_off st r0, r3, 0 # Overwrite wait_off with new delay value @@ -57,9 +59,9 @@ move r3, wait_on st r1, r3, 0 # Overwrite wait_on with new value - WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S + RTCIO_GPIO15_CHANNEL, 1, 0) # turn off led + WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S + led_pin, 1, 0) # turn off led (clear GPIO) wait_off: wait 0 # Placeholder; value overwritten dynamically - WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S + RTCIO_GPIO15_CHANNEL, 1, 1) # turn on led + WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S + led_pin, 1, 1) # turn on led (set GPIO) wait_on: wait 0 # Placeholder; value overwritten dynamically jump set_waits # Loop program pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy