|
6 | 6 | # SPDX-License-Identifier: MIT |
7 | 7 |
|
8 | 8 | """ |
9 | | -Example for: ESP32-S2 Wemos mini development board V1.0.0 with led on pin 15 |
| 9 | +Example for: ESP32-S2 |
10 | 10 |
|
11 | 11 | This example creates a PWM-like dimming effect using self-modifying ULP code. |
12 | 12 | The ULP program rewrites the `WAIT` instructions to control on/off LED durations, |
|
23 | 23 | fromtimeimportsleep |
24 | 24 |
|
25 | 25 | source="""\ |
| 26 | +# Pin with LED: (0 to 21) |
| 27 | +.set led_pin, 4 |
| 28 | +
|
26 | 29 | # constants from: |
27 | 30 | # https://github.com/espressif/esp-idf/blob/v5.0.2/components/soc/esp32s2/include/soc/reg_base.h |
28 | 31 | #define DR_REG_RTCIO_BASE 0x3f408400 |
29 | 32 |
|
| 33 | +# constants from: |
| 34 | +# Espressif Technical Reference Manual (TRM) Chapter 5.15 Register 5.63: |
| 35 | +#define RTCIO_TOUCH_PADn_REG (DR_REG_RTCIO_BASE + 0x84 + 4 * led_pin) |
| 36 | +#define RTCIO_TOUCH_PADn_MUX_SEL_M (BIT(19)) |
| 37 | +
|
30 | 38 | # constants from: |
31 | 39 | # https://github.com/espressif/esp-idf/blob/v5.0.2/components/soc/esp32s2/include/soc/rtc_io_reg.h |
32 | | -#define RTC_IO_XTAL_32P_PAD_REG (DR_REG_RTCIO_BASE + 0xC0) |
33 | | -#define RTC_IO_X32P_MUX_SEL_M (BIT(19)) |
34 | 40 | #define RTC_GPIO_OUT_REG (DR_REG_RTCIO_BASE + 0x0) |
35 | 41 | #define RTC_GPIO_ENABLE_REG (DR_REG_RTCIO_BASE + 0xc) |
36 | 42 | #define RTC_GPIO_ENABLE_S 10 |
37 | 43 | #define RTC_GPIO_OUT_DATA_S 10 |
38 | 44 |
|
39 | | -# constants from: |
40 | | -# https://github.com/espressif/esp-idf/blob/v5.0.2/components/soc/esp32s2/include/soc/rtc_io_channel.h |
41 | | -#define RTCIO_GPIO15_CHANNEL 15 |
42 | | -
|
43 | 45 | .global entry |
44 | 46 | program_init: |
45 | | -# connect GPIO to ULP (0: GPIO connected to digital GPIO module, 1: GPIO connected to analog RTC module) |
46 | | -WRITE_RTC_REG(RTC_IO_XTAL_32P_PAD_REG, RTC_IO_X32P_MUX_SEL_M, 1, 1); |
| 47 | + # connect GPIO to ULP (0: GPIO connected to digital GPIO module, 1: GPIO connected to analog RTC module) |
| 48 | + WRITE_RTC_REG(RTCIO_TOUCH_PADn_REG, RTCIO_TOUCH_PADn_MUX_SEL_M, 1, 1); |
47 | 49 |
|
48 | | -# enable GPIO as output, not input (this also enables a pull-down by default) |
49 | | -WRITE_RTC_REG(RTC_GPIO_ENABLE_REG, RTC_GPIO_ENABLE_S +RTCIO_GPIO15_CHANNEL, 1, 1) |
| 50 | + # enable GPIO as output, not input (this also enables a pull-down by default) |
| 51 | + WRITE_RTC_REG(RTC_GPIO_ENABLE_REG, RTC_GPIO_ENABLE_S +led_pin, 1, 1) |
50 | 52 |
|
51 | | -set_waits: add r0, r0,0xFF # Increase r0 (delay time) |
| 53 | +set_waits: add r0, r0,200 # Increase r0 (delay time) |
52 | 54 | move r3, wait_off |
53 | 55 | st r0, r3, 0 # Overwrite wait_off with new delay value |
54 | 56 |
|
|
57 | 59 | move r3, wait_on |
58 | 60 | st r1, r3, 0 # Overwrite wait_on with new value |
59 | 61 |
|
60 | | - WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S +RTCIO_GPIO15_CHANNEL, 1, 0) # turn off led |
| 62 | + WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S +led_pin, 1, 0) # turn off led (clear GPIO) |
61 | 63 | wait_off: wait 0 # Placeholder; value overwritten dynamically |
62 | | - WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S +RTCIO_GPIO15_CHANNEL, 1, 1) # turn on led |
| 64 | + WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S +led_pin, 1, 1) # turn on led (set GPIO) |
63 | 65 | wait_on: wait 0 # Placeholder; value overwritten dynamically |
64 | 66 |
|
65 | 67 | jump set_waits # Loop program |
|