]> git.donarmstrong.com Git - kiibohd-controller.git/blob - LoadFile/load.teensy
751a97f86aa1b4d29be166a684d73616b74a2157
[kiibohd-controller.git] / LoadFile / load.teensy
1 #!/bin/bash
2 # Convenience script for loading firmware onto a teensy type device
3 # By default, initiates teensy-load-cli
4
5 SERIAL_PORT=""
6 AUTO_SCREEN_SESSION=""
7 PROG_NAME=$(basename $0)
8
9 # Parse all the command line arguments
10 while (( "$#" >= "1" )); do
11         # Scan each argument
12         key="$1"
13         case $key in
14         -a|--autoscreen)
15                 AUTO_SCREEN_SESSION="$2"
16                 shift
17                 ;;
18         -f|--fastload)
19                 SERIAL_PORT="$2"
20                 shift
21                 ;;
22         -h|--help)
23                 echo "Usage: $PROG_NAME [options...]"
24                 echo ""
25                 echo "Loads the most recent built firmware (@TARGET_BIN@) to the device."
26                 echo "  (load.teensy)"
27                 echo ""
28                 echo "Arguments:"
29                 echo " -a, --autoscreen SERIAL_PORT  Use screen on the specified serial port after loading."
30                 echo "                               e.g. /dev/ttyACM0"
31                 echo " -f, --fastload SERIAL_PORT    Send the reload command to the debug terminal."
32                 echo "                               e.g. /dev/ttyACM0"
33                 echo "                               NOTE: May not work due to non-functional terminal, or disable remote flashing"
34                 echo " -h, --help                    This message."
35                 exit 1
36                 ;;
37         *)
38                 echo "INVALID ARG: '$1'"
39                 exit 2
40                 ;;
41         esac
42
43         # Shift to the next argument
44         shift
45 done
46
47 # First check to see teensy-loader-cli has been compiled
48 if [ ! -e teensy-loader-cli/teensy-loader-cli ]; then
49         # Compile teensy-loader-cli
50         mkdir -p teensy-loader-cli
51         cd teensy-loader-cli
52         cmake -G "Unix Makefiles" @CMAKE_SOURCE_DIR@/LoadFile
53         make || exit 3
54         cd -
55 fi
56
57 # If a SERIAL_PORT was specified set the uC into reflash mode
58 # XXX May not be successful if uC is not in a good state (or does not allow remote flashing)
59 if [[ "$SERIAL_PORT" != "" ]] && [[ -e "$SERIAL_PORT" ]]; then
60         echo "NOTE: This may fail if the uC is in a bad state or does not support remote flashing"
61         printf "reload\r" > $SERIAL_PORT
62         sleep 1
63 fi
64
65 # Loads the hex file onto the teensy
66 teensy-loader-cli/teensy-loader-cli -mmcu=@MCU@ -w @TARGET_HEX@
67 EXIT_STATUS=$?
68
69 # Load Screen Session if specified
70 if (( "$EXIT_STATUS" == "0" )) && [[ "$AUTO_SCREEN_SESSION" != "" ]]; then
71         if type screen &>/dev/null; then
72                 sleep 2
73                 screen $AUTO_SCREEN_SESSION
74         else
75                 echo "screen is not installed"
76                 exit 3
77         fi
78 fi
79
80 exit $EXIT_STATUS
81