mkstevo - 2018-12-12

With the generous assistance of Trev, I have a script that I use to compile the current source code and, assuming no errors are found, program a target device using the NSDSP programmer. The script automatically detects the #Chip type and then passes this to the command line utility (nsprog) provided by NS. This script requires that it is placed into the same directory as your GCB compiler (gcbasic) and that the NS utility (nsprog) is also located in the same directory. Trev gave me lots of advice on making this more flexible by searching for the GCB installation directory, but this failed to work correctly for me.

I have saved this script as 'MakeHex_Burn.sh' and I call it from the Geany IDE using this:
/Users/mkstevo/GreatCowBasic/makehex_burn.sh "%f" "%d"

In Geany the parameters "%f" and "%d" are replaced by Geany with the current filename, and the directory path to that file.

Script:

#!/bin/bash
##  Version 0.95.002
##  Adapted for use on Linux from makeHEX.bat. Not to be used on Windows.
##  You can adapt this file to your needs including removing the .sh extension for convenience.
## In this script, $1 gets replaced with whatever you enter for <sourcefile> on the command line.

# $0 contains the path to the directory containing this script.
# If this location also includes both the gcbasic and nsprog
# executables, this script should be able to locate them correctly.

TIMEFORMAT='Compilation and programming time: %Rs'
time {
basename=${0##*/}

# installdir=$(dirname $(which gcbasic))  # Didn't work for me?
# installdir=/Users/mkstevo/GreatCowBasic # Static address

installdir=`echo "$0" | rev | cut -d"/" -f2- | rev`
echo "Installation directory assumed to be: "$installdir # Dynamic address

dirName=${2}/

hex_file=$(basename "$1" | rev | cut -d"." -f2-  | rev).hex

html_file=$(basename "$1" | rev | cut -d"." -f2-  | rev).html

if [ $# -ne 2 ]; then
 echo "Usage: $basename <sourcefile> <path\directory> in Geany use ''"%f"'' ''"%d"'' (double quotes) "
 echo "Example: $basename myfile.gcb /my_Path/my_Directory/"
 echo
 exit 1
fi

out_name=$(basename "$1" | cut -d. -f1).hex # Note HEX suffix

${installdir}/gcbasic /NP /K:A /v /A:GCASM "${1}"
Err=$?
if [ $Err -eq 0 ]; then
 echo "No compilation errors found. Continuing with device programming process."
else
 echo "ERROR running ${0} with parameters: ${*}"
 echo
 echo "Error during compile. Error: ($Err) Skipping device programming."
 exit ${Err}
fi

# Extract PIC device name from html file
device_name=`grep "Chip Model" "${dirName}${html_file}"`
device_name=PIC`echo ${device_name} | cut -d" " -f3 | cut -d"<" -f1`

# Program device
${installdir}/nsprog p -h -d "${device_name}" -i "${hex_file}"

Err=$?
if [ $Err -ne 0 ]; then
echo "Error: ($Err) running nsprog with parameters: p -h -d ${device_name} -i ${hex_file}"
exit ${Err}
fi

##  --- edit END ----------------------------------------------
}

If you copy and paste the script contents into an existing script file, be sure to change the command that invokes it to reflect the filename of the script.

Note that on the line:

${installdir}/nsprog p -h -d "${device_name}" -i "${hex_file}"

The -h parameter selects High Voltage Programming for the NSDSP programming interface. If you do not want to use HVP, remove the -h.

Should anyone else be using Geany (or SynWrite) on the Mac, to allow the IDE to correctly locate any errors found in the source code on compile, the error Regular Expression should be set to:

(?P<file>[^(]+) \((?P<line>\d+)

Hope this helps someone.