Skip to content

Commit

Permalink
Add support for producing UF2-format binaries. (#12435)
Browse files Browse the repository at this point in the history
* First stab at enabling builds of UF2-format binaries.

* Add description on producing a UF2 file.
  • Loading branch information
tzarc authored Apr 6, 2021
1 parent 3ce8c9f commit 64a0f5a
Show file tree
Hide file tree
Showing 6 changed files with 373 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.eep
*.elf
*.hex
*.uf2
*.qmk
!util/bootloader.hex
!quantum/tools/eeprom_reset.hex
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ clean:

.PHONY: distclean
distclean: clean
echo -n 'Deleting *.bin and *.hex ... '
rm -f *.bin *.hex
echo -n 'Deleting *.bin, *.hex, and *.uf2 ... '
rm -f *.bin *.hex *.uf2
echo 'done.'

#Compatibility with the old make variables, anything you specify directly on the command line
Expand Down
2 changes: 2 additions & 0 deletions message.mk
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ MSG_SIZE_AFTER = Size after:
MSG_COFF = Converting to AVR COFF:
MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
MSG_FLASH = Creating load file for flashing:
MSG_UF2 = Creating UF2 file for deployment:
MSG_EEPROM = Creating load file for EEPROM:
MSG_BIN = Creating binary load file for flashing:
MSG_EXTENDED_LISTING = Creating Extended Listing:
MSG_SYMBOL_TABLE = Creating Symbol Table:
MSG_EXECUTING = Executing:
MSG_LINKING = Linking:
MSG_COMPILING = Compiling:
MSG_COMPILING_CXX = Compiling:
Expand Down
24 changes: 24 additions & 0 deletions quantum/mcu_selection.mk
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ ifneq ($(findstring STM32F042, $(MCU)),)
# Options to pass to dfu-util when flashing
DFU_ARGS ?= -d 0483:DF11 -a 0 -s 0x08000000:leave
DFU_SUFFIX_ARGS ?= -v 0483 -p DF11

# UF2 settings
UF2_FAMILY ?= STM32F0
endif

ifneq ($(findstring STM32F072, $(MCU)),)
Expand Down Expand Up @@ -172,6 +175,9 @@ ifneq ($(findstring STM32F072, $(MCU)),)
# Options to pass to dfu-util when flashing
DFU_ARGS ?= -d 0483:DF11 -a 0 -s 0x08000000:leave
DFU_SUFFIX_ARGS ?= -v 0483 -p DF11

# UF2 settings
UF2_FAMILY ?= STM32F0
endif

ifneq ($(findstring STM32F103, $(MCU)),)
Expand Down Expand Up @@ -205,6 +211,9 @@ ifneq ($(findstring STM32F103, $(MCU)),)
# Options to pass to dfu-util when flashing
DFU_ARGS ?= -d 0483:DF11 -a 0 -s 0x08000000:leave
DFU_SUFFIX_ARGS ?= -v 0483 -p DF11

# UF2 settings
UF2_FAMILY ?= STM32F1
endif

ifneq ($(findstring STM32F303, $(MCU)),)
Expand Down Expand Up @@ -238,6 +247,9 @@ ifneq ($(findstring STM32F303, $(MCU)),)
# Options to pass to dfu-util when flashing
DFU_ARGS ?= -d 0483:DF11 -a 0 -s 0x08000000:leave
DFU_SUFFIX_ARGS ?= -v 0483 -p DF11

# UF2 settings
UF2_FAMILY ?= STM32F3
endif

ifneq ($(findstring STM32F401, $(MCU)),)
Expand Down Expand Up @@ -271,6 +283,9 @@ ifneq ($(findstring STM32F401, $(MCU)),)
# Options to pass to dfu-util when flashing
DFU_ARGS ?= -d 0483:DF11 -a 0 -s 0x08000000:leave
DFU_SUFFIX_ARGS ?= -v 0483 -p DF11

# UF2 settings
UF2_FAMILY ?= STM32F4
endif

ifneq ($(findstring STM32F411, $(MCU)),)
Expand Down Expand Up @@ -304,6 +319,9 @@ ifneq ($(findstring STM32F411, $(MCU)),)
# Options to pass to dfu-util when flashing
DFU_ARGS ?= -d 0483:DF11 -a 0 -s 0x08000000:leave
DFU_SUFFIX_ARGS ?= -v 0483 -p DF11

# UF2 settings
UF2_FAMILY ?= STM32F4
endif

ifneq ($(findstring STM32G431, $(MCU)),)
Expand Down Expand Up @@ -337,6 +355,9 @@ ifneq ($(findstring STM32G431, $(MCU)),)
# Options to pass to dfu-util when flashing
DFU_ARGS ?= -d 0483:DF11 -a 0 -s 0x08000000:leave
DFU_SUFFIX_ARGS ?= -v 0483 -p DF11

# UF2 settings
UF2_FAMILY ?= STM32G4
endif

ifneq ($(findstring STM32G474, $(MCU)),)
Expand Down Expand Up @@ -370,6 +391,9 @@ ifneq ($(findstring STM32G474, $(MCU)),)
# Options to pass to dfu-util when flashing
DFU_ARGS ?= -d 0483:DF11 -a 0 -s 0x08000000:leave
DFU_SUFFIX_ARGS ?= -v 0483 -p DF11

# UF2 settings
UF2_FAMILY ?= STM32G4
endif

ifneq (,$(filter $(MCU),at90usb162 atmega16u2 atmega32u2 atmega16u4 atmega32u4 at90usb646 at90usb647 at90usb1286 at90usb1287))
Expand Down
31 changes: 25 additions & 6 deletions tmk_core/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ ifneq ($(filter Darwin FreeBSD,$(shell uname -s)),)
MD5SUM = md5
endif

# UF2 format settings
# To produce a UF2 file in your build, add to your keyboard's rules.mk:
# FIRMWARE_FORMAT = uf2
UF2CONV = $(TOP_DIR)/util/uf2conv.py
UF2_FAMILY ?= 0x0

# Compiler flags to generate dependency files.
#GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
GENDEPFLAGS = -MMD -MP -MF $(patsubst %.o,%.td,$@)
Expand Down Expand Up @@ -255,6 +261,7 @@ DFU_SUFFIX_ARGS ?=

elf: $(BUILD_DIR)/$(TARGET).elf
hex: $(BUILD_DIR)/$(TARGET).hex
uf2: $(BUILD_DIR)/$(TARGET).uf2
cpfirmware: $(FIRMWARE_FORMAT)
$(SILENT) || printf "Copying $(TARGET).$(FIRMWARE_FORMAT) to qmk_firmware folder" | $(AWK_CMD)
$(COPY) $(BUILD_DIR)/$(TARGET).$(FIRMWARE_FORMAT) $(TARGET).$(FIRMWARE_FORMAT) && $(PRINT_OK)
Expand Down Expand Up @@ -283,32 +290,44 @@ gccversion :

# Create final output files (.hex, .eep) from ELF output file.
%.hex: %.elf
@$(SILENT) || printf "$(MSG_FLASH) $@" | $(AWK_CMD)
$(eval CMD=$(HEX) $< $@)
#@$(SILENT) || printf "$(MSG_EXECUTING) '$(CMD)':\n"
@$(SILENT) || printf "$(MSG_FLASH) $@" | $(AWK_CMD)
@$(BUILD_CMD)

%.uf2: %.hex
$(eval CMD=$(UF2CONV) $(BUILD_DIR)/$(TARGET).hex -o $(BUILD_DIR)/$(TARGET).uf2 -c -f $(UF2_FAMILY) >/dev/null 2>&1)
#@$(SILENT) || printf "$(MSG_EXECUTING) '$(CMD)':\n"
@$(SILENT) || printf "$(MSG_UF2) $@" | $(AWK_CMD)
@$(BUILD_CMD)

%.eep: %.elf
@$(SILENT) || printf "$(MSG_EEPROM) $@" | $(AWK_CMD)
$(eval CMD=$(EEP) $< $@ || exit 0)
#@$(SILENT) || printf "$(MSG_EXECUTING) '$(CMD)':\n"
@$(SILENT) || printf "$(MSG_EEPROM) $@" | $(AWK_CMD)
@$(BUILD_CMD)

# Create extended listing file from ELF output file.
%.lss: %.elf
@$(SILENT) || printf "$(MSG_EXTENDED_LISTING) $@" | $(AWK_CMD)
$(eval CMD=$(OBJDUMP) -h -S -z $< > $@)
#@$(SILENT) || printf "$(MSG_EXECUTING) '$(CMD)':\n"
@$(SILENT) || printf "$(MSG_EXTENDED_LISTING) $@" | $(AWK_CMD)
@$(BUILD_CMD)

# Create a symbol table from ELF output file.
%.sym: %.elf
@$(SILENT) || printf "$(MSG_SYMBOL_TABLE) $@" | $(AWK_CMD)
$(eval CMD=$(NM) -n $< > $@ )
#@$(SILENT) || printf "$(MSG_EXECUTING) '$(CMD)':\n"
@$(SILENT) || printf "$(MSG_SYMBOL_TABLE) $@" | $(AWK_CMD)
@$(BUILD_CMD)

%.bin: %.elf
@$(SILENT) || printf "$(MSG_BIN) $@" | $(AWK_CMD)
$(eval CMD=$(BIN) $< $@ || exit 0)
#@$(SILENT) || printf "$(MSG_EXECUTING) '$(CMD)':\n"
@$(SILENT) || printf "$(MSG_BIN) $@" | $(AWK_CMD)
@$(BUILD_CMD)
if [ ! -z "$(DFU_SUFFIX_ARGS)" ]; then \
#$(SILENT) || printf "$(MSG_EXECUTING) '$(DFU_SUFFIX) $(DFU_SUFFIX_ARGS) -a $(BUILD_DIR)/$(TARGET).bin 1>/dev/null':\n" ;\
$(DFU_SUFFIX) $(DFU_SUFFIX_ARGS) -a $(BUILD_DIR)/$(TARGET).bin 1>/dev/null ;\
fi
$(COPY) $(BUILD_DIR)/$(TARGET).bin $(TARGET).bin;
Expand Down Expand Up @@ -476,7 +495,7 @@ $(eval $(foreach OUTPUT,$(OUTPUTS),$(shell mkdir -p $(OUTPUT) 2>/dev/null)))

# Listing of phony targets.
.PHONY : all dump_vars finish sizebefore sizeafter qmkversion \
gccversion build elf hex eep lss sym coff extcoff \
gccversion build elf hex uf2 eep lss sym coff extcoff \
clean clean_list debug gdb-config show_path \
program teensy dfu dfu-ee dfu-start \
flash dfu-split-left dfu-split-right \
Expand Down
Loading

0 comments on commit 64a0f5a

Please sign in to comment.