Last active
April 8, 2021 07:08
-
-
Save purdeaandrei/76677015eec1d6aaba57021254cf22ea to your computer and use it in GitHub Desktop.
flash_pro_micro.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Partially insipred by qmk_firmware/tmk_core/avr.mk | |
# License: GPLv2 | |
set -e | |
if [ $# -ne 1 ]; then | |
echo "Usage: $0 path/to/filename/to/flash.hex" | |
exit 1 | |
fi | |
if ! which avrdude > /dev/null; then | |
tput setaf 1 | |
tput bold | |
echo "ERROR: Make sure avrdude is installed" | |
tput sgr0 | |
exit 4 | |
fi | |
MMRUNNING= | |
if which systemctl > /dev/null; then | |
if systemctl --quiet is-active ModemManager.service; then | |
MMRUNNING=yes | |
fi | |
else | |
MMRUNNING=`ps aux | grep ModemManager | grep -o 'filter-policy=\(.*\)' | cut -c 15-` | |
fi | |
if [ ! -z "$MMRUNNING" ]; then | |
tput setaf 1 | |
tput bold | |
echo "WARNING: ModemManager is running" | |
echo "You need to make sure that either:" | |
echo "1) You stop ModemManager for the duration of the flashing operation" | |
echo "-or-" | |
echo "2) Modemmanager is not configured with filter-policy=strict AND you have the proper udev rules set up" | |
tput setaf 2 | |
echo "Please see the 'Linux udev rules' section here: https://beta.docs.qmk.fm/faqs/faq_build#linux-udev-rules-id-linux-udev-rules" | |
tput sgr0 | |
fi | |
BASEFN=$(basename "$1") | |
if [[ ($BASEFN =~ .*_xwhatsit) || ($BASEFN =~ .*_wcass) ]]; then | |
tput setaf 1 | |
tput bold | |
echo "ERROR: the filename $BASEFN suggests you are trying to flash an atmega32u2 image onto an atmega32u4 device." | |
exit 2 | |
fi | |
if test -f "$1"; then | |
lastModified=$(date +%s -r "$1") | |
currentTime=$(date +%s) | |
secondsAgo=$((currentTime - lastModified )) | |
minutesAgo=$((secondsAgo / 60 )) | |
secondsAgo=$((secondsAgo - minutesAgo * 60 )) | |
hoursAgo=$((minutesAgo / 60 )) | |
minutesAgo=$((minutesAgo - hoursAgo * 60 )) | |
daysAgo=$((hoursAgo / 24 )) | |
hoursAgo=$((hoursAgo - daysAgo * 24 )) | |
if [[ $daysAgo -ne 0 ]]; then | |
daysAgoStr=" $daysAgo days," | |
hoursAgoStr=" $hoursAgo hours," | |
minutesAgoStr=" $minutesAgo minutes," | |
else | |
daysAgoStr="" | |
if [[ $hoursAgo -ne 0 ]]; then | |
hoursAgoStr=" $hoursAgo hours," | |
minutesAgoStr=" $minutesAgo minutes," | |
else | |
hoursAgoStr="" | |
if [[ $minutesAgo -ne 0 ]]; then | |
minutesAgoStr=" $minutesAgo minutes," | |
else | |
minutesAgoStr="" | |
fi | |
fi | |
fi | |
echo "File $1 last modified$(tput bold; tput setaf 2)${daysAgoStr}${hoursAgoStr}${minutesAgoStr} ${secondsAgo} seconds ago$(tput sgr0)" | |
else | |
echo "ERROR: File $1 doesn't exist." | |
exit 3 | |
fi | |
GREP=grep | |
USB= | |
if ${GREP} -q -s Microsoft /proc/version; then | |
echo 'ERROR: AVR flashing cannot be automated within the Windows Subsystem for Linux (WSL) currently. Instead, take the .hex file generated and flash it using AVRDUDE, AVRDUDESS, or XLoader.' | |
else | |
printf "Detecting USB port, reset your controller now." | |
ls /dev/tty* > /tmp/1 | |
while [ -z $USB ]; do | |
sleep 0.5 | |
printf "." | |
ls /dev/tty* > /tmp/2 | |
USB=`(comm -13 /tmp/1 /tmp/2 | ${GREP} -o '/dev/tty.*') || true` | |
mv /tmp/2 /tmp/1 | |
done | |
echo "" | |
echo "Device $USB has appeared; assuming it is the controller." | |
if ${GREP} -q -s 'MINGW\|MSYS' /proc/version; then | |
USB=`echo "$USB" | perl -pne 's/\/dev\/ttyS(\d+)/COM.(\$1+1)/e'` | |
echo "Remapped MSYS2 USB port to $USB" | |
sleep 1 | |
else | |
printf "Waiting for $USB to become writable." | |
while [ ! -w "$USB" ]; do | |
sleep 0.5 | |
printf "." | |
done | |
echo "" | |
fi | |
avrdude -p atmega32u4 -c avr109 -P $USB -U "flash:w:$1" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment