Newer
Older
m5scp2_exp / 11_write_factorytest_by_esptool.sh
#!/bin/bash

# This script uploads the factory test firmware to an ESP device using esptool.py.

# 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

# 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

# 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
fi

echo "Using port: $PORT"

# Define the paths to the firmware files
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
if [ ! -f "$BOOTLOADER_PATH" ]; then
    echo "Bootloader file not found at $BOOTLOADER_PATH"
    exit 1
fi
if [ ! -f "$PARTITIONS_PATH" ]; then
    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
fi

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
esptool.py --chip esp32 --port "$PORT" --baud 460800 write_flash -z \
    0x1000 "$BOOTLOADER_PATH" \
    0x8000 "$PARTITIONS_PATH" \
    0x10000 "$FIRMWARE_PATH" 
#    0xe000 "$BOOT_APP0_PATH" \