zephyr-configurator/script.sh

421 lines
13 KiB
Bash
Raw Permalink Normal View History

2023-07-31 08:47:57 +00:00
#!/bin/bash
DIALOG_CANCEL=1
DIALOG_ESC=255
HEIGHT=-1
WIDTH=-1
# Choices
2023-09-08 11:28:04 +00:00
PLATTFORM=esp32_devkitc_wroom
2023-07-31 08:47:57 +00:00
CONNECTIVITY=
INTERFACES=
PROTOCOLS=
SENSORS=
ACTUATORS=
display_result() {
dialog --title "$1" \
--backtitle "Configure Zephyr RTOS" \
--no-collapse \
--msgbox "$2" -1 -1
}
main_dialog() {
selection=$(dialog --stdout \
--backtitle "Configure Zephyr RTOS" \
--title "Menu" \
--no-cancel \
--menu "Please select:" $HEIGHT $WIDTH 4 \
"1" "Init Zephyr workspace" \
"2" "Create empty project" \
"3" "Add additional functionalities" \
"4" "Build project" \
"5" "Flash project" \
"6" "Clean build directory" \
2023-09-08 11:28:04 +00:00
"7" "Get Shell" \
"8" "Exit")
2023-07-31 08:47:57 +00:00
case $selection in
1 )
init_workspace
;;
2 )
init_project
;;
3 )
settings_dialog
;;
4 )
build_project
;;
5 )
flash_project
;;
6 )
clean_build_dir
;;
7 )
2023-09-08 11:28:04 +00:00
clear
/bin/bash
;;
8 )
2023-07-31 08:47:57 +00:00
clear
exit
;;
esac
}
settings_dialog() {
selection=$(dialog --stdout \
--backtitle "Configure Zephyr RTOS" \
--title "Menu" \
--cancel-label "Exit" \
--menu "Please select:" $HEIGHT $WIDTH 4 \
"1" "Connectivity" \
"2" "Protocols" \
"3" "Interfaces" \
"4" "Sensors" \
"5" "Actuators" )
case $selection in
1 )
connectivity_dialog
;;
2 )
protocols_dialog
;;
3 )
interfaces_dialog
;;
4 )
sensors_dialog
;;
5 )
actuators_dialog
;;
esac
}
plattform_dialog() {
plattform=$( dialog --stdout \
--backtitle "Plattform" \
--default-item "$PLATTFORM" \
--no-tags \
--no-cancel \
--radiolist "Select plattform:" $HEIGHT $WIDTH 4 \
esp32c3_devkitm "ESP32-C3" $(equals "$PLATTFORM" "esp32c3_devkitm") \
2023-09-08 11:28:04 +00:00
esp32_devkitc_wroom "ESP32" $(equals "$PLATTFORM" "esp32_devkitc_wroom") \
2023-07-31 08:47:57 +00:00
nrf5340dk_nrf5340_cpuapp "nRF5340 DK" $(equals "$PLATTFORM" "nrf5340dk_nrf5340_cpuapp") \
nrf52840dongle_nrf52840 "nRF52840 Dongle" $(equals "$PLATTFORM" "nrf52840dongle_nrf52840") \
thingy91_nrf9160_ns "Thingy:91 (nRF9160)" $(equals "$PLATTFORM" "thingy91_nrf9160_ns") \
thingy91_nrf52840 "Thingy:91 (nRF52840)" $(equals "$PLATTFORM" "thingy91_nrf52840") )
if [ ! -z "$plattform" ]; then
PLATTFORM=$plattform
fi
}
2023-09-08 11:28:04 +00:00
ble_dialog() {
# TODO
ble=$( dialog --stdout \
--backtitle "Connectivity" \
--menu "Please select:" $HEIGHT $WIDTH 4 \
1 "WiFi" \
2 "Bluetooth" \
3 "BLE" \
4 "LoRaWAN" \
5 "NB-IoT")
case $connectivity in
1 ) # WiFi
# TODO
;;
2 ) # Bluetooth
sed -i '/int main(void)/e cat /opt/snippets/ble_gatt/code1.txt' /workdir/app/src/main.c
code2=$(cat /opt/snippets/ble_gatt/code2.txt)
echo "$code2" >> /workdir/app/src/peripheral_gatt_write.c
code3=$(cat /opt/snippets/ble_gatt/code3.txt)
echo "$code3" >> /workdir/app/src/gatt_write_common.c
config=$(cat /opt/snippets/ble_gatt/config.txt)
echo "$config" >> /workdir/app/prj.conf
display_result 'Added functionality' "Added functionality successfully at the end of main.c.\nYou have to call the function like in the example manually."
;;
3 ) # BLE
;;
4 ) # LoRaWAN
# TODO
;;
5 ) # NB-IoT
# TODO
;;
esac
}
2023-07-31 08:47:57 +00:00
connectivity_dialog() {
2023-08-03 06:20:34 +00:00
# TODO
2023-07-31 08:47:57 +00:00
connectivity=$( dialog --stdout \
--backtitle "Connectivity" \
2023-08-03 06:20:34 +00:00
--menu "Please select:" $HEIGHT $WIDTH 4 \
1 "WiFi" \
2 "Bluetooth" \
2023-09-08 11:28:04 +00:00
3 "BLE" \
4 "LoRaWAN" \
5 "NB-IoT")
2023-07-31 08:47:57 +00:00
2023-08-03 06:20:34 +00:00
case $connectivity in
1 ) # WiFi
# TODO
;;
2 ) # Bluetooth
2023-09-08 11:28:04 +00:00
sed -i '/int main(void)/e cat /opt/snippets/ble_gatt/code1.txt' /workdir/app/src/main.c
2023-08-04 08:27:09 +00:00
2023-09-08 11:28:04 +00:00
code2=$(cat /opt/snippets/ble_gatt/code2.txt)
2023-08-04 08:27:09 +00:00
echo "$code2" >> /workdir/app/src/peripheral_gatt_write.c
2023-09-08 11:28:04 +00:00
code3=$(cat /opt/snippets/ble_gatt/code3.txt)
2023-08-04 08:27:09 +00:00
echo "$code3" >> /workdir/app/src/gatt_write_common.c
2023-09-08 11:28:04 +00:00
config=$(cat /opt/snippets/ble_gatt/config.txt)
2023-08-04 08:27:09 +00:00
echo "$config" >> /workdir/app/prj.conf
display_result 'Added functionality' "Added functionality successfully at the end of main.c.\nYou have to call the function like in the example manually."
2023-08-03 06:20:34 +00:00
;;
2023-09-08 11:28:04 +00:00
3 ) # BLE
features=$(dialog --stdout \
--backtitle "BLE features" \
--checklist "Select BLE features:" $HEIGHT $WIDTH 4 \
2 "LED" ON \
3 "Button" ON )
# Base
sed -i '/int main(void)/e cat /opt/snippets/bluetooth/base/code1.txt' /workdir/app/src/main.c
config=$(cat /opt/snippets/bluetooth/base/config.txt)
echo "$config" >> /workdir/app/prj.conf
# Button
if [[ "$features" == *"3"* ]]; then
sed -i '/#include <zephyr\/bluetooth\/gatt.h>/a#include "button_svc.h"' /workdir/app/src/main.c
sed -i '/LOG_MODULE_REGISTER(main);/astatic uint16_t but_val;' /workdir/app/src/main.c
sed -i '/static struct bt_uuid_128 st_service_uuid = BT_UUID_INIT_128(BT_UUID_128_ENCODE(0x0000fe40, 0xcc7a, 0x482a, 0x984a, 0x7f2ed5b3e58f));/astatic struct bt_uuid_128 but_notif_uuid = BT_UUID_INIT_128(BT_UUID_128_ENCODE(0x0000fe42, 0x8e22, 0x4541, 0x9d4c, 0x21edae82ed19));' /workdir/app/src/main.c
sed -i '/BT_GATT_PRIMARY_SERVICE(&st_service_uuid),/aBT_GATT_CHARACTERISTIC(&but_notif_uuid.uuid, BT_GATT_CHRC_NOTIFY, BT_GATT_PERM_READ, NULL, NULL, &but_val),' /workdir/app/src/main.c
sed -i '/static void bt_ready(int err)/e cat /opt/snippets/bluetooth/button/code1.txt' /workdir/app/src/main.c
sed -i '/int main(void)/e cat /opt/snippets/bluetooth/button/code2.txt' /workdir/app/src/main.c
cp /opt/snippets/bluetooth/button/button_svc.h /workdir/app/src/
cp /opt/snippets/bluetooth/button/button_svc.c /workdir/app/src/
fi
# LED
if [[ "$features" == *"2"* ]]; then
sed -i '/#include <zephyr\/bluetooth\/gatt.h>/a#include "led_svc.h"' /workdir/app/src/main.c
sed -i '/static struct bt_uuid_128 st_service_uuid = BT_UUID_INIT_128(BT_UUID_128_ENCODE(0x0000fe40, 0xcc7a, 0x482a, 0x984a, 0x7f2ed5b3e58f));/astatic struct bt_uuid_128 led_char_uuid = BT_UUID_INIT_128(BT_UUID_128_ENCODE(0x0000fe41, 0x8e22, 0x4541, 0x9d4c, 0x21edae82ed19));' /workdir/app/src/main.c
sed -i '/BT_GATT_PRIMARY_SERVICE(&st_service_uuid),/aBT_GATT_CHARACTERISTIC(&led_char_uuid.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE_WITHOUT_RESP, BT_GATT_PERM_WRITE, NULL, recv, (void *)1),' /workdir/app/src/main.c
sed -i '/static void bt_ready(int err)/e cat /opt/snippets/bluetooth/led/code1.txt' /workdir/app/src/main.c
sed -i '/int main(void)/e cat /opt/snippets/bluetooth/led/code2.txt' /workdir/app/src/main.c
cp /opt/snippets/bluetooth/led/led_svc.h /workdir/app/src/
cp /opt/snippets/bluetooth/led/led_svc.c /workdir/app/src/
fi
display_result 'Added functionality' "Added functionality successfully at the end of main.c.\nYou have to call the function like in the example manually."
;;
4 ) # LoRaWAN
2023-08-03 06:20:34 +00:00
# TODO
;;
2023-09-08 11:28:04 +00:00
5 ) # NB-IoT
2023-08-03 06:20:34 +00:00
# TODO
;;
esac
2023-07-31 08:47:57 +00:00
}
protocols_dialog() {
2023-08-03 06:20:34 +00:00
# TODO
2023-07-31 08:47:57 +00:00
protocols=$( dialog --stdout \
--backtitle "Protocols" \
2023-08-03 06:20:34 +00:00
--menu "Please select:" $HEIGHT $WIDTH 4 \
1 "MQTT" \
2 "REST" \
3 "CoAP")
2023-07-31 08:47:57 +00:00
2023-08-03 06:20:34 +00:00
case $protocols in
1 ) # MQTT
# TODO
;;
2 ) # REST
# TODO
;;
3 ) # CoAP
# TODO
;;
esac
2023-07-31 08:47:57 +00:00
}
interfaces_dialog() {
2023-08-03 06:20:34 +00:00
# TODO
2023-07-31 08:47:57 +00:00
interfaces=$( dialog --stdout \
--backtitle "Interfaces" \
2023-08-03 06:20:34 +00:00
--menu "Please select:" $HEIGHT $WIDTH 4 \
1 "I2C" \
2 "SPI" \
3 "UART")
2023-07-31 08:47:57 +00:00
2023-08-03 06:20:34 +00:00
case $interfaces in
1 ) # I2C
# TODO
;;
2 ) # SPI
# TODO
;;
esac
2023-07-31 08:47:57 +00:00
}
sensors_dialog() {
2023-08-03 06:20:34 +00:00
# TODO
2023-07-31 08:47:57 +00:00
sensors=$( dialog --stdout \
--backtitle "Sensors" \
2023-08-03 06:20:34 +00:00
--menu "Please select:" $HEIGHT $WIDTH 4 \
1 "Button" \
2 "Sensor 2" \
3 "Sensor 3" \
4 "Sensor 4" )
2023-07-31 08:47:57 +00:00
2023-08-03 06:20:34 +00:00
case $sensors in
1 ) # Button
2023-09-08 11:28:04 +00:00
sed -i '/int main(void)/e cat /opt/snippets/button/code.txt' /workdir/app/src/main.c
2023-08-03 12:44:16 +00:00
2023-09-08 11:28:04 +00:00
config=$(cat /opt/snippets/button/config.txt)
2023-08-03 12:44:16 +00:00
echo "$config" >> /workdir/app/prj.conf
2023-08-04 08:27:09 +00:00
2023-08-03 12:44:16 +00:00
display_result 'Added functionality' "Added functionality successfully at the end of main.c.\nYou have to call the function like in the example manually."
2023-08-03 06:20:34 +00:00
;;
2 )
# TODO
;;
3 )
# TODO
;;
esac
2023-07-31 08:47:57 +00:00
}
actuators_dialog() {
2023-08-03 06:20:34 +00:00
# TODO
2023-07-31 08:47:57 +00:00
actuators=$( dialog --stdout \
--backtitle "Actuators" \
2023-08-03 06:20:34 +00:00
--menu "Please select:" $HEIGHT $WIDTH 4 \
1 "LED" \
2 "Actuators 2" \
3 "Actuators 3" \
4 "Actuators 4" )
2023-07-31 08:47:57 +00:00
2023-08-03 06:20:34 +00:00
case $actuators in
1 ) # LED
2023-09-08 11:28:04 +00:00
sed -i '/int main(void)/e cat /opt/snippets/led/code.txt' /workdir/app/src/main.c
2023-08-04 08:27:09 +00:00
2023-09-08 11:28:04 +00:00
config=$(cat /opt/snippets/led/config.txt)
2023-08-03 12:44:16 +00:00
echo "$config" >> /workdir/app/prj.conf
2023-08-04 08:27:09 +00:00
2023-08-03 12:44:16 +00:00
display_result 'Added functionality' "Added functionality successfully at the end of main.c.\nYou have to call the function like in the example manually."
2023-08-03 06:20:34 +00:00
;;
2 )
# TODO
;;
3 )
# TODO
;;
4 )
# TODO
;;
esac
2023-07-31 08:47:57 +00:00
}
init_workspace() {
cd /workdir
{ west init; west update; west zephyr-export; echo "Created workspace successfully"; } 2>&1 | dialog --programbox -1 -1
}
init_project() {
mkdir /workdir/app
mkdir /workdir/app/src
cp /opt/data/main.c /workdir/app/src/main.c
cp /opt/data/app.overlay /workdir/app/app.overlay
cp /opt/data/CMakeLists.txt /workdir/app/CMakeLists.txt
cp /opt/data/prj.conf /workdir/app/prj.conf
cp /opt/data/VERSION /workdir/app/VERSION
display_result 'Created project' "Project created successfully.\nThe project is located in /workdir.\nYou can edit it by using your favorite IDE."
}
build_project() {
plattform_dialog
2023-08-04 08:27:09 +00:00
clear
cd /workdir/app/
west build -b $PLATTFORM
read -p "Press any key to continue... " -n1 -s
2023-07-31 08:47:57 +00:00
}
flash_project() {
plattform_dialog
2023-08-03 06:20:34 +00:00
2023-07-31 08:47:57 +00:00
case "$PLATTFORM" in
"nrf52840dongle_nrf52840")
2023-09-08 11:28:04 +00:00
display_result 'Flash project' \
"Please run those commands in an environment where nrfutil and the compiled hex files are available: \
\\n\\n\\nnrfutil pkg generate --hw-version 52 --sd-req=0x00 --application build/zephyr/zephyr.hex --application-version 1 mcuboot.zip \
\\n\\nnrfutil dfu usb-serial -pkg mcuboot.zip -p /dev/ttyACM0" ;;
"esp32_devkitc_wroom")
display_result 'Flash project' \
"Please run those commands in an environment where esptool.py and the compiled bin files are available: \
\\n\\n\\nesptool.py --chip auto --baud 921600 --before default_reset --after hard_reset write_flash -u --flash_mode dio --flash_freq 40m --flash_size detect 0x1000 app/build/esp-idf/build/bootloader/bootloader.bin 0x8000 app/build/esp-idf/build/partitions_singleapp.bin 0x10000 app/build/zephyr/zephyr.bin"
2023-07-31 08:47:57 +00:00
;;
*)
display_result "Error" "Board not configured in this tool yet\nSee https://docs.zephyrproject.org/latest/boards/index.html#boards"
;;
esac
}
clean_build_dir() {
rm -rf /workdir/app/build/*
2023-08-03 06:20:34 +00:00
2023-07-31 08:47:57 +00:00
display_result "Cleaned build" "Cleaned build directory successfully"
}
in_array() {
if [[ $1 =~ (^|[[:space:]])"$2"($|[[:space:]]) ]] ; then
echo on;
else
echo off;
fi
}
equals() {
if [[ $1 == "$2" ]] ; then
echo on;
else
echo off;
fi
}
if ! command -v dialog &> /dev/null
then
sudo apt update && sudo apt install dialog
fi
2023-09-08 11:28:04 +00:00
cd /workdir
2023-07-31 08:47:57 +00:00
while true; do
main_dialog
done