]> git.donarmstrong.com Git - kiibohd-controller.git/blob - LoadFile/load.dfu
Start removing select Linux-isms
[kiibohd-controller.git] / LoadFile / load.dfu
1 #!/usr/bin/env bash
2 # Convenience script for loading firmware onto a dfu type device
3 # By default, initiates dfu-util
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.dfu)"
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 # If a SERIAL_PORT was specified set the uC into reflash mode
48 # XXX May not be successful if uC is not in a good state (or does not allow remote flashing)
49 if [[ "$SERIAL_PORT" != "" ]] && [[ -e "$SERIAL_PORT" ]]; then
50         echo "NOTE: This may fail if the uC is in a bad state or does not support remote flashing"
51         printf "reload\r" > $SERIAL_PORT
52         sleep 2
53 fi
54
55 # Load via dfu-util
56 # Used for McHCK based uCs
57 if type dfu-util &>/dev/null; then
58         dfu-util -D @TARGET_BIN@
59         EXIT_STATUS=$?
60 else
61         echo "dfu-util is required to reprogram the device"
62         exit 3
63 fi
64
65 # Load Screen Session if specified
66 if (( "$EXIT_STATUS" == "0" )) && [[ "$AUTO_SCREEN_SESSION" != "" ]]; then
67         if type screen &>/dev/null; then
68                 sleep 0.1
69                 screen $AUTO_SCREEN_SESSION
70         else
71                 echo "screen is not installed"
72                 exit 3
73         fi
74 fi
75
76 exit $EXIT_STATUS
77