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

Commita640042

Browse files
committed
update examples
1 parenta16ebf0 commita640042

22 files changed

+700
-124
lines changed

‎README.rst‎

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ This project is WIP
55
The goal is to be able to write ESP-ULP Assembly code and run it from within circuit python.
66
This requires `my branch<https://github.com/Sola85/circuitpython/tree/improve_espulp>`_ of the circuit python repo, since the `espulp` module in default circuit python is basically not functional.
77

8-
The readme below is outdated.
8+
The examples are tested with on esp32s3.
9+
10+
The documentation in `docs`_ is outdated and the tests do not work.
911

1012
=====================
1113
micropython-esp32-ulp
@@ -58,23 +60,7 @@ The following features are supported:
5860
Quick start
5961
-----------
6062

61-
To get going run the following directly on the ESP32:
62-
63-
..code-block::python
64-
65-
# IMPORTANT: Ensure the ESP32 is connected to a network with internet connectivity.
66-
67-
# Step 1: Install micropython-esp32-ulp (for MicroPython v1.20 or newer)
68-
import mip
69-
mip.install('github:micropython/micropython-esp32-ulp')
70-
71-
# Step 1: Install micropython-esp32-ulp (for MicroPython older than v1.20)
72-
import upip
73-
upip.install('micropython-esp32-ulp')
74-
75-
# Step 2: Run an example
76-
# First, upload examples/counter.py to the ESP32.
77-
import counter
63+
To get going, copy the esp32_ulp folder to the lib folder on the circuitpy drive.
7864

7965
The `examples/counter.py</examples/counter.py>`_ example shows how to assemble code,
8066
load and run the resulting binary and exchange data between the ULP and the main CPU.

‎demo.S‎

Lines changed: 0 additions & 79 deletions
This file was deleted.

‎demo.sh‎

Lines changed: 0 additions & 8 deletions
This file was deleted.

