-
Notifications
You must be signed in to change notification settings - Fork 24
/
counter.py
53 lines (37 loc) · 1.5 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
44
45
46
47
48
49
50
51
52
#
# This file is part of the micropython-esp32-ulp project,
# https://github.com/micropython/micropython-esp32-ulp
#
# SPDX-FileCopyrightText: 2018-2023, the micropython-esp32-ulp authors, see AUTHORS file.
# SPDX-License-Identifier: MIT
"""
Example for: ESP32
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))