micropython-install.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/sh
  2. basedir="$(realpath -e "$0" | xargs dirname)"
  3. rootdir="$(realpath -m "$basedir/..")"
  4. die()
  5. {
  6. echo "ERROR: $*" >&2
  7. exit 1
  8. }
  9. echos()
  10. {
  11. printf '%s' "$*"
  12. }
  13. pyboard()
  14. {
  15. echo "$pyboard -d $dev $*"
  16. "$pyboard" -d "$dev" "$@" || die "pyboard: $pyboard failed."
  17. }
  18. reboot_dev()
  19. {
  20. pyboard --no-follow -c 'import machine as m;m.reset()'
  21. }
  22. format_flash()
  23. {
  24. local wd_timeout="5000"
  25. local cmd="import machine as m, flashbdev as f, uos as u;"
  26. cmd="${cmd}m.WDT(0,${wd_timeout}).feed();"
  27. cmd="${cmd}u.umount('/');"
  28. cmd="${cmd}u.VfsLfs2.mkfs(f.bdev);"
  29. cmd="${cmd}u.mount(u.VfsLfs2(f.bdev), '/');"
  30. pyboard -c "$cmd"
  31. }
  32. transfer()
  33. {
  34. local from="$1"
  35. local to="$2"
  36. if [ -d "$from" ]; then
  37. pyboard -f mkdir "$to"
  38. for f in "$from"/*; do
  39. if [ "$(basename "$f")" = "__pycache__" ]; then
  40. continue
  41. fi
  42. transfer "$f" "$to/$(basename "$f")"
  43. done
  44. return
  45. fi
  46. pyboard -f cp "$from" "$to"
  47. }
  48. transfer_to_device()
  49. {
  50. echo "=== transfer to device $dev ==="
  51. format_flash
  52. reboot_dev
  53. sleep 2
  54. transfer "$rootdir/bme280" :/bme280
  55. transfer "$basedir/micropython-i2c.py" :/example_i2c.py
  56. transfer "$basedir/micropython-i2c-async.py" :/example_i2c_async.py
  57. reboot_dev
  58. }
  59. dev="/dev/ttyUSB0"
  60. pyboard="pyboard.py"
  61. while [ $# -ge 1 ]; do
  62. [ "$(echos "$1" | cut -c1)" != "-" ] && break
  63. case "$1" in
  64. -h|--help)
  65. echo "install.sh [OPTIONS] [TARGET-UART-DEVICE]"
  66. echo
  67. echo "TARGET-UART-DEVICE:"
  68. echo " Target serial device. Default: /dev/ttyUSB0"
  69. echo
  70. echo "Options:"
  71. echo " -p|--pyboard PATH Path to pyboard executable."
  72. echo " Default: pyboard.py"
  73. echo " -h|--help Show this help."
  74. exit 0
  75. ;;
  76. -p|--pyboard)
  77. shift
  78. pyboard="$1"
  79. ;;
  80. *)
  81. die "Unknown option: $1"
  82. ;;
  83. esac
  84. shift
  85. done
  86. if [ $# -ge 1 ]; then
  87. dev="$1"
  88. shift
  89. fi
  90. if [ $# -ge 1 ]; then
  91. die "Too many arguments."
  92. fi
  93. transfer_to_device
  94. exit 0