10000 Allow selecting cpu to assemble for from cmdline · wnienhaus/micropython-esp32-ulp@9f04bd7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9f04bd7

Browse files
committed
Allow selecting cpu to assemble for from cmdline
Select cpu with -c when running the assembler (--mcpu as used by Espressif's esp32ulp-elf-as also works). The possible values are 'esp32' and 'esp32s2'. (Note esp32s2 also works for the ESP32-S3, because those two MCUs share the same ULP-FSM binary format). If no cpu is specified the original 'esp32' will be used as before.
1 parent d0c9f88 commit 9f04bd7

File tree

7 files changed

+41
-17
lines changed

7 files changed

+41
-17
lines changed

docs/index.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ follows:
5858
cd micropython-esp32-ulp
5959
micropython -m esp32_ulp path/to/code.S # this results in path/to/code.ulp
6060
61+
The assembler supports selecting a CPU to assemble for using the ``-c`` option
62+
(valid cpu's are ``esp32`` and ``esp32s2``):
63+
64+
.. code-block:: shell
65+
66+
micropython -m esp32_ulp -c esp32s2 path/to/code.S # assemble for an ESP32-S2
67+
6168
6269
More examples
6370
+++++++++++++
@@ -91,7 +98,7 @@ That file can then be loaded directly without assembling the source again.
9198
.. code-block:: python
9299
93100
import esp32_ulp
94-
esp32_ulp.assemble_file('code.S') # this results in code.ulp
101+
esp32_ulp.assemble_file('code.S', cpu='esp32') # this results in code.ulp
95102
96103
2. The above prints out the offsets of all global symbols/labels. For the next
97104
step, you will need to note down the offset of the label, which represents

esp32_ulp/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
garbage_collect('after import')
77

88

9-
def src_to_binary(src):
10-
assembler = Assembler()
9+
def src_to_binary(src, cpu):
10+
assembler = Assembler(cpu)
1111
src = preprocess(src)
1212
assembler.assemble(src, remove_comments=False) # comments already removed by preprocessor
1313
garbage_collect('before symbols export')
@@ -19,11 +19,11 @@ def src_to_binary(src):
1919
return make_binary(text, data, bss_len)
2020

2121

22-
def assemble_file(filename):
22+
def assemble_file(filename, cpu):
2323
with open(filename) as f:
2424
src = f.read()
2525

26-
binary = src_to_binary(src)
26+
binary = src_to_binary(src, cpu)
2727

2828
if filename.endswith('.s') or filename.endswith('.S'):
2929
filename = filename[:-2]

esp32_ulp/__main__.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22
from . import assemble_file
33

44

5-
def main(fn):
6-
assemble_file(fn)
5+
def main(fn, cpu):
6+
assemble_file(fn, cpu)
77

88

99
if __name__ == '__main__':
10-
main(sys.argv[1])
10+
cpu = 'esp32'
11+
filename = sys.argv[1]
12+
if len(sys.argv) > 3:
13+
if sys.argv[1] in ('-c', '--mcpu'):
14+
cpu = sys.argv[2].lower()
15+
if cpu not in ('esp32', 'esp32s2'):
16+
raise ValueError('Invalid cpu')
17+
filename = sys.argv[3]
18+
main(filename, cpu)
1119

esp32_ulp/assemble.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""
44

55
import re
6-
from . import opcodes
76
from .nocomment import remove_comments as do_remove_comments
87
from .util import garbage_collect
98

@@ -88,9 +87,19 @@ def set_global(self, symbol):
8887

8988
class Assembler:
9089

91-
def __init__(self, symbols=None, bases=None, globals=None):
90+
def __init__(self, cpu='esp32', symbols=None, bases=None, globals=None):
91+
if cpu == 'esp32':
92+
opcode_module = 'opcodes'
93+
elif cpu == 'esp32s2':
94+
opcode_module = 'opcodes_s2'
95+
else:
96+
raise ValueError('Invalid cpu')
97+
98+
relative_import = 1 if '/' in __file__ else 0
99+
self.opcodes = __import__(opcode_module, None, None, [], relative_import)
100+
92101
self.symbols = SymbolTable(symbols or {}, bases or {}, globals or {})
93-
opcodes.symbols = self.symbols # XXX dirty hack
102+
self.opcodes.symbols = self.symbols # XXX dirty hack
94103

95104
# regex for parsing assembly lines
96105
# format: [[whitespace]label:][whitespace][opcode[whitespace arg[,arg...]]]
@@ -223,7 +232,7 @@ def d_align(self, align=4, fill=None):
223232
self.fill(self.section, amount, fill)
224233

225234
def d_set(self, symbol, expr):
226-
value = int(opcodes.eval_arg(expr))
235+
value = int(self.opcodes.eval_arg(expr))
227236
self.symbols.set_sym(symbol, ABS, None, value)
228237

229238
def d_global(self, symbol):
@@ -264,13 +273,13 @@ def assembler_pass(self, lines):
264273
else:
265274
# machine instruction
266275
opcode_lower = opcode.lower()
267-
func = getattr(opcodes, 'i_' + opcode_lower, None)
276+
func = getattr(self.opcodes, 'i_' + opcode_lower, None)
268277
if func is not None:
269278
if self.a_pass == 1:
270279
# during the first pass, symbols are not all known yet.
271280
# so we add empty instructions to the section, to determine
272281
# section sizes and symbol offsets for pass 2.
273-
result = (0,) * opcodes.no_of_instr(opcode_lower, args)
282+
result = (0,) * self.opcodes.no_of_instr(opcode_lower, args)
274283
else:
275284
result = func(*args)
276285

examples/blink.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
halt # go back to sleep until next wakeup period
9494
"""
9595

96-
binary = src_to_binary(source)
96+
binary = src_to_binary(source, cpu="esp32") # cpu is esp32 or esp32s2
9797

9898
load_addr, entry_addr = 0, 8
9999

examples/counter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
halt # halt ULP co-prozessor (until it gets waked up again)
2626
"""
2727

28-
binary = src_to_binary(source)
28+
binary = src_to_binary(source, cpu="esp32") # cpu is esp32 or esp32s2
2929

3030
load_addr, entry_addr = 0, 4
3131

examples/readgpio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
halt
5353
"""
5454

55-
binary = src_to_binary(source)
55+
binary = src_to_binary(source, cpu="esp32") # cpu is esp32 or esp32s2
5656

5757
load_addr, entry_addr = 0, 4
5858

0 commit comments

Comments
 (0)
0