Arduino_STM32/tools/linux/maple_upload

50 lines
1.1 KiB
Plaintext
Raw Normal View History

#!/bin/bash
#set -e
if [ $# -lt 4 ]; then
echo "Usage: $0 $# <dummy_port> <altID> <usbID> <binfile>" >&2
exit 1
fi
dummy_port="$1"; altID="$2"; usbID="$3"; binfile="$4"; dummy_port_fullpath="/dev/$1"
if [ $# -eq 5 ]; then
dfuse_addr="--dfuse-address $5"
else
dfuse_addr=""
fi
# Get the directory where the script is running.
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# ----------------- IMPORTANT -----------------
# The 2nd parameter to upload-reset is the delay after resetting before it exits
# This value is in milliseonds
# You may need to tune this to your system
# 750ms to 1500ms seems to work on my Mac
"${DIR}/upload-reset" ${dummy_port_fullpath} 750
#DFU_UTIL=$(dirname $0)/dfu-util/dfu-util
DFU_UTIL=/usr/bin/dfu-util
DFU_UTIL=${DIR}/dfu-util/dfu-util
if [ ! -x "${DFU_UTIL}" ]; then
echo "$0: error: cannot find ${DFU_UTIL}" >&2
exit 2
fi
"${DFU_UTIL}" -d ${usbID} -a ${altID} -D ${binfile} ${dfuse_addr} -R
echo -n Waiting for ${dummy_port_fullpath} serial...
COUNTER=0
Solve race condition with arduino IDE monitoring serial port I encountered a race condition upon which, even though the udev rules were working and uploads succeed, the serial monitor can not re-establish communications. If the serial monitor is left open when doing an upload the error shown is: [...] Copying data from PC to DFU device Starting download: [##################################################] finished! state(8) = dfuMANIFEST-WAIT-RESET, status(0) = No error condition is present Done! Resetting USB to switch back to runtime mode Waiting for /dev/ttyACM0 serial...Done processing.app.SerialException: Error opening serial port '/dev/ttyACM0'. Try consulting the documentation at http://playground.arduino.cc/Linux/All#Permission at processing.app.Serial.<init>(Serial.java:145) at processing.app.Serial.<init>(Serial.java:82) at processing.app.SerialMonitor$4.<init>(SerialMonitor.java:101) at processing.app.SerialMonitor.open(SerialMonitor.java:101) at processing.app.AbstractMonitor.resume(AbstractMonitor.java:104) at processing.app.Editor.resumeOrCloseSerialMonitor(Editor.java:2218) at processing.app.Editor.access$2200(Editor.java:79) at processing.app.Editor$DefaultExportHandler.run(Editor.java:2196) at java.lang.Thread.run(Thread.java:748) Error opening serial port '/dev/ttyACM0'. Try consulting the documentation at http://playground.arduino.cc/Linux/All#Permission I traced it to the device being opened prior to the permissions/ownership changes. When testing via -c the upload script waits for the character device creation, but at that point the udev rules have not changed the permissions. By testing the readability of the device via the -r test, we wait until the new permissions have been applied. I imagine this is specific to the Linux kernel and the speed of the machine being used. In my case: arduino-1.8.5 Linux 4.4.0-98-generic #121-Ubuntu SMP but the change should work on any system with proper udev rules / permissions.
2017-11-13 10:15:07 -08:00
while [ ! -r ${dummy_port_fullpath} ] && ((COUNTER++ < 40)); do
sleep 0.1
done
echo Done