‎examples/README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
These examples require[my branch](https://github.com/Sola85/circuitpython/tree/improve_espulp) of circuitpython to work.
2+
3+
They are tested on an esp32s3.
4+
5+
`main_counter.py` should work on an esp32s2 without any changes and should work on an esp32 by simply changing the target cpu in the call to`src_to_binary`.
6+
7+
All other examples require the addresses of registers to be adjusted in the main python files, as indicated by the comments in the appropriate places.

‎examples/main_blink1.py‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
"""
2+
A example that blinks an LED connected to board.IO13 from the ULP.
3+
4+
In order to change the pin that does the blinking, one needs to change:
5+
- RTC_IO_TOUCH_PAD13_REG
6+
- RTCIO_GPIO13_CHANNEL
7+
8+
In order to switch to an ESP32 or ESP32S2 on needs to change:
9+
- DR_REG_RTCIO_BASE
10+
- The compile target in the call to src_to_binary
11+
"""
12+
13+
fromesp32_ulpimportsrc_to_binary
14+
importespulp
15+
importmemorymap
16+
importboard
17+
fromtimeimportsleep
18+
19+
20+
source="""
21+
# constants from:
22+
# https://github.com/espressif/esp-idf/blob/v5.0.2/components/soc/esp32s2/include/soc/reg_base.h
23+
#define DR_REG_RTCIO_BASE 0x60008400 #for esp32s3. use 0x3f408400 for esp32s2
24+
25+
# constants from:
26+
# https://github.com/espressif/esp-idf/blob/v5.0.2/components/soc/esp32s2/include/soc/rtc_io_reg.h
27+
#define RTC_IO_TOUCH_PAD13_REG (DR_REG_RTCIO_BASE + 0xB8)
28+
#define RTC_IO_TOUCH_PAD13_MUX_SEL_M (BIT(19))
29+
#define RTC_GPIO_OUT_REG (DR_REG_RTCIO_BASE + 0x0)
30+
#define RTC_GPIO_ENABLE_REG (DR_REG_RTCIO_BASE + 0xc)
31+
#define RTC_GPIO_ENABLE_S 10
32+
#define RTC_GPIO_OUT_DATA_S 10
33+
34+
# constants from:
35+
# https://github.com/espressif/esp-idf/blob/v5.0.2/components/soc/esp32s2/include/soc/rtc_io_channel.h
36+
#define RTCIO_GPIO13_CHANNEL 13
37+
38+
# When accessed from the RTC module (ULP) GPIOs need to be addressed by their channel number
39+
.set gpio, RTCIO_GPIO13_CHANNEL
40+
.set token, 0xcafe # magic token
41+
42+
.text
43+
magic: .long 0
44+
state: .long 0
45+
46+
.global entry
47+
entry:
48+
# load magic flag
49+
move r0, magic
50+
ld r1, r0, 0
51+
52+
# test if we have initialised already
53+
sub r1, r1, token
54+
jump after_init, eq # jump if magic == token (note: "eq" means the last instruction (sub) resulted in 0)
55+
56+
init:
57+
# connect GPIO to ULP (0: GPIO connected to digital GPIO module, 1: GPIO connected to analog RTC module)
58+
WRITE_RTC_REG(RTC_IO_TOUCH_PAD13_REG, RTC_IO_TOUCH_PAD13_MUX_SEL_M, 1, 1);
59+
60+
# GPIO shall be output, not input (this also enables a pull-down by default)
61+
WRITE_RTC_REG(RTC_GPIO_ENABLE_REG, RTC_GPIO_ENABLE_S + gpio, 1, 1)
62+
63+
# store that we're done with initialisation
64+
move r0, magic
65+
move r1, token
66+
st r1, r0, 0
67+
68+
after_init:
69+
move r1, state
70+
ld r0, r1, 0
71+
72+
move r2, 1
73+
sub r0, r2, r0 # toggle state
74+
st r0, r1, 0 # store updated state
75+
76+
jumpr on, 0, gt # if r0 (state) > 0, jump to 'on'
77+
jump off # else jump to 'off'
78+
79+
on:
80+
# turn on led (set GPIO)
81+
WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S + gpio, 1, 1)
82+
jump exit
83+
84+
off:
85+
# turn off led (clear GPIO)
86+
WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S + gpio, 1, 0)
87+
jump exit
88+
89+
exit:
90+
halt # go back to sleep until next wakeup period
91+
"""
92+
93+
94+
95+
binary=src_to_binary(source,cpu="esp32s2")# cpu is esp32 or esp32s2
96+
97+
98+
ulp=espulp.ULP()
99+
ulp.halt()
100+
ulp.set_wakeup_period(0,500000)# wake up every 500 000us = 0.5s
101+
ulp.run(binary,entrypoint=8,pins=[board.IO13])# entrypoint = 8 because we have 2 variables ('magic' and 'state')
102+
# before the main entry, each consisting of 4 bytes.
103+
104+
state_variable=memorymap.AddressRange(start=0x50000004,length=4)
105+
whileTrue:
106+
print(int.from_bytes(state_variable[:],"little"))
107+
sleep(0.5)

‎examples/main_blink2.py‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
An alternative implementation of the blink example.
3+
4+
Here we initialize the GPIO from within circuitpython, so the assembly looks a bit simpler.
5+
"""
6+
7+
fromesp32_ulpimportsrc_to_binary,Register
8+
importesp32_ulp.soc_s3assoc
9+
importespulp
10+
importmemorymap
11+
importboard
12+
fromtimeimportsleep
13+
14+
15+
source="""
16+
#define DR_REG_RTCIO_BASE 0x60008400 #for esp32s3. use 0x3f408400 for esp32s2
17+
#define RTC_GPIO_OUT_REG (DR_REG_RTCIO_BASE + 0x0)
18+
#define RTC_GPIO_OUT_DATA_S 10
19+
20+
.set gpio, 13
21+
22+
.text
23+
state: .long 0
24+
25+
.global entry
26+
entry:
27+
move r1, state
28+
ld r0, r1, 0
29+
30+
move r2, 1
31+
sub r0, r2, r0 # toggle state
32+
st r0, r1, 0 # store updated state
33+
34+
jumpr on, 0, gt # if r0 (state) > 0, jump to 'on'
35+
jump off # else jump to 'off'
36+
37+
on:
38+
# turn on led (set GPIO)
39+
WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S + gpio, 1, 1)
40+
jump exit
41+
42+
off:
43+
# turn off led (clear GPIO)
44+
WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S + gpio, 1, 0)
45+
jump exit
46+
47+
exit:
48+
halt # go back to sleep until next wakeup period
49+
"""
50+
51+
52+
binary=src_to_binary(source,cpu="esp32s2")# cpu is esp32 or esp32s2
53+
54+
55+
ulp=espulp.ULP()
56+
ulp.halt()
57+
58+
pin_number=13
59+
RTC_IO_TOUCH_PAD13_REG=Register(soc.DR_REG_RTCIO_BASE+0xB8)
60+
RTC_IO_TOUCH_PAD13_REG.set_bit(19)# connect GPIO to ULP (bit cleared: GPIO connected to digital GPIO module, bit set: GPIO connected to analog RTC module) (19 = RTC_IO_TOUCH_PAD13_MUX_SEL_M)
61+
RTC_GPIO_ENABLE_REG=Register(soc.DR_REG_RTCIO_BASE+0x0C)
62+
RTC_GPIO_ENABLE_REG.set_bit(10+pin_number)# GPIO shall be output, not input (this also enables a pull-down by default)
63+
64+
ulp.set_wakeup_period(0,500000)
65+
ulp.run(binary,entrypoint=4,pins=[board.IO13])
66+
67+
state_variable=memorymap.AddressRange(start=0x50000000,length=4)
68+
whileTrue:
69+
print(int.from_bytes(state_variable[:],"little"))
70+
sleep(0.5)

‎examples/main_blink3.py‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""
2+
A third version of the blink example.
3+
4+
Here we dont rely on the wakeup period to act as a delay.
5+
Instead we implement the delay on the ULP itself.
6+
7+
Note that this version is less desirable, since the ULP is running continuously
8+
and hence consumes much more power.
9+
"""
10+
11+
12+
fromesp32_ulpimportsrc_to_binary,Register
13+
importesp32_ulp.soc_s3assoc
14+
importespulp
15+
importboard
16+
17+
18+
source="""
19+
#define DR_REG_RTCIO_BASE 0x60008400 #for esp32s3. use 0x3f408400 for esp32s2
20+
#define RTC_GPIO_OUT_REG (DR_REG_RTCIO_BASE + 0x0)
21+
#define RTC_GPIO_OUT_DATA_S 10
22+
23+
.set gpio, 13
24+
25+
.text
26+
27+
.global entry
28+
entry:
29+
# turn on led (set GPIO)
30+
WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S + gpio, 1, 1)
31+
move r1, 100 # 100 ms delay
32+
move r2, off
33+
jump delay
34+
35+
off:
36+
# turn off led (clear GPIO)
37+
WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S + gpio, 1, 0)
38+
move r1, 1000 # 1000 ms delay
39+
move r2, entry
40+
jump delay
41+
42+
delay:
43+
wait 8000 # 8000 cycles at 8 MHz -> 1 ms
44+
sub r1, r1, 1
45+
jump r2, eq # if ms count is zero, then return to caller
46+
jump delay
47+
"""
48+
49+
50+
binary=src_to_binary(source,cpu="esp32s2")# cpu is esp32 or esp32s2
51+
52+
53+
ulp=espulp.ULP()
54+
ulp.halt()
55+
56+
pin_number=13
57+
RTC_IO_TOUCH_PAD13_REG=Register(soc.DR_REG_RTCIO_BASE+0xB8)
58+
RTC_IO_TOUCH_PAD13_REG.set_bit(19)# connect GPIO to ULP (bit cleared: GPIO connected to digital GPIO module, bit set: GPIO connected to analog RTC module) (19 = RTC_IO_TOUCH_PAD13_MUX_SEL_M)
59+
RTC_GPIO_ENABLE_REG=Register(soc.DR_REG_RTCIO_BASE+0x0C)
60+
RTC_GPIO_ENABLE_REG.set_bit(10+pin_number)# GPIO shall be output, not input (this also enables a pull-down by default)
61+
62+
# dont need to set wakeup_period for this example
63+
# ulp.set_wakeup_period(0, 50000)
64+
ulp.run(binary,entrypoint=0,pins=[board.IO13])

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp