install.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/bin/sh
  2. #
  3. # Build pyprofibus and install on a Micropython board.
  4. #
  5. basedir="$(dirname "$0")"
  6. [ "$(echo "$basedir" | cut -c1)" = '/' ] || basedir="$PWD/$basedir"
  7. rootdir="$(realpath -m "$basedir/..")"
  8. die()
  9. {
  10. echo "ERROR: $*" >&2
  11. exit 1
  12. }
  13. echos()
  14. {
  15. printf '%s' "$*"
  16. }
  17. build()
  18. {
  19. echo "=== build ==="
  20. cd "$rootdir" || die "Failed to switch to root dir."
  21. local gsds="$(find . -maxdepth 1 -name '*.gsd') $(find misc/ -maxdepth 1 -name '*.gsd')"
  22. local pys="$(find pyprofibus/ -name '*.py') $(find stublibs/ -name '*.py') $(find . -maxdepth 1 -name 'example_*.py')"
  23. local confs="$(find . -maxdepth 1 -name '*.conf')"
  24. local gsdparser_opts="--dump-strip --dump-notext --dump-noextuserprmdata \
  25. --dump-module '6ES7 138-4CA01-0AA0 PM-E DC24V' \
  26. --dump-module '6ES7 132-4BB30-0AA0 2DO DC24V' \
  27. --dump-module '6ES7 132-4BB30-0AA0 2DO DC24V' \
  28. --dump-module '6ES7 131-4BD01-0AA0 4DI DC24V' \
  29. --dump-module 'Master_O Slave_I 1 by unit' \
  30. --dump-module 'Master_I Slave_O 1 by unit' \
  31. --dump-module 'dummy output module' \
  32. --dump-module 'dummy input module' \
  33. $modules"
  34. local targets=
  35. [ -n "$clean" ] && local targets="$targets clean"
  36. local targets="$targets all"
  37. for target in $targets; do
  38. echo "--- $target ---"
  39. make -j "$(getconf _NPROCESSORS_ONLN)" -f "$rootdir/micropython/Makefile" \
  40. SRCDIR="." \
  41. MAINPYDIR="./micropython" \
  42. BUILDDIR="$builddir" \
  43. MPYCROSS="$mpycross" \
  44. MARCH="$march" \
  45. GSDPARSER_OPTS="$gsdparser_opts" \
  46. PYS="$pys" \
  47. GSDS="$gsds" \
  48. CONFS="$confs" \
  49. $target || die "make failed."
  50. done
  51. }
  52. pyboard()
  53. {
  54. echo "$pyboard -d $dev $*"
  55. "$pyboard" -d "$dev" "$@" || die "pyboard: $pyboard failed."
  56. }
  57. reboot_dev()
  58. {
  59. pyboard --no-follow -c 'import machine as m;m.reset()'
  60. }
  61. format_flash()
  62. {
  63. local wd_timeout="5000"
  64. local cmd="import machine as m, flashbdev as f, uos as u;"
  65. cmd="${cmd}m.WDT(0,${wd_timeout}).feed();"
  66. cmd="${cmd}u.umount('/');"
  67. cmd="${cmd}u.VfsLfs2.mkfs(f.bdev);"
  68. cmd="${cmd}u.mount(u.VfsLfs2(f.bdev), '/');"
  69. pyboard -c "$cmd"
  70. }
  71. transfer()
  72. {
  73. local from="$1"
  74. local to="$2"
  75. if [ -d "$from" ]; then
  76. pyboard -f mkdir "$to"
  77. for f in "$from"/*; do
  78. transfer "$f" "$to/$(basename "$f")"
  79. done
  80. return
  81. fi
  82. pyboard -f cp "$from" "$to"
  83. }
  84. transfer_to_device()
  85. {
  86. echo "=== transfer to device $dev ==="
  87. format_flash
  88. reboot_dev
  89. sleep 2
  90. for f in "$builddir"/*; do
  91. transfer "$f" :/"$(basename $f)"
  92. done
  93. reboot_dev
  94. }
  95. builddir="$rootdir/build/micropython"
  96. buildonly=0
  97. dev="/dev/ttyUSB0"
  98. march="xtensawin"
  99. pyboard="pyboard.py"
  100. mpycross="mpy-cross"
  101. modules=
  102. clean=
  103. while [ $# -ge 1 ]; do
  104. [ "$(echos "$1" | cut -c1)" != "-" ] && break
  105. case "$1" in
  106. -h|--help)
  107. echo "install.sh [OPTIONS] [TARGET-UART-DEVICE]"
  108. echo
  109. echo "Build pyprofibus and install on a Micropython board."
  110. echo
  111. echo "TARGET-UART-DEVICE:"
  112. echo " Target serial device. Default: /dev/ttyUSB0"
  113. echo
  114. echo "Options:"
  115. echo " -c|--clean Clean before build."
  116. echo " -b|--build-only Build only. Do not install."
  117. echo " -B|--build-dir DIR Set the build directory."
  118. echo " Default: build/micropython"
  119. echo " -a|--march ARCH Target architecture for cross compile."
  120. echo " Default: xtensawin"
  121. echo " -m|--module NAME Include GSD module NAME."
  122. echo " Can be specified multiple times for multiple modules."
  123. echo " Enter your 'module_X' names from your configuration here."
  124. echo " -M|--mpycross PATH Path to mpy-cross executable."
  125. echo " Default: mpy-cross"
  126. echo " -p|--pyboard PATH Path to pyboard executable."
  127. echo " Default: pyboard.py"
  128. echo " -h|--help Show this help."
  129. exit 0
  130. ;;
  131. -b|--build-only)
  132. buildonly=1
  133. ;;
  134. -B|--build-dir)
  135. shift
  136. builddir="$1"
  137. ;;
  138. -a|--march)
  139. shift
  140. march="$1"
  141. ;;
  142. -p|--pyboard)
  143. shift
  144. pyboard="$1"
  145. ;;
  146. -M|--mpycross)
  147. shift
  148. mpycross="$1"
  149. ;;
  150. -m|--module)
  151. shift
  152. modules="$modules --dump-module '$1'"
  153. ;;
  154. -c|--clean)
  155. clean=1
  156. ;;
  157. *)
  158. die "Unknown option: $1"
  159. ;;
  160. esac
  161. shift
  162. done
  163. if [ $# -ge 1 ]; then
  164. dev="$1"
  165. shift
  166. fi
  167. if [ $# -ge 1 ]; then
  168. die "Too many arguments."
  169. fi
  170. build
  171. [ $buildonly -eq 0 ] && transfer_to_device
  172. exit 0