diff --git a/11_write_factorytest_by_esptool.sh b/11_write_factorytest_by_esptool.sh index 4156425..f4ec148 100755 --- a/11_write_factorytest_by_esptool.sh +++ b/11_write_factorytest_by_esptool.sh @@ -1,79 +1,16 @@ #!/bin/bash -# This script uploads the factory test firmware to an ESP device using esptool.py. +source ./lib.sh -# Check if esptool.py is installed -if command -v esptool.py &> /dev/null -then - BUILD_TOOL="esptool.py" -else - if ! command -v py &> /dev/null - then - echo "esptool.py nor py could not be found. Please install it first." - exit 1 - fi - BUILD_TOOL="py -m esptool" -fi +# Get esptool command +BUILD_TOOL=$(lib_get_esptool_command) # Find port - Cross-platform detection -PORT="" -# Detect OS and find appropriate port -OS_TYPE=$(uname -s) -case "$OS_TYPE" in - "Darwin") # macOS - echo "Detected macOS" - # Try common macOS patterns - for pattern in "/dev/cu.usbserial*" "/dev/cu.usbmodem*" "/dev/cu.wchusbserial*"; do - PORT=$(ls $pattern 2>/dev/null | head -n 1) - if [ ! -z "$PORT" ]; then - break - fi - done - ;; - "Linux") # Linux - echo "Detected Linux" - # Try common Linux patterns - for pattern in "/dev/ttyUSB*" "/dev/ttyACM*"; do - PORT=$(ls $pattern 2>/dev/null | head -n 1) - if [ ! -z "$PORT" ]; then - break - fi - done - ;; - "CYGWIN"*|"MINGW"*|"MSYS"*) # Windows (Git Bash, Cygwin, etc.) - echo "Detected Windows" - # For Windows, try to detect COM ports - # Note: This works in Git Bash or similar environments - for num in {3..20}; do - if [ -e "/dev/ttyS$((num-1))" ]; then - PORT="COM$num" - break - fi - done - # Alternative: use mode command if available - if [ -z "$PORT" ] && command -v mode.com &> /dev/null; then - PORT=$(mode.com 2>/dev/null | grep -o "COM[0-9]*" | head -n 1) - fi - ;; - *) - echo "Unknown OS: $OS_TYPE" - echo "Please manually specify the port using PORT environment variable" - echo "Example: PORT=/dev/ttyUSB0 $0" - ;; -esac +PORT=$(lib_detect_esptool_port) # Check if PORT is set, otherwise prompt user if [ -z "$PORT" ]; then - echo "No ESP device found automatically." - echo "Please connect your ESP device and ensure drivers are installed." - echo "Common ports:" - echo " Linux: /dev/ttyUSB0, /dev/ttyACM0" - echo " macOS: /dev/cu.usbserial*, /dev/cu.usbmodem*" - echo " Windows: COM3, COM4, etc." - echo "" - echo "You can also manually specify the port:" - echo " PORT=/dev/ttyUSB0 $0" - exit 1 + lib_no_esp_device_message_and_exit fi echo "Using port: $PORT" @@ -82,7 +19,6 @@ BUILD_PATH="FactoryTest/build/esp32.esp32.m5stack_stickc_plus2" BOOTLOADER_PATH="$BUILD_PATH/FactoryTest.ino.bootloader.bin" PARTITIONS_PATH="$BUILD_PATH/FactoryTest.ino.partitions.bin" -BOOT_APP0_PATH="$BUILD_PATH/boot_app0.bin" FIRMWARE_PATH="$BUILD_PATH/FactoryTest.ino.bin" # Check if all required files exist @@ -94,10 +30,6 @@ echo "Partitions file not found at $PARTITIONS_PATH" exit 1 fi -# if [ ! -f "$BOOT_APP0_PATH" ]; then -# echo "Boot app0 file not found at $BOOT_APP0_PATH" -# exit 1 -# fi if [ ! -f "$FIRMWARE_PATH" ]; then echo "Firmware file not found at $FIRMWARE_PATH" exit 1 @@ -106,12 +38,12 @@ echo "Flashing complete ESP32 firmware..." echo "Bootloader: $BOOTLOADER_PATH" echo "Partitions: $PARTITIONS_PATH" -echo "Boot app0: $BOOT_APP0_PATH" echo "Main firmware: $FIRMWARE_PATH" # Upload the complete firmware using esptool.py $BUILD_TOOL --chip esp32 --port "$PORT" --baud 460800 write_flash -z \ 0x1000 "$BOOTLOADER_PATH" \ 0x8000 "$PARTITIONS_PATH" \ - 0x10000 "$FIRMWARE_PATH" -# 0xe000 "$BOOT_APP0_PATH" \ + 0x10000 "$FIRMWARE_PATH" + +echo "Flashing completed." \ No newline at end of file diff --git a/22_write_byhash_esptool.sh b/22_write_byhash_esptool.sh index bf41eca..e76e3c1 100755 --- a/22_write_byhash_esptool.sh +++ b/22_write_byhash_esptool.sh @@ -1,74 +1,15 @@ #!/bin/bash -# This script uploads the factory test firmware to an ESP device using esptool.py. +source ./lib.sh -# Check if esptool.py is installed -if ! command -v esptool.py &> /dev/null -then - echo "esptool.py could not be found. Please install it first." - exit 1 -fi +# Get esptool command +BUILD_TOOL=$(lib_get_esptool_command) # Find port - Cross-platform detection -PORT="" +PORT=$(lib_detect_esptool_port) -# Detect OS and find appropriate port -OS_TYPE=$(uname -s) -case "$OS_TYPE" in - "Darwin") # macOS - echo "Detected macOS" - # Try common macOS patterns - for pattern in "/dev/cu.usbserial*" "/dev/cu.usbmodem*" "/dev/cu.wchusbserial*"; do - PORT=$(ls $pattern 2>/dev/null | head -n 1) - if [ ! -z "$PORT" ]; then - break - fi - done - ;; - "Linux") # Linux - echo "Detected Linux" - # Try common Linux patterns - for pattern in "/dev/ttyUSB*" "/dev/ttyACM*"; do - PORT=$(ls $pattern 2>/dev/null | head -n 1) - if [ ! -z "$PORT" ]; then - break - fi - done - ;; - "CYGWIN"*|"MINGW"*|"MSYS"*) # Windows (Git Bash, Cygwin, etc.) - echo "Detected Windows" - # For Windows, try to detect COM ports - # Note: This works in Git Bash or similar environments - for num in {3..20}; do - if [ -e "/dev/ttyS$((num-1))" ]; then - PORT="COM$num" - break - fi - done - # Alternative: use mode command if available - if [ -z "$PORT" ] && command -v mode.com &> /dev/null; then - PORT=$(mode.com 2>/dev/null | grep -o "COM[0-9]*" | head -n 1) - fi - ;; - *) - echo "Unknown OS: $OS_TYPE" - echo "Please manually specify the port using PORT environment variable" - echo "Example: PORT=/dev/ttyUSB0 $0" - ;; -esac - -# Check if PORT is set, otherwise prompt user if [ -z "$PORT" ]; then - echo "No ESP device found automatically." - echo "Please connect your ESP device and ensure drivers are installed." - echo "Common ports:" - echo " Linux: /dev/ttyUSB0, /dev/ttyACM0" - echo " macOS: /dev/cu.usbserial*, /dev/cu.usbmodem*" - echo " Windows: COM3, COM4, etc." - echo "" - echo "You can also manually specify the port:" - echo " PORT=/dev/ttyUSB0 $0" - exit 1 + lib_no_esp_device_message_and_exit fi echo "Using port: $PORT" @@ -99,7 +40,9 @@ echo "Main firmware: $FIRMWARE_PATH" # Upload the complete firmware using esptool.py -esptool.py --chip esp32 --port "$PORT" --baud 460800 write_flash -z \ +$BUILD_TOOL --chip esp32 --port "$PORT" --baud 460800 write_flash -z \ 0x1000 "$BOOTLOADER_PATH" \ 0x8000 "$PARTITIONS_PATH" \ 0x10000 "$FIRMWARE_PATH" + +echo "Flashing completed." \ No newline at end of file diff --git a/33_write_latest_esptool.sh b/33_write_latest_esptool.sh index e12985a..12c3d59 100755 --- a/33_write_latest_esptool.sh +++ b/33_write_latest_esptool.sh @@ -1,74 +1,16 @@ #!/bin/bash -# This script uploads the factory test firmware to an ESP device using esptool.py. +source ./lib.sh -# Check if esptool.py is installed -if ! command -v esptool.py &> /dev/null -then - echo "esptool.py could not be found. Please install it first." - exit 1 -fi +# Get esptool command +BUILD_TOOL=$(lib_get_esptool_command) # Find port - Cross-platform detection -PORT="" - -# Detect OS and find appropriate port -OS_TYPE=$(uname -s) -case "$OS_TYPE" in - "Darwin") # macOS - echo "Detected macOS" - # Try common macOS patterns - for pattern in "/dev/cu.usbserial*" "/dev/cu.usbmodem*" "/dev/cu.wchusbserial*"; do - PORT=$(ls $pattern 2>/dev/null | head -n 1) - if [ ! -z "$PORT" ]; then - break - fi - done - ;; - "Linux") # Linux - echo "Detected Linux" - # Try common Linux patterns - for pattern in "/dev/ttyUSB*" "/dev/ttyACM*"; do - PORT=$(ls $pattern 2>/dev/null | head -n 1) - if [ ! -z "$PORT" ]; then - break - fi - done - ;; - "CYGWIN"*|"MINGW"*|"MSYS"*) # Windows (Git Bash, Cygwin, etc.) - echo "Detected Windows" - # For Windows, try to detect COM ports - # Note: This works in Git Bash or similar environments - for num in {3..20}; do - if [ -e "/dev/ttyS$((num-1))" ]; then - PORT="COM$num" - break - fi - done - # Alternative: use mode command if available - if [ -z "$PORT" ] && command -v mode.com &> /dev/null; then - PORT=$(mode.com 2>/dev/null | grep -o "COM[0-9]*" | head -n 1) - fi - ;; - *) - echo "Unknown OS: $OS_TYPE" - echo "Please manually specify the port using PORT environment variable" - echo "Example: PORT=/dev/ttyUSB0 $0" - ;; -esac +PORT=$(lib_detect_esptool_port) # Check if PORT is set, otherwise prompt user if [ -z "$PORT" ]; then - echo "No ESP device found automatically." - echo "Please connect your ESP device and ensure drivers are installed." - echo "Common ports:" - echo " Linux: /dev/ttyUSB0, /dev/ttyACM0" - echo " macOS: /dev/cu.usbserial*, /dev/cu.usbmodem*" - echo " Windows: COM3, COM4, etc." - echo "" - echo "You can also manually specify the port:" - echo " PORT=/dev/ttyUSB0 $0" - exit 1 + lib_no_esp_device_message_and_exit fi echo "Using port: $PORT" @@ -99,7 +41,9 @@ echo "Main firmware: $FIRMWARE_PATH" # Upload the complete firmware using esptool.py -esptool.py --chip esp32 --port "$PORT" --baud 460800 write_flash -z \ +$BUILD_TOOL --chip esp32 --port "$PORT" --baud 460800 write_flash -z \ 0x1000 "$BOOTLOADER_PATH" \ 0x8000 "$PARTITIONS_PATH" \ 0x10000 "$FIRMWARE_PATH" + +echo "Flashing completed." \ No newline at end of file diff --git a/lib.sh b/lib.sh new file mode 100644 index 0000000..5090f6e --- /dev/null +++ b/lib.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bash + +# +lib_get_esptool_command() { + if command -v esptool.py &> /dev/null + then + echo "esptool.py" + else + if ! command -v py &> /dev/null + then + echo "esptool.py nor py could not be found. Please install it first." + exit 1 + fi + # check if esptool is installed as a module + if py -m esptool --help &> /dev/null + then + echo "py -m esptool" + else + echo "install esptool module for python first." + py -m pip install esptool + fi + echo "py -m esptool" + fi +} + +lib_detect_esptool_port() { + local PORT="" + local OS_TYPE=$(uname -s) + case "$OS_TYPE" in + "Darwin") # macOS + # Try common macOS patterns + for pattern in "/dev/cu.usbserial*" "/dev/cu.usbmodem*" "/dev/cu.wchusbserial*"; do + PORT=$(ls $pattern 2>/dev/null | head -n 1) + if [ ! -z "$PORT" ]; then + break + fi + done + ;; + "Linux") # Linux + # Try common Linux patterns + for pattern in "/dev/ttyUSB*" "/dev/ttyACM*"; do + PORT=$(ls $pattern 2>/dev/null | head -n 1) + if [ ! -z "$PORT" ]; then + break + fi + done + ;; + "CYGWIN"*|"MINGW"*|"MSYS"*) # Windows (Git Bash, Cygwin, etc.) + # For Windows, try to detect COM ports + # Note: This works in Git Bash or similar environments + for num in {3..20}; do + if [ -e "/dev/ttyS$((num-1))" ]; then + PORT="COM$num" + break + fi + done + # Alternative: use mode command if available + if [ -z "$PORT" ] && command -v mode.com &> /dev/null; then + PORT=$(mode.com 2>/dev/null | grep -o "COM[0-9]*" | head -n 1) + fi + ;; + *) + echo "Unknown OS: $OS_TYPE" + echo "Please manually specify the port using PORT environment variable" + echo "Example: PORT=/dev/ttyUSB0 $0" + ;; + esac + + echo "$PORT" +} + +lib_no_esp_device_message_and_exit(){ + echo "No ESP device found automatically." + echo "Please connect your ESP device and ensure drivers are installed." + echo "Common ports:" + echo " Linux: /dev/ttyUSB0, /dev/ttyACM0" + echo " macOS: /dev/cu.usbserial*, /dev/cu.usbmodem*" + echo " Windows: COM3, COM4, etc." + echo "エラー:デバイスが接続されていない?" + exit 1 +} \ No newline at end of file