avrflash 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #! /bin/bash
  2. #
  3. # Flash an AVR with optiboot using the meta-id built-in programmer
  4. # Basically we first reset the AVR and get in sync, and then send the hex file
  5. #
  6. # ----------------------------------------------------------------------------
  7. # "THE BEER-WARE LICENSE" (Revision 42):
  8. # Thorsten von Eicken wrote this file. As long as you retain
  9. # this notice you can do whatever you want with this stuff. If we meet some day,
  10. # and you think this stuff is worth it, you can buy me a beer in return.
  11. # ----------------------------------------------------------------------------
  12. show_help() {
  13. cat <<EOT
  14. Usage: ${0##*/} [-options...] hostname sketch.hex
  15. Flash the AVR running optiboot attached to meta-id with the sketch.
  16. -v Be verbose
  17. -h show this help
  18. Example: ${0##*/} -v meta-id mysketch.hex
  19. ${0##*/} 192.168.4.1 mysketch.hex
  20. EOT
  21. }
  22. if ! which curl >/dev/null; then
  23. echo "ERROR: Cannot find curl: it is required for this script." >&2
  24. exit 1
  25. fi
  26. start=`date +%s`
  27. # ===== Parse arguments
  28. verbose=
  29. while getopts "hvx:" opt; do
  30. case "$opt" in
  31. h) show_help; exit 0 ;;
  32. v) verbose=1 ;;
  33. x) foo="$OPTARG" ;;
  34. '?') show_help >&2; exit 1 ;;
  35. esac
  36. done
  37. # Shift off the options and optional --.
  38. shift "$((OPTIND-1))"
  39. # Get the fixed arguments
  40. if [[ $# != 2 ]]; then
  41. show_help >&2
  42. exit 1
  43. fi
  44. hostname=$1
  45. hex=$2
  46. re='[-A-Za-z0-9.]+'
  47. if [[ ! "$hostname" =~ $re ]]; then
  48. echo "ERROR: hostname ${hostname} is not a valid hostname or ip address" >&2
  49. exit 1
  50. fi
  51. if [[ ! -r "$hex" ]]; then
  52. echo "ERROR: cannot read hex file ($hex)" >&2
  53. exit 1
  54. fi
  55. # ===== Get AVR in sync
  56. [[ -n "$verbose" ]] && echo "Resetting AVR with http://$hostname/pgm/sync" >&2
  57. v=; [[ -n "$verbose" ]] && v=-v
  58. sync=`curl -m 10 $v -s -w '%{http_code}' -XPOST "http://$hostname/pgm/sync"`
  59. if [[ $? != 0 || "$sync" != 204 ]]; then
  60. echo "Error resetting AVR" >&2
  61. exit 1
  62. fi
  63. while true; do
  64. sync=`curl -m 10 $v -s "http://$hostname/pgm/sync"`
  65. if [[ $? != 0 ]]; then
  66. echo "Error checking sync" >&2
  67. exit 1
  68. fi
  69. case "$sync" in
  70. SYNC*)
  71. echo "AVR in $sync" >&2
  72. break;;
  73. "NOT READY"*)
  74. [[ -n "$verbose" ]] && echo " Waiting for sync..." >&2
  75. ;;
  76. *)
  77. echo "Error checking sync: $sync" >&2
  78. exit 1
  79. ;;
  80. esac
  81. sleep 0.1
  82. done
  83. # ===== Send HEX file
  84. [[ -n "$verbose" ]] && echo "Sending HEX file for programming" >&2
  85. sync=`curl -m 10 $v -s -g -d "@$hex" "http://$hostname/pgm/upload"`
  86. echo $sync
  87. if [[ $? != 0 || ! "$sync" =~ ^Success ]]; then
  88. echo "Error programming AVR" >&2
  89. exit 1
  90. fi
  91. sec=$(( `date +%s` - $start ))
  92. echo "Success, took $sec seconds" >&2
  93. exit 0