Skip to content

Commit a356f38

Browse files
committed
builder: add support for ASR160x chipset
Signed-off-by: Ajay Bhargav <[email protected]>
1 parent 46987a4 commit a356f38

File tree

5 files changed

+442
-8
lines changed

5 files changed

+442
-8
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
# Copyright 2023 Waybyte Solutions
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
6+
"""
7+
Arduino
8+
9+
Arduino Wiring-based Framework allows writing cross-platform software to
10+
control devices attached to a wide range of Arduino boards to create all
11+
kinds of creative coding, interactive objects, spaces or physical experiences.
12+
13+
http://arduino.cc/en/Reference/HomePage
14+
"""
15+
16+
from os.path import isdir, isfile, join
17+
from shutil import copyfile
18+
from asrutils import gen_release_file, gen_fota_file
19+
from platformio.util import get_systype
20+
21+
from SCons.Script import DefaultEnvironment
22+
23+
env = DefaultEnvironment()
24+
platform = env.PioPlatform()
25+
board = env.BoardConfig()
26+
27+
FRAMEWORK_DIR = platform.get_package_dir("framework-logicromarduino")
28+
assert isdir(FRAMEWORK_DIR)
29+
30+
LOGICROMSDK_DIR = join(FRAMEWORK_DIR, "cores", board.get("build.core"), "logicromsdk")
31+
assert isdir(LOGICROMSDK_DIR)
32+
33+
# RDA Tools
34+
if "windows" in get_systype():
35+
systype = "win32"
36+
else:
37+
systype = "linux"
38+
39+
# Generate linker script
40+
linker_script = env.Command(
41+
join("$BUILD_DIR", "linkerscript_out.ld"),
42+
join(
43+
LOGICROMSDK_DIR, "lib", "asr160x", "app_linker.ld"
44+
),
45+
env.VerboseAction(
46+
'$CC -I"' + join(LOGICROMSDK_DIR, "lib", "asr160x") + '" -DFLASHSZ_' + board.get("build.flashsz") + ' -P -x c -E $SOURCE -o $TARGET',
47+
"Generating LD script $TARGET",
48+
),
49+
)
50+
env.Depends(join("$BUILD_DIR", "$PROGNAME" + "$PROGSUFFIX"), linker_script)
51+
env.Replace(LDSCRIPT_PATH="linkerscript_out.ld")
52+
53+
54+
def gen_zip_file(target, source, env):
55+
(target_firm, ) = target
56+
(source_elf, ) = source
57+
imagepath = join(LOGICROMSDK_DIR, "lib", "asr160x", "pack")
58+
gen_release_file(target_firm, source_elf, imagepath, env)
59+
60+
61+
# Setup ENV
62+
env.Append(
63+
ASFLAGS=["-x", "assembler-with-cpp"],
64+
65+
CCFLAGS=[
66+
"-Os", # optimize for size
67+
"-g",
68+
"-fmessage-length=0",
69+
"-ffunction-sections", # place each function in its own section
70+
"-fdata-sections",
71+
"-fsigned-char",
72+
"-fno-strict-aliasing",
73+
"-Wall",
74+
"-mthumb",
75+
"-mthumb-interwork",
76+
"-mcpu=cortex-r4",
77+
"-march=armv7-r",
78+
"-mfloat-abi=soft",
79+
"-mno-unaligned-access",
80+
],
81+
82+
CFLAGS=[
83+
"-std=gnu11"
84+
],
85+
86+
CXXFLAGS=[
87+
"-std=gnu++11",
88+
"-fno-rtti",
89+
"-fno-exceptions",
90+
"-fno-use-cxa-atexit",
91+
"-fno-threadsafe-statics",
92+
],
93+
94+
CPPDEFINES=[
95+
("__BUFSIZ__", "512"),
96+
("__FILENAME_MAX__", "256"),
97+
("F_CPU", "$BOARD_F_CPU"),
98+
("ARDUINO", 10816),
99+
"ARDUINO_ARCH_ARM",
100+
("ARDUINO_VARIANT", '\\"%s\\"' % board.get("build.variant").replace('"', "")),
101+
("ARDUINO_BOARD", '\\"%s\\"' % board.get("name").replace('"', "")),
102+
],
103+
104+
CPPPATH=[
105+
join(LOGICROMSDK_DIR, "include"),
106+
join(LOGICROMSDK_DIR, "include", "ril"),
107+
join(FRAMEWORK_DIR, "cores", board.get("build.core")),
108+
],
109+
110+
LINKFLAGS=[
111+
"-mthumb",
112+
"-mthumb-interwork",
113+
"-mcpu=cortex-r4",
114+
"-march=armv7-r",
115+
"-mfloat-abi=soft",
116+
"-mno-unaligned-access",
117+
"-Os",
118+
"-Wl,--gc-sections,--relax",
119+
"-nostartfiles",
120+
"-nostdlib",
121+
"-nostartfiles",
122+
"-nodefaultlibs",
123+
"-u", "main",
124+
],
125+
126+
LIBS=["logicromasr", "c", "gcc", "m", "stdc++"],
127+
128+
LIBPATH=[
129+
join(LOGICROMSDK_DIR, "lib"),
130+
join(LOGICROMSDK_DIR, "lib", "asr160x"),
131+
],
132+
133+
LIBSOURCE_DIRS=[join(FRAMEWORK_DIR, "libraries")],
134+
135+
BUILDERS=dict(
136+
ElfToBin=Builder(
137+
action=env.VerboseAction(gen_zip_file, "Generating $TARGET"),
138+
suffix=".zip"
139+
),
140+
BinToFOTA=Builder(
141+
action=env.VerboseAction(gen_fota_file, "Generating FOTA firmware $TARGET"),
142+
suffix=".bin"
143+
)
144+
)
145+
)
146+
147+
# copy CCFLAGS to ASFLAGS (-x assembler-with-cpp mode)
148+
env.Append(ASFLAGS=env.get("CCFLAGS", [])[:])
149+
150+
151+
def load_logicrom_debug():
152+
for i, libs in enumerate(env["LIBS"]):
153+
if libs.startswith("logicrom"):
154+
env["LIBS"][i] = libs + "_debug"
155+
156+
157+
if board.get("build.logicromtype") == "debug":
158+
load_logicrom_debug()
159+
160+
if env.GetBuildType() == "debug":
161+
load_logicrom_debug()
162+
163+
#
164+
# Target: Build Core Library
165+
#
166+
167+
libs = []
168+
169+
if "build.variant" in env.BoardConfig():
170+
env.Append(
171+
CPPPATH=[
172+
join(FRAMEWORK_DIR, "variants", env.BoardConfig().get("build.variant"))
173+
]
174+
)
175+
libs.append(env.BuildLibrary(
176+
join("$BUILD_DIR", "FrameworkArduinoVariant"),
177+
join(FRAMEWORK_DIR, "variants", board.get("build.variant"))
178+
))
179+
180+
envsafe = env.Clone()
181+
182+
libs.append(envsafe.BuildLibrary(
183+
join("$BUILD_DIR", "FrameworkArduino"),
184+
join(FRAMEWORK_DIR, "cores", board.get("build.core"))
185+
))
186+
187+
env.Prepend(LIBS=libs)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Copyright 2023 Waybyte Solutions
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
6+
from os.path import getsize, join
7+
from zipfile import ZipFile
8+
from zlib import crc32
9+
from json import load, dump
10+
11+
12+
def makebin(target, source, env):
13+
cmd = ["$OBJCOPY"]
14+
cmd.extend(["-O", "binary"])
15+
cmd.append(source)
16+
cmd.append(target)
17+
env.Execute(env.VerboseAction(" ".join(cmd), " "))
18+
19+
# fix bin size to 32 byte boundary
20+
binsz = getsize(target)
21+
f_binsz = (binsz + 0x1F) & ~0x1F
22+
f = open(target, "rb")
23+
f_bin = bytearray(f.read())
24+
f.close()
25+
f_bin += bytes(f_binsz - binsz)
26+
# Fix header size
27+
f_bin[4:8] = f_binsz.to_bytes(4, "little")
28+
# Fix checksum
29+
f_bin[8:0xC] = crc32(f_bin).to_bytes(4, "little")
30+
# write final binary
31+
f = open(target, "wb")
32+
f.write(f_bin)
33+
f.close()
34+
35+
36+
def gen_fota_file(target, source, env):
37+
(target_bin, ) = target
38+
(source_elf, ) = source
39+
makebin(target_bin.get_abspath(), source_elf.get_abspath(), env)
40+
41+
42+
def gen_release_file(target_firm, source_elf, imagepath, env):
43+
board = env.BoardConfig()
44+
mcu = board.get("build.mcu")
45+
flashsz = board.get("build.flashsz")
46+
# Generate image file
47+
print("Generating Firmware Binary")
48+
target_img = join(env.subst("$BUILD_DIR"), env.subst("$PROGNAME") + '.bin')
49+
makebin(target_img, source_elf.get_abspath(), env)
50+
target_binsz = getsize(target_img)
51+
print("Updating download.json")
52+
# Load and update download.json
53+
f = open(join(imagepath, "download.json"), "r")
54+
download = load(f)
55+
f.close()
56+
for cmd in download:
57+
if cmd["command"] == "require" and cmd["name"] == "product":
58+
cmd["value"] = "arom-tiny" if mcu == "ASR1603" else "arom|arom-tiny"
59+
elif cmd["command"] == "require" and cmd["name"] == "version-bootrom":
60+
cmd["value"] = "2020.07.30" if mcu == "ASR1603" else "2019.01.15"
61+
elif cmd["command"] == "progress":
62+
cmd["value"] = target_binsz
63+
elif cmd["command"] == "flash":
64+
cmd["weight"] = target_binsz
65+
cmd["image"] = env.subst("$PROGNAME") + '.bin'
66+
f = open(join(env.subst("$BUILD_DIR"), "download.json"), "w")
67+
dump(download, f, indent=4)
68+
f.close()
69+
# Update partition info, might not be needed
70+
print("Updating partition_" + flashsz + ".bin")
71+
f = open(join(imagepath, "partition_" + flashsz + ".bin"), "rb")
72+
part = bytearray(f.read())
73+
f.close()
74+
# Update app size
75+
part[0x3B8:0x3BC] = target_binsz.to_bytes(4, "little")
76+
# update CRC
77+
part[0x888:] = crc32(part[:(len(part) - 4)]).to_bytes(4, "little")
78+
f = open(join(env.subst("$BUILD_DIR"), "partition.bin"), "wb")
79+
f.write(part)
80+
f.close()
81+
# Generate zip file for flasher
82+
print("Creating final zip")
83+
firm = ZipFile(target_firm.get_abspath(), "w")
84+
firm.write(join(env.subst("$BUILD_DIR"), "download.json"), "download.json")
85+
firm.write(join(env.subst("$BUILD_DIR"), "partition.bin"), "partition.bin")
86+
firm.write(join(imagepath, mcu, "flasher.img"), "flasher.img")
87+
firm.write(join(imagepath, "flashinfo_" + flashsz + ".bin"), "flashinfo.bin")
88+
firm.write(join(imagepath, mcu, "preboot.img"), "preboot.img")
89+
firm.write(target_img, env.subst("$PROGNAME") + ".bin")
90+

0 commit comments

Comments
 (0)