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