From f7afa9a81e998bb117fe5506471eaf26dc78af5b Mon Sep 17 00:00:00 2001 From: Kalle Lilja <15094562+ThatKalle@users.noreply.github.com> Date: Thu, 6 Mar 2025 23:58:08 +0100 Subject: [PATCH] [Task]: 2.6 device-install scripts (#6248) * update device-install.bat * add device-install unittest * update device-update.bat * update uf2-convert.bat * update regen-protos.bat * update rem * bump version * update device-install.sh * add esptool * move esptool to setup.sh * trunk check+fmt * update uf2-convert.bat --- .devcontainer/setup.sh | 3 + bin/device-install.bat | 433 ++++++++++++++++++++++++------------ bin/device-install.sh | 226 +++++++++---------- bin/device-install_test.ps1 | 111 +++++++++ bin/device-update.bat | 205 +++++++++++++---- bin/regen-protos.bat | 11 +- bin/uf2-convert.bat | 126 ++++++++++- 7 files changed, 812 insertions(+), 303 deletions(-) create mode 100644 bin/device-install_test.ps1 diff --git a/.devcontainer/setup.sh b/.devcontainer/setup.sh index 0b2665f84..7c7487cc8 100755 --- a/.devcontainer/setup.sh +++ b/.devcontainer/setup.sh @@ -1,3 +1,6 @@ #!/usr/bin/env sh git submodule update --init + +pip install --no-cache-dir setuptools +pipx install esptool diff --git a/bin/device-install.bat b/bin/device-install.bat index 4d13d9f3b..3e2ea49aa 100755 --- a/bin/device-install.bat +++ b/bin/device-install.bat @@ -1,149 +1,292 @@ @ECHO OFF SETLOCAL EnableDelayedExpansion -set "SCRIPTNAME=%~nx0" -set "PYTHON=python" -set "WEB_APP=0" -set "TFT8=0" -set "TFT16=0" -SET "TFT_BUILD=0" -SET "DO_SPECIAL_OTA=0" +TITLE Meshtastic device-install -:: Determine the correct esptool command to use -where esptool >nul 2>&1 -if %ERRORLEVEL% EQU 0 ( - set "ESPTOOL_CMD=esptool" -) else ( - set "ESPTOOL_CMD=%PYTHON% -m esptool" -) - -goto GETOPTS -:HELP -echo Usage: %SCRIPTNAME% [-h] [-p ESPTOOL_PORT] [-P PYTHON] [-f FILENAME] [--web] [--tft] [--tft-16mb] -echo Flash image file to device, but first erasing and writing system information -echo. -echo -h Display this help and exit -echo -p ESPTOOL_PORT Set the environment variable for ESPTOOL_PORT. If not set, ESPTOOL iterates all ports (Dangerrous). -echo -P PYTHON Specify alternate python interpreter to use to invoke esptool. (Default: %PYTHON%) -echo -f FILENAME The .bin file to flash. Custom to your device type and region. -echo --web Flash WEB APP. -echo --tft Flash MUI 8mb -echo --tft-16mb Flash MUI 16mb -goto EOF - -:GETOPTS -if /I "%~1"=="-h" goto HELP & exit /b -if /I "%~1"=="--help" goto HELP & exit /b -if "%~1"=="-p" set "ESPTOOL_PORT=%~2" & SHIFT & SHIFT & goto GETOPTS -if "%~1"=="-P" set "PYTHON=%~2" & SHIFT & SHIFT & goto GETOPTS -if /I "%~1"=="-f" set "FILENAME=%~2" & SHIFT & SHIFT & goto GETOPTS -if /I "%~1"=="--web" set "WEB_APP=1" & SHIFT & goto GETOPTS -if /I "%~1"=="--tft" set "TFT8=1" & SHIFT & goto GETOPTS -if /I "%~1"=="--tft-16mb" set "TFT16=1" & SHIFT & goto GETOPTS -SHIFT -IF NOT "%~1"=="" goto GETOPTS - -IF "__%FILENAME%__" == "____" ( - echo "Missing FILENAME" - goto HELP -) - -:: Check if FILENAME contains "-tft-" and either TFT8 or TFT16 is 1 (--tft, -tft-16mb) -IF NOT "%FILENAME:-tft-=%"=="%FILENAME%" ( - SET "TFT_BUILD=1" - IF NOT "%TFT8%"=="1" IF NOT "%TFT16%"=="1" ( - echo Error: Either --tft or --tft-16mb must be set to use a TFT build. - goto EOF - ) - IF "%TFT8%"=="1" IF "%TFT16%"=="1" ( - echo Error: Both --tft and --tft-16mb must NOT be set at the same time. - goto EOF - ) -) - -:: Extract BASENAME from %FILENAME% for later use. -SET BASENAME=%FILENAME:firmware-=% - -IF EXIST %FILENAME% IF x%FILENAME:update=%==x%FILENAME% ( - @REM Default littlefs* offset (--web). - SET "OFFSET=0x300000" - - @REM Default OTA Offset - SET "OTA_OFFSET=0x260000" - - @REM littlefs* offset for MUI 8mb (--tft) and OTA OFFSET. - IF "%TFT8%"=="1" IF "%TFT_BUILD%"=="1" ( - SET "OFFSET=0x670000" - SET "OTA_OFFSET=0x340000" - ) else ( - echo Ignoring --tft, not a TFT Build. - ) - - @REM littlefs* offset for MUI 16mb (--tft-16mb) and OTA OFFSET. - IF "%TFT16%"=="1" IF "%TFT_BUILD%"=="1" ( - SET "OFFSET=0xc90000" - SET "OTA_OFFSET=0x650000" - ) else ( - echo Ignoring --tft-16mb, not a TFT Build. - ) - - echo Trying to flash update %FILENAME%, but first erasing and writing system information" - %ESPTOOL_CMD% --baud 115200 erase_flash - %ESPTOOL_CMD% --baud 115200 write_flash 0x00 "%FILENAME%" - - @REM Account for S3 and C3 board's different OTA partition - IF NOT "%FILENAME%"=="%FILENAME:s3=%" SET "DO_SPECIAL_OTA=1" - IF NOT "%FILENAME%"=="%FILENAME:v3=%" SET "DO_SPECIAL_OTA=1" - IF NOT "%FILENAME%"=="%FILENAME:t-deck=%" SET "DO_SPECIAL_OTA=1" - IF NOT "%FILENAME%"=="%FILENAME:wireless-paper=%" SET "DO_SPECIAL_OTA=1" - IF NOT "%FILENAME%"=="%FILENAME:wireless-tracker=%" SET "DO_SPECIAL_OTA=1" - IF NOT "%FILENAME%"=="%FILENAME:station-g2=%" SET "DO_SPECIAL_OTA=1" - IF NOT "%FILENAME%"=="%FILENAME:unphone=%" SET "DO_SPECIAL_OTA=1" - IF NOT "%FILENAME%"=="%FILENAME:esp32c3=%" SET "DO_SPECIAL_OTA=1" - - IF "!DO_SPECIAL_OTA!"=="1" ( - IF NOT "%FILENAME%"=="%FILENAME:esp32c3=%" ( - %ESPTOOL_CMD% --baud 115200 write_flash !OTA_OFFSET! bleota-c3.bin - ) ELSE ( - %ESPTOOL_CMD% --baud 115200 write_flash !OTA_OFFSET! bleota-s3.bin - ) - ) ELSE ( - %ESPTOOL_CMD% --baud 115200 write_flash !OTA_OFFSET! bleota.bin - ) - - @REM Check if WEB_APP (--web) is enabled and add "littlefswebui-" to BASENAME else "littlefs-". - IF "%WEB_APP%"=="1" ( - @REM Check it the file exist before trying to write it. - IF EXIST "littlefswebui-%BASENAME%" ( - %ESPTOOL_CMD% --baud 115200 write_flash !OFFSET! "littlefswebui-%BASENAME%" - ) else ( - echo Error: file "littlefswebui-%BASENAME%" wasn't found, littlefswebui not written. - goto EOF - ) - ) else ( - @REM Check it the file exist before trying to write it. - IF EXIST "littlefs-%BASENAME%" ( - %ESPTOOL_CMD% --baud 115200 write_flash !OFFSET! "littlefs-%BASENAME%" - ) else ( - echo Error: file "littlefs-%BASENAME%" wasn't found, littlefs not written. - goto EOF - ) - ) -) else ( - echo "Invalid file: %FILENAME%" - goto HELP -) - -:EOF -@REM Cleanup vars. -SET "SCRIPTNAME=" +SET "SCRIPT_NAME=%~nx0" +SET "DEBUG=0" SET "PYTHON=" -SET "WEB_APP=" -SET "TFT8=" -Set "TFT16=" -SET "OFFSET=" -SET "OTA_OFFSET=" -SET "DO_SPECIAL_OTA=" -SET "FILENAME=" -SET "BASENAME=" -endlocal -exit /b 0 \ No newline at end of file +SET "WEB_APP=0" +SET "TFT_BUILD=0" +SET "TFT8=0" +SET "TFT16=0" +SET "ESPTOOL_BAUD=115200" +SET "ESPTOOL_CMD=" +SET "LOGCOUNTER=0" + +GOTO getopts +:help +ECHO Flash image file to device, but first erasing and writing system information. +ECHO. +ECHO Usage: %SCRIPT_NAME% -f filename [-p PORT] [-P python] (--web) +ECHO. +ECHO Options: +ECHO -f filename The .bin file to flash. Custom to your device type and region. (required) +ECHO The file must be located in this current directory. +ECHO -p PORT Set the environment variable for ESPTOOL_PORT. +ECHO If not set, ESPTOOL iterates all ports (Dangerous). +ECHO -P python Specify alternate python interpreter to use to invoke esptool. (default: python) +ECHO If supplied the script will use python. +ECHO If not supplied the script will try to find esptool in Path. +ECHO --web Enable WebUI. (default: false) +ECHO. +ECHO Example: %SCRIPT_NAME% -f firmware-t-deck-tft-2.6.0.0b106d4.bin -p COM11 +ECHO Example: %SCRIPT_NAME% -f littlefs-unphone-2.6.0.0b106d4.bin -p COM11 --web +GOTO eof + +:version +ECHO %SCRIPT_NAME% [Version 2.6.0] +ECHO Meshtastic +GOTO eof + +:getopts +IF "%~1"=="" GOTO endopts +IF /I "%~1"=="-?" GOTO help +IF /I "%~1"=="-h" GOTO help +IF /I "%~1"=="--help" GOTO help +IF /I "%~1"=="-v" GOTO version +IF /I "%~1"=="--version" GOTO version +IF /I "%~1"=="--debug" SET "DEBUG=1" & CALL :LOG_MESSAGE DEBUG "DEBUG mode: enabled." +IF /I "%~1"=="-f" SET "FILENAME=%~2" & SHIFT +IF "%~1"=="-p" SET "ESPTOOL_PORT=%~2" & SHIFT +IF /I "%~1"=="--port" SET "ESPTOOL_PORT=%~2" & SHIFT +IF "%~1"=="-P" SET "PYTHON=%~2" & SHIFT +IF /I "%~1"=="--web" SET "WEB_APP=1" +SHIFT +GOTO getopts +:endopts + +CALL :LOG_MESSAGE DEBUG "Checking FILENAME parameter..." +IF "__!FILENAME!__"=="____" ( + CALL :LOG_MESSAGE DEBUG "Missing -f filename input." + GOTO help +) ELSE ( + IF NOT "__!FILENAME: =!__"=="__!FILENAME!__" ( + CALL :LOG_MESSAGE ERROR "Filename containing spaces are not supported." + GOTO help + ) + @REM Remove ".\" or "./" file prefix if present. + SET "FILENAME=!FILENAME:.\=!" + SET "FILENAME=!FILENAME:./=!" +) + +CALL :LOG_MESSAGE DEBUG "Filename: !FILENAME!" +CALL :LOG_MESSAGE DEBUG "Checking if !FILENAME! exists..." +IF NOT EXIST !FILENAME! ( + CALL :LOG_MESSAGE ERROR "File does not exist: !FILENAME!. Terminating." + GOTO eof +) + +IF NOT "!FILENAME:update=!"=="!FILENAME!" ( + CALL :LOG_MESSAGE DEBUG "We are working with a *update* file. !FILENAME!" + CALL :LOG_MESSAGE INFO "Use script device-update.bat to flash update !FILENAME!." + GOTO eof +) ELSE ( + CALL :LOG_MESSAGE DEBUG "We are NOT working with a *update* file. !FILENAME!" +) + +CALL :LOG_MESSAGE DEBUG "Determine the correct esptool command to use..." +IF NOT "__%PYTHON%__"=="____" ( + SET "ESPTOOL_CMD=!PYTHON! -m esptool" + CALL :LOG_MESSAGE DEBUG "Python interpreter supplied." +) ELSE ( + CALL :LOG_MESSAGE DEBUG "Python interpreter NOT supplied. Looking for esptool... + WHERE esptool >nul 2>&1 + IF %ERRORLEVEL% EQU 0 ( + @REM WHERE exits with code 0 if esptool is found. + SET "ESPTOOL_CMD=esptool" + ) ELSE ( + SET "ESPTOOL_CMD=python -m esptool" + CALL :RESET_ERROR + ) +) + +CALL :LOG_MESSAGE DEBUG "Checking esptool command !ESPTOOL_CMD!..." +!ESPTOOL_CMD! >nul 2>&1 +IF %ERRORLEVEL% GTR 2 ( + @REM esptool exits with code 1 if help is displayed. + CALL :LOG_MESSAGE ERROR "esptool not found: !ESPTOOL_CMD!" + EXIT /B 1 + GOTO eof +) +IF %DEBUG% EQU 1 ( + CALL :LOG_MESSAGE DEBUG "Skipping ESPTOOL_CMD steps." + SET "ESPTOOL_CMD=REM !ESPTOOL_CMD!" +) + +CALL :LOG_MESSAGE DEBUG "Using esptool command: !ESPTOOL_CMD!" +IF "__!ESPTOOL_PORT!__" == "____" ( + CALL :LOG_MESSAGE WARN "Using esptool port: UNSET." +) ELSE ( + CALL :LOG_MESSAGE INFO "Using esptool port: !ESPTOOL_PORT!." +) +CALL :LOG_MESSAGE INFO "Using esptool baud: !ESPTOOL_BAUD!." + +@REM Check if FILENAME contains "-tft-" and set target partitionScheme accordingly. +@REM https://github.com/meshtastic/web-flasher/blob/main/types/resources.ts#L3 +IF NOT "!FILENAME:-tft-=!"=="!FILENAME!" ( + CALL :LOG_MESSAGE DEBUG "We are working with a *-tft-* file. !FILENAME!" + IF %WEB_APP% EQU 1 ( + CALL :LOG_MESSAGE ERROR "Cannot enable WebUI (--web) and MUI." & GOTO eof + ) + SET "TFT_BUILD=1" + GOTO tft +) ELSE ( + CALL :LOG_MESSAGE DEBUG "We are NOT working with a *-tft-* file. !FILENAME!" + GOTO no_tft +) + +:tft +SET "TFT8MB=picomputer-s3 unphone seeed-sensecap-indicator" +FOR %%a IN (%TFT8MB%) DO ( + IF NOT "!FILENAME:%%a=!"=="!FILENAME!" ( + @REM We are working with any of %TFT8MB%. + SET "TFT8=1" + GOTO end_loop_tft8mb + ) +) +:end_loop_tft8mb + +SET "TFT16MB=t-deck" +FOR %%a IN (%TFT16MB%) DO ( + IF NOT "!FILENAME:%%a=!"=="!FILENAME!" ( + @REM We are working with any of %TFT16MB%. + SET "TFT16=1" + GOTO end_loop_tft16mb + ) +) +:end_loop_tft16mb + +IF %TFT8% EQU 1 CALL :LOG_MESSAGE INFO "tft and MUI 8mb selected." +IF %TFT16% EQU 1 CALL :LOG_MESSAGE INFO "tft and MUI 16mb selected." + +:no_tft + +@REM Extract BASENAME from %FILENAME% for later use. +SET "BASENAME=!FILENAME:firmware-=!" +CALL :LOG_MESSAGE DEBUG "Computed firmware basename: !BASENAME!" + +@REM Account for S3 and C3 board's different OTA partition. +SET "S3=s3 v3 t-deck wireless-paper wireless-tracker station-g2 unphone" +FOR %%a IN (%S3%) DO ( + IF NOT "!FILENAME:%%a=!"=="!FILENAME!" ( + @REM We are working with any of %S3%. + SET "OTA_FILENAME=bleota-s3.bin" + GOTO :end_loop_s3 + ) +) + +SET "C3=esp32c3" +FOR %%a IN (%C3%) DO ( + IF NOT "!FILENAME:%%a=!"=="!FILENAME!" ( + @REM We are working with any of %C3%. + SET "OTA_FILENAME=bleota-c3.bin" + GOTO :end_loop_c3 + ) +) + +@REM Everything else +SET "OTA_FILENAME=bleota.bin" +:end_loop_s3 +:end_loop_c3 +CALL :LOG_MESSAGE DEBUG "Set OTA_FILENAME to: !OTA_FILENAME!" + +@REM Check if (--web) is enabled and prefix BASENAME with "littlefswebui-" else "littlefs-". +IF %WEB_APP% EQU 1 ( + CALL :LOG_MESSAGE INFO "WebUI selected." + SET "SPIFFS_FILENAME=littlefswebui-%BASENAME%" +) ELSE ( + SET "SPIFFS_FILENAME=littlefs-%BASENAME%" +) +CALL :LOG_MESSAGE DEBUG "Set SPIFFS_FILENAME to: !SPIFFS_FILENAME!" + +@REM Default offsets. +@REM https://github.com/meshtastic/web-flasher/blob/main/stores/firmwareStore.ts#L202 +SET "OTA_OFFSET=0x260000" +SET "SPIFFS_OFFSET=0x300000" + +@REM Offsets for MUI 8mb. +IF %TFT8% EQU 1 IF %TFT_BUILD% EQU 1 ( + SET "OTA_OFFSET=0x340000" + SET "SPIFFS_OFFSET=0x670000" +) + +@REM Offsets for MUI 16mb. +IF %TFT16% EQU 1 IF %TFT_BUILD% EQU 1 ( + SET "OTA_OFFSET=0x650000" + SET "SPIFFS_OFFSET=0xc90000" +) + +CALL :LOG_MESSAGE DEBUG "Set OTA_OFFSET to: !OTA_OFFSET!" +CALL :LOG_MESSAGE DEBUG "Set SPIFFS_OFFSET to: !SPIFFS_OFFSET!" + +@REM Ensure target files exist before flashing operations. +IF NOT EXIST !FILENAME! CALL :LOG_MESSAGE ERROR "File does not exist: "!FILENAME!". Terminating." & EXIT /B 2 & GOTO eof +IF NOT EXIST !OTA_FILENAME! CALL :LOG_MESSAGE ERROR "File does not exist: "!OTA_FILENAME!". Terminating." & EXIT /B 2 & GOTO eof +IF NOT EXIST !SPIFFS_FILENAME! CALL :LOG_MESSAGE ERROR "File does not exist: "!SPIFFS_FILENAME!". Terminating." & EXIT /B 2 & GOTO eof + +@REM Flashing operations. +CALL :LOG_MESSAGE INFO "Trying to flash "!FILENAME!", but first erasing and writing system information..." +CALL :RUN_ESPTOOL !ESPTOOL_BAUD! erase_flash || GOTO eof +CALL :RUN_ESPTOOL !ESPTOOL_BAUD! write_flash 0x00 "!FILENAME!" || GOTO eof + +CALL :LOG_MESSAGE INFO "Trying to flash BLEOTA "!OTA_FILENAME!" at OTA_OFFSET !OTA_OFFSET!..." +CALL :RUN_ESPTOOL !ESPTOOL_BAUD! write_flash !OTA_OFFSET! "!OTA_FILENAME!" || GOTO eof + +CALL :LOG_MESSAGE INFO "Trying to flash SPIFFS "!SPIFFS_FILENAME!" at SPIFFS_OFFSET !SPIFFS_OFFSET!..." +CALL :RUN_ESPTOOL !ESPTOOL_BAUD! write_flash !SPIFFS_OFFSET! "!SPIFFS_FILENAME!" || GOTO eof + +CALL :LOG_MESSAGE INFO "Script complete!." + +:eof +ENDLOCAL +EXIT /B %ERRORLEVEL% + + +:RUN_ESPTOOL +@REM Subroutine used to run ESPTOOL_CMD with arguments. +@REM Also handles %ERRORLEVEL%. +@REM CALL :RUN_ESPTOOL [Baud] [erase_flash|write_flash] [OFFSET] [Filename] +@REM. +@REM Example:: CALL :RUN_ESPTOOL 115200 write_flash 0x10000 "firmwarefile.bin" +IF %DEBUG% EQU 1 CALL :LOG_MESSAGE DEBUG "About to run command: !ESPTOOL_CMD! --baud %~1 %~2 %~3 %~4" +CALL :RESET_ERROR +!ESPTOOL_CMD! --baud %~1 %~2 %~3 %~4 +IF %ERRORLEVEL% NEQ 0 ( + CALL :LOG_MESSAGE ERROR "Error running command: !ESPTOOL_CMD! --baud %~1 %~2 %~3 %~4" + EXIT /B %ERRORLEVEL% +) +GOTO :eof + +:LOG_MESSAGE +@REM Subroutine used to print log messages in four different levels. +@REM DEBUG messages only get printed if [-d] flag is passed to script. +@REM CALL :LOG_MESSAGE [ERROR|INFO|WARN|DEBUG] "Message" +@REM. +@REM Example:: CALL :LOG_MESSAGE INFO "Message." +SET /A LOGCOUNTER=LOGCOUNTER+1 +IF "%1" == "ERROR" CALL :GET_TIMESTAMP & ECHO %1 ^| !TIMESTAMP! !LOGCOUNTER! %~2 +IF "%1" == "INFO" CALL :GET_TIMESTAMP & ECHO %1 ^| !TIMESTAMP! !LOGCOUNTER! %~2 +IF "%1" == "WARN" CALL :GET_TIMESTAMP & ECHO %1 ^| !TIMESTAMP! !LOGCOUNTER! %~2 +IF "%1" == "DEBUG" IF %DEBUG% EQU 1 CALL :GET_TIMESTAMP & ECHO %1 ^| !TIMESTAMP! !LOGCOUNTER! %~2 +GOTO :eof + +:GET_TIMESTAMP +@REM Subroutine used to set !TIMESTAMP! to HH:MM:ss. +@REM CALL :GET_TIMESTAMP +@REM. +@REM Updates: !TIMESTAMP! +FOR /F "tokens=1,2,3 delims=:,." %%a IN ("%TIME%") DO ( + SET "HH=%%a" + SET "MM=%%b" + SET "ss=%%c" +) +SET "TIMESTAMP=!HH!:!MM!:!ss!" +GOTO :eof + +:RESET_ERROR +@REM Subroutine to reset %ERRORLEVEL% to 0. +@REM CALL :RESET_ERROR +@REM. +@REM Updates: %ERRORLEVEL% +EXIT /B 0 +GOTO :eof diff --git a/bin/device-install.sh b/bin/device-install.sh index 96a204a5a..c1ba33c4a 100755 --- a/bin/device-install.sh +++ b/bin/device-install.sh @@ -8,158 +8,152 @@ TFT_BUILD=false # Determine the correct esptool command to use if "$PYTHON" -m esptool version >/dev/null 2>&1; then - ESPTOOL_CMD="$PYTHON -m esptool" + ESPTOOL_CMD="$PYTHON -m esptool" elif command -v esptool >/dev/null 2>&1; then - ESPTOOL_CMD="esptool" + ESPTOOL_CMD="esptool" elif command -v esptool.py >/dev/null 2>&1; then - ESPTOOL_CMD="esptool.py" + ESPTOOL_CMD="esptool.py" else - echo "Error: esptool not found" - exit 1 + echo "Error: esptool not found" + exit 1 fi set -e # Usage info show_help() { - cat <&2 - exit 1 - ;; - esac - shift # Move to the next argument + case "$1" in + -h | --help) + show_help + exit 0 + ;; + -p) + ESPTOOL_PORT="$2" + shift # Shift past the option argument + ;; + -P) + PYTHON="$2" + shift + ;; + -f) + FILENAME="$2" + shift + ;; + --web) + WEB_APP=true + ;; + --) # Stop parsing options + shift + break + ;; + *) + echo "Unknown argument: $1" >&2 + exit 1 + ;; + esac + shift # Move to the next argument done [ -z "$FILENAME" -a -n "$1" ] && { - FILENAME=$1 - shift + FILENAME=$1 + shift } -# Check if FILENAME contains "-tft-" and either TFT8 or TFT16 is 1 (--tft, -tft-16mb) -if [[ "${FILENAME//-tft-/}" != "$FILENAME" ]]; then - TFT_BUILD=true - if [[ "$TFT8" != true && "$TFT16" != true ]]; then - echo "Error: Either --tft or --tft-16mb must be set to use a TFT build." - exit 1 - fi - if [[ "$TFT8" == true && "$TFT16" == true ]]; then - echo "Error: Both --tft and --tft-16mb must NOT be set at the same time." - exit 1 - fi +# Check if FILENAME contains "-tft-" and set target partitionScheme accordingly. +if [[ ${FILENAME//-tft-/} != "$FILENAME" ]]; then + TFT_BUILD=true + if [[ $WEB_APP == true ]] && [[ $TFT_BUILD == true ]]; then + echo "Cannot enable WebUI (--web) and MUI." + exit 1 + fi + + if [[ $FILENAME == *"picomputer-s3"* || $FILENAME == *"unphone"* || $FILENAME == *"seeed-sensecap-indicator"* ]]; then + TFT8=true + fi + + if [[ $FILENAME == *"t-deck"* ]]; then + TFT16=true + fi fi # Extract BASENAME from %FILENAME% for later use. BASENAME="${FILENAME/firmware-/}" if [ -f "${FILENAME}" ] && [ -n "${FILENAME##*"update"*}" ]; then - # Default littlefs* offset (--web). - OFFSET=0x300000 + # Default littlefs* offset (--web). + OFFSET=0x300000 - # Default OTA Offset - OTA_OFFSET=0x260000 + # Default OTA Offset + OTA_OFFSET=0x260000 - # littlefs* offset for MUI 8mb (--tft) and OTA OFFSET. - if [ "$TFT8" = true ]; then - if [ "$TFT_BUILD" = true ]; then - OFFSET=0x670000 - OTA_OFFSET=0x340000 - else - echo "Ignoring --tft, not a TFT Build." - fi - fi + # littlefs* offset for MUI 8mb and OTA OFFSET. + if [ "$TFT8" = true ] && [ "$TFT_BUILD" = true ]; then + OFFSET=0x670000 + OTA_OFFSET=0x340000 + fi - # littlefs* offset for MUI 16mb (--tft-16mb) and OTA OFFSET. - if [ "$TFT16" = true ]; then - if [ "$TFT_BUILD" = true ]; then - OFFSET=0xc90000 - OTA_OFFSET=0x650000 - else - echo "Ignoring --tft-16mb, not a TFT Build." - fi - fi + # littlefs* offset for MUI 16mb and OTA OFFSET. + if [ "$TFT16" = true ] && [ "$TFT_BUILD" = true ]; then + OFFSET=0xc90000 + OTA_OFFSET=0x650000 + fi - echo "Trying to flash ${FILENAME}, but first erasing and writing system information" - $ESPTOOL_CMD erase_flash - $ESPTOOL_CMD write_flash 0x00 "${FILENAME}" - # Account for S3 board's different OTA partition - if [ -n "${FILENAME##*"s3"*}" ] && [ -n "${FILENAME##*"-v3"*}" ] && [ -n "${FILENAME##*"t-deck"*}" ] && [ -n "${FILENAME##*"wireless-paper"*}" ] && [ -n "${FILENAME##*"wireless-tracker"*}" ] && [ -n "${FILENAME##*"station-g2"*}" ] && [ -n "${FILENAME##*"unphone"*}" ]; then - if [ -n "${FILENAME##*"esp32c3"*}" ]; then - $ESPTOOL_CMD write_flash $OTA_OFFSET bleota.bin - else - $ESPTOOL_CMD write_flash $OTA_OFFSET bleota-c3.bin - fi - else - $ESPTOOL_CMD write_flash $OTA_OFFSET bleota-s3.bin - fi + # Account for S3 board's different OTA partition + if [ -n "${FILENAME##*"s3"*}" ] && [ -n "${FILENAME##*"-v3"*}" ] && [ -n "${FILENAME##*"t-deck"*}" ] && [ -n "${FILENAME##*"wireless-paper"*}" ] && [ -n "${FILENAME##*"wireless-tracker"*}" ] && [ -n "${FILENAME##*"station-g2"*}" ] && [ -n "${FILENAME##*"unphone"*}" ]; then + if [ -n "${FILENAME##*"esp32c3"*}" ]; then + OTAFILE=bleota.bin + else + OTAFILE=bleota-c3.bin + fi + else + OTAFILE=bleota-s3.bin + fi - # Check if WEB_APP (--web) is enabled and add "littlefswebui-" to BASENAME else "littlefs-". - if [ "$WEB_APP" = true ]; then - # Check it the file exist before trying to write it. - if [ -f "littlefswebui-${BASENAME}" ]; then - $ESPTOOL_CMD write_flash $OFFSET "littlefswebui-${BASENAME}" - else - echo "Error: file "littlefswebui-${BASENAME}" wasn't found, littlefs not written." - exit 1 - fi - else - # Check it the file exist before trying to write it. - if [ -f "littlefs-${BASENAME}" ]; then - $ESPTOOL_CMD write_flash $OFFSET "littlefs-${BASENAME}" - else - echo "Error: file "littlefs-${BASENAME}" wasn't found, littlefs not written." - exit 1 - fi - fi + # Check if WEB_APP (--web) is enabled and add "littlefswebui-" to BASENAME else "littlefs-". + if [ "$WEB_APP" = true ]; then + SPIFFSFILE=littlefswebui-${BASENAME} + else + SPIFFSFILE=littlefs-${BASENAME} + fi + + if [[ ! -f $FILENAME ]]; then + echo "Error: file ${FILENAME} wasn't found. Terminating." + exit 1 + fi + if [[ ! -f $OTAFILE ]]; then + echo "Error: file ${OTAFILE} wasn't found. Terminating." + exit 1 + fi + if [[ ! -f $SPIFFSFILE ]]; then + echo "Error: file ${SPIFFSFILE} wasn't found. Terminating." + exit 1 + fi + + echo "Trying to flash ${FILENAME}, but first erasing and writing system information" + $ESPTOOL_CMD erase_flash + $ESPTOOL_CMD write_flash 0x00 "${FILENAME}" + echo "Trying to flash ${OTAFILE} at offset ${OTA_OFFSET}" + $ESPTOOL_CMD write_flash $OTA_OFFSET "${OTAFILE}" + echo "Trying to flash ${SPIFFSFILE}, at offset ${OFFSET}" + $ESPTOOL_CMD write_flash $OFFSET "${SPIFFSFILE}" else - show_help - echo "Invalid file: ${FILENAME}" + show_help + echo "Invalid file: ${FILENAME}" fi exit 0 diff --git a/bin/device-install_test.ps1 b/bin/device-install_test.ps1 new file mode 100644 index 000000000..d7d3e6178 --- /dev/null +++ b/bin/device-install_test.ps1 @@ -0,0 +1,111 @@ +<# + .SYNOPSIS + Unit-test for .\device-install.bat. + + .DESCRIPTION + This script performs a positive unit-test on .\device-install.bat by creating the expected .bin + files for a device followed by running the .bat script without flashing the firmware (--debug). + If any errors are hit they are presented in the standard output. Investigate accordingly. + + This script needs to be placed in the same directory as .\device-install.bat. + + .EXAMPLE + .\device-install_test.ps1 + + .EXAMPLE + .\device-install_test.ps1 -Verbose + + .LINK + .\device-install.bat --help +#> + +[CmdletBinding()] +param() + +function New-EmptyFile() { + [CmdletBinding()] + param ( + [Parameter(Position=0,Mandatory=$true)] + # Specifies the file name. + [string]$FileName, + [Parameter(Position=1)] + # Specifies the target path. (Get-Location).Path is the default. + [string]$Directory = (Get-Location).Path + ) + + $filePath = Join-Path -Path $Directory -ChildPath $FileName + + Write-Verbose -Message "Create empty test file if it doesn't exist: $($FileName)" + New-Item -Path "$filePath" -ItemType File -ErrorAction SilentlyContinue | Out-Null +} + +function Remove-EmptyFile() { + [CmdletBinding()] + param ( + [Parameter(Position=0,Mandatory=$true)] + # Specifies the file name. + [string]$FileName, + [Parameter(Position=1)] + # Specifies the target path. (Get-Location).Path is the default. + [string]$Directory = (Get-Location).Path + ) + + $filePath = Join-Path -Path $Directory -ChildPath $FileName + + Write-Verbose -Message "Deleted empty test file: $($FileName)" + Remove-Item -Path "$filePath" | Out-Null +} + + +$TestCases = New-Object -TypeName PSObject -Property @{ + # Use this PSObject to define testcases according to this syntax: + # "testname" = @("firmware-testname","bleota","littlefs-testname","args") + "t-deck" = @("firmware-t-deck-2.6.0.0b106d4.bin", "bleota-s3.bin", "littlefs-t-deck-2.6.0.0b106d4.bin","") + "t-deck_web" = @("firmware-t-deck-2.6.0.0b106d4.bin", "bleota-s3.bin", "littlefswebui-t-deck-2.6.0.0b106d4.bin","--web") + "t-deck-tft" = @("firmware-t-deck-tft-2.6.0.0b106d4.bin", "bleota-s3.bin", "littlefs-t-deck-tft-2.6.0.0b106d4.bin","") + "heltec-ht62-esp32c3" = @("firmware-heltec-ht62-esp32c3-sx1262-2.6.0.0b106d4.bin", "bleota-c3.bin", "littlefs-heltec-ht62-esp32c3-sx1262-2.6.0.0b106d4.bin","") + "tlora-c6" = @("firmware-tlora-c6-2.6.0.0b106d4.bin", "bleota.bin", "littlefs-tlora-c6-2.6.0.0b106d4.bin","") + "heltec-v3_web" = @("firmware-heltec-v3-2.6.0.0b106d4.bin", "bleota-s3.bin", "littlefswebui-heltec-v3-2.6.0.0b106d4.bin","--web") + "seeed-sensecap-indicator-tft" = @("firmware-seeed-sensecap-indicator-tft-2.6.0.0b106d4.bin", "bleota.bin", "littlefs-seeed-sensecap-indicator-tft-2.6.0.0b106d4.bin","") + "picomputer-s3-tft" = @("firmware-picomputer-s3-tft-2.6.0.0b106d4.bin", "bleota-s3.bin", "littlefs-picomputer-s3-tft-2.6.0.0b106d4.bin","") +} + +foreach ($TestCase in $TestCases.PSObject.Properties) { + $Name = $TestCase.Name + $Files = $TestCase.Value + $Errors = $null + $Counter = 0 + + Write-Host -Object "Testcase: $Name`:" -ForegroundColor Green + foreach ($File in $Files) { + if ($File.EndsWith(".bin")) { + New-EmptyFile -FileName $File + } + } + + Write-Host -Object "Performing test on $Name..." -ForegroundColor Blue + $Test = Invoke-Expression -Command "cmd /c .\device-install.bat --debug -f $($TestCases."$Name"[0]) $($TestCases."$Name"[3])" + + foreach ($Line in $Test) { + if ($Line -match "Set OTA_OFFSET to" -or ` + $Line -match "Set SPIFFS_OFFSET to") { + Write-Host -Object "$($Line -replace "^.*?Set","Set")" -ForegroundColor Blue + } elseif ($VerbosePreference -eq "Continue") { + Write-Host -Object $Line + } + if ($Line -match "ERROR") { + $Errors += $Line + $Counter++ + } + } + if ($null -ne $Errors) { + Write-Host -Object "$Counter ERROR(s) detected!" -ForegroundColor Red + if (-not ($VerbosePreference -eq "Continue")) {Write-Host -Object $Errors} + } + + foreach ($File in $Files) { + if ($File.EndsWith(".bin")) { + Remove-EmptyFile -FileName $File + } + } +} diff --git a/bin/device-update.bat b/bin/device-update.bat index a52f3d33f..ecfeec187 100755 --- a/bin/device-update.bat +++ b/bin/device-update.bat @@ -1,48 +1,175 @@ @ECHO OFF +SETLOCAL EnableDelayedExpansion +TITLE Meshtastic device-update -set PYTHON=python +SET "SCRIPT_NAME=%~nx0" +SET "DEBUG=0" +SET "PYTHON=" +SET "ESPTOOL_BAUD=115200" +SET "ESPTOOL_CMD=" +SET "LOGCOUNTER=0" -:: Determine the correct esptool command to use -where esptool >nul 2>&1 -if %ERRORLEVEL% EQU 0 ( - set "ESPTOOL_CMD=esptool" -) else ( - set "ESPTOOL_CMD=%PYTHON% -m esptool" -) +GOTO getopts +:help +ECHO Flash image file to device, but leave existing system intact. +ECHO. +ECHO Usage: %SCRIPT_NAME% -f filename [-p PORT] [-P python] +ECHO. +ECHO Options: +ECHO -f filename The .bin file to flash. Custom to your device type and region. (required) +ECHO The file must be located in this current directory. +ECHO -p PORT Set the environment variable for ESPTOOL_PORT. +ECHO If not set, ESPTOOL iterates all ports (Dangerous). +ECHO -P python Specify alternate python interpreter to use to invoke esptool. (default: python) +ECHO If supplied the script will use python. +ECHO If not supplied the script will try to find esptool in Path. +ECHO. +ECHO Example: %SCRIPT_NAME% -f firmware-t-deck-tft-2.6.0.0b106d4-update.bin -p COM11 +GOTO eof -goto GETOPTS -:HELP -echo Usage: %~nx0 [-h] [-p ESPTOOL_PORT] [-P PYTHON] [-f FILENAME^|FILENAME] -echo Flash image file to device, leave existing system intact. -echo. -echo -h Display this help and exit -echo -p ESPTOOL_PORT Set the environment variable for ESPTOOL_PORT. If not set, ESPTOOL iterates all ports (Dangerrous). -echo -P PYTHON Specify alternate python interpreter to use to invoke esptool. (Default: %PYTHON%) -echo -f FILENAME The *update.bin file to flash. Custom to your device type. -goto EOF +:version +ECHO %SCRIPT_NAME% [Version 2.6.0] +ECHO Meshtastic +GOTO eof -:GETOPTS -if /I "%1"=="-h" goto HELP -if /I "%1"=="--help" goto HELP -if /I "%1"=="-F" set "FILENAME=%2" & SHIFT -if /I "%1"=="-p" set ESPTOOL_PORT=%2 & SHIFT -if /I "%1"=="-P" set PYTHON=%2 & SHIFT +:getopts +IF "%~1"=="" GOTO endopts +IF /I "%~1"=="-?" GOTO help +IF /I "%~1"=="-h" GOTO help +IF /I "%~1"=="--help" GOTO help +IF /I "%~1"=="-v" GOTO version +IF /I "%~1"=="--version" GOTO version +IF /I "%~1"=="--debug" SET "DEBUG=1" & CALL :LOG_MESSAGE DEBUG "DEBUG mode: enabled." +IF /I "%~1"=="-f" SET "FILENAME=%~2" & SHIFT +IF "%~1"=="-p" SET "ESPTOOL_PORT=%~2" & SHIFT +IF /I "%~1"=="--port" SET "ESPTOOL_PORT=%~2" & SHIFT +IF "%~1"=="-P" SET "PYTHON=%~2" & SHIFT SHIFT -IF NOT "__%1__"=="____" goto GETOPTS +GOTO getopts +:endopts -IF "__%FILENAME%__" == "____" ( - echo "Missing FILENAME" - goto HELP -) -IF EXIST %FILENAME% IF NOT x%FILENAME:update=%==x%FILENAME% ( - echo Trying to flash update %FILENAME% - %ESPTOOL_CMD% --baud 115200 write_flash 0x10000 %FILENAME% -) else ( - echo "Invalid file: %FILENAME%" - goto HELP -) else ( - echo "Invalid file: %FILENAME%" - goto HELP +CALL :LOG_MESSAGE DEBUG "Checking FILENAME parameter..." +IF "__!FILENAME!__"=="____" ( + CALL :LOG_MESSAGE DEBUG "Missing -f filename input." + GOTO help +) ELSE ( + IF NOT "__!FILENAME: =!__"=="__!FILENAME!__" ( + CALL :LOG_MESSAGE ERROR "Filename containing spaces are not supported." + GOTO help + ) + @REM Remove ".\" or "./" file prefix if present. + SET "FILENAME=!FILENAME:.\=!" + SET "FILENAME=!FILENAME:./=!" ) -:EOF +CALL :LOG_MESSAGE DEBUG "Filename: !FILENAME!" +CALL :LOG_MESSAGE DEBUG "Checking if !FILENAME! exists..." +IF NOT EXIST !FILENAME! ( + CALL :LOG_MESSAGE ERROR "File does not exist: !FILENAME!. Terminating." + GOTO eof +) + +IF "!FILENAME:update=!"=="!FILENAME!" ( + CALL :LOG_MESSAGE DEBUG "We are NOT working with a *update* file. !FILENAME!" + CALL :LOG_MESSAGE INFO "Use script device-install.bat to flash update !FILENAME!." + GOTO eof +) ELSE ( + CALL :LOG_MESSAGE DEBUG "We are working with a *update* file. !FILENAME!" +) + +CALL :LOG_MESSAGE DEBUG "Determine the correct esptool command to use..." +IF NOT "__%PYTHON%__"=="____" ( + SET "ESPTOOL_CMD=!PYTHON! -m esptool" + CALL :LOG_MESSAGE DEBUG "Python interpreter supplied." +) ELSE ( + CALL :LOG_MESSAGE DEBUG "Python interpreter NOT supplied. Looking for esptool... + WHERE esptool >nul 2>&1 + IF %ERRORLEVEL% EQU 0 ( + @REM WHERE exits with code 0 if esptool is found. + SET "ESPTOOL_CMD=esptool" + ) ELSE ( + SET "ESPTOOL_CMD=python -m esptool" + CALL :RESET_ERROR + ) +) + +CALL :LOG_MESSAGE DEBUG "Checking esptool command !ESPTOOL_CMD!..." +!ESPTOOL_CMD! >nul 2>&1 +IF %ERRORLEVEL% GTR 2 ( + @REM esptool exits with code 1 if help is displayed. + CALL :LOG_MESSAGE ERROR "esptool not found: !ESPTOOL_CMD!" + EXIT /B 1 + GOTO eof +) +IF %DEBUG% EQU 1 ( + CALL :LOG_MESSAGE DEBUG "Skipping ESPTOOL_CMD steps." + SET "ESPTOOL_CMD=REM !ESPTOOL_CMD!" +) + +CALL :LOG_MESSAGE DEBUG "Using esptool command: !ESPTOOL_CMD!" +IF "__!ESPTOOL_PORT!__" == "____" ( + CALL :LOG_MESSAGE WARN "Using esptool port: UNSET." +) ELSE ( + CALL :LOG_MESSAGE INFO "Using esptool port: !ESPTOOL_PORT!." +) +CALL :LOG_MESSAGE INFO "Using esptool baud: !ESPTOOL_BAUD!." + +@REM Flashing operations. +CALL :LOG_MESSAGE INFO "Trying to flash update "!FILENAME!" at OFFSET 0x10000..." +CALL :RUN_ESPTOOL !ESPTOOL_BAUD! write_flash 0x10000 "!FILENAME!" || GOTO eof + +CALL :LOG_MESSAGE INFO "Script complete!." + +:eof +ENDLOCAL +EXIT /B %ERRORLEVEL% + + +:RUN_ESPTOOL +@REM Subroutine used to run ESPTOOL_CMD with arguments. +@REM Also handles %ERRORLEVEL%. +@REM CALL :RUN_ESPTOOL [Baud] [erase_flash|write_flash] [OFFSET] [Filename] +@REM. +@REM Example:: CALL :RUN_ESPTOOL 115200 write_flash 0x10000 "firmwarefile.bin" +IF %DEBUG% EQU 1 CALL :LOG_MESSAGE DEBUG "About to run command: !ESPTOOL_CMD! --baud %~1 %~2 %~3 %~4" +CALL :RESET_ERROR +!ESPTOOL_CMD! --baud %~1 %~2 %~3 %~4 +IF %ERRORLEVEL% NEQ 0 ( + CALL :LOG_MESSAGE ERROR "Error running command: !ESPTOOL_CMD! --baud %~1 %~2 %~3 %~4" + EXIT /B %ERRORLEVEL% +) +GOTO :eof + +:LOG_MESSAGE +@REM Subroutine used to print log messages in four different levels. +@REM DEBUG messages only get printed if [-d] flag is passed to script. +@REM CALL :LOG_MESSAGE [ERROR|INFO|WARN|DEBUG] "Message" +@REM. +@REM Example:: CALL :LOG_MESSAGE INFO "Message." +SET /A LOGCOUNTER=LOGCOUNTER+1 +IF "%1" == "ERROR" CALL :GET_TIMESTAMP & ECHO %1 ^| !TIMESTAMP! !LOGCOUNTER! %~2 +IF "%1" == "INFO" CALL :GET_TIMESTAMP & ECHO %1 ^| !TIMESTAMP! !LOGCOUNTER! %~2 +IF "%1" == "WARN" CALL :GET_TIMESTAMP & ECHO %1 ^| !TIMESTAMP! !LOGCOUNTER! %~2 +IF "%1" == "DEBUG" IF %DEBUG% EQU 1 CALL :GET_TIMESTAMP & ECHO %1 ^| !TIMESTAMP! !LOGCOUNTER! %~2 +GOTO :eof + +:GET_TIMESTAMP +@REM Subroutine used to set !TIMESTAMP! to HH:MM:ss. +@REM CALL :GET_TIMESTAMP +@REM. +@REM Updates: !TIMESTAMP! +FOR /F "tokens=1,2,3 delims=:,." %%a IN ("%TIME%") DO ( + SET "HH=%%a" + SET "MM=%%b" + SET "ss=%%c" +) +SET "TIMESTAMP=!HH!:!MM!:!ss!" +GOTO :eof + +:RESET_ERROR +@REM Subroutine to reset %ERRORLEVEL% to 0. +@REM CALL :RESET_ERROR +@REM. +@REM Updates: %ERRORLEVEL% +EXIT /B 0 +GOTO :eof diff --git a/bin/regen-protos.bat b/bin/regen-protos.bat index 7fa8f333d..0bbfbe38a 100644 --- a/bin/regen-protos.bat +++ b/bin/regen-protos.bat @@ -1 +1,10 @@ -cd protobufs && ..\nanopb-0.4.9\generator-bin\protoc.exe --experimental_allow_proto3_optional "--nanopb_out=-S.cpp -v:..\src\mesh\generated" -I=..\protobufs\ ..\protobufs\meshtastic\*.proto +@ECHO OFF +SETLOCAL + +cd protobufs +..\nanopb-0.4.9\generator-bin\protoc.exe --experimental_allow_proto3_optional "--nanopb_out=-S.cpp -v:..\src\mesh\generated" -I=..\protobufs\ ..\protobufs\meshtastic\*.proto +GOTO eof + +:eof +ENDLOCAL +EXIT /B %ERRORLEVEL% diff --git a/bin/uf2-convert.bat b/bin/uf2-convert.bat index 242bec3ab..5e36617e3 100644 --- a/bin/uf2-convert.bat +++ b/bin/uf2-convert.bat @@ -1,2 +1,124 @@ -@echo off -if [%1]==[] (echo "Please specify a platformio NRF target (i.e. rak4631) as the first argument.") else (python3 .\bin\uf2conv.py .\.pio\build\%1\firmware.hex -c -o .\.pio\build\%1\firmware.uf2 -f 0xADA52840) \ No newline at end of file +@ECHO OFF +SETLOCAL EnableDelayedExpansion +TITLE Meshtastic uf2-convert + +SET "SCRIPT_NAME=%~nx0" +SET "DEBUG=0" +SET "NRF=0" +SET "UF2CONV_CMD=python3 .\bin\uf2conv.py" + +GOTO getopts +:help +ECHO. +ECHO Usage: %SCRIPT_NAME% -t [t-echo^|rak4631^|nano-g2-ultra^|wio-tracker-wm1110^|canaryone^| +ECHO heltec-mesh-node-t114^|tracker-t1000-e^|rak_wismeshtap^|rak2560^| +ECHO nrf52_promicro_diy_tcxo] +ECHO. +ECHO Options: +ECHO -t target Specify a platformio NRF target to build for. (required) +ECHO. +ECHO Example: %SCRIPT_NAME% -t rak4631 +GOTO eof + +:version +ECHO %SCRIPT_NAME% [Version 2.6.0] +ECHO Meshtastic +GOTO eof + +:getopts +IF "%~1"=="" GOTO endopts +IF /I "%~1"=="-?" GOTO help +IF /I "%~1"=="-h" GOTO help +IF /I "%~1"=="--help" GOTO help +IF /I "%~1"=="-v" GOTO version +IF /I "%~1"=="--version" GOTO version +IF /I "%~1"=="--debug" SET "DEBUG=1" & CALL :LOG_MESSAGE DEBUG "DEBUG mode: enabled." +IF /I "%~1"=="-t" SET "TARGETNAME=%~2" & SHIFT +IF /I "%~1"=="--target" SET "TARGETNAME=%~2" & SHIFT +SHIFT +GOTO getopts +:endopts + +CALL :LOG_MESSAGE DEBUG "Checking TARGETNAME parameter..." +IF "__!TARGETNAME!__"=="____" ( + CALL :LOG_MESSAGE DEBUG "Missing -t target input." + GOTO help +) + +IF %DEBUG% EQU 1 SET "UF2CONV_CMD=REM python3 .\bin\uf2conv.py" + +SET "NRFTARGETS=t-echo rak4631 nano-g2-ultra wio-tracker-wm1110 canaryone heltec-mesh-node-t114 tracker-t1000-e rak_wismeshtap rak2560 nrf52_promicro_diy_tcxo" +FOR %%a IN (%NRFTARGETS%) DO ( + IF /I "%%a"=="!TARGETNAME!" ( + @REM We are working with any of %NRFTARGETS%. + SET "NRF=1" + GOTO end_loop_nrf + ) +) +:end_loop_nrf + +@REM Building operations. +IF !NRF! EQU 1 ( + CALL :LOG_MESSAGE INFO "Trying to build for !TARGETNAME!..." + CALL :RUN_UF2CONV !TARGETNAME! || GOTO eof +) ELSE ( + CALL :LOG_MESSAGE WARN "!TARGETNAME! is not supported..." + GOTO eof +) + +CALL :LOG_MESSAGE INFO "Script complete!." + + +:eof +ENDLOCAL +EXIT /B %ERRORLEVEL% + + +:RUN_UF2CONV +@REM Subroutine used to run .\bin\uf2conv.py with arguments. +@REM Also handles %ERRORLEVEL%. +@REM CALL :RUN_UF2CONV [target] +@REM. +@REM Example:: CALL :RUN_UF2CONV rak4631 +IF %DEBUG% EQU 1 CALL :LOG_MESSAGE DEBUG "About to run command: !UF2CONV_CMD! .\.pio\build\%~1\firmware.hex -c -o .\.pio\build\%~1\firmware.uf2 -f 0xADA52840" +CALL :RESET_ERROR +!UF2CONV_CMD! .\.pio\build\%~1\firmware.hex -c -o .\.pio\build\%~1\firmware.uf2 -f 0xADA52840 +IF %ERRORLEVEL% NEQ 0 ( + CALL :LOG_MESSAGE ERROR "Error running command: !UF2CONV_CMD! .\.pio\build\%~1\firmware.hex -c -o .\.pio\build\%~1\firmware.uf2 -f 0xADA52840" + EXIT /B %ERRORLEVEL% +) +GOTO :eof + +:LOG_MESSAGE +@REM Subroutine used to print log messages in four different levels. +@REM DEBUG messages only get printed if [-d] flag is passed to script. +@REM CALL :LOG_MESSAGE [ERROR|INFO|WARN|DEBUG] "Message" +@REM. +@REM Example:: CALL :LOG_MESSAGE INFO "Message." +SET /A LOGCOUNTER=LOGCOUNTER+1 +IF "%1" == "ERROR" CALL :GET_TIMESTAMP & ECHO %1 ^| !TIMESTAMP! !LOGCOUNTER! %~2 +IF "%1" == "INFO" CALL :GET_TIMESTAMP & ECHO %1 ^| !TIMESTAMP! !LOGCOUNTER! %~2 +IF "%1" == "WARN" CALL :GET_TIMESTAMP & ECHO %1 ^| !TIMESTAMP! !LOGCOUNTER! %~2 +IF "%1" == "DEBUG" IF %DEBUG% EQU 1 CALL :GET_TIMESTAMP & ECHO %1 ^| !TIMESTAMP! !LOGCOUNTER! %~2 +GOTO :eof + +:GET_TIMESTAMP +@REM Subroutine used to set !TIMESTAMP! to HH:MM:ss. +@REM CALL :GET_TIMESTAMP +@REM. +@REM Updates: !TIMESTAMP! +FOR /F "tokens=1,2,3 delims=:,." %%a IN ("%TIME%") DO ( + SET "HH=%%a" + SET "MM=%%b" + SET "ss=%%c" +) +SET "TIMESTAMP=!HH!:!MM!:!ss!" +GOTO :eof + +:RESET_ERROR +@REM Subroutine to reset %ERRORLEVEL% to 0. +@REM CALL :RESET_ERROR +@REM. +@REM Updates: %ERRORLEVEL% +EXIT /B 0 +GOTO :eof