Skip to content

Commit f26e657

Browse files
authored
Fix esptool detection and baud rate issues in Windows batch scripts (#7856)
- Fix esptool detection to use 'version' subcommand instead of no arguments - Fix device-update.bat to use 115200 bps for flashing, 1200 bps only for reset - Add missing closing quotes in debug messages Replace magic numbers with named constants for better maintainability - Add RESET_BAUD=1200 constant for reset baud rate - Add UPDATE_OFFSET=0x10000 constant for update flash offset - Use constants instead of hardcoded values throughout script Extract magic numbers to constants in shell scripts for consistency - Add FLASH_BAUD, RESET_BAUD, UPDATE_OFFSET constants to device-update.sh - Add RESET_BAUD, FIRMWARE_OFFSET constants to device-install.sh - Replace hardcoded values with named constants throughout - Maintain consistency with batch script improvements Fix Python path quoting and remove unreachable code - Quote Python interpreter paths to handle spaces in paths like 'C:\Program Files\Python\python.exe' - Remove unreachable GOTO statements after EXIT /B commands - Improve robustness when custom Python interpreters are specified Fix esptool detection for pipx installations - Change from checking ERRORLEVEL GEQ 2 to EQU 9009 - Pipx-installed esptool returns exit code 2 when showing help (normal) - Only treat Windows 'command not found' error (9009) as truly not found - Add debug output to show actual exit codes for troubleshooting
1 parent a25bfd2 commit f26e657

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

bin/device-install.bat

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,10 @@ IF NOT "__%PYTHON%__"=="____" (
119119

120120
CALL :LOG_MESSAGE DEBUG "Checking esptool command !ESPTOOL_CMD!..."
121121
!ESPTOOL_CMD! >nul 2>&1
122-
IF %ERRORLEVEL% GEQ 2 (
123-
@REM esptool exits with code 1 if help is displayed.
122+
IF %ERRORLEVEL% EQU 9009 (
123+
@REM 9009 = command not found on Windows
124124
CALL :LOG_MESSAGE ERROR "esptool not found: !ESPTOOL_CMD!"
125125
EXIT /B 1
126-
GOTO eof
127126
)
128127
IF %DEBUG% EQU 1 (
129128
CALL :LOG_MESSAGE DEBUG "Skipping ESPTOOL_CMD steps."

bin/device-install.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ BPS_RESET=false
55
TFT_BUILD=false
66
MCU=""
77

8+
# Constants
9+
RESET_BAUD=1200
10+
FIRMWARE_OFFSET=0x00
11+
812
# Variant groups
913
BIGDB_8MB=(
1014
"picomputer-s3"
@@ -121,7 +125,7 @@ while [ $# -gt 0 ]; do
121125
done
122126

123127
if [[ $BPS_RESET == true ]]; then
124-
$ESPTOOL_CMD --baud 1200 --after no_reset read_flash_status
128+
$ESPTOOL_CMD --baud $RESET_BAUD --after no_reset read_flash_status
125129
exit 0
126130
fi
127131

@@ -202,7 +206,7 @@ if [ -f "${FILENAME}" ] && [ -n "${FILENAME##*"update"*}" ]; then
202206

203207
echo "Trying to flash ${FILENAME}, but first erasing and writing system information"
204208
$ESPTOOL_CMD erase-flash
205-
$ESPTOOL_CMD write-flash 0x00 "${FILENAME}"
209+
$ESPTOOL_CMD write-flash $FIRMWARE_OFFSET "${FILENAME}"
206210
echo "Trying to flash ${OTAFILE} at offset ${OTA_OFFSET}"
207211
$ESPTOOL_CMD write-flash $OTA_OFFSET "${OTAFILE}"
208212
echo "Trying to flash ${SPIFFSFILE}, at offset ${OFFSET}"

bin/device-update.bat

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ SET "SCRIPT_NAME=%~nx0"
66
SET "DEBUG=0"
77
SET "PYTHON="
88
SET "ESPTOOL_BAUD=115200"
9+
SET "RESET_BAUD=1200"
10+
SET "UPDATE_OFFSET=0x10000"
911
SET "ESPTOOL_CMD="
1012
SET "LOGCOUNTER=0"
1113
SET "CHANGE_MODE=0"
@@ -85,14 +87,13 @@ IF "!FILENAME:update=!"=="!FILENAME!" (
8587
)
8688

8789
:skip-filename
88-
SET "ESPTOOL_BAUD=1200"
8990

9091
CALL :LOG_MESSAGE DEBUG "Determine the correct esptool command to use..."
9192
IF NOT "__%PYTHON%__"=="____" (
92-
SET "ESPTOOL_CMD=!PYTHON! -m esptool"
93+
SET "ESPTOOL_CMD=""!PYTHON!"" -m esptool"
9394
CALL :LOG_MESSAGE DEBUG "Python interpreter supplied."
9495
) ELSE (
95-
CALL :LOG_MESSAGE DEBUG "Python interpreter NOT supplied. Looking for esptool...
96+
CALL :LOG_MESSAGE DEBUG "Python interpreter NOT supplied. Looking for esptool..."
9697
WHERE esptool >nul 2>&1
9798
IF %ERRORLEVEL% EQU 0 (
9899
@REM WHERE exits with code 0 if esptool is found.
@@ -105,11 +106,11 @@ IF NOT "__%PYTHON%__"=="____" (
105106

106107
CALL :LOG_MESSAGE DEBUG "Checking esptool command !ESPTOOL_CMD!..."
107108
!ESPTOOL_CMD! >nul 2>&1
108-
IF %ERRORLEVEL% GEQ 2 (
109-
@REM esptool exits with code 1 if help is displayed.
109+
CALL :LOG_MESSAGE DEBUG "esptool exit code: %ERRORLEVEL%"
110+
IF %ERRORLEVEL% EQU 9009 (
111+
@REM 9009 = command not found on Windows
110112
CALL :LOG_MESSAGE ERROR "esptool not found: !ESPTOOL_CMD!"
111113
EXIT /B 1
112-
GOTO eof
113114
)
114115
IF %DEBUG% EQU 1 (
115116
CALL :LOG_MESSAGE DEBUG "Skipping ESPTOOL_CMD steps."
@@ -127,13 +128,13 @@ CALL :LOG_MESSAGE INFO "Using esptool baud: !ESPTOOL_BAUD!."
127128

128129
IF %CHANGE_MODE% EQU 1 (
129130
@REM Attempt to change mode via 1200bps Reset.
130-
CALL :RUN_ESPTOOL !ESPTOOL_BAUD! --after no_reset read_flash_status
131+
CALL :RUN_ESPTOOL !RESET_BAUD! --after no_reset read_flash_status
131132
GOTO eof
132133
)
133134

134135
@REM Flashing operations.
135-
CALL :LOG_MESSAGE INFO "Trying to flash update "!FILENAME!" at OFFSET 0x10000..."
136-
CALL :RUN_ESPTOOL !ESPTOOL_BAUD! write-flash 0x10000 "!FILENAME!" || GOTO eof
136+
CALL :LOG_MESSAGE INFO "Trying to flash update "!FILENAME!" at OFFSET !UPDATE_OFFSET!..."
137+
CALL :RUN_ESPTOOL !ESPTOOL_BAUD! write-flash !UPDATE_OFFSET! "!FILENAME!" || GOTO eof
137138

138139
CALL :LOG_MESSAGE INFO "Script complete!."
139140

bin/device-update.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
PYTHON=${PYTHON:-$(which python3 python|head -n 1)}
44
CHANGE_MODE=false
55

6+
# Constants
7+
FLASH_BAUD=115200
8+
RESET_BAUD=1200
9+
UPDATE_OFFSET=0x10000
10+
611
# Determine the correct esptool command to use
712
if "$PYTHON" -m esptool version >/dev/null 2>&1; then
813
ESPTOOL_CMD="$PYTHON -m esptool"
@@ -64,7 +69,7 @@ done
6469
shift "$((OPTIND-1))"
6570

6671
if [ "$CHANGE_MODE" = true ]; then
67-
$ESPTOOL_CMD --baud 1200 --after no_reset read_flash_status
72+
$ESPTOOL_CMD --baud $RESET_BAUD --after no_reset read_flash_status
6873
exit 0
6974
fi
7075

@@ -75,7 +80,7 @@ fi
7580

7681
if [ -f "${FILENAME}" ] && [ -z "${FILENAME##*"update"*}" ]; then
7782
echo "Trying to flash update ${FILENAME}"
78-
$ESPTOOL_CMD --baud 115200 write-flash 0x10000 "${FILENAME}"
83+
$ESPTOOL_CMD --baud $FLASH_BAUD write-flash $UPDATE_OFFSET "${FILENAME}"
7984
else
8085
show_help
8186
echo "Invalid file: ${FILENAME}"

0 commit comments

Comments
 (0)