Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd11afd1

Browse files
authored
Merge pull request#105 from mjaspers2mtu/master
Add LED Fade and Temperature sensing example
2 parents1098afb +e6d5d96 commitd11afd1

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

‎examples/fade_s2.py‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#
2+
# This file is part of the micropython-esp32-ulp project,
3+
# https://github.com/micropython/micropython-esp32-ulp
4+
#
5+
# SPDX-FileCopyrightText: 2018-2023, the micropython-esp32-ulp authors, see AUTHORS file.
6+
# SPDX-License-Identifier: MIT
7+
8+
"""
9+
Example for: ESP32-S2
10+
11+
This example creates a PWM-like dimming effect using self-modifying ULP code.
12+
The ULP program rewrites the `WAIT` instructions to control on/off LED durations,
13+
simulating a variable duty cycle.
14+
15+
Note:
16+
The `WAIT` instruction uses an immediate operand (fixed value) for delay cycles. However, we can change the lower half of memory
17+
to modify these values at runtime, simulating variable wait times via registers.
18+
"""
19+
20+
fromesp32importULP
21+
frommachineimportmem32
22+
fromesp32_ulpimportsrc_to_binary
23+
fromtimeimportsleep
24+
25+
source="""\
26+
# Pin with LED: (0 to 21)
27+
.set led_pin, 4
28+
29+
# constants from:
30+
# https://github.com/espressif/esp-idf/blob/v5.0.2/components/soc/esp32s2/include/soc/reg_base.h
31+
#define DR_REG_RTCIO_BASE 0x3f408400
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+
38+
# constants from:
39+
# https://github.com/espressif/esp-idf/blob/v5.0.2/components/soc/esp32s2/include/soc/rtc_io_reg.h
40+
#define RTC_GPIO_OUT_REG (DR_REG_RTCIO_BASE + 0x0)
41+
#define RTC_GPIO_ENABLE_REG (DR_REG_RTCIO_BASE + 0xc)
42+
#define RTC_GPIO_ENABLE_S 10
43+
#define RTC_GPIO_OUT_DATA_S 10
44+
45+
.global entry
46+
program_init:
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);
49+
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)
52+
53+
set_waits: add r0, r0, 200 # Increase r0 (delay time)
54+
move r3, wait_off
55+
st r0, r3, 0 # Overwrite wait_off with new delay value
56+
57+
move r2, 0xFFFF
58+
sub r1, r2, r0 # Calculate complementary delay time
59+
move r3, wait_on
60+
st r1, r3, 0 # Overwrite wait_on with new value
61+
62+
WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S + led_pin, 1, 0) # turn off led (clear GPIO)
63+
wait_off: wait 0 # Placeholder; value overwritten dynamically
64+
WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S + led_pin, 1, 1) # turn on led (set GPIO)
65+
wait_on: wait 0 # Placeholder; value overwritten dynamically
66+
67+
jump set_waits # Loop program
68+
69+
"""
70+
71+
binary=src_to_binary(source,cpu="esp32s2")# cpu is esp32 or esp32s2
72+
73+
load_addr,entry_addr=0,0
74+
75+
ULP_MEM_BASE=0x50000000
76+
77+
ulp=ULP()
78+
ulp.load_binary(load_addr,binary)
79+
80+
ulp.run(entry_addr)
81+
82+
whileTrue:
83+
print(hex(mem32[ULP_MEM_BASE+40]))# show that the WAIT cycles are changing
84+
sleep(0.5)

‎examples/tsens_s2.py‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#
2+
# This file is part of the micropython-esp32-ulp project,
3+
# https://github.com/micropython/micropython-esp32-ulp
4+
#
5+
# SPDX-FileCopyrightText: 2018-2023, the micropython-esp32-ulp authors, see AUTHORS file.
6+
# SPDX-License-Identifier: MIT
7+
8+
9+
"""
10+
Example for: ESP32-S2
11+
12+
Example showing how to use the TSENS instruction from the ULP
13+
and access temperature data from the main CPU.
14+
15+
Note that the temperature sensor clock needs to be enabled for the TSENS instruction to complete.
16+
17+
"""
18+
19+
fromesp32importULP
20+
frommachineimportmem32
21+
fromesp32_ulpimportsrc_to_binary
22+
fromtimeimportsleep
23+
24+
source="""\
25+
# constants from:
26+
# https://github.com/espressif/esp-idf/blob/v5.0.2/components/soc/esp32s2/include/soc/reg_base.h
27+
#define DR_REG_SENS_BASE 0x3f408800
28+
29+
# constants from:
30+
# https://github.com/espressif/esp-idf/blob/v5.0.2/components/soc/esp32s2/include/soc/sens_reg.h
31+
#define SENS_SAR_TSENS_CTRL2_REG (DR_REG_SENS_BASE + 0x0054)
32+
#define SENS_TSENS_CLKGATE_EN_M (BIT(15))
33+
34+
.set token, 0xACED
35+
36+
.text
37+
magic: .long 0
38+
temperature_data: .long 0
39+
40+
.global entry
41+
entry:
42+
move r3, magic
43+
ld r0, r3, 0
44+
jumpr start, token, eq #check if we have already initialized
45+
46+
init:
47+
# Set SENS_TSENS_CLKGATE_EN to enable temperature sensor clock.
48+
WRITE_RTC_REG(SENS_SAR_TSENS_CTRL2_REG, SENS_TSENS_CLKGATE_EN_M, 1, 1)
49+
50+
# Store temperature_data memory location in r2
51+
move r2, temperature_data
52+
53+
# store that we're done with initialisation
54+
move r0, token
55+
st r0, r3, 0
56+
57+
start:
58+
tsens r0, 1000 # make measurement for 1000 clock cycles
59+
st r0, r2, 0 # store the temperature in memory to be read by main CPU
60+
halt # go back to sleep until next wakeup period
61+
"""
62+
63+
binary=src_to_binary(source,cpu="esp32s2")# cpu is esp32 or esp32s2
64+
65+
load_addr,entry_addr=0,8
66+
67+
ULP_MEM_BASE=0x50000000
68+
ULP_DATA_MASK=0xffff# ULP data is only in lower 16 bits
69+
70+
ulp=ULP()
71+
ulp.set_wakeup_period(0,500000)# use timer0, wakeup after 500000usec (0.5s)
72+
ulp.load_binary(load_addr,binary)
73+
74+
ulp.run(entry_addr)
75+
76+
whileTrue:
77+
magic_token=hex(mem32[ULP_MEM_BASE+load_addr]&ULP_DATA_MASK)
78+
current_temperature=0.4386*(mem32[ULP_MEM_BASE+load_addr+4]&ULP_DATA_MASK)-20.52
79+
print(magic_token,current_temperature)
80+
sleep(0.5)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp