Skip to content

Commit a19ae7b

Browse files
add first example: a simple counter running on the ULP coprocessor
1 parent 052e638 commit a19ae7b

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

examples/README

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
To run the micropython examples which load and run binaries on the ULP,
2+
you need a ESP32 MicroPython build that was made after 2018-05-01.
3+

examples/counter.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Very basic example showing data exchange main CPU <--> ULP coprocessor.
3+
4+
To show that the ULP is doing something, it just increments the value <data>.
5+
It does that once per ulp timer wakeup (and then the ULP halts until it gets
6+
waked up via the timer again).
7+
8+
The timer is set to a rather long period, so you can watch the data value
9+
incrementing (see loop at the end).
10+
"""
11+
12+
from esp32 import ULP
13+
from machine import mem32
14+
15+
from esp32_ulp.__main__ import src_to_binary
16+
17+
source = """\
18+
data: .long 0
19+
20+
entry: move r3, data # load address of data into r3
21+
ld r2, r3, 0 # load data contents ([r3+0]) into r2
22+
add r2, r2, 1 # increment r2
23+
st r2, r3, 0 # store r2 contents into data ([r3+0])
24+
25+
halt # halt ULP co-prozessor (until it gets waked up again)
26+
"""
27+
28+
binary = src_to_binary(source)
29+
30+
load_addr, entry_addr = 0, 4
31+
32+
ULP_MEM_BASE = 0x50000000
33+
ULP_DATA_MASK = 0xffff # ULP data is only in lower 16 bits
34+
35+
ulp = ULP()
36+
ulp.set_wakeup_period(0, 50000) # use timer0, wakeup after 50.000 cycles
37+
ulp.load_binary(load_addr, binary)
38+
39+
mem32[ULP_MEM_BASE + load_addr] = 0x1000
40+
ulp.run(entry_addr)
41+
42+
while True:
43+
print(hex(mem32[ULP_MEM_BASE + load_addr] & ULP_DATA_MASK))
44+

0 commit comments

Comments
 (0)