automate/020_zephyr.sh

164 lines
5.6 KiB
Bash

#!/usr/bin/env bash
# https://www.zephyrproject.org/zephyr-rtos-on-esp32/
# https://zmk.dev/docs/development/setup/
# Manual flash instructions https://github.com/seemoo-lab/openhaystack/wiki/Flashing-nRF-with-OpenOCD---ST-Link
# $ openocd \
# -f ${ZEPHYR_SDK_INSTALL_DIR}/sysroots/${MACHINE}-pokysdk-linux/usr/share/openocd/scripts/interface/stlink.cfg \
# -f ${ZEPHYR_SDK_INSTALL_DIR}/sysroots/${MACHINE}-pokysdk-linux/usr/share/openocd/scripts/target/nrf51.cfg
#
# $ telnet localhost 4444
# $ help
#
# You can backup the existing firmware:
# $ dump_image /Your/File/Path/flash.bin 0 0x40000
# $ dump_image /tmp/zephyr-shell_flash.bin 0 0x40000
#
# Erase the flash using
# $ nrf5 mass_erase
# Verify and program your device by using (use the .bin not .hex file)
# $ program /your/path/to/the/firmware.bin verify
# $ program /your/path/to/the/firmware.bin
#
# $ program /home/default/zephyrproject/zephyr/build/zephyr/zephyr.bin verify
# $ program /home/default/zephyrproject/zephyr/build/zephyr/zephyr.bin
#
# $ program /tmp/zephyr-shell_flash.bin verify
# $ program /tmp/zephyr-shell_flash.bin
#
# $ openocd \
# -f ${ZEPHYR_SDK_INSTALL_DIR}/sysroots/${MACHINE}-pokysdk-linux/usr/share/openocd/scripts/interface/stlink.cfg \
# -f ${ZEPHYR_SDK_INSTALL_DIR}/sysroots/${MACHINE}-pokysdk-linux/usr/share/openocd/scripts/target/nrf51.cfg
# -c init \
# -c "reset halt" \
# -c "nrf51 mass_erase" \
# -c "program build/zephyr/zephyr.hex verify reset" \
# -c exit
DEST=${1:-${HOME}}
ARCH=$(dpkg --print-architecture)
MACHINE=$(uname -m)
USER=zephyrproject-rtos
PROJECT=sdk-ng
API_URL=https://api.github.com/repos/${USER}/${PROJECT}/releases/latest
RELEASE=$(curl -s ${API_URL} | grep tarball_url | cut -d '"' -f 4 | awk -F "/" {'print $8'})
SDK_FILENAME=zephyr-sdk-${RELEASE#v}_linux-${MACHINE}_minimal.tar.xz # 41.8 MB
# SDK_FILENAME=zephyr-sdk-${RELEASE#v}_linux-${MACHINE}.tar.xz # 1.35 GB
SDK_URL=https://github.com/${USER}/${PROJECT}/releases/download/${RELEASE}/
# fetch the sdk
# wget ${SDK_URL}/${SDK_FILENAME}
# wget -O - ${SDK_URL}/sha256.sum | shasum --check --ignore-missing
SDK_DEST=${DEST}/zephyr-sdk-${RELEASE#v}
PROJECT_DEST=${DEST}/zephyrproject
# https://docs.zephyrproject.org/latest/develop/env_vars.html#env-vars-important
ZEPHYR_ENV_FILE="${HOME}/.zshrc.d/001_zephyr.zsh"
cat <<-EOF | tee ${ZEPHYR_ENV_FILE}
# zephyr specific
export ZEPHYR_TOOLCHAIN_VARIANT=zephyr
export ZEPHYR_SDK_INSTALL_DIR="${SDK_DEST}"
EOF
# use the sdk setup script to add the target toolchains and the host tools.
pushd ${ZEPHYR_SDK_INSTALL_DIR}
west update
# bash ./setup.sh -t x86_64-zephyr-elf
bash ./setup.sh -t arm-zephyr-eabi
bash ./setup.sh -t xtensa-espressif_esp32_zephyr-elf
bash ./setup.sh -t xtensa-espressif_esp32s3_zephyr-elf
bash ./setup.sh -h
popd
# install udev rules
sudo cp ~/zephyr-sdk-${RELEASE#v}/sysroots/x86_64-pokysdk-linux/usr/share/openocd/contrib/60-openocd.rules /etc/udev/rules.d
sudo udevadm control --reload
RSS_FEED="https://github.com/${USER}/${PROJECT}/releases.atom"
# xdg-open ${RSS_FEED}
cat ${RSS_FEED}
echo "*** Installing dependencies. ***"
sudo apt-get update
sudo apt install --no-install-recommends -y autoconf automake \
build-essential bzip2 ccache coccinelle cmake device-tree-compiler dfu-util \
git gperf gcc g++ gcc-multilib g++-multilib libsdl2-dev libtool python3-dev \
python3-pip python3-setuptools python3-tk python3-wheel ninja-build file make \
wget xz-utils
echo "*** Getting Zephyr and installing Python dependencies. ***"
# Install west and ensure that ${HOME}/.local/bin is part of your PATH environment variable:
echo "*** Installing west with pip3 ***"
# pip3 install -U --user west
# echo 'export PATH=${HOME}/.local/bin:”$PATH”' >>${HOME}/.bashrc
# if [[ -d ${HOME}/.zshrc.d ]]; then
# echo 'export PATH=${HOME}/.local/bin:"$PATH"' >>${HOME}/.zshrc.d/001_dot_local_path.sh
# fi
# ensure a conda env exists for zephyr python requirements
# https://www.nordicsemi.com/Products/Development-tools/nRF-Command-Line-Tools/Download?lang=en#infotabs
# https://nsscprodmedia.blob.core.windows.net/prod/software-and-other-downloads/desktop-software/nrf-command-line-tools/sw/versions-10-x-x/10-24-2/nrf-command-line-tools_10.24.2_amd64.deb
# py -3 -m pip install --pre -U git+https://github.com/makerdiary/uf2utils.git@main
if [[ ! -d ${HOME}/.conda/envs/zephyr ]];
conda init -n zephyr python west pynrfjprog dtsh # pyocd pylink
fi
conda activate zephyr
# add 'west' completion to zsh - use /usr/share/when packaging for debian
COMPLETION_FILE="/usr/share/zsh/site-functions/_west"
COMPLETION_FILE="${HOME}/.zshrc.d/001_west-completion.zsh"
west completion zsh > ${COMPLETION_FILE}
# Create west configuation file.
cat <<EOF | tee ${HOME}/.westconfig
[zephyr]
# base-prefer = configfile
[build]
# pristine = auto
#board = nrf52840dk/nrf52840
#dir-fmt = build/{board}
#guess-dir = runners
EOF
# 'west' is the tool that manages the entire life-cycle of a Zephyr-based project.
echo "*** Getting zephyr source code ***"
git clone https://github.com/${USER}/zephyr
mkdir -p ${DEST}/zephyr
cd ${DEST}/zephyr
git show -s --pretty=format:%cd
west update
conda activate zephyr
echo "Exporting a Zephyr CMake package."
west zephyr-export
echo "Installing additional python dependencies via pip3."
pip install -r \
${HOME}/zephyrproject/zephyr/scripts/requirements.txt
echo "Installing Zephyr's toolchain."
#Zephyr's SDK adds several additional tools for the host. Download the SDK installer:
cd ${HOME}
wget -O /var/tmp/${SDK_FILENAME} ${SDK_URL}/{SDK_FILENAME}
echo "Running the installer, this will install the SDK under the ${DEST}-${RELEASE:1} folder."
chmod +x /var/tmp/${SDK_FILENAME}
/var/tmp/${SDK_FILENAME} -- -d ${DEST}-${RELEASE:1}