#!/bin/bash
# Script to program J-Link image onto MCU-Link board
################################################################################
# Copyright 2021-2025 NXP
#
# SPDX-License-Identifier: BSD-3-Clause
#
#################################################################################


usage() {
    echo Usage:
    echo "Connect an MCU-Link probe configured for ISP USB mode."
    echo "This script will program the probe with a J-Link image."
    echo
    echo "The following arguments are accepted:"
    echo "$0                [Programs J-Link Image]"
    echo "$0 'path to s19 bin'   [Programs the chosen binary]"
}


run() {

    # Identify image
    if [ -z "$1" ]; then
        MCULinkImageWild=${FwName}_*.s19
        for file in "$ImageHome/"$MCULinkImageWild; do MCULinkImage=${file} ; done
    else
        MCULinkImage=$1
    fi

    if [ ! -e "$MCULinkImage" ] ; then
        echo "No Probe image found"
        exit 1
    fi

    ShortMCULinkImage=$(basename "$MCULinkImage")

    # Check ready
    RET=1
    until [ ${RET} -eq 0 ]; do
        sleep 1
        $BinHome/rblhost --usb 0x1fc9,$DfuPid get-property 1 2>&1 | grep "Success\." >/dev/null
        RET=$?
        if [ $RET -ne 0 ]; then 
            echo "Configure board for ISP mode and connect via USB then press Space."
            read -s -r -n 1
        fi
    done
    echo

    # Apply CMPA fix for MCU-Link Mini (LPC55S16)
    if [[ ${FwName} == *Lite* ]]; then
        $BinHome/rblhost --usb 0x1fc9,$DfuPid read-memory 0x0003E400 0x200 ${TmpFile} 0 >/dev/null 2>&1 >$Log
        [ -s ${TmpFile} ] && cmp ${TmpFile} ${ImageHome}/MCU-MINI_CMSIS-DAP_CMPA.bin >/dev/null 2>&1 >$Log
        RET=$?
        if [ ${RET} -ne 0 ]; then
            echo "Programming CMPA"
            $BinHome/rblhost --usb 0x1fc9,$DfuPid write-memory 0x0003E400 ${ImageHome}/MCU-MINI_CMSIS-DAP_CMPA.bin 0 >/dev/null 2>&1 >$Log
            RET=$?
            if [ ${RET} -ne 0 ]; then
                cat $Log
                echo
                echo "Programming CMPA has failed!"
                exit ${RET}
            fi
        fi
        rm ${TmpFile}
    fi

    # Program part
    echo "Programming "$ShortMCULinkImage

    $BinHome/rblhost --usb 0x1fc9,$DfuPid flash-image "$MCULinkImage" erase 0 >/dev/null 2>&1 >$Log
    RET=$?
    if [ ${RET} -ne 0 ]; then
        cat $Log
        echo
        echo "Programming "$MCULinkImage" has failed!"
        exit 1
    fi

    cat $Log | grep "Success\." >/dev/null
    if [ ${RET} -eq 0 ]; then
        echo Programmed successfully.
        echo
        echo "- To use: remove ISP jumper and reboot."
        echo
        export IFS= 
        read -s -r -p "Connect next board then press Space (or <return> to Quit)" -n 1 fin
        echo
        if [ -z $fin ]; then
            echo
            exit 1
        fi   
    else
        cat $Log
        exit 1
    fi
}

detect_target() {

    # Detect target by VID:PID
    $BinHome/rblhost --usb 0x1fc9,0x0022 get-property 1 2>&1 | grep "Success\." >/dev/null
    RET=$?
    if [ $RET -eq 0 ]; then
        FwName=Firmware_J-Link-MCU-LinkLite
        DfuPid=0x0022
    else
        FwName=Firmware_J-Link-MCU-Link
    fi
}

version="v3.0 June 2025"
ScriptHome="$( cd "$( dirname "$0" )" && pwd )"
BinHome=$ScriptHome/../bin
ImageHome=$ScriptHome/../probe_firmware
Log=/tmp/MCU-LINK_JLINK-DAP_FW_program.log
TmpFile=/tmp/MCU-LINK_FW_scratch.bin
DfuPid=0x0021
# will be set in detect_target
FwName=

clear
case `echo $1 | tr '[A-Z]' '[a-z]'` in
   help)
       usage
       exit 1
esac

detect_target

echo "J-Link firmware for MCU-Link programming script "$version"."
echo
echo "NOTE: J-Link firmware supports MCU-Link Pro and MCU-Link on-board systems"
echo "that use an LPC55S16 or an LPC55S69 in LQFP100 or BGA98 package."
echo "Although the firmware can be programmed in to MCU-Link and MCU-Link on-board"
echo "systems that use LPC55S69 LQFP64 package, the firmware will not run."
echo
echo "Place the board in ISP USB mode using the appropriate jumper on your MCU-Link."
echo "Refer to the board documentation for more information."
echo "Connect board via USB then press Space."

read -s -r -n 1

while true; do run $1; done
