Skip to content

Commit b2342be

Browse files
loadable binary format creation, fixes #3
1 parent 54675f5 commit b2342be

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

esp32_ulp/link.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from uctypes import struct, addressof, LITTLE_ENDIAN, UINT16, UINT32
2+
3+
4+
def make_binary(text, data, bss_size):
5+
if not isinstance(text, bytes):
6+
raise TypeError('text section must be binary bytes')
7+
if not isinstance(data, bytes):
8+
raise TypeError('data section must be binary bytes')
9+
binary_header_struct_def = dict(
10+
magic = 0 | UINT32,
11+
text_offset = 4 | UINT16,
12+
text_size = 6 | UINT16,
13+
data_size = 8 | UINT16,
14+
bss_size = 10 | UINT16,
15+
)
16+
header = bytearray(12)
17+
h = struct(addressof(header), binary_header_struct_def, LITTLE_ENDIAN)
18+
# https://github.com/espressif/esp-idf/blob/master/components/ulp/ld/esp32.ulp.ld
19+
# ULP program binary should have the following format (all values little-endian):
20+
h.magic = 0x00706c75 # (4 bytes)
21+
h.text_offset = 12 # offset of .text section from binary start (2 bytes)
22+
h.text_size = len(text) # size of .text section (2 bytes)
23+
h.data_size = len(data) # size of .data section (2 bytes)
24+
h.bss_size = bss_size # size of .bss section (2 bytes)
25+
return bytes(header) + text + data
26+

tests/00_run_tests.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# export PYTHONPATH=.:$PYTHONPATH
22

3-
for file in opcodes assemble ; do
3+
for file in opcodes assemble link ; do
44
echo testing $file...
55
micropython $file.py
66
done

tests/link.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from esp32_ulp.link import make_binary
2+
3+
4+
def test_make_binary():
5+
text = b'\x12\x34\x56\x78'
6+
data = b'\x11\x22\x33\x44'
7+
bss_size = 40
8+
bin = make_binary(text, data, bss_size)
9+
assert len(bin) == 12 + len(text) + len(data)
10+
assert bin.startswith(b'\x75\x6c\x70\x00') # magic, LE
11+
assert bin.endswith(text+data)
12+
13+
14+
test_make_binary()
15+

0 commit comments

Comments
 (0)