megaflash 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #! /bin/bash
  2. #
  3. # Flash an Arduino Mega with STK500v2 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. # Danny Backx wrote the changes for Arduino Mega.
  13. # ----------------------------------------------------------------------------
  14. show_help() {
  15. cat <<EOT
  16. Usage: ${0##*/} [-options...] hostname sketch.hex
  17. Flash the Mega running optiboot attached to meta-id with the sketch.
  18. Note : this is for stk500v2 MCUs, use avrflash for Arduino Uno etc instead.
  19. -v Be verbose
  20. -h show this help
  21. Example: ${0##*/} -v meta-id mysketch.hex
  22. ${0##*/} 192.168.4.1 mysketch.hex
  23. EOT
  24. }
  25. if ! which curl >/dev/null; then
  26. echo "ERROR: Cannot find curl: it is required for this script." >&2
  27. exit 1
  28. fi
  29. start=`date +%s`
  30. # ===== Parse arguments
  31. verbose=
  32. while getopts "hvx:" opt; do
  33. case "$opt" in
  34. h) show_help; exit 0 ;;
  35. v) verbose=1 ;;
  36. x) foo="$OPTARG" ;;
  37. '?') show_help >&2; exit 1 ;;
  38. esac
  39. done
  40. # Shift off the options and optional --.
  41. shift "$((OPTIND-1))"
  42. # Get the fixed arguments
  43. if [[ $# != 2 ]]; then
  44. show_help >&2
  45. exit 1
  46. fi
  47. hostname=$1
  48. hex=$2
  49. re='[-A-Za-z0-9.]+'
  50. if [[ ! "$hostname" =~ $re ]]; then
  51. echo "ERROR: hostname ${hostname} is not a valid hostname or ip address" >&2
  52. exit 1
  53. fi
  54. if [[ ! -r "$hex" ]]; then
  55. echo "ERROR: cannot read hex file ($hex)" >&2
  56. exit 1
  57. fi
  58. # ===== Get AVR in sync
  59. [[ -n "$verbose" ]] && echo "Resetting AVR with http://$hostname/pgmmega/sync" >&2
  60. v=; [[ -n "$verbose" ]] && v=-v
  61. sync=`curl -m 10 $v -s -w '%{http_code}' -XPOST "http://$hostname/pgmmega/sync"`
  62. if [[ $? != 0 || "$sync" != 204 ]]; then
  63. echo "Error resetting AVR" >&2
  64. exit 1
  65. fi
  66. while true; do
  67. # sync=`curl -m 10 $v -s "http://$hostname/pgmmega/sync"`
  68. sync=`curl $v -s "http://$hostname/pgmmega/sync"`
  69. if [[ $? != 0 ]]; then
  70. echo "Error checking sync" >&2
  71. exit 1
  72. fi
  73. case "$sync" in
  74. SYNC*)
  75. echo "AVR in $sync" >&2
  76. break;;
  77. "NOT READY"*)
  78. [[ -n "$verbose" ]] && echo " Waiting for sync..." >&2
  79. ;;
  80. *)
  81. echo "Error checking sync: $sync" >&2
  82. exit 1
  83. ;;
  84. esac
  85. sleep 0.1
  86. done
  87. # ===== Send HEX file
  88. [[ -n "$verbose" ]] && echo "Sending HEX file for programming" >&2
  89. sync=`curl -m 20 $v -s -g -d "@$hex" "http://$hostname/pgmmega/upload"`
  90. echo $sync
  91. if [[ $? != 0 || ! "$sync" =~ ^Success ]]; then
  92. echo "Error programming AVR" >&2
  93. exit 1
  94. fi
  95. sec=$(( `date +%s` - $start ))
  96. echo "Success, took $sec seconds" >&2
  97. exit 0