Files
RTOS/tools/build_scripts/flash_target.sh
T
root ca13734bf0 Add full automotive RTOS project
Add kernel (Cortex-M0/M3/M4, Tricore, S32K, RISC-V ports), drivers,
middleware (CAN stack, diagnostics, safety), applications, board
support, build/test tooling, and documentation.
2026-08-23 03:35:29 -04:00

190 lines
4.4 KiB
Bash
Executable File

#!/bin/bash
/**
* @file flash_target.sh
* @brief Flash firmware to target board
*/
set -e
# Script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
# Default values
TARGET=""
BUILD_TYPE="debug"
PROGRAMMER=""
INTERFACE=""
# Color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# Function to print usage
print_usage() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -t, --target <target> Target board (required)"
echo " -b, --build-type <type> Build type (default: debug)"
echo " -p, --programmer <prog> Programmer type"
echo " Available: stlink, jlink, openocd"
echo " -i, --interface <iface> Debug interface"
echo " Available: swd, jtag"
echo " -h, --help Show this help"
echo ""
}
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-t|--target)
TARGET="$2"
shift 2
;;
-b|--build-type)
BUILD_TYPE="$2"
shift 2
;;
-p|--programmer)
PROGRAMMER="$2"
shift 2
;;
-i|--interface)
INTERFACE="$2"
shift 2
;;
-h|--help)
print_usage
exit 0
;;
*)
echo "Unknown option: $1"
print_usage
exit 1
;;
esac
done
# Validate target
if [ -z "${TARGET}" ]; then
echo -e "${RED}Error: Target not specified${NC}"
print_usage
exit 1
fi
# Set default programmer based on target
if [ -z "${PROGRAMMER}" ]; then
case "${TARGET}" in
stm32f407_discovery)
PROGRAMMER="stlink"
INTERFACE="swd"
;;
nxp_s32k144_evb)
PROGRAMMER="jlink"
INTERFACE="swd"
;;
custom_ecu)
PROGRAMMER="openocd"
INTERFACE="swd"
;;
esac
fi
# Firmware file
FIRMWARE_FILE="${PROJECT_ROOT}/build/${TARGET}/${BUILD_TYPE}/automotive_rtos.elf"
HEX_FILE="${PROJECT_ROOT}/build/${TARGET}/${BUILD_TYPE}/automotive_rtos.hex"
# Check if firmware exists
if [ ! -f "${FIRMWARE_FILE}" ]; then
echo -e "${RED}Error: Firmware not found: ${FIRMWARE_FILE}${NC}"
echo -e "${YELLOW}Run build_target.sh first${NC}"
exit 1
fi
# Print flash info
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE} Flashing Firmware${NC}"
echo -e "${BLUE}========================================${NC}"
echo -e " Target: ${YELLOW}${TARGET}${NC}"
echo -e " Programmer: ${YELLOW}${PROGRAMMER}${NC}"
echo -e " Interface: ${YELLOW}${INTERFACE}${NC}"
echo -e " Firmware: ${YELLOW}${FIRMWARE_FILE}${NC}"
echo ""
# Flash based on programmer
case "${PROGRAMMER}" in
stlink)
echo -e "${YELLOW}Flashing with ST-Link...${NC}"
st-flash --reset write "${HEX_FILE}" 0x08000000
;;
jlink)
echo -e "${YELLOW}Flashing with J-Link...${NC}"
# Create J-Link script
JLINK_SCRIPT="${PROJECT_ROOT}/build/flash.jlink"
cat > "${JLINK_SCRIPT}" << EOF
device ${TARGET}
si ${INTERFACE}
speed 4000
loadfile ${HEX_FILE}
r
g
q
EOF
JLinkExe -CommanderScript "${JLINK_SCRIPT}"
;;
openocd)
echo -e "${YELLOW}Flashing with OpenOCD...${NC}"
# OpenOCD configuration
case "${TARGET}" in
stm32f407_discovery)
OOCD_CFG="board/stm32f4discovery.cfg"
;;
*)
OOCD_CFG="board/${TARGET}.cfg"
;;
esac
openocd \
-f "interface/${INTERFACE}.cfg" \
-f "${OOCD_CFG}" \
-c "program ${FIRMWARE_FILE} verify reset exit"
;;
*)
echo -e "${RED}Error: Unknown programmer: ${PROGRAMMER}${NC}"
exit 1
;;
esac
if [ $? -eq 0 ]; then
echo -e "${GREEN}✓ Flash successful${NC}"
else
echo -e "${RED}✗ Flash failed${NC}"
exit 1
fi
# Verify flash
echo -e "${YELLOW}Verifying flash...${NC}"
case "${PROGRAMMER}" in
stlink)
st-flash verify "${HEX_FILE}"
;;
*)
echo -e "${YELLOW}Verification not supported for ${PROGRAMMER}${NC}"
;;
esac
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN} Flash Complete${NC}"
echo -e "${GREEN}========================================${NC}"