diff --git a/scripts/flash.sh b/scripts/flash.sh index 86d05f4e3f..70ff9c5487 100755 --- a/scripts/flash.sh +++ b/scripts/flash.sh @@ -1,10 +1,24 @@ #!/bin/bash -if [ $1 = "olimex-stm32-e407" ]; then - openocd -f interface/ftdi/olimex-arm-usb-ocd-h.cfg -f target/stm32f4x.cfg -c init -c "reset halt" -c "flash write_image erase nuttx.bin 0x08000000" -elif [ $1 = "stm32l1" ]; then +# Source tools +currdir="${0%/*}" +. $currdir/tools.sh + +# lsusb dependency installation +install_dep usbutils + +if [ $1 = "olimex-stm32-e407" ];then + if lsusb -d 15BA:002a; then + openocd -f interface/ftdi/olimex-arm-usb-tiny-h.cfg -f target/stm32f4x.cfg -c init -c "reset halt" -c "flash write_image erase nuttx.bin 0x08000000" + elif lsusb -d 15BA:0003;then + openocd -f interface/ftdi/olimex-arm-usb-ocd.cfg -f target/stm32f4x.cfg -c init -c "reset halt" -c "flash write_image erase nuttx.bin 0x08000000" + elif lsusb -d 15BA:002b;then + openocd -f interface/ftdi/olimex-arm-usb-ocd-h.cfg -f target/stm32f4x.cfg -c init -c "reset halt" -c "flash write_image erase nuttx.bin 0x08000000" + else + echo "Error. Unsuported olimex-stm32-e407 flashing device. Try openocd" + fi +elif [ $1 = "stm32l1" ];then openocd -f interface/stlink-v2.cfg -f target/stm32l1.cfg -c init -c "reset halt" -c "flash write_image erase nuttx.bin 0x08000000" else echo "Error. Try: ./flash.sh olimex-stm32-e407 or ./flash.sh stm32l1" fi - diff --git a/scripts/tools.sh b/scripts/tools.sh new file mode 100644 index 0000000000..8595d6f92c --- /dev/null +++ b/scripts/tools.sh @@ -0,0 +1,49 @@ +#!/bin/bash + + +# Returns if a command is available in the current machine +function has_command() { + command -v "$1" >/dev/null 2>&1 +} + +# Sets multiple variables with system specifics. +# INSTALL_COMMAND: is the package manager of the linux distribution +# INSTALL_OPTIONS: are the options to install packages without any input from the user required +# INSTALL_QUERY: command and options to use for checking the existence of packages. +function set_host_installer() { + if [ -f /etc/arch-release ]; then + if has_command pacman; then + INSTALL_COMMAND="pacman" + INSTALL_OPTIONS="-S" + INSTALL_QUERY="pacman -Qi" + fi + elif [ -f /etc/debian_version ]; then + if has_command apt-get; then + INSTALL_COMMAND="apt-get" + INSTALL_OPTIONS="install -y" + INSTALL_QUERY="dpkg -s" + fi + fi +} + +# Takes a list of pakages and sets MISSING_PKGS with those missing in the current system. +function filter_installed() { + for package in "$@" + do + if $INSTALL_QUERY $package; then + echo $package "Already installed" + else + MISSING_PKGS+=" "$package + fi + done +} + +# Install the list of packages in the current system. +function install_dep() { + set_host_installer + filter_installed "$@" + if ! test -z MISSING_PKGS; then + $INSTALL_COMMAND $INSTALL_OPTIONS $MISSING_PKGS + fi + +} \ No newline at end of file