Skip to content
This repository was archived by the owner on Apr 16, 2021. It is now read-only.

Commit a38f0bd

Browse files
committed
mbed-os-to-arduino-v2
1 parent b2c9c01 commit a38f0bd

File tree

1 file changed

+240
-74
lines changed

1 file changed

+240
-74
lines changed

mbed-os-to-arduino

Lines changed: 240 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,265 @@
1-
#!/bin/bash -ex
1+
#!/usr/bin/env bash
22

3-
MBED_CORE_LOCATION=$1
4-
BOARDNAMES=(ARDUINO_NANO33BLE)
3+
set -e
54

6-
#always work in /tmp
7-
cd /tmp/
5+
check_tools () {
6+
if not hash jq &>/dev/null ; then
7+
echo "Please, install jq."
8+
exit 1
9+
fi
810

9-
if [ ! -d mbed-os-program ]; then
10-
mbed new mbed-os-program
11-
fi
12-
cd mbed-os-program
13-
# checkout the mbed-os version you prefer
14-
# cd mbed-os && git checkout --track origin/feature-hal-spec-usb-device && cd ..
11+
if not hash rsync &>/dev/null ; then
12+
echo "Please, install rsync."
13+
exit 1
14+
fi
15+
}
1516

16-
if [ ! -f gron ]; then
17-
echo "Please copy gron executable here"
18-
exit 1
19-
fi
17+
mbed_new () {
18+
#always work in /tmp
19+
cd /tmp/
2020

21-
for BOARDNAME in "${BOARDNAMES[@]}"
22-
do
21+
if [ ! -d mbed-os-program ]; then
22+
mbed new mbed-os-program
23+
fi
24+
cd mbed-os-program
25+
}
2326

24-
export ARDUINOCORE=$MBED_CORE_LOCATION/variants/$BOARDNAME/
27+
mbed_revision () {
2528

26-
if [ -d $ARDUINOCORE ]; then
27-
mkdir -p $ARDUINOCORE/libs
28-
fi
29+
if [ "$MBED_UPDATE" -eq 1 ]; then
30+
set +e
31+
mbed update
32+
set -e
33+
fi
2934

30-
mbed target $BOARDNAME
31-
mbed toolchain GCC_ARM
32-
echo -e "#include \"mbed.h\"\nint main() {Thread thd;}" > main.cpp
33-
echo -e "{
34-
\"macros\": [
35-
\"MBED_HEAP_STATS_ENABLED=1\",
36-
\"MBED_STACK_STATS_ENABLED=1\",
37-
\"MBED_MEM_TRACING_ENABLED=1\",
38-
],
39-
\"target_overrides\": {
40-
\"*\": {
41-
\"platform.stdio-buffered-serial\": true,
42-
\"platform.stdio-baud-rate\": 115200,
43-
\"platform.default-serial-baud-rate\": 115200,
44-
}
45-
}
46-
}" > mbed_app.json
47-
48-
if [ $BOARDNAME == "ARDUINO_NANO33BLE" ]; then
49-
echo -e "{
50-
\"macros\": [
51-
\"MBED_HEAP_STATS_ENABLED=1\",
52-
\"MBED_STACK_STATS_ENABLED=1\",
53-
\"MBED_MEM_TRACING_ENABLED=1\",
54-
\"NRFX_GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS=8\"
35+
if [ -n "$REMOTE_BRANCH" ]; then
36+
# checkout the mbed-os version you prefer...
37+
cd mbed-os
38+
git checkout --track "$REMOTE_BRANCH"
39+
cd ..
40+
fi
41+
42+
if [ -n "$LOCAL_REPO" ]; then
43+
# ... or link your local repo
44+
if [ -d mbed-os ]; then
45+
if [ ! -L mbed-os ]; then
46+
rm -rf mbed-os
47+
ln -s ~/Work/Devel/BCMI/Code/SDKs/mbed-os .
48+
fi
49+
fi
50+
fi
51+
52+
}
53+
54+
create_mbed_program () {
55+
rm -rf .mbedignore
56+
57+
mbed target "$BOARDNAME"
58+
mbed toolchain GCC_ARM
59+
60+
cat > main.cpp << MAIN_C
61+
#include "mbed.h"
62+
int main() {}
63+
MAIN_C
64+
65+
if [ ! -f "$ARDUINOVARIANT"/conf/mbed_app.json ]; then
66+
echo "================================================"
67+
echo "Please, consider creating a 'conf/mbed_app.json'"
68+
echo "to avoid mbed-cli always recompile from scratch."
69+
echo "================================================"
70+
cat > mbed_app.json << MBED_APP
71+
{
72+
"macros": [
73+
"MBED_HEAP_STATS_ENABLED=1",
74+
"MBED_STACK_STATS_ENABLED=1",
75+
"MBED_MEM_TRACING_ENABLED=1",
5576
],
56-
\"target_overrides\": {
57-
\"*\": {
58-
\"platform.stdio-buffered-serial\": true,
59-
\"platform.stdio-baud-rate\": 115200,
60-
\"platform.default-serial-baud-rate\": 115200,
61-
\"rtos.main-thread-stack-size\": 32768,
62-
\"target.mbed_app_start\": \"0x10000\"
77+
"target_overrides": {
78+
"*": {
79+
"platform.stdio-buffered-serial": true,
80+
"platform.stdio-baud-rate": 115200,
81+
"platform.default-serial-baud-rate": 115200,
6382
}
6483
}
65-
}" > mbed_app.json
84+
}
85+
MBED_APP
86+
fi
87+
88+
if [ -d "$ARDUINOVARIANT"/conf ]; then
89+
for conf in "$ARDUINOVARIANT"/conf/*; do
90+
cp -p "$conf" .
91+
done
92+
fi
93+
}
94+
95+
96+
apply_patches () {
97+
98+
if [ -d "$MBED_CORE_LOCATION"/patches ]; then
99+
for p in "$MBED_CORE_LOCATION"/patches/* ; do
100+
patch -p1 -t -d mbed-os -i "$p"
101+
done
102+
fi
103+
}
104+
105+
mbed_compile () {
106+
107+
if [ "$MBED_CLEAN" -eq 1 ]; then
108+
rm -rf BUILD
109+
fi
110+
111+
mbed compile --source . --source ./mbed-os/features/unsupported/dsp/ -v | tee >(cat | grep 'Compile \[' >&2) | grep "Macros:" > $BOARDNAME.macros.txt
112+
}
113+
114+
generate_defines () {
115+
cut -f2 -d":" < "$BOARDNAME".macros.txt | tr ' ' '\n' | sed 's#\"#\\"#g' | sort > "$ARDUINOVARIANT"/defines.txt
116+
echo "-DMBED_NO_GLOBAL_USING_DIRECTIVE=1" >> "$ARDUINOVARIANT"/defines.txt
117+
if [ -f "$ARDUINOVARIANT"/variant.cpp ]; then
118+
echo '-DUSE_ARDUINO_PINOUT' >> "$ARDUINOVARIANT"/defines.txt
119+
fi
120+
}
121+
122+
generate_includes () {
123+
cat ./BUILD/"$BOARDNAME"/GCC_ARM/.include* | tr ' ' '\n' | tr -d '"' | sed -e 's#-I./mbed-os#-iwithprefixbefore/mbed#g' | sed '/^-I./d' | tac > "$ARDUINOVARIANT"/includes.txt
124+
}
125+
126+
generate_flags () {
127+
for fl in c cxx ld; do
128+
jq -r '.flags | .[] | select(. != "-MMD")' ./BUILD/"$BOARDNAME"/GCC_ARM/.profile-${fl} > "$ARDUINOVARIANT"/${fl}flags.txt
129+
done
130+
}
131+
132+
generate_libs () {
133+
tr ' ' '\n' < ./BUILD/"$BOARDNAME"/GCC_ARM/.link_options.txt | grep "\.o" | grep -v "/main\.o" | xargs arm-none-eabi-ar rcs ./BUILD/mbed-core-$BOARDNAME.a
134+
cp ./BUILD/mbed-core-"$BOARDNAME".a "$ARDUINOVARIANT"/libs/libmbed.a
135+
cp ./BUILD/"$BOARDNAME"/GCC_ARM/.link_script.ld "$ARDUINOVARIANT"/linker_script.ld
136+
cp ./BUILD/"$BOARDNAME"/GCC_ARM/mbed_config.h "$ARDUINOVARIANT"/
137+
find -L . -name 'libcc_310_*.a' -exec cp '{}' "$ARDUINOVARIANT"/libs/ ';'
138+
}
139+
140+
copy_core_files () {
141+
rsync -zar --include="*/" --include="*.h" --exclude="*" mbed-os/ "$ARDUINOCOREMBED"/
142+
cp mbed-os/platform/cxxsupport/mstd_* "$ARDUINOCOREMBED"/platform/cxxsupport/
143+
}
144+
145+
patch_mbed_h () {
146+
ed "$ARDUINOCOREMBED"/mbed.h <<EOF
147+
/#include "platform\/mbed_version.h"/
148+
a
149+
#include "mbed_config.h"
150+
.
151+
wq
152+
EOF
153+
}
154+
155+
patch_spi_h () {
156+
mv "$ARDUINOCOREMBED"/drivers/SPI.h "$ARDUINOCOREMBED"/drivers/SPIMaster.h
157+
for header in mbed.h features/nfc/controllers/PN512SPITransportDriver.h components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.h; do
158+
sed -i.bak 's#drivers/SPI\.h#drivers/SPIMaster\.h#g' "$ARDUINOCOREMBED"/$header
159+
rm "$ARDUINOCOREMBED"/$header.bak
160+
done
161+
}
162+
163+
while getopts "cur:b:p:" opt; do
164+
case $opt in
165+
c ) export MBED_CLEAN=1 ;;
166+
u ) export MBED_UPDATE=1 ;;
167+
r ) export LOCAL_REPO="$OPTARG" ;;
168+
b ) export REMOTE_BRANCH="$OPTARG" ;;
169+
p )
170+
MBED_CORE_LOCATION="$(cd "$OPTARG" && pwd -P)"
171+
export MBED_CORE_LOCATION ;;
172+
* )
173+
echo "Unknown parameter."
174+
exit 1
175+
;;
176+
esac
177+
done
178+
179+
# Done with processing all the starting flags with getopts...
180+
shift $(( OPTIND - 1 ))
181+
182+
if [ $# -eq 0 ] ; then
183+
echo "Usage: $(basename "$0") [-c] [-u] [-r /path/to/local/repo] [-b mbed/remote/branch] [-p /mbed/core/location] [VARIANT1:BOARD1 VARIANT2:BOARD1 VARIANT3:BOARD3 ... ]"
184+
echo " -c clean Mbed application BUILD directory"
185+
echo " -u Update to latest mbed-os release"
186+
echo " -r specify remote mbed-os branch to checkout"
187+
echo " -b specify local mbed-os directory to link"
188+
echo " -p specify local mbed core directory (defaults to PWD)"
189+
exit 0
66190
fi
67191

68-
rm -rf BUILD
69192

70-
mbed compile --source . --source ./mbed-os/features/unsupported/dsp/ -v | grep "Macros:" > $BOARDNAME.macros.txt
71-
cat $BOARDNAME.macros.txt | cut -f2 -d":" | sed 's# #\n#g' | sed 's#\"#\\"#g' | sort > $ARDUINOCORE/defines.txt
72-
echo "-DMBED_NO_GLOBAL_USING_DIRECTIVE=1" >> $ARDUINOCORE/defines.txt
73-
xargs -n 1 < ./BUILD/$BOARDNAME/GCC_ARM/.include* | sed -e 's#-I./mbed-os#-iwithprefixbefore/mbed#g' | sed '/^-I./ d' | tac > $ARDUINOCORE/includes.txt
193+
declare -A VARIANT_BOARDS
194+
# VARIANT_BOARDS[MTB_MURATA_ABZ]="MTB_MURATA_ABZ"
195+
196+
TUPLES=( "$@" )
74197

75-
./gron ./BUILD/$BOARDNAME/GCC_ARM/.profile-c | grep json.flags | cut -f2 -d"\"" | grep -v "json.flags" | grep -v "MMD" > $ARDUINOCORE/cflags.txt
76-
./gron ./BUILD/$BOARDNAME/GCC_ARM/.profile-cxx | grep json.flags | cut -f2 -d"\"" | grep -v "json.flags" | grep -v "MMD" > $ARDUINOCORE/cxxflags.txt
77-
./gron ./BUILD/$BOARDNAME/GCC_ARM/.profile-ld | grep json.flags | cut -f2 -d"\"" | grep -v "json.flags" | grep -v "MMD" > $ARDUINOCORE/ldflags.txt
198+
for tuple in "${TUPLES[@]}"; do
199+
OLD_IFS="$IFS"
200+
IFS=":"
201+
set -- $tuple
202+
VARIANT_BOARDS[$1]="$2"
203+
IFS="$OLD_IFS"
204+
done
205+
206+
# VARIANT=$2
207+
# BOARDNAMES=( ${BOARDNAMES:=MTB_MURATA_ABZ} )
208+
209+
export MBED_CORE_LOCATION=${MBED_CORE_LOCATION:-$PWD}
210+
export MBED_CLEAN=${MBED_CLEAN:-0}
211+
export MBED_UPDATE=${MBED_UPDATE:-0}
212+
export LOCAL_REPO=${LOCAL_REPO:-""}
213+
export REMOTE_BRANCH=${REMOTE_BRANCH:-""}
78214

79-
arm-none-eabi-ar rcs BUILD/mbed-core-$BOARDNAME.a `xargs -n 1 < ./BUILD/$BOARDNAME/GCC_ARM/.link_options.txt | grep "\.o" | grep -v "/main\.o"`
80-
cp BUILD/mbed-core-$BOARDNAME.a $ARDUINOCORE/libs/libmbed.a
81-
cp ./BUILD/$BOARDNAME/GCC_ARM/.link_script.ld $ARDUINOCORE/linker_script.ld
82-
cp ./BUILD/$BOARDNAME/GCC_ARM/mbed_config.h $ARDUINOCORE/
215+
echo MBED_CLEAN=$MBED_CLEAN
216+
echo MBED_UPDATE=$MBED_UPDATE
217+
echo LOCAL_REPO=$LOCAL_REPO
218+
echo REMOTE_BRANCH=$REMOTE_BRANCH
219+
echo MBED_CORE_LOCATION=$MBED_CORE_LOCATION
83220

221+
for variant in ${!VARIANT_BOARDS[*]}; do
222+
echo "VARIANT=$variant BOARD=${VARIANT_BOARDS[$variant]}"
84223
done
85224

86-
rm -rf $ARDUINOCORE/../../cores/arduino/mbed/*
87-
cd mbed-os && find . | grep "\.h" | xargs -I % cp --parents % $ARDUINOCORE/../../cores/arduino/mbed && cd ..
88-
cp mbed-os/platform/cxxsupport/mstd_* $ARDUINOCORE/../../cores/arduino/mbed/platform/cxxsupport/
225+
check_tools
226+
mbed_new
227+
mbed_revision
228+
229+
# for MBED_BOARD in "${BOARDNAMES[@]}"; do
230+
for VARIANT in ${!VARIANT_BOARDS[*]}; do
231+
export BOARDNAME="${VARIANT_BOARDS[$variant]}"
232+
export ARDUINOVARIANT=$MBED_CORE_LOCATION/variants/$VARIANT
233+
export ARDUINOCOREMBED=$MBED_CORE_LOCATION/cores/arduino/mbed
234+
235+
if [ -d "$ARDUINOVARIANT" ]; then
236+
mkdir -p "$ARDUINOVARIANT"/libs
237+
fi
89238

90-
#TODO: add #include "mbed_config.h" in "mbed.h"
91-
#TODO: rename drivers/SPI.h in drivers/SPIMaster.h and modify include in mbed.h
92-
mv $ARDUINOCORE/../../cores/arduino/mbed/drivers/SPI.h $ARDUINOCORE/../../cores/arduino/mbed/drivers/SPIMaster.h
239+
create_mbed_program
240+
apply_patches
241+
mbed_compile
242+
generate_defines
243+
generate_includes
244+
generate_flags
245+
generate_libs
246+
done
247+
248+
copy_core_files
249+
patch_mbed_h
250+
patch_spi_h
93251

94252
# TODO
95-
# - Add -DUSE_ARDUINO_PINOUT to targets with a variant.cpp
96-
#
253+
# - Add include path for rpc library to envie
254+
# - Remove -fno-exception from Envie cppflags
97255
#
98256

99257
exit 0
258+
259+
##################
260+
261+
Using Arduino as an mbed library
262+
263+
echo -e "arduino/cores/arduino/main.cpp\n arduino/cores/arduino/mbed/\narduino/libraries/" > .mbedignore
264+
#add ARDUINO_AS_MBED_LIBRARY=1 to macros section in mbed_app.json
265+
echo "https://github.com/arduino/ArduinoCore-mbed#bf6e64771ebe20285b0364756dff856ebbc679dc" > arduino.lib

0 commit comments

Comments
 (0)