Skip to content

Commit 88452c4

Browse files
authored
Merge pull request #1603 from oltaco/fix-build.sh-for-RP2040-and-STM32
Add RP2040 and STM32 support to build.sh
2 parents 2220eca + 5df139f commit 88452c4

2 files changed

Lines changed: 42 additions & 14 deletions

File tree

build.sh

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ case $1 in
6464
;;
6565
esac
6666

67+
# cache project config json for use in get_platform_for_env()
68+
PIO_CONFIG_JSON=$(pio project config --json-output)
6769

6870
# $1 should be the string to find (case insensitive)
6971
get_pio_envs_containing_string() {
@@ -87,6 +89,25 @@ get_pio_envs_ending_with_string() {
8789
done
8890
}
8991

92+
# get platform flag for a given environment
93+
# $1 should be the environment name
94+
get_platform_for_env() {
95+
local env_name=$1
96+
echo "$PIO_CONFIG_JSON" | python3 -c "
97+
import sys, json, re
98+
data = json.load(sys.stdin)
99+
for section, options in data:
100+
if section == 'env:$env_name':
101+
for key, value in options:
102+
if key == 'build_flags':
103+
for flag in value:
104+
match = re.search(r'(ESP32_PLATFORM|NRF52_PLATFORM|STM32_PLATFORM|RP2040_PLATFORM)', flag)
105+
if match:
106+
print(match.group(1))
107+
sys.exit(0)
108+
"
109+
}
110+
90111
# disable all debug logging flags if DISABLE_DEBUG=1 is set
91112
disable_debug_flags() {
92113
if [ "$DISABLE_DEBUG" == "1" ]; then
@@ -96,6 +117,8 @@ disable_debug_flags() {
96117

97118
# build firmware for the provided pio env in $1
98119
build_firmware() {
120+
# get env platform for post build actions
121+
ENV_PLATFORM=($(get_platform_for_env $1))
99122

100123
# get git commit sha
101124
COMMIT_HASH=$(git rev-parse --short HEAD)
@@ -126,27 +149,31 @@ build_firmware() {
126149
# build firmware target
127150
pio run -e $1
128151

129-
# build merge-bin for esp32 fresh install
130-
if [ -f .pio/build/$1/firmware.bin ]; then
152+
# build merge-bin for esp32 fresh install, copy .bins to out folder (e.g: Heltec_v3_room_server-v1.0.0-SHA.bin)
153+
if [ "$ENV_PLATFORM" == "ESP32_PLATFORM" ]; then
131154
pio run -t mergebin -e $1
155+
cp .pio/build/$1/firmware.bin out/${FIRMWARE_FILENAME}.bin 2>/dev/null || true
156+
cp .pio/build/$1/firmware-merged.bin out/${FIRMWARE_FILENAME}-merged.bin 2>/dev/null || true
132157
fi
133158

134-
# build .uf2 for nrf52 boards
135-
if [[ -f .pio/build/$1/firmware.zip && -f .pio/build/$1/firmware.hex ]]; then
159+
# build .uf2 for nrf52 boards, copy .uf2 and .zip to out folder (e.g: RAK_4631_Repeater-v1.0.0-SHA.uf2)
160+
if [ "$ENV_PLATFORM" == "NRF52_PLATFORM" ]; then
136161
python3 bin/uf2conv/uf2conv.py .pio/build/$1/firmware.hex -c -o .pio/build/$1/firmware.uf2 -f 0xADA52840
162+
cp .pio/build/$1/firmware.uf2 out/${FIRMWARE_FILENAME}.uf2 2>/dev/null || true
163+
cp .pio/build/$1/firmware.zip out/${FIRMWARE_FILENAME}.zip 2>/dev/null || true
137164
fi
138165

139-
# copy .bin, .uf2, and .zip to out folder
140-
# e.g: Heltec_v3_room_server-v1.0.0-SHA.bin
141-
# e.g: RAK_4631_Repeater-v1.0.0-SHA.uf2
142-
143-
# copy .bin for esp32 boards
144-
cp .pio/build/$1/firmware.bin out/${FIRMWARE_FILENAME}.bin 2>/dev/null || true
145-
cp .pio/build/$1/firmware-merged.bin out/${FIRMWARE_FILENAME}-merged.bin 2>/dev/null || true
166+
# for stm32, copy .bin and .hex to out folder
167+
if [ "$ENV_PLATFORM" == "STM32_PLATFORM" ]; then
168+
cp .pio/build/$1/firmware.bin out/${FIRMWARE_FILENAME}.bin 2>/dev/null || true
169+
cp .pio/build/$1/firmware.hex out/${FIRMWARE_FILENAME}.hex 2>/dev/null || true
170+
fi
146171

147-
# copy .zip and .uf2 of nrf52 boards
148-
cp .pio/build/$1/firmware.uf2 out/${FIRMWARE_FILENAME}.uf2 2>/dev/null || true
149-
cp .pio/build/$1/firmware.zip out/${FIRMWARE_FILENAME}.zip 2>/dev/null || true
172+
# for rp2040, copy .bin and .uf2 to out folder
173+
if [ "$ENV_PLATFORM" == "RP2040_PLATFORM" ]; then
174+
cp .pio/build/$1/firmware.bin out/${FIRMWARE_FILENAME}.bin 2>/dev/null || true
175+
cp .pio/build/$1/firmware.uf2 out/${FIRMWARE_FILENAME}.uf2 2>/dev/null || true
176+
fi
150177

151178
}
152179

platformio.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ platform = platformio/espressif32@6.11.0
5959
monitor_filters = esp32_exception_decoder
6060
extra_scripts = merge-bin.py
6161
build_flags = ${arduino_base.build_flags}
62+
-D ESP32_PLATFORM
6263
; -D ESP32_CPU_FREQ=80 ; change it to your need
6364
build_src_filter = ${arduino_base.build_src_filter}
6465

0 commit comments

Comments
 (0)