Zoltan Hudak / QtCreator_Support
Revision:
7:b567983e76f4
Parent:
6:f3529afba368
Child:
8:66aa32957926
--- a/mbed.bat	Sun Aug 23 19:48:40 2020 +0000
+++ b/mbed.bat	Sun Sep 27 13:57:00 2020 +0000
@@ -1,142 +1,189 @@
 #!/bin/bash
 
-echo off
+# Mbed tool for the QtCreator IDE.
 
+# default settings
 MBED_SYS_PATH=~/src/arm/sys
 LIB_PATH=~/src/arm/lib
 OS=6
-PROF=DEBUG
+PROFILE=DEBUG
 PROJ_NAME=${PWD##*/}
 PORT=/dev/ttyACM0
 
+# Mbed command parser. Sets bash variables based on the selected Mbed CLI options.
+# Ones parsing is complete the options and arguments can be passed to Mbec CLI.
 MACROS=()
 SRC_LIB_PATH=()
 while [[ $# -gt 0 ]]
 do
-key="$1"
+    # Get first token
+    key="$1"
+
+    case $key in
+        # Flashes target with binary.
+        all|-f)
+        FLASH='-f'
+        shift # past argument
+        ;;
+
+        # Cleans files before building.
+        -c|clean)
+        CLEAN='-c'
+        shift # past argument
+        ;;
+
+        # Adds macro.
+        -D|--macro)
+        MACROS+=('-D '$2)
+        shift # past argument
+        shift # past value
+        ;;
+
+        # Adds library with relative path.
+        -l)
+        SRC_LIB_PATH+=' --source '${LIB_PATH}/$2
+        shift # past argument
+        shift # past value
+        ;;
 
-case $key in
-    all|-f)
-    FLASH='-f'
-    shift # past argument
-    ;;
-    -c|clean)
-    CLEAN='-c'
-    shift # past argument
-    ;;
-    -D|--macro)
-    MACROS+=('-D '$2)
-    shift # past argument
-    shift # past value
-    ;;
-    -l)
-    SRC_LIB_PATH+=' --source '${LIB_PATH}/$2
-    shift # past argument
-    shift # past value
-    ;;
-    -m)
-    MCU="$2"
-    shift # past argument
-    shift # past value
-    ;;
-    -os)
-    OS="$2"
-    shift # past argument
-    shift # past value
-    ;;
-    -p|--port)
-    PORT="$2"
-    shift # past argument
-    shift # past value
-    ;;
-    -r|--release)
-    PROF=RELEASE
-    shift # past argument
-    ;;
-    *)    # additional args
-    ARGS+=("$key $2")
-    shift # past argument
-    ;;
-esac
+        # Defines target Micro-Controller Unit (MCU).
+        -m)
+        MCU="$2"
+        shift # past argument
+        shift # past value
+        ;;
+
+        # Selects Mbed Operating System (2 or other).
+        -os)
+        OS="$2"
+        shift # past argument
+        shift # past value
+        ;;
+
+        # Selects serial port.
+        -p|--port)
+        PORT="$2"
+        shift # past argument
+        shift # past value
+        ;;
+
+        # Sets build profile (debug or release)
+        -r|--release)
+        PROFILE=RELEASE
+        shift # past argument
+        ;;
+
+        # Takes additional options and passes them to Mbed CLI.
+        *)    # additional make and Mbed CLI args
+        ARGS+=("$key ")
+        shift # past argument
+        ;;
+    esac
 done
 
+# Sets path to the selected Mbed OS tools and profile.
 if [ ${OS} == 2 ]
 then
 	GCC_ARM_PATH=/opt/gcc-arm-none-eabi-6-2017-q2-update
 	MBED_OS_PATH=${MBED_SYS_PATH}/mbed
 	CUSTOM_TARGETS_PATH=${MBED_SYS_PATH}/custom_targets/mbed
-	PROFILE=debug
-	if [ ${PROF} == RELEASE ]
+	PROF=debug
+	if [ ${PROFILE} == RELEASE ]
 	then
-		PROFILE=release
+	    PROF=release
 	fi
 else
 	GCC_ARM_PATH=/opt/gcc-arm-none-eabi-9-2019-q4-major
 	MBED_OS_PATH=${MBED_SYS_PATH}/mbed-os
 	CUSTOM_TARGETS_PATH=${MBED_SYS_PATH}/custom_targets/mbed-os
-	PROFILE=${MBED_SYS_PATH}/mbed-os/tools/profiles/debug.json
-	if [ ${PROF} == RELEASE ]
+	PROF=${MBED_SYS_PATH}/mbed-os/tools/profiles/debug.json
+	if [ ${PROFILE} == RELEASE ]
 	then
-		PROFILE=${MBED_SYS_PATH}/mbed-os/tools/profiles/release.json
+	    PROF=${MBED_SYS_PATH}/mbed-os/tools/profiles/release.json
 	fi
 fi
 
+# Calls Mbed CLI "config" command to set path to the toolchain (GCC ARM) and Mbed OS (2 or other)
 mbed config -G GCC_ARM_PATH ${GCC_ARM_PATH}/bin
 mbed config -G MBED_OS_DIR ${MBED_OS_PATH}
+
+# Creates a new project if no such exists yet.
 if ! [ -f ./.mbed ]
 then
     mbed new .
 fi
+
+# Executes Mbed CLI "export" command using the selected options.
 mbed export -i qtcreator -m ${MCU} --source . --source ${CUSTOM_TARGETS_PATH} --source ${MBED_OS_PATH} --source ${GCC_ARM_PATH}/arm-none-eabi/include ${SRC_LIB_PATH}
-#mbed export -i vscode_gcc_arm -m ${MCU} --source . --source ${CUSTOM_TARGETS_PATH} --source ${MBED_OS_PATH} --source ${GCC_ARM_PATH}/arm-none-eabi/include ${SRC_LIB_PATH}
-mbed compile -t GCC_ARM -m ${MCU} ${ARGS} --source . --source ${CUSTOM_TARGETS_PATH} --source ${MBED_OS_PATH} ${SRC_LIB_PATH} $CLEAN --profile ${PROFILE}
+
+# Adds files to the project to be available in the QtCreator IDE for editing.
+find ~/.local/bin -name "mbed.bat" >> ${PROJ_NAME}.files
+find . -name "*.json" >> ${PROJ_NAME}.files
+find . -name "*.mbedignore"  >> ${PROJ_NAME}.files
 
-#if [ "${MCU}" == "LPC1768DBG" ]
-#then
-#	${GCC_ARM_PATH}/bin/arm-none-eabi-objcopy -I binary -O ihex ./BUILD/${MCU}/GCC_ARM-${PROF}/${PROJ_NAME}.bin ./BUILD/${MCU}/GCC_ARM-${PROF}/${PROJ_NAME}.hex
-#fi
+# Executes Mbed CLI "compile" command using the selected options.
+mbed compile -t GCC_ARM -m ${MCU} -j2 ${ARGS} --source . --source ${CUSTOM_TARGETS_PATH} --source ${MBED_OS_PATH} ${SRC_LIB_PATH} $CLEAN --profile ${PROF} |tee ./BUILD/compile_output.txt
 
-if [ "${FLASH}" == "-f" ]
+# Checks for a binary built by Mbed CLI.
+RESULT=$(grep "Elf2Bin" ./BUILD/compile_output.txt)
+
+# If the compilation succeeded (a binary exists) flash the target MCU with the resulted binary file.
+if [ "${RESULT}" != "" ]
 then
-	case ${MCU} in
-    	LPC1768)
-    		MCU_CFG="pyOCD"
-    		;;
-    	LPC1768DBG)
-    		MCU_CFG="pyOCD"
-    		;;
-    	BLUEPILL|MAPLE_MINI|NUCLEO_F103RB|STM32F103RC|STM32F103VE)
-    		MCU_CFG="/usr/local/share/openocd/scripts/board/st_nucleo_f103rb.cfg"
-    		;;
-    	NUCLEO_F446RE|STM32F407VE)
-    		MCU_CFG="/usr/local/share/openocd/scripts/board/st_nucleo_f4.cfg"
-    		;;
-    	NUCLEO_F767ZI)
-    		MCU_CFG="/usr/local/share/openocd/scripts/board/stm32f7discovery.cfg"
-    		;;
-    	*)
-    		MCU_CFG="not_supported" 
-    		echo "No support to flash "${MCU}
-    		;;
-	esac
-	if [ "$MCU_CFG" != "not_supported" ]
+    # Uncomment the following lines if you want to build a "hex" file for Flash Magic.
+	#if [ "${MCU}" == "LPC1768DBG" ]
+	#then
+	#	${GCC_ARM_PATH}/bin/arm-none-eabi-objcopy -I binary -O ihex ./BUILD/${MCU}/GCC_ARM-${PROFILE}/${PROJ_NAME}.bin ./BUILD/${MCU}/GCC_ARM-${PROFILE}/${PROJ_NAME}.hex
+	#fi
+
+    # If the "all" or "-f" option was selected choose the right tool and configuration.
+    if [ "${FLASH}" == "-f" ]
 	then
-		echo "Flashing the "${MCU}" ..."
-		case ${MCU_CFG} in
-			pyOCD)
-				if [ "$PORT" == "/dev/ttyACM0" ]
-				then
-    				~/.local/bin/pyocd-flashtool -t lpc1768 ./BUILD/${MCU}/GCC_ARM-${PROF}/${PROJ_NAME}.bin
-    				#~/.local/bin/pyocd-flashtool -t lpc1768 ./BUILD/${MCU}/GCC_ARM-${PROF}/${PROJ_NAME}.bin > /dev/null 2>&1
-    			else
-    				lpc21isp -bin ./BUILD/${MCU}/GCC_ARM-${PROF}/${PROJ_NAME}.bin ${PORT} 115200 96000
-    			fi
-    			;;
+		case ${MCU} in
+		    # Targets supported by pyOCD
+			LPC1768|LPC1768DBG)
+			    MCU_CFG="pyOCD"
+				;;
+				# Targets supported by OpenOCD.
+			# Configure OpenOCD according to the selected target.
+			BLUEPILL|MAPLE_MINI|NUCLEO_F103RB|STM32F103RC|STM32F103VE)
+				MCU_CFG="/usr/local/share/openocd/scripts/board/st_nucleo_f103rb.cfg"
+				;;
+			NUCLEO_F411RE|NUCLEO_F446RE|STM32F407VE)
+				MCU_CFG="/usr/local/share/openocd/scripts/board/st_nucleo_f4.cfg"
+				;;
+			NUCLEO_F767ZI)
+				MCU_CFG="/usr/local/share/openocd/scripts/board/stm32f7discovery.cfg"
+				;;
+				# Warn when flashing of selected target MCU is not supported.
 			*)
-  				openocd -f ${MCU_CFG} -c """program ./BUILD/${MCU}/GCC_ARM-${PROF}/${PROJ_NAME}.elf verify reset exit"""
-  				#openocd -f ${MCU_CFG} -c """program ./BUILD/${MCU}/GCC_ARM-${PROF}/${PROJ_NAME}.elf verify reset exit""" > /dev/null 2>&1
-  				;;
-  		esac
-  		echo "Done"
-  	fi
+				MCU_CFG="not_supported" 
+				echo "No support to flash "${MCU}
+				;;
+		esac
+
+        # When the target MCU is supported flash the target MCU with the binary code.
+		if [ "$MCU_CFG" != "not_supported" ]
+		then
+			echo "Flashing the "${MCU}" ..."
+			case ${MCU_CFG} in
+			    # Use pyOCD to flash the target when port was specified.
+				pyOCD)
+					if [ "$PORT" == "/dev/ttyACM0" ]
+					then
+					    # Use pyOCD to flash the target when port was specified.
+						~/.local/bin/pyocd-flashtool -t lpc1768 ./BUILD/${MCU}/GCC_ARM-${PROFILE}/${PROJ_NAME}.bin
+					else
+					    # Use "lpc2isp" otherwise.
+						lpc21isp -bin ./BUILD/${MCU}/GCC_ARM-${PROFILE}/${PROJ_NAME}.bin ${PORT} 115200 96000
+					fi
+					;;
+				# Use OpenOCD to flash the target MCU
+				*)
+				    openocd -f ${MCU_CFG} -c """program ./BUILD/${MCU}/GCC_ARM-${PROFILE}/${PROJ_NAME}.elf verify reset exit"""
+	  				;;
+	  		esac
+			echo "Done."
+	  	fi
+	fi
 fi