-ifneq (, $(findstring 0.7, $(shell dfu-programmer --version 2>&1)))
- dfu-programmer $(MCU) flash --eeprom $(BUILD_DIR)/$(TARGET).eep
-else
- dfu-programmer $(MCU) flash-eeprom $(BUILD_DIR)/$(TARGET).eep
-endif
- dfu-programmer $(MCU) reset
+ if $(DFU_PROGRAMMER) --version 2>&1 | $(GREP) -q 0.7 ; then\
+ $(DFU_PROGRAMMER) $(MCU) flash --eeprom $(BUILD_DIR)/$(TARGET).eep;\
+ else\
+ $(DFU_PROGRAMMER) $(MCU) flash-eeprom $(BUILD_DIR)/$(TARGET).eep;\
+ fi
+ $(DFU_PROGRAMMER) $(MCU) reset
+
+define EXEC_AVRDUDE
+ 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.*'`; \
+ mv /tmp/2 /tmp/1; \
+ done; \
+ echo ""; \
+ echo "Detected controller on USB port at $$USB"; \
+ 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"; \
+ fi; \
+ sleep 1; \
+ avrdude -p $(MCU) -c avr109 -P $$USB -U flash:w:$(BUILD_DIR)/$(TARGET).hex; \
+ fi
+endef
+
+avrdude: $(BUILD_DIR)/$(TARGET).hex check-size cpfirmware
+ $(call EXEC_AVRDUDE)
+
+avrdude-loop: $(BUILD_DIR)/$(TARGET).hex check-size cpfirmware
+ while true; do \
+ $(call EXEC_AVRDUDE) ; \
+ done