diff --git a/Makefile b/Makefile index 9d52f73..1ff8a65 100644 --- a/Makefile +++ b/Makefile @@ -1,95 +1,70 @@ -#****************************************************************************** +#------------------------------------------------------------------------------ # CONFIGURE -# - SDK_PATH : path to SDK directory -# - SRC_PATH : path to src folder +# - SDK_PATH : path to SDK directory # -# - SD_NAME : e.g s132, s140 +# - SD_NAME : e.g s132, s140 # - SD_VERSION : SoftDevice version e.g 6.0.0 -# - SD_HEX : to bootloader hex binary -#****************************************************************************** -SRC_PATH = src +# - SD_HEX : to bootloader hex binary +#------------------------------------------------------------------------------ SDK_PATH = lib/sdk/components SDK11_PATH = lib/sdk11/components -SD_PATH = lib/softdevice/$(SD_FILENAME) - TUSB_PATH = lib/tinyusb/src NRFX_PATH = lib/nrfx +SD_PATH = lib/softdevice/$(SD_FILENAME) SD_VERSION = 6.1.1 SD_FILENAME = $(SD_NAME)_nrf52_$(SD_VERSION) -SD_API_PATH = $(SD_PATH)/$(SD_FILENAME)_API SD_HEX = $(SD_PATH)/$(SD_FILENAME)_softdevice.hex -LD_FILE = $(SRC_PATH)/linker/$(MCU_SUB_VARIANT)_$(SD_NAME)_v$(word 1, $(subst ., ,$(SD_VERSION))).ld - -MERGED_FNAME = $(OUTPUT_FILENAME)_$(SD_NAME)_$(SD_VERSION) +# linker by MCU and SoftDevice eg. nrf52840_s140_v6.ld +LD_FILE = linker/$(MCU_SUB_VARIANT)_$(SD_NAME)_v$(word 1, $(subst ., ,$(SD_VERSION))).ld GIT_VERSION = $(shell git describe --dirty --always --tags) GIT_SUBMODULE_VERSIONS = $(shell git submodule status | cut -d' ' -f3,4 | paste -s -d" " -) -OUTPUT_FILENAME = $(BOARD)_bootloader-$(GIT_VERSION) +# compiled file name +OUT_FILE = $(BOARD)_bootloader-$(GIT_VERSION) -#****************************************************************************** +# merged file = compiled + sd +MERGED_FILE = $(OUT_FILE)_$(SD_NAME)_$(SD_VERSION) + +#------------------------------------------------------------------------------ # Tool configure -#****************************************************************************** -NRFUTIL = adafruit-nrfutil +#------------------------------------------------------------------------------ -ifneq ($(JLINK),) -NRFJPROG = nrfjprog -s $(JLINK) -else +# Toolchain commands +# Should be added to your PATH +CROSS_COMPILE ?= arm-none-eabi- +CC = $(CROSS_COMPILE)gcc +AS = $(CROSS_COMPILE)as +OBJCOPY = $(CROSS_COMPILE)objcopy +SIZE = $(CROSS_COMPILE)size +GDB = $(CROSS_COMPILE)gdb + +NRFUTIL = adafruit-nrfutil NRFJPROG = nrfjprog -endif + +MK = mkdir -p +RM = rm -rf # auto-detect BMP on macOS, otherwise have to specify BMP_PORT ?= $(shell ls -1 /dev/cu.usbmodem????????1 | head -1) +GDB_BMP = $(GDB) -ex 'target extended-remote $(BMP_PORT)' -ex 'monitor swdp_scan' -ex 'attach 1' -ifeq ($(OS),Windows_NT) -PROGFILES = C:/Program Files (x86) -GNU_INSTALL_ROOT = $(PROGFILES)/GNU Tools ARM Embedded/7 2018-q2-update/bin/ -endif - -MK := mkdir -RM := rm -rf - -# Verbose mode (V=). 0: default, 1: print out CFLAG, LDFLAG 2: print all compile command -ifeq ("$(V)","2") -QUIET = -else -QUIET = @ -endif - -GNU_PREFIX = arm-none-eabi - -# Toolchain commands -CC := '$(GNU_INSTALL_ROOT)$(GNU_PREFIX)-gcc' -AS := '$(GNU_INSTALL_ROOT)$(GNU_PREFIX)-as' -AR := '$(GNU_INSTALL_ROOT)$(GNU_PREFIX)-ar' -r -LD := '$(GNU_INSTALL_ROOT)$(GNU_PREFIX)-ld' -NM := '$(GNU_INSTALL_ROOT)$(GNU_PREFIX)-nm' -OBJDUMP := '$(GNU_INSTALL_ROOT)$(GNU_PREFIX)-objdump' -OBJCOPY := '$(GNU_INSTALL_ROOT)$(GNU_PREFIX)-objcopy' -SIZE := '$(GNU_INSTALL_ROOT)$(GNU_PREFIX)-size' -GDB := '$(GNU_INSTALL_ROOT)$(GNU_PREFIX)-gdb' - -GDB_BMP = arm-none-eabi-gdb -ex 'target extended-remote $(BMP_PORT)' -ex 'monitor swdp_scan' -ex 'attach 1' - -#function for removing duplicates in a list -remduplicates = $(strip $(if $1,$(firstword $1) $(call remduplicates,$(filter-out $(firstword $1),$1)))) - -#********************************* +#--------------------------------- # Select the board to build -#********************************* -BOARD_LIST = $(sort $(subst .h,,$(subst src/boards/,,$(wildcard src/boards/*)))) +#--------------------------------- +BOARD_LIST = $(sort $(subst src/boards/,,$(wildcard src/boards/*))) ifeq ($(filter $(BOARD),$(BOARD_LIST)),) $(info You must provide a BOARD parameter with 'BOARD='. Supported boards are:) - $(info $(BOARD_LIST)) + $(foreach b,$(BOARD_LIST),$(info - $(b))) $(error Invalid BOARD specified) endif # Build directory -BUILD = _build-$(BOARD) +BUILD = _build/build-$(BOARD) # Board specific -include src/boards/$(BOARD)/board.mk @@ -98,107 +73,100 @@ BUILD = _build-$(BOARD) ifeq ($(MCU_SUB_VARIANT),nrf52) SD_NAME = s132 DFU_DEV_REV = 0xADAF - MCU_FLAGS = -DNRF52 -DNRF52832_XXAA -DS132 + CFLAGS += -DNRF52 -DNRF52832_XXAA -DS132 else ifeq ($(MCU_SUB_VARIANT),nrf52833) SD_NAME = s140 DFU_DEV_REV = 52840 - MCU_FLAGS = -DNRF52833_XXAA -DS140 + CFLAGS += -DNRF52833_XXAA -DS140 else ifeq ($(MCU_SUB_VARIANT),nrf52840) SD_NAME = s140 DFU_DEV_REV = 52840 - MCU_FLAGS = -DNRF52840_XXAA -DS140 + CFLAGS += -DNRF52840_XXAA -DS140 else $(error Sub Variant $(MCU_SUB_VARIANT) is unknown) endif -#****************************************************************************** +#------------------------------------------------------------------------------ # SOURCE FILES -#****************************************************************************** +#------------------------------------------------------------------------------ -# src -C_SOURCE_FILES += $(SRC_PATH)/main.c -C_SOURCE_FILES += $(SRC_PATH)/boards.c -C_SOURCE_FILES += $(SRC_PATH)/flash_nrf5x.c -C_SOURCE_FILES += $(SRC_PATH)/dfu_ble_svc.c -C_SOURCE_FILES += $(SRC_PATH)/dfu_init.c +# all files in src +C_SRC += $(wildcard src/*.c) # all sources files in specific board -C_SOURCE_FILES += $(wildcard $(SRC_PATH)/boards/$(BOARD)/*.c) +C_SRC += $(wildcard src/boards/$(BOARD)/*.c) # nrfx -C_SOURCE_FILES += $(NRFX_PATH)/drivers/src/nrfx_power.c -C_SOURCE_FILES += $(NRFX_PATH)/drivers/src/nrfx_nvmc.c -C_SOURCE_FILES += $(NRFX_PATH)/mdk/system_$(MCU_SUB_VARIANT).c +C_SRC += $(NRFX_PATH)/drivers/src/nrfx_power.c +C_SRC += $(NRFX_PATH)/drivers/src/nrfx_nvmc.c +C_SRC += $(NRFX_PATH)/mdk/system_$(MCU_SUB_VARIANT).c -# SDK 11 files -C_SOURCE_FILES += $(SDK11_PATH)/libraries/bootloader_dfu/bootloader.c -C_SOURCE_FILES += $(SDK11_PATH)/libraries/bootloader_dfu/bootloader_settings.c -C_SOURCE_FILES += $(SDK11_PATH)/libraries/bootloader_dfu/bootloader_util.c -C_SOURCE_FILES += $(SDK11_PATH)/libraries/bootloader_dfu/dfu_transport_serial.c -C_SOURCE_FILES += $(SDK11_PATH)/libraries/bootloader_dfu/dfu_transport_ble.c -C_SOURCE_FILES += $(SDK11_PATH)/libraries/bootloader_dfu/dfu_single_bank.c +# SDK 11 files: serial + OTA DFU +C_SRC += $(SDK11_PATH)/libraries/bootloader_dfu/bootloader.c +C_SRC += $(SDK11_PATH)/libraries/bootloader_dfu/bootloader_settings.c +C_SRC += $(SDK11_PATH)/libraries/bootloader_dfu/bootloader_util.c +C_SRC += $(SDK11_PATH)/libraries/bootloader_dfu/dfu_transport_serial.c +C_SRC += $(SDK11_PATH)/libraries/bootloader_dfu/dfu_transport_ble.c +C_SRC += $(SDK11_PATH)/libraries/bootloader_dfu/dfu_single_bank.c +C_SRC += $(SDK11_PATH)/ble/ble_services/ble_dfu/ble_dfu.c +C_SRC += $(SDK11_PATH)/ble/ble_services/ble_dis/ble_dis.c +C_SRC += $(SDK11_PATH)/drivers_nrf/pstorage/pstorage_raw.c -C_SOURCE_FILES += $(SDK11_PATH)/drivers_nrf/pstorage/pstorage_raw.c - -C_SOURCE_FILES += $(SDK11_PATH)/ble/ble_services/ble_dfu/ble_dfu.c -C_SOURCE_FILES += $(SDK11_PATH)/ble/ble_services/ble_dis/ble_dis.c - -# Latest SDK files -C_SOURCE_FILES += $(SDK_PATH)/libraries/timer/app_timer.c -C_SOURCE_FILES += $(SDK_PATH)/libraries/scheduler/app_scheduler.c -C_SOURCE_FILES += $(SDK_PATH)/libraries/util/app_error.c -C_SOURCE_FILES += $(SDK_PATH)/libraries/util/app_util_platform.c -C_SOURCE_FILES += $(SDK_PATH)/libraries/crc16/crc16.c -C_SOURCE_FILES += $(SDK_PATH)/libraries/hci/hci_mem_pool.c -C_SOURCE_FILES += $(SDK_PATH)/libraries/hci/hci_slip.c -C_SOURCE_FILES += $(SDK_PATH)/libraries/hci/hci_transport.c -C_SOURCE_FILES += $(SDK_PATH)/libraries/util/nrf_assert.c +# Latest SDK files: peripheral drivers +C_SRC += $(SDK_PATH)/libraries/timer/app_timer.c +C_SRC += $(SDK_PATH)/libraries/scheduler/app_scheduler.c +C_SRC += $(SDK_PATH)/libraries/util/app_error.c +C_SRC += $(SDK_PATH)/libraries/util/app_util_platform.c +C_SRC += $(SDK_PATH)/libraries/crc16/crc16.c +C_SRC += $(SDK_PATH)/libraries/hci/hci_mem_pool.c +C_SRC += $(SDK_PATH)/libraries/hci/hci_slip.c +C_SRC += $(SDK_PATH)/libraries/hci/hci_transport.c +C_SRC += $(SDK_PATH)/libraries/util/nrf_assert.c # UART or USB Serial ifeq ($(MCU_SUB_VARIANT),nrf52) -C_SOURCE_FILES += $(SDK_PATH)/libraries/uart/app_uart.c -C_SOURCE_FILES += $(SDK_PATH)/drivers_nrf/uart/nrf_drv_uart.c -C_SOURCE_FILES += $(SDK_PATH)/drivers_nrf/common/nrf_drv_common.c + +C_SRC += $(SDK_PATH)/libraries/uart/app_uart.c +C_SRC += $(SDK_PATH)/drivers_nrf/uart/nrf_drv_uart.c +C_SRC += $(SDK_PATH)/drivers_nrf/common/nrf_drv_common.c IPATH += $(SDK11_PATH)/libraries/util IPATH += $(SDK_PATH)/drivers_nrf/common IPATH += $(SDK_PATH)/drivers_nrf/uart else -# src -C_SOURCE_FILES += $(SRC_PATH)/usb/usb_desc.c -C_SOURCE_FILES += $(SRC_PATH)/usb/usb.c -C_SOURCE_FILES += $(SRC_PATH)/usb/msc_uf2.c -C_SOURCE_FILES += $(SRC_PATH)/usb/uf2/ghostfat.c + +# USB Application ( MSC + UF2 ) +C_SRC += $(wildcard src/usb/*.c) +C_SRC += $(wildcard src/usb/uf2/*.c) # TinyUSB stack -C_SOURCE_FILES += $(TUSB_PATH)/portable/nordic/nrf5x/dcd_nrf5x.c -C_SOURCE_FILES += $(TUSB_PATH)/common/tusb_fifo.c -C_SOURCE_FILES += $(TUSB_PATH)/device/usbd.c -C_SOURCE_FILES += $(TUSB_PATH)/device/usbd_control.c -C_SOURCE_FILES += $(TUSB_PATH)/class/cdc/cdc_device.c -C_SOURCE_FILES += $(TUSB_PATH)/class/msc/msc_device.c -C_SOURCE_FILES += $(TUSB_PATH)/tusb.c +C_SRC += $(TUSB_PATH)/portable/nordic/nrf5x/dcd_nrf5x.c +C_SRC += $(TUSB_PATH)/common/tusb_fifo.c +C_SRC += $(TUSB_PATH)/device/usbd.c +C_SRC += $(TUSB_PATH)/device/usbd_control.c +C_SRC += $(TUSB_PATH)/class/cdc/cdc_device.c +C_SRC += $(TUSB_PATH)/class/msc/msc_device.c +C_SRC += $(TUSB_PATH)/tusb.c endif - -#****************************************************************************** +#------------------------------------------------------------------------------ # Assembly Files -#****************************************************************************** -ASM_SOURCE_FILES = $(NRFX_PATH)/mdk/gcc_startup_$(MCU_SUB_VARIANT).S +#------------------------------------------------------------------------------ +ASM_SRC = $(NRFX_PATH)/mdk/gcc_startup_$(MCU_SUB_VARIANT).S -#****************************************************************************** +#------------------------------------------------------------------------------ # INCLUDE PATH -#****************************************************************************** +#------------------------------------------------------------------------------ # src -IPATH += $(SRC_PATH) -IPATH += $(SRC_PATH)/boards/$(BOARD) +IPATH += src +IPATH += src/boards/$(BOARD) -IPATH += $(SRC_PATH)/cmsis/include -IPATH += $(SRC_PATH)/usb -IPATH += $(SRC_PATH)/boards +IPATH += src/cmsis/include +IPATH += src/usb +IPATH += src/boards IPATH += $(TUSB_PATH) # nrfx @@ -225,36 +193,61 @@ IPATH += $(SDK_PATH)/libraries/hci IPATH += $(SDK_PATH)/drivers_nrf/delay # Softdevice -IPATH += $(SD_API_PATH)/include -IPATH += $(SD_API_PATH)/include/nrf52 +IPATH += $(SD_PATH)/$(SD_FILENAME)_API/include +IPATH += $(SD_PATH)/$(SD_FILENAME)_API/include/nrf52 INC_PATHS = $(addprefix -I,$(IPATH)) -#****************************************************************************** +#------------------------------------------------------------------------------ # Compiler Flags -# - Additional compiler flags -#****************************************************************************** +#------------------------------------------------------------------------------ + +# Debugging/Optimization +ifeq ($(DEBUG), 1) + CFLAGS += -Og -ggdb +else + CFLAGS += -Os +endif #flags common to all targets -CFLAGS += -mcpu=cortex-m4 -CFLAGS += -mthumb -mabi=aapcs --std=gnu99 -CFLAGS += -Wall -Werror -Os -g3 -CFLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16 +CFLAGS += \ + -mthumb \ + -mabi=aapcs \ + -mcpu=cortex-m4 \ + -mfloat-abi=hard \ + -mfpu=fpv4-sp-d16 \ + -ffunction-sections \ + -fdata-sections \ + -fno-builtin \ + -fshort-enums \ + -fstack-usage \ + -fno-strict-aliasing \ + -Wall \ + -Wextra \ + -Werror \ + -Wfatal-errors \ + -Werror-implicit-function-declaration \ + -Wfloat-equal \ + -Wundef \ + -Wshadow \ + -Wwrite-strings \ + -Wsign-compare \ + -Wmissing-format-attribute \ + -Wno-endif-labels \ + -Wunreachable-code -# keep every function in separate section. This will allow linker to dump unused functions -CFLAGS += -ffunction-sections -fdata-sections -fno-strict-aliasing -CFLAGS += -fno-builtin --short-enums -fstack-usage +# Suppress warning caused by SDK +CFLAGS += -Wno-unused-parameter -Wno-expansion-to-defined + +# TinyUSB tusb_hal_nrf_power_event +CFLAGS += -Wno-cast-function-type # Defined Symbol (MACROS) CFLAGS += -D__HEAP_SIZE=0 CFLAGS += -DCONFIG_GPIO_AS_PINRESET CFLAGS += -DCONFIG_NFCT_PINS_AS_GPIOS -CFLAGS += -DBLE_STACK_SUPPORT_REQD -CFLAGS += -DSWI_DISABLE0 CFLAGS += -DSOFTDEVICE_PRESENT -CFLAGS += -DFLOAT_ABI_HARD CFLAGS += -DDFU_APP_DATA_RESERVED=7*4096 -CFLAGS += $(MCU_FLAGS) CFLAGS += -DUF2_VERSION='"$(GIT_VERSION) $(GIT_SUBMODULE_VERSIONS) $(SD_NAME) $(SD_VERSION)"' CFLAGS += -DBLEDIS_FW_VERSION='"$(GIT_VERSION) $(SD_NAME) $(SD_VERSION)"' @@ -262,42 +255,33 @@ CFLAGS += -DBLEDIS_FW_VERSION='"$(GIT_VERSION) $(SD_NAME) $(SD_VERSION)"' _VER = $(subst ., ,$(word 1, $(subst -, ,$(GIT_VERSION)))) CFLAGS += -DMK_BOOTLOADER_VERSION='($(word 1,$(_VER)) << 16) + ($(word 2,$(_VER)) << 8) + $(word 3,$(_VER))' - -#****************************************************************************** +#------------------------------------------------------------------------------ # Linker Flags -# -#****************************************************************************** +#------------------------------------------------------------------------------ -# keep every function in separate section. This will allow linker to dump unused functions -LDFLAGS += -Xlinker -Map=$(BUILD)/$(OUTPUT_FILENAME).map -LDFLAGS += -mthumb -mabi=aapcs -L$(SRC_PATH)/linker -T$(LD_FILE) -LDFLAGS += -mcpu=cortex-m4 -LDFLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16 +LDFLAGS += \ + $(CFLAGS) \ + -Wl,-L,linker -Wl,-T,$(LD_FILE) \ + -Wl,-Map=$@.map -Wl,-cref -Wl,-gc-sections \ + -specs=nosys.specs -specs=nano.specs -# let linker to dump unused sections -LDFLAGS += -Wl,--gc-sections +LIBS += -lm -lc -# use newlib in nano version -LDFLAGS += --specs=nano.specs -lc -lnosys - -#****************************************************************************** +#------------------------------------------------------------------------------ # Assembler flags -# -#****************************************************************************** -ASMFLAGS += -x assembler-with-cpp -ASMFLAGS += -D__HEAP_SIZE=0 -ASMFLAGS += -DBLE_STACK_SUPPORT_REQD -ASMFLAGS += -DSWI_DISABLE0 -ASMFLAGS += -DSOFTDEVICE_PRESENT -ASMFLAGS += -DFLOAT_ABI_HARD -ASMFLAGS += $(MCU_FLAGS) +#------------------------------------------------------------------------------ +ASFLAGS += $(CFLAGS) -C_SOURCE_FILE_NAMES = $(notdir $(C_SOURCE_FILES)) -C_PATHS = $(call remduplicates, $(dir $(C_SOURCE_FILES) ) ) + +#function for removing duplicates in a list +remduplicates = $(strip $(if $1,$(firstword $1) $(call remduplicates,$(filter-out $(firstword $1),$1)))) + +C_SOURCE_FILE_NAMES = $(notdir $(C_SRC)) +C_PATHS = $(call remduplicates, $(dir $(C_SRC) ) ) C_OBJECTS = $(addprefix $(BUILD)/, $(C_SOURCE_FILE_NAMES:.c=.o) ) -ASM_SOURCE_FILE_NAMES = $(notdir $(ASM_SOURCE_FILES)) -ASM_PATHS = $(call remduplicates, $(dir $(ASM_SOURCE_FILES) )) +ASM_SOURCE_FILE_NAMES = $(notdir $(ASM_SRC)) +ASM_PATHS = $(call remduplicates, $(dir $(ASM_SRC) )) ASM_OBJECTS = $(addprefix $(BUILD)/, $(ASM_SOURCE_FILE_NAMES:.S=.o) ) vpath %.c $(C_PATHS) @@ -305,25 +289,26 @@ vpath %.S $(ASM_PATHS) OBJECTS = $(C_OBJECTS) $(ASM_OBJECTS) -#****************************************************************************** +#------------------------------------------------------------------------------ # BUILD TARGETS -#****************************************************************************** +#------------------------------------------------------------------------------ +# Verbose mode (V=). 0: default, 1: print out CFLAG, LDFLAG 2: print all compile command ifeq ("$(V)","1") $(info CFLAGS $(CFLAGS)) $(info ) $(info LDFLAGS $(LDFLAGS)) $(info ) -$(info ASMFLAGS $(ASMFLAGS)) +$(info ASFLAGS $(ASFLAGS)) $(info ) endif -.phony: all clean size flash sd erase +.PHONY: all clean flash dfu-flash sd gdbflash gdb # default target to build -all: $(BUILD)/$(OUTPUT_FILENAME)-nosd.out size +all: $(BUILD)/$(OUT_FILE)-nosd.out $(BUILD)/$(MERGED_FILE).hex -#******************* Flash target ******************* +#------------------- Flash target ------------------- check_defined = \ $(strip $(foreach 1,$1, \ @@ -333,11 +318,11 @@ __check_defined = \ $(error Undefined make flag: $1$(if $2, ($2)))) # Flash the compiled -flash: $(BUILD)/$(OUTPUT_FILENAME)-nosd.hex +flash: $(BUILD)/$(OUT_FILE)-nosd.hex @echo Flashing: $< $(NRFJPROG) --program $< --sectoranduicrerase -f nrf52 --reset -dfu-flash: $(BUILD)/$(MERGED_FNAME).zip +dfu-flash: $(BUILD)/$(MERGED_FILE).zip @:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyACM0) $(NRFUTIL) --verbose dfu serial --package $< -p $(SERIAL) -b 115200 --singlebank --touch 1200 @@ -345,18 +330,14 @@ sd: @echo Flashing: $(SD_HEX) $(NRFJPROG) --program $(SD_HEX) -f nrf52 --chiperase --reset -erase: - @echo Erasing chip - $(NRFJPROG) --eraseall -f nrf52 - -gdbflash: $(BUILD)/$(MERGED_FNAME).hex +gdbflash: $(BUILD)/$(MERGED_FILE).hex @echo Flashing: $< @$(GDB_BMP) -nx --batch -ex 'load $<' -ex 'compare-sections' -ex 'kill' -gdb: $(BUILD)/$(OUTPUT_FILENAME)-nosd.out +gdb: $(BUILD)/$(OUT_FILE)-nosd.out $(GDB_BMP) $< -#******************* Compile rules ******************* +#------------------- Compile rules ------------------- ## Create build directories $(BUILD): @@ -368,43 +349,34 @@ clean: # Create objects from C SRC files $(BUILD)/%.o: %.c @echo CC $(notdir $<) - $(QUIET)$(CC) $(CFLAGS) $(INC_PATHS) -c -o $@ $< + @$(CC) $(CFLAGS) $(INC_PATHS) -c -o $@ $< # Assemble files $(BUILD)/%.o: %.S @echo AS $(notdir $<) - $(QUIET)$(CC) $(ASMFLAGS) $(INC_PATHS) -c -o $@ $< + @$(CC) -x assembler-with-cpp $(ASFLAGS) $(INC_PATHS) -c -o $@ $< # Link -$(BUILD)/$(OUTPUT_FILENAME)-nosd.out: $(BUILD) $(OBJECTS) - @echo LD $(OUTPUT_FILENAME)-nosd.out - $(QUIET)$(CC) $(LDFLAGS) $(OBJECTS) $(LIBS) -lm -o $@ +$(BUILD)/$(OUT_FILE)-nosd.out: $(BUILD) $(OBJECTS) + @echo LD $(OUT_FILE)-nosd.out + @$(CC) -o $@ $(LDFLAGS) $(OBJECTS) -Wl,--start-group $(LIBS) -Wl,--end-group + @$(SIZE) $@ -size: $(BUILD)/$(OUTPUT_FILENAME)-nosd.out - -@echo '' - $(QUIET)$(SIZE) $< - -@echo '' - - -#******************* Binary generator ******************* -.phony: genhex genpkg +#------------------- Binary generator ------------------- ## Create binary .hex file from the .out file -genhex: $(BUILD)/$(OUTPUT_FILENAME)-nosd.hex - -$(BUILD)/$(OUTPUT_FILENAME)-nosd.hex: $(BUILD)/$(OUTPUT_FILENAME)-nosd.out - @echo CR $(OUTPUT_FILENAME)-nosd.hex - $(QUIET)$(OBJCOPY) -O ihex $< $@ +$(BUILD)/$(OUT_FILE)-nosd.hex: $(BUILD)/$(OUT_FILE)-nosd.out + @echo CR $(OUT_FILE)-nosd.hex + @$(OBJCOPY) -O ihex $< $@ # merge bootloader and sd hex together -combinehex: $(BUILD)/$(MERGED_FNAME).hex - -$(BUILD)/$(MERGED_FNAME).hex: $(BUILD)/$(OUTPUT_FILENAME)-nosd.hex - @echo CR $(MERGED_FNAME).hex +$(BUILD)/$(MERGED_FILE).hex: $(BUILD)/$(OUT_FILE)-nosd.hex + @echo CR $(MERGED_FILE).hex @mergehex -q -m $< $(SD_HEX) -o $@ -## Create pkg file for bootloader+SD combo to use with DFU -genpkg: $(BUILD)/$(MERGED_FNAME).zip +## Create pkg zip file for bootloader+SD combo to use with DFU Serial +.PHONY: genpkg +genpkg: $(BUILD)/$(MERGED_FILE).zip -$(BUILD)/$(MERGED_FNAME).zip: $(BUILD)/$(OUTPUT_FILENAME)-nosd.hex +$(BUILD)/$(MERGED_FILE).zip: $(BUILD)/$(OUT_FILE)-nosd.hex @$(NRFUTIL) dfu genpkg --dev-type 0x0052 --dev-revision $(DFU_DEV_REV) --bootloader $< --softdevice $(SD_HEX) $@ diff --git a/README.md b/README.md index 1ff105d..6c65816 100644 --- a/README.md +++ b/README.md @@ -105,9 +105,7 @@ both bootloader and the Nordic SoftDevice, you can freely upgrade/downgrade to a ## How to compile and build You should only continue if you are looking to develop bootloader for your own. -You must have have a J-Link available to "unbrick" your device. - -### Option 1: Build with Makefile +You must have have a J-Link available to "unbrick" your device. Prerequisites @@ -117,7 +115,7 @@ Prerequisites To build: ``` -make BOARD=feather_nrf52840_express all combinehex +make BOARD=feather_nrf52840_express all ``` To flash the bootloader with JLink: @@ -138,12 +136,6 @@ To flash SoftDevice (and chip erase): make BOARD=feather_nrf52840_express sd ``` -To erase all of flash: - -``` -make BOARD=feather_nrf52840_express erase -``` - For the list of supported boards, run `make` without `BOARD=` : ``` @@ -167,13 +159,11 @@ make: *** [_build/main.o] Error 127 ``` ... you may need to pass the location of the GCC ARM toolchain binaries to `make` using -the variable `GNU_INSTALL_ROOT` as below: +the variable `CROSS_COMPILE` as below: ``` -$ make GNU_INSTALL_ROOT=/opt/gcc-arm-none-eabi-9-2019-q4-major/bin/ BOARD=feather_nrf52832 all +$ make CROSS_COMPILE=/opt/gcc-arm-none-eabi-9-2019-q4-major/bin/arm-none-eabi- BOARD=feather_nrf52832 all ``` -_Please note that the path needs a trailing path separator (a `/`)_ - #### 2. `mergehex: No such file or directory` Make sure that `mergehex` is available from the command-line. This binary is @@ -184,22 +174,3 @@ part of Nordic's nRF5x Command Line Tools. Make sure that `nrfjprog` is available from the command-line. This binary is part of Nordic's nRF5x Command Line Tools. -On POSIX-type systems you can temporarily add the path to `nrfjprog` via a -variation on the following command: - -``` -$ export PATH=$PATH:/location/of/nRF5x-Command-Line-Tools_9_7_2_OSX/nrfjprog -``` - -### Option 2: Build using Segger Embedded Studio - -For easier debugging you can also use [SES](https://www.segger.com/products/development-tools/embedded-studio/). -The project file is located at `src/segger/Adafruit_nRF52_Bootloader.emProject`. - -> **Note**: SES only flashes the bootloader when you click download, not the SoftDevice. -You need to flash the SoftDevice beforehand if you haven't already done so. -As mentioned above do something like: - -``` -make BOARD=feather_nrf52840_express sd -``` diff --git a/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c b/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c index 2ad777f..44465a3 100644 --- a/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c +++ b/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c @@ -442,7 +442,8 @@ uint32_t bootloader_dfu_sd_update_continue(void) uint32_t bootloader_dfu_sd_update_finalize(void) { - dfu_update_status_t update_status = {DFU_UPDATE_SD_SWAPPED, }; + dfu_update_status_t update_status = { 0 }; + update_status.status_code = DFU_UPDATE_SD_SWAPPED; bootloader_dfu_update_process(update_status); diff --git a/lib/sdk11/components/libraries/bootloader_dfu/dfu_dual_bank.c b/lib/sdk11/components/libraries/bootloader_dfu/dfu_dual_bank.c deleted file mode 100644 index c98e229..0000000 --- a/lib/sdk11/components/libraries/bootloader_dfu/dfu_dual_bank.c +++ /dev/null @@ -1,843 +0,0 @@ -/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved. - * - * The information contained herein is property of Nordic Semiconductor ASA. - * Terms and conditions of usage are described in detail in NORDIC - * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. - * - * Licensees are granted free, non-transferable use of the information. NO - * WARRANTY of ANY KIND is provided. This heading must NOT be removed from - * the file. - * - */ - -#include -#include "dfu.h" -#include -#include "dfu_bank_internal.h" -#include "nrf.h" -#include "nrf_sdm.h" -#include "app_error.h" -#include "app_timer.h" -#include "bootloader.h" -#include "bootloader_types.h" -#include "pstorage.h" -#include "nrf_mbr.h" -#include "dfu_init.h" -#include "sdk_common.h" - -static dfu_state_t m_dfu_state; /**< Current DFU state. */ -static uint32_t m_image_size; /**< Size of the image that will be transmitted. */ - -static dfu_start_packet_t m_start_packet; /**< Start packet received for this update procedure. Contains update mode and image sizes information to be used for image transfer. */ -static uint8_t m_init_packet[128]; /**< Init packet, can hold CRC, Hash, Signed Hash and similar, for image validation, integrety check and authorization checking. */ -static uint8_t m_init_packet_length; /**< Length of init packet received. */ -static uint16_t m_image_crc; /**< Calculated CRC of the image received. */ - -APP_TIMER_DEF(m_dfu_timer_id); /**< Application timer id. */ -static bool m_dfu_timed_out = false; /**< Boolean flag value for tracking DFU timer timeout state. */ - -static pstorage_handle_t m_storage_handle_swap; /**< Pstorage handle for the swap area (bank 1). Bank used when updating an application or bootloader without SoftDevice. */ -static pstorage_handle_t m_storage_handle_app; /**< Pstorage handle for the application area (bank 0). Bank used when updating a SoftDevice w/wo bootloader. Handle also used when swapping received application from bank 1 to bank 0. */ -static pstorage_handle_t * mp_storage_handle_active; /**< Pointer to the pstorage handle for the active bank for receiving of data packets. */ - -static dfu_callback_t m_data_pkt_cb; /**< Callback from DFU Bank module for notification of asynchronous operation such as flash prepare. */ -static dfu_bank_func_t m_functions; /**< Structure holding operations for the selected update process. */ - - -/**@brief Function for handling callbacks from pstorage module. - * - * @details Handles pstorage results for clear and storage operation. For detailed description of - * the parameters provided with the callback, please refer to \ref pstorage_ntf_cb_t. - */ -static void pstorage_callback_handler(pstorage_handle_t * p_handle, - uint8_t op_code, - uint32_t result, - uint8_t * p_data, - uint32_t data_len) -{ - switch (op_code) - { - case PSTORAGE_STORE_OP_CODE: - if ((m_dfu_state == DFU_STATE_RX_DATA_PKT) && (m_data_pkt_cb != NULL)) - { - m_data_pkt_cb(DATA_PACKET, result, p_data); - } - break; - - case PSTORAGE_CLEAR_OP_CODE: - if (m_dfu_state == DFU_STATE_PREPARING) - { - m_functions.cleared(); - m_dfu_state = DFU_STATE_RDY; - if (m_data_pkt_cb != NULL) - { - m_data_pkt_cb(START_PACKET, result, p_data); - } - } - break; - - default: - break; - } - APP_ERROR_CHECK(result); -} - - -/**@brief Function for handling the DFU timeout. - * - * @param[in] p_context The timeout context. - */ -static void dfu_timeout_handler(void * p_context) -{ - UNUSED_PARAMETER(p_context); - dfu_update_status_t update_status; - - m_dfu_timed_out = true; - update_status.status_code = DFU_TIMEOUT; - - bootloader_dfu_update_process(update_status); -} - - -/**@brief Function for restarting the DFU Timer. - * - * @details This function will stop and restart the DFU timer. This function will be called by the - * functions handling any DFU packet received from the peer that is transferring a firmware - * image. - */ -static uint32_t dfu_timer_restart(void) -{ - if (m_dfu_timed_out) - { - // The DFU timer had already timed out. - return NRF_ERROR_INVALID_STATE; - } - - uint32_t err_code = app_timer_stop(m_dfu_timer_id); - APP_ERROR_CHECK(err_code); - - err_code = app_timer_start(m_dfu_timer_id, DFU_TIMEOUT_INTERVAL, NULL); - APP_ERROR_CHECK(err_code); - - return err_code; -} - - -/**@brief Function for preparing of flash before receiving SoftDevice image. - * - * @details This function will erase current application area to ensure sufficient amount of - * storage for the SoftDevice image. Upon erase complete a callback will be done. - * See \ref dfu_bank_prepare_t for further details. - */ -static void dfu_prepare_func_app_erase(uint32_t image_size) -{ - uint32_t err_code; - - mp_storage_handle_active = &m_storage_handle_app; - - // Doing a SoftDevice update thus current application must be cleared to ensure enough space - // for new SoftDevice. - m_dfu_state = DFU_STATE_PREPARING; - err_code = pstorage_clear(&m_storage_handle_app, m_image_size); - APP_ERROR_CHECK(err_code); -} - - -/**@brief Function for preparing swap before receiving application or bootloader image. - * - * @details This function will erase current swap area to ensure flash is ready for storage of the - * Application or Bootloader image. Upon erase complete a callback will be done. - * See \ref dfu_bank_prepare_t for further details. - */ -static void dfu_prepare_func_swap_erase(uint32_t image_size) -{ - uint32_t err_code; - - mp_storage_handle_active = &m_storage_handle_swap; - - m_dfu_state = DFU_STATE_PREPARING; -// err_code = pstorage_clear(&m_storage_handle_swap, DFU_IMAGE_MAX_SIZE_BANKED); - err_code = pstorage_clear(&m_storage_handle_swap, image_size); - APP_ERROR_CHECK(err_code); -} - - -/**@brief Function for handling behaviour when clear operation has completed. - */ -static void dfu_cleared_func_swap(void) -{ - // Do nothing. -} - - -/**@brief Function for handling behaviour when clear operation has completed. - */ -static void dfu_cleared_func_app(void) -{ - dfu_update_status_t update_status = {DFU_BANK_0_ERASED, }; - bootloader_dfu_update_process(update_status); -} - - -/**@brief Function for calculating storage offset for receiving SoftDevice image. - * - * @details When a new SoftDevice is received it will be temporary stored in flash before moved to - * address 0x0. In order to succesfully validate transfer and relocation it is important - * that temporary image and final installed image does not ovwerlap hence an offset must - * be calculated in case new image is larger than currently installed SoftDevice. - */ -uint32_t offset_calculate(uint32_t sd_image_size) -{ - uint32_t offset = 0; - - if (m_start_packet.sd_image_size > DFU_BANK_0_REGION_START) - { - uint32_t page_mask = (CODE_PAGE_SIZE - 1); - uint32_t diff = m_start_packet.sd_image_size - DFU_BANK_0_REGION_START; - - offset = diff & ~page_mask; - - // Align offset to next page if image size is not page sized. - if ((diff & page_mask) > 0) - { - offset += CODE_PAGE_SIZE; - } - } - - return offset; -} - - -/**@brief Function for activating received SoftDevice image. - * - * @note This function will not move the SoftDevice image. - * The bootloader settings will be marked as SoftDevice update complete and the swapping of - * current SoftDevice will occur after system reset. - * - * @return NRF_SUCCESS on success. - */ -static uint32_t dfu_activate_sd(void) -{ - dfu_update_status_t update_status; - - update_status.status_code = DFU_UPDATE_SD_COMPLETE; - update_status.app_crc = m_image_crc; - update_status.sd_image_start = DFU_BANK_0_REGION_START; - update_status.sd_size = m_start_packet.sd_image_size; - update_status.bl_size = m_start_packet.bl_image_size; - update_status.app_size = m_start_packet.app_image_size; - - bootloader_dfu_update_process(update_status); - - return NRF_SUCCESS; -} - - -/**@brief Function for activating received Application image. - * - * @details This function will move the received application image fram swap (bank 1) to - * application area (bank 0). - * - * @return NRF_SUCCESS on success. Error code otherwise. - */ -static uint32_t dfu_activate_app(void) -{ - uint32_t err_code; - - // Erase BANK 0. - err_code = pstorage_clear(&m_storage_handle_app, m_start_packet.app_image_size); - APP_ERROR_CHECK(err_code); - - err_code = pstorage_store(&m_storage_handle_app, - (uint8_t *)m_storage_handle_swap.block_id, - m_start_packet.app_image_size, - 0); - - if (err_code == NRF_SUCCESS) - { - dfu_update_status_t update_status; - - memset(&update_status, 0, sizeof(dfu_update_status_t )); - update_status.status_code = DFU_UPDATE_APP_COMPLETE; - update_status.app_crc = m_image_crc; - update_status.app_size = m_start_packet.app_image_size; - - bootloader_dfu_update_process(update_status); - } - - return err_code; -} - - -/**@brief Function for activating received Bootloader image. - * - * @note This function will not move the bootloader image. - * The bootloader settings will be marked as Bootloader update complete and the swapping of - * current bootloader will occur after system reset. - * - * @return NRF_SUCCESS on success. - */ -static uint32_t dfu_activate_bl(void) -{ - dfu_update_status_t update_status; - - update_status.status_code = DFU_UPDATE_BOOT_COMPLETE; - update_status.app_crc = m_image_crc; - update_status.sd_size = m_start_packet.sd_image_size; - update_status.bl_size = m_start_packet.bl_image_size; - update_status.app_size = m_start_packet.app_image_size; - - bootloader_dfu_update_process(update_status); - - return NRF_SUCCESS; -} - - -uint32_t dfu_init(void) -{ - uint32_t err_code; - pstorage_module_param_t storage_module_param = {.cb = pstorage_callback_handler}; - - m_init_packet_length = 0; - m_image_crc = 0; - - err_code = pstorage_register(&storage_module_param, &m_storage_handle_app); - if (err_code != NRF_SUCCESS) - { - m_dfu_state = DFU_STATE_INIT_ERROR; - return err_code; - } - - m_storage_handle_app.block_id = DFU_BANK_0_REGION_START; - m_storage_handle_swap = m_storage_handle_app; - m_storage_handle_swap.block_id = DFU_BANK_1_REGION_START; - - // Create the timer to monitor the activity by the peer doing the firmware update. - err_code = app_timer_create(&m_dfu_timer_id, - APP_TIMER_MODE_SINGLE_SHOT, - dfu_timeout_handler); - APP_ERROR_CHECK(err_code); - - // Start the DFU timer. - err_code = app_timer_start(m_dfu_timer_id, DFU_TIMEOUT_INTERVAL, NULL); - APP_ERROR_CHECK(err_code); - - m_data_received = 0; - m_dfu_state = DFU_STATE_IDLE; - - return NRF_SUCCESS; -} - - -void dfu_register_callback(dfu_callback_t callback_handler) -{ - m_data_pkt_cb = callback_handler; -} - - -uint32_t dfu_start_pkt_handle(dfu_update_packet_t * p_packet) -{ - uint32_t err_code; - - m_start_packet = *(p_packet->params.start_packet); - - // Check that the requested update procedure is supported. - // Currently the following combinations are allowed: - // - Application - // - SoftDevice - // - Bootloader - // - SoftDevice with Bootloader - if (IS_UPDATING_APP(m_start_packet) && - (IS_UPDATING_SD(m_start_packet) || IS_UPDATING_BL(m_start_packet))) - { - // App update is only supported independently. - return NRF_ERROR_NOT_SUPPORTED; - } - - if (!(IS_WORD_SIZED(m_start_packet.sd_image_size) && - IS_WORD_SIZED(m_start_packet.bl_image_size) && - IS_WORD_SIZED(m_start_packet.app_image_size))) - { - // Image_sizes are not a multiple of 4 (word size). - return NRF_ERROR_NOT_SUPPORTED; - } - - m_image_size = m_start_packet.sd_image_size + m_start_packet.bl_image_size + - m_start_packet.app_image_size; - - if (m_start_packet.bl_image_size > DFU_BL_IMAGE_MAX_SIZE) - { - return NRF_ERROR_DATA_SIZE; - } - - if (IS_UPDATING_SD(m_start_packet)) - { - if (m_image_size > (DFU_IMAGE_MAX_SIZE_FULL)) - { - return NRF_ERROR_DATA_SIZE; - } - m_functions.prepare = dfu_prepare_func_app_erase; - m_functions.cleared = dfu_cleared_func_app; - m_functions.activate = dfu_activate_sd; - } - else - { - if (m_image_size > DFU_IMAGE_MAX_SIZE_BANKED) - { - return NRF_ERROR_DATA_SIZE; - } - - m_functions.prepare = dfu_prepare_func_swap_erase; - m_functions.cleared = dfu_cleared_func_swap; - if (IS_UPDATING_BL(m_start_packet)) - { - m_functions.activate = dfu_activate_bl; - } - else - { - m_functions.activate = dfu_activate_app; - } - } - - switch (m_dfu_state) - { - case DFU_STATE_IDLE: - // Valid peer activity detected. Hence restart the DFU timer. - err_code = dfu_timer_restart(); - VERIFY_SUCCESS(err_code); - m_functions.prepare(m_image_size); - - break; - - default: - err_code = NRF_ERROR_INVALID_STATE; - break; - } - - return err_code; -} - - -uint32_t dfu_data_pkt_handle(dfu_update_packet_t * p_packet) -{ - uint32_t data_length; - uint32_t err_code; - uint32_t * p_data; - - VERIFY_PARAM_NOT_NULL(p_packet); - - // Check pointer alignment. - if (!is_word_aligned(p_packet->params.data_packet.p_data_packet)) - { - // The p_data_packet is not word aligned address. - return NRF_ERROR_INVALID_ADDR; - } - - switch (m_dfu_state) - { - case DFU_STATE_RDY: - case DFU_STATE_RX_INIT_PKT: - return NRF_ERROR_INVALID_STATE; - - case DFU_STATE_RX_DATA_PKT: - data_length = p_packet->params.data_packet.packet_length * sizeof(uint32_t); - - if ((m_data_received + data_length) > m_image_size) - { - // The caller is trying to write more bytes into the flash than the size provided to - // the dfu_image_size_set function. This is treated as a serious error condition and - // an unrecoverable one. Hence point the variable mp_app_write_address to the top of - // the flash area. This will ensure that all future application data packet writes - // will be blocked because of the above check. - m_data_received = 0xFFFFFFFF; - - return NRF_ERROR_DATA_SIZE; - } - - // Valid peer activity detected. Hence restart the DFU timer. - err_code = dfu_timer_restart(); - VERIFY_SUCCESS(err_code); - - p_data = (uint32_t *)p_packet->params.data_packet.p_data_packet; - - err_code = pstorage_store(mp_storage_handle_active, - (uint8_t *)p_data, - data_length, - m_data_received); - VERIFY_SUCCESS(err_code); - - m_data_received += data_length; - - if (m_data_received != m_image_size) - { - // The entire image is not received yet. More data is expected. - err_code = NRF_ERROR_INVALID_LENGTH; - } - else - { - // The entire image has been received. Return NRF_SUCCESS. - err_code = NRF_SUCCESS; - } - break; - - default: - err_code = NRF_ERROR_INVALID_STATE; - break; - } - - return err_code; -} - - -uint32_t dfu_init_pkt_complete(void) -{ - uint32_t err_code = NRF_ERROR_INVALID_STATE; - - // DFU initialization has been done and a start packet has been received. - if (IMAGE_WRITE_IN_PROGRESS()) - { - // Image write is already in progress. Cannot handle an init packet now. - return NRF_ERROR_INVALID_STATE; - } - - if (m_dfu_state == DFU_STATE_RX_INIT_PKT) - { - err_code = dfu_init_prevalidate(m_init_packet, m_init_packet_length, m_start_packet.dfu_update_mode); - if (err_code == NRF_SUCCESS) - { - m_dfu_state = DFU_STATE_RX_DATA_PKT; - } - else - { - m_init_packet_length = 0; - } - } - return err_code; -} - - -uint32_t dfu_init_pkt_handle(dfu_update_packet_t * p_packet) -{ - uint32_t err_code = NRF_SUCCESS; - uint32_t length; - - switch (m_dfu_state) - { - case DFU_STATE_RDY: - m_dfu_state = DFU_STATE_RX_INIT_PKT; - // When receiving init packet in state ready just update and fall through this case. - - case DFU_STATE_RX_INIT_PKT: - // DFU initialization has been done and a start packet has been received. - if (IMAGE_WRITE_IN_PROGRESS()) - { - // Image write is already in progress. Cannot handle an init packet now. - return NRF_ERROR_INVALID_STATE; - } - - // Valid peer activity detected. Hence restart the DFU timer. - err_code = dfu_timer_restart(); - VERIFY_SUCCESS(err_code); - - length = p_packet->params.data_packet.packet_length * sizeof(uint32_t); - if ((m_init_packet_length + length) > sizeof(m_init_packet)) - { - return NRF_ERROR_INVALID_LENGTH; - } - - memcpy(&m_init_packet[m_init_packet_length], - &p_packet->params.data_packet.p_data_packet[0], - length); - m_init_packet_length += length; - break; - - default: - // Either the start packet was not received or dfu_init function was not called before. - err_code = NRF_ERROR_INVALID_STATE; - break; - } - - return err_code; -} - - -uint32_t dfu_image_validate() -{ - uint32_t err_code; - - switch (m_dfu_state) - { - case DFU_STATE_RX_DATA_PKT: - // Check if the application image write has finished. - if (m_data_received != m_image_size) - { - // Image not yet fully transfered by the peer or the peer has attempted to write - // too much data. Hence the validation should fail. - err_code = NRF_ERROR_INVALID_STATE; - } - else - { - m_dfu_state = DFU_STATE_VALIDATE; - - // Valid peer activity detected. Hence restart the DFU timer. - err_code = dfu_timer_restart(); - if (err_code == NRF_SUCCESS) - { - err_code = dfu_init_postvalidate((uint8_t *)mp_storage_handle_active->block_id, - m_image_size); - VERIFY_SUCCESS(err_code); - - m_dfu_state = DFU_STATE_WAIT_4_ACTIVATE; - } - } - break; - - default: - err_code = NRF_ERROR_INVALID_STATE; - break; - } - - return err_code; -} - - -uint32_t dfu_image_activate() -{ - uint32_t err_code; - - switch (m_dfu_state) - { - case DFU_STATE_WAIT_4_ACTIVATE: - - // Stop the DFU Timer because the peer activity need not be monitored any longer. - err_code = app_timer_stop(m_dfu_timer_id); - APP_ERROR_CHECK(err_code); - - err_code = m_functions.activate(); - break; - - default: - err_code = NRF_ERROR_INVALID_STATE; - break; - } - - return err_code; -} - - -void dfu_reset(void) -{ - dfu_update_status_t update_status; - - update_status.status_code = DFU_RESET; - - bootloader_dfu_update_process(update_status); -} - - -static uint32_t dfu_compare_block(uint32_t * ptr1, uint32_t * ptr2, uint32_t len) -{ - sd_mbr_command_t sd_mbr_cmd; - - sd_mbr_cmd.command = SD_MBR_COMMAND_COMPARE; - sd_mbr_cmd.params.compare.ptr1 = ptr1; - sd_mbr_cmd.params.compare.ptr2 = ptr2; - sd_mbr_cmd.params.compare.len = len / sizeof(uint32_t); - - return sd_mbr_command(&sd_mbr_cmd); -} - - -static uint32_t dfu_copy_sd(uint32_t * src, uint32_t * dst, uint32_t len) -{ - sd_mbr_command_t sd_mbr_cmd; - - sd_mbr_cmd.command = SD_MBR_COMMAND_COPY_SD; - sd_mbr_cmd.params.copy_sd.src = src; - sd_mbr_cmd.params.copy_sd.dst = dst; - sd_mbr_cmd.params.copy_sd.len = len / sizeof(uint32_t); - - return sd_mbr_command(&sd_mbr_cmd); -} - - -static uint32_t dfu_sd_img_block_swap(uint32_t * src, - uint32_t * dst, - uint32_t len, - uint32_t block_size) -{ - // It is neccesarry to swap the new SoftDevice in 3 rounds to ensure correct copy of data - // and verifucation of data in case power reset occurs during write to flash. - // To ensure the robustness of swapping the images are compared backwards till start of - // image swap. If the back is identical everything is swapped. - uint32_t err_code = dfu_compare_block(src, dst, len); - if (err_code == NRF_SUCCESS) - { - return err_code; - } - - if ((uint32_t)dst > SOFTDEVICE_REGION_START) - { - err_code = dfu_sd_img_block_swap((uint32_t *)((uint32_t)src - block_size), - (uint32_t *)((uint32_t)dst - block_size), - block_size, - block_size); - VERIFY_SUCCESS(err_code); - } - - err_code = dfu_copy_sd(src, dst, len); - VERIFY_SUCCESS(err_code); - - return dfu_compare_block(src, dst, len); -} - - -uint32_t dfu_sd_image_swap(void) -{ - bootloader_settings_t boot_settings; - - bootloader_settings_get(&boot_settings); - - if (boot_settings.sd_image_size == 0) - { - return NRF_SUCCESS; - } - - if ((SOFTDEVICE_REGION_START + boot_settings.sd_image_size) > boot_settings.sd_image_start) - { - uint32_t err_code; - uint32_t sd_start = SOFTDEVICE_REGION_START; - uint32_t block_size = (boot_settings.sd_image_start - sd_start) / 2; - - /* ##### FIX START ##### */ - block_size &= ~(uint32_t)(CODE_PAGE_SIZE - 1); - /* ##### FIX END ##### */ - - uint32_t image_end = boot_settings.sd_image_start + boot_settings.sd_image_size; - - uint32_t img_block_start = boot_settings.sd_image_start + 2 * block_size; - uint32_t sd_block_start = sd_start + 2 * block_size; - - if (SD_SIZE_GET(MBR_SIZE) < boot_settings.sd_image_size) - { - // This will clear a page thus ensuring the old image is invalidated before swapping. - err_code = dfu_copy_sd((uint32_t *)(sd_start + block_size), - (uint32_t *)(sd_start + block_size), - sizeof(uint32_t)); - VERIFY_SUCCESS(err_code); - - err_code = dfu_copy_sd((uint32_t *)sd_start, (uint32_t *)sd_start, sizeof(uint32_t)); - VERIFY_SUCCESS(err_code); - } - - return dfu_sd_img_block_swap((uint32_t *)img_block_start, - (uint32_t *)sd_block_start, - image_end - img_block_start, - block_size); - } - else - { - if (boot_settings.sd_image_size != 0) - { - return dfu_copy_sd((uint32_t *)boot_settings.sd_image_start, - (uint32_t *)SOFTDEVICE_REGION_START, - boot_settings.sd_image_size); - } - } - - return NRF_SUCCESS; -} - - -uint32_t dfu_bl_image_swap(void) -{ - bootloader_settings_t bootloader_settings; - sd_mbr_command_t sd_mbr_cmd; - - bootloader_settings_get(&bootloader_settings); - - if (bootloader_settings.bl_image_size != 0) - { - uint32_t bl_image_start = (bootloader_settings.sd_image_size == 0) ? - DFU_BANK_1_REGION_START : - bootloader_settings.sd_image_start + - bootloader_settings.sd_image_size; - - sd_mbr_cmd.command = SD_MBR_COMMAND_COPY_BL; - sd_mbr_cmd.params.copy_bl.bl_src = (uint32_t *)(bl_image_start); - sd_mbr_cmd.params.copy_bl.bl_len = bootloader_settings.bl_image_size / sizeof(uint32_t); - - return sd_mbr_command(&sd_mbr_cmd); - } - return NRF_SUCCESS; -} - - -uint32_t dfu_bl_image_validate(void) -{ - bootloader_settings_t bootloader_settings; - sd_mbr_command_t sd_mbr_cmd; - - bootloader_settings_get(&bootloader_settings); - - if (bootloader_settings.bl_image_size != 0) - { - uint32_t bl_image_start = (bootloader_settings.sd_image_size == 0) ? - DFU_BANK_1_REGION_START : - bootloader_settings.sd_image_start + - bootloader_settings.sd_image_size; - - sd_mbr_cmd.command = SD_MBR_COMMAND_COMPARE; - sd_mbr_cmd.params.compare.ptr1 = (uint32_t *)BOOTLOADER_REGION_START; - sd_mbr_cmd.params.compare.ptr2 = (uint32_t *)(bl_image_start); - sd_mbr_cmd.params.compare.len = bootloader_settings.bl_image_size / sizeof(uint32_t); - - return sd_mbr_command(&sd_mbr_cmd); - } - return NRF_SUCCESS; -} - - -uint32_t dfu_sd_image_validate(void) -{ - bootloader_settings_t bootloader_settings; - sd_mbr_command_t sd_mbr_cmd; - - bootloader_settings_get(&bootloader_settings); - - if (bootloader_settings.sd_image_size == 0) - { - return NRF_SUCCESS; - } - - if ((SOFTDEVICE_REGION_START + bootloader_settings.sd_image_size) > bootloader_settings.sd_image_start) - { - uint32_t sd_start = SOFTDEVICE_REGION_START; - uint32_t block_size = (bootloader_settings.sd_image_start - sd_start) / 2; - uint32_t image_end = bootloader_settings.sd_image_start + - bootloader_settings.sd_image_size; - - /* ##### FIX START ##### */ - block_size &= ~(uint32_t)(CODE_PAGE_SIZE - 1); - /* ##### FIX END ##### */ - uint32_t img_block_start = bootloader_settings.sd_image_start + 2 * block_size; - uint32_t sd_block_start = sd_start + 2 * block_size; - - if (SD_SIZE_GET(MBR_SIZE) < bootloader_settings.sd_image_size) - { - return NRF_ERROR_NULL; - } - - return dfu_sd_img_block_swap((uint32_t *)img_block_start, - (uint32_t *)sd_block_start, - image_end - img_block_start, - block_size); - } - - sd_mbr_cmd.command = SD_MBR_COMMAND_COMPARE; - sd_mbr_cmd.params.compare.ptr1 = (uint32_t *)SOFTDEVICE_REGION_START; - sd_mbr_cmd.params.compare.ptr2 = (uint32_t *)bootloader_settings.sd_image_start; - sd_mbr_cmd.params.compare.len = bootloader_settings.sd_image_size / sizeof(uint32_t); - - return sd_mbr_command(&sd_mbr_cmd); -} diff --git a/lib/sdk11/components/libraries/bootloader_dfu/dfu_single_bank.c b/lib/sdk11/components/libraries/bootloader_dfu/dfu_single_bank.c index 60a1729..80d8293 100644 --- a/lib/sdk11/components/libraries/bootloader_dfu/dfu_single_bank.c +++ b/lib/sdk11/components/libraries/bootloader_dfu/dfu_single_bank.c @@ -164,7 +164,8 @@ static void dfu_prepare_func_app_erase(uint32_t image_size) */ static void dfu_cleared_func_app(void) { - dfu_update_status_t update_status = {DFU_BANK_0_ERASED, }; + dfu_update_status_t update_status = { 0 }; + update_status.status_code = DFU_BANK_0_ERASED; bootloader_dfu_update_process(update_status); } @@ -498,7 +499,8 @@ uint32_t dfu_init_pkt_handle(dfu_update_packet_t * p_packet) { case DFU_STATE_RDY: m_dfu_state = DFU_STATE_RX_INIT_PKT; - // When receiving init packet in state ready just update and fall through this case. + // When receiving init packet in state ready just update and fall through this case. + /* FALLTHRU */ case DFU_STATE_RX_INIT_PKT: // DFU initialization has been done and a start packet has been received. diff --git a/lib/sdk11/components/libraries/bootloader_dfu/dfu_transport_ble.c b/lib/sdk11/components/libraries/bootloader_dfu/dfu_transport_ble.c index 89c2096..d731d31 100644 --- a/lib/sdk11/components/libraries/bootloader_dfu/dfu_transport_ble.c +++ b/lib/sdk11/components/libraries/bootloader_dfu/dfu_transport_ble.c @@ -967,7 +967,7 @@ static void service_error_handler(uint32_t nrf_error) } -static void ascii_to_utf8(ble_srv_utf8_str_t * p_utf8, char * p_ascii) +static void ascii_to_utf8(ble_srv_utf8_str_t * p_utf8, const char * p_ascii) { p_utf8->length = (uint16_t)strlen(p_ascii); p_utf8->p_str = (uint8_t *)p_ascii; diff --git a/lib/tinyusb b/lib/tinyusb index 23df777..f2b761f 160000 --- a/lib/tinyusb +++ b/lib/tinyusb @@ -1 +1 @@ -Subproject commit 23df777b301af128d3543e286cd865212214b44d +Subproject commit f2b761fee98cf9d09500e2475861e51c6ee58e61 diff --git a/src/linker/nrf52833_s140_v6.ld b/linker/nrf52833_s140_v6.ld similarity index 100% rename from src/linker/nrf52833_s140_v6.ld rename to linker/nrf52833_s140_v6.ld diff --git a/src/linker/nrf52840_s140_v6.ld b/linker/nrf52840_s140_v6.ld similarity index 100% rename from src/linker/nrf52840_s140_v6.ld rename to linker/nrf52840_s140_v6.ld diff --git a/src/linker/nrf52_s132_v6.ld b/linker/nrf52_s132_v6.ld similarity index 100% rename from src/linker/nrf52_s132_v6.ld rename to linker/nrf52_s132_v6.ld diff --git a/src/linker/nrf_common.ld b/linker/nrf_common.ld similarity index 100% rename from src/linker/nrf_common.ld rename to linker/nrf_common.ld diff --git a/segger/Adafruit_nRF52_Bootloader.emProject b/segger/Adafruit_nRF52_Bootloader.emProject new file mode 100644 index 0000000..d5e8133 --- /dev/null +++ b/segger/Adafruit_nRF52_Bootloader.emProject @@ -0,0 +1,390 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/segger/SEGGER_RTT.c b/segger/SEGGER_RTT.c similarity index 100% rename from src/segger/SEGGER_RTT.c rename to segger/SEGGER_RTT.c diff --git a/src/segger/SEGGER_RTT.h b/segger/SEGGER_RTT.h similarity index 100% rename from src/segger/SEGGER_RTT.h rename to segger/SEGGER_RTT.h diff --git a/src/segger/SEGGER_RTT_Conf.h b/segger/SEGGER_RTT_Conf.h similarity index 100% rename from src/segger/SEGGER_RTT_Conf.h rename to segger/SEGGER_RTT_Conf.h diff --git a/src/segger/SEGGER_RTT_SES.c b/segger/SEGGER_RTT_SES.c similarity index 100% rename from src/segger/SEGGER_RTT_SES.c rename to segger/SEGGER_RTT_SES.c diff --git a/src/segger/flash_placement.xml b/segger/flash_placement.xml similarity index 100% rename from src/segger/flash_placement.xml rename to segger/flash_placement.xml diff --git a/src/segger/nRF52832_xxAA_MemoryMap.xml b/segger/nRF52832_xxAA_MemoryMap.xml similarity index 100% rename from src/segger/nRF52832_xxAA_MemoryMap.xml rename to segger/nRF52832_xxAA_MemoryMap.xml diff --git a/src/segger/nRF52840_xxAA_MemoryMap.xml b/segger/nRF52840_xxAA_MemoryMap.xml similarity index 100% rename from src/segger/nRF52840_xxAA_MemoryMap.xml rename to segger/nRF52840_xxAA_MemoryMap.xml diff --git a/src/segger/nRF_Target.js b/segger/nRF_Target.js similarity index 100% rename from src/segger/nRF_Target.js rename to segger/nRF_Target.js diff --git a/src/segger/nrf52840_Registers.xml b/segger/nrf52840_Registers.xml similarity index 100% rename from src/segger/nrf52840_Registers.xml rename to segger/nrf52840_Registers.xml diff --git a/src/segger/thumb_crt0.s b/segger/thumb_crt0.s similarity index 100% rename from src/segger/thumb_crt0.s rename to segger/thumb_crt0.s diff --git a/src/boards.c b/src/boards.c index fa68ddd..b4d7ede 100644 --- a/src/boards.c +++ b/src/boards.c @@ -295,7 +295,7 @@ void led_state(uint32_t state) } else if (temp_color_active) { final_color = (uint8_t*)&rgb_color; } - #if LED_NEOPIXEL || defined(LED_RGB_RED_PIN) + #if defined(LED_NEOPIXEL) || defined(LED_RGB_RED_PIN) if (final_color != NULL) { neopixel_write(final_color); } diff --git a/src/boards/aramcon_badge_2019/board.h b/src/boards/aramcon_badge_2019/board.h index 9e84b23..3dc666d 100644 --- a/src/boards/aramcon_badge_2019/board.h +++ b/src/boards/aramcon_badge_2019/board.h @@ -46,16 +46,9 @@ #define BUTTON_2 _PINNUM(0, 29) // middle button #define BUTTON_PULL NRF_GPIO_PIN_PULLUP -/*------------------------------------------------------------------*/ -/* UART (only used by nRF52832) - *------------------------------------------------------------------*/ -#define RX_PIN_NUMBER _PINNUM(0, 28) // SDA -#define TX_PIN_NUMBER _PINNUM(0, 3) // SCL -#define CTS_PIN_NUMBER 0 -#define RTS_PIN_NUMBER 0 -#define HWFC false - -// Used as model string in OTA mode +//--------------------------------------------------------------------+ +// BLE OTA +//--------------------------------------------------------------------+ #define BLEDIS_MANUFACTURER "ARAMCON Badge Team" #define BLEDIS_MODEL "ARAMCON Badge 2019" diff --git a/src/boards/arcade_feather_nrf52840_express/board.h b/src/boards/arcade_feather_nrf52840_express/board.h index 738d8a9..aed04ed 100644 --- a/src/boards/arcade_feather_nrf52840_express/board.h +++ b/src/boards/arcade_feather_nrf52840_express/board.h @@ -47,15 +47,6 @@ #define BUTTON_2 _PINNUM(0, 10) #define BUTTON_PULL NRF_GPIO_PIN_PULLUP -/*------------------------------------------------------------------*/ -/* UART (only used by nRF52832) - *------------------------------------------------------------------*/ -#define RX_PIN_NUMBER 8 -#define TX_PIN_NUMBER 6 -#define CTS_PIN_NUMBER 0 -#define RTS_PIN_NUMBER 0 -#define HWFC false - //--------------------------------------------------------------------+ // BLE OTA //--------------------------------------------------------------------+ diff --git a/src/boards/arduino_nano_33_ble/board.h b/src/boards/arduino_nano_33_ble/board.h index ad3420e..00fb2ef 100644 --- a/src/boards/arduino_nano_33_ble/board.h +++ b/src/boards/arduino_nano_33_ble/board.h @@ -42,16 +42,9 @@ #define BUTTON_2 _PINNUM(1, 12) // D3 switch #define BUTTON_PULL NRF_GPIO_PIN_PULLUP -/*------------------------------------------------------------------*/ -/* UART (only used by nRF52832) - *------------------------------------------------------------------*/ -#define RX_PIN_NUMBER _PINNUM(1, 10) -#define TX_PIN_NUMBER _PINNUM(1, 3) -#define CTS_PIN_NUMBER 0 -#define RTS_PIN_NUMBER 0 -#define HWFC false - -// Used as model string in OTA mode +//--------------------------------------------------------------------+ +// BLE OTA +//--------------------------------------------------------------------+ #define BLEDIS_MANUFACTURER "Arduino" #define BLEDIS_MODEL "Nano 33 BLE" diff --git a/src/boards/circuitplayground_nrf52840/board.h b/src/boards/circuitplayground_nrf52840/board.h index 8f58e13..5544502 100644 --- a/src/boards/circuitplayground_nrf52840/board.h +++ b/src/boards/circuitplayground_nrf52840/board.h @@ -46,16 +46,9 @@ #define BUTTON_2 _PINNUM(1, 15) // right button #define BUTTON_PULL NRF_GPIO_PIN_PULLDOWN -/*------------------------------------------------------------------*/ -/* UART (only used by nRF52832) - *------------------------------------------------------------------*/ -#define RX_PIN_NUMBER _PINNUM(0, 30) -#define TX_PIN_NUMBER _PINNUM(0, 14) -#define CTS_PIN_NUMBER 0 -#define RTS_PIN_NUMBER 0 -#define HWFC false - -// Used as model string in OTA mode +//--------------------------------------------------------------------+ +// BLE OTA +//--------------------------------------------------------------------+ #define BLEDIS_MANUFACTURER "Adafruit Industries" #define BLEDIS_MODEL "Circuit Playground nRF52840" diff --git a/src/boards/clue_nrf52840/board.h b/src/boards/clue_nrf52840/board.h index f8d32aa..efba060 100644 --- a/src/boards/clue_nrf52840/board.h +++ b/src/boards/clue_nrf52840/board.h @@ -46,16 +46,9 @@ #define BUTTON_2 _PINNUM(1, 10) // right button #define BUTTON_PULL NRF_GPIO_PIN_PULLUP -/*------------------------------------------------------------------*/ -/* UART (only used by nRF52832) - *------------------------------------------------------------------*/ -#define RX_PIN_NUMBER _PINNUM(0, 09) -#define TX_PIN_NUMBER _PINNUM(0, 10) -#define CTS_PIN_NUMBER 0 -#define RTS_PIN_NUMBER 0 -#define HWFC false - -// Used as model string in OTA mode +//--------------------------------------------------------------------+ +// BLE OTA +//--------------------------------------------------------------------+ #define BLEDIS_MANUFACTURER "Adafruit Industries" #define BLEDIS_MODEL "CLUE nRF52840" diff --git a/src/boards/electronut_labs_papyr/board.h b/src/boards/electronut_labs_papyr/board.h index 408f62a..738dd50 100644 --- a/src/boards/electronut_labs_papyr/board.h +++ b/src/boards/electronut_labs_papyr/board.h @@ -47,19 +47,15 @@ #define BUTTON_2 1 // P0.1 not exposed anywhere, FRST n/a #define BUTTON_PULL NRF_GPIO_PIN_PULLUP -/*------------------------------------------------------------------*/ -/* UART (only used by nRF52832) - *------------------------------------------------------------------*/ -#define RX_PIN_NUMBER 7 -#define TX_PIN_NUMBER 8 -#define CTS_PIN_NUMBER 0 -#define RTS_PIN_NUMBER 0 -#define HWFC false - -// Used as model string in OTA mode +//--------------------------------------------------------------------+ +// BLE OTA +//--------------------------------------------------------------------+ #define BLEDIS_MANUFACTURER "Electronut Labs" #define BLEDIS_MODEL "Papyr" +//--------------------------------------------------------------------+ +// USB +//--------------------------------------------------------------------+ #define UF2_PRODUCT_NAME "Electronut Labs Papyr" #define UF2_BOARD_ID "nRF52840-Papyr-v1" #define UF2_INDEX_URL "https://docs.electronut.in/papyr" diff --git a/src/boards/feather_nrf52832/board.h b/src/boards/feather_nrf52832/board.h index aa35ee3..a31bafa 100644 --- a/src/boards/feather_nrf52832/board.h +++ b/src/boards/feather_nrf52832/board.h @@ -50,7 +50,9 @@ #define RTS_PIN_NUMBER 0 #define HWFC false -// Used as model string in OTA mode +//--------------------------------------------------------------------+ +// BLE OTA +//--------------------------------------------------------------------+ #define BLEDIS_MANUFACTURER "Adafruit Industries" #define BLEDIS_MODEL "Feather nRF52832" diff --git a/src/boards/feather_nrf52840_sense/board.h b/src/boards/feather_nrf52840_sense/board.h index 9d3611b..fbaf9bc 100644 --- a/src/boards/feather_nrf52840_sense/board.h +++ b/src/boards/feather_nrf52840_sense/board.h @@ -47,15 +47,6 @@ #define BUTTON_2 _PINNUM(0, 10) #define BUTTON_PULL NRF_GPIO_PIN_PULLUP -/*------------------------------------------------------------------*/ -/* UART (only used by nRF52832) - *------------------------------------------------------------------*/ -#define RX_PIN_NUMBER 8 -#define TX_PIN_NUMBER 6 -#define CTS_PIN_NUMBER 0 -#define RTS_PIN_NUMBER 0 -#define HWFC false - //--------------------------------------------------------------------+ // BLE OTA //--------------------------------------------------------------------+ diff --git a/src/boards/itsybitsy_nrf52840_express/board.h b/src/boards/itsybitsy_nrf52840_express/board.h index 68ea958..2bc82e0 100644 --- a/src/boards/itsybitsy_nrf52840_express/board.h +++ b/src/boards/itsybitsy_nrf52840_express/board.h @@ -46,16 +46,9 @@ #define BUTTON_2 _PINNUM(1, 02) // D2 breakout #define BUTTON_PULL NRF_GPIO_PIN_PULLUP -/*------------------------------------------------------------------*/ -/* UART (only used by nRF52832) - *------------------------------------------------------------------*/ -#define RX_PIN_NUMBER _PINNUM(0, 25) -#define TX_PIN_NUMBER _PINNUM(0, 24) -#define CTS_PIN_NUMBER 0 -#define RTS_PIN_NUMBER 0 -#define HWFC false - -// Used as model string in OTA mode +//--------------------------------------------------------------------+ +// BLE OTA +//--------------------------------------------------------------------+ #define BLEDIS_MANUFACTURER "Adafruit Industries" #define BLEDIS_MODEL "ItsyBitsy nRF52840 Express" diff --git a/src/boards/mdk_nrf52840_dongle/board.h b/src/boards/mdk_nrf52840_dongle/board.h index 3f3c6a3..c45213e 100644 --- a/src/boards/mdk_nrf52840_dongle/board.h +++ b/src/boards/mdk_nrf52840_dongle/board.h @@ -39,6 +39,7 @@ //#define LED_RGB_GREEN_PIN _PINNUM(0, 22) //#define LED_RGB_BLUE_PIN _PINNUM(0, 24) #define BOARD_RGB_BRIGHTNESS 0x404040 + /*------------------------------------------------------------------*/ /* BUTTON *------------------------------------------------------------------*/ @@ -48,10 +49,15 @@ #define BUTTON_2 _PINNUM(0, 19) // no connection #define BUTTON_PULL NRF_GPIO_PIN_PULLUP -// Used as model string in OTA mode +//--------------------------------------------------------------------+ +// BLE OTA +//--------------------------------------------------------------------+ #define BLEDIS_MANUFACTURER "MakerDiary" #define BLEDIS_MODEL "nRF52840 Micro Dev Kit USB Dongle" +//--------------------------------------------------------------------+ +// USB +//--------------------------------------------------------------------+ #define UF2_PRODUCT_NAME "MDK nRF52840 USB Dongle" #define UF2_VOLUME_LABEL "MDK840DONGL" #define UF2_BOARD_ID "nRF52840-Dongle-v1" diff --git a/src/boards/metro_nrf52840_express/board.h b/src/boards/metro_nrf52840_express/board.h index ef3c8a2..0b47a1c 100644 --- a/src/boards/metro_nrf52840_express/board.h +++ b/src/boards/metro_nrf52840_express/board.h @@ -47,16 +47,9 @@ #define BUTTON_2 _PINNUM(2, 2) #define BUTTON_PULL NRF_GPIO_PIN_PULLUP -/*------------------------------------------------------------------*/ -/* UART (only used by nRF52832) - *------------------------------------------------------------------*/ -#define RX_PIN_NUMBER 8 -#define TX_PIN_NUMBER 6 -#define CTS_PIN_NUMBER 0 -#define RTS_PIN_NUMBER 0 -#define HWFC false - -// Used as model string in OTA mode +//--------------------------------------------------------------------+ +// BLE OTA +//--------------------------------------------------------------------+ #define BLEDIS_MANUFACTURER "Adafruit Industries" #define BLEDIS_MODEL "Metro nRF52840 Express" diff --git a/src/boards/ohs2020_badge/board.h b/src/boards/ohs2020_badge/board.h index 4776999..1a62811 100644 --- a/src/boards/ohs2020_badge/board.h +++ b/src/boards/ohs2020_badge/board.h @@ -43,16 +43,9 @@ #define BUTTON_2 _PINNUM(0, 03) // SW1 #define BUTTON_PULL NRF_GPIO_PIN_PULLUP -/*------------------------------------------------------------------*/ -/* UART (only used by nRF52832) - *------------------------------------------------------------------*/ -#define RX_PIN_NUMBER _PINNUM(0, 09) -#define TX_PIN_NUMBER _PINNUM(0, 10) -#define CTS_PIN_NUMBER 0 -#define RTS_PIN_NUMBER 0 -#define HWFC false - -// Used as model string in OTA mode +//--------------------------------------------------------------------+ +// BLE OTA +//--------------------------------------------------------------------+ #define BLEDIS_MANUFACTURER "OSHWA" #define BLEDIS_MODEL "OHS2020 Badge" diff --git a/src/boards/particle_argon/board.h b/src/boards/particle_argon/board.h index 312bfe3..467a7df 100644 --- a/src/boards/particle_argon/board.h +++ b/src/boards/particle_argon/board.h @@ -47,19 +47,15 @@ #define BUTTON_FRESET _PINNUM(0, 03) // A0 #define BUTTON_PULL NRF_GPIO_PIN_PULLUP -/*------------------------------------------------------------------*/ -/* UART (only used by nRF52832) - *------------------------------------------------------------------*/ -#define RX_PIN_NUMBER 8 -#define TX_PIN_NUMBER 6 -#define CTS_PIN_NUMBER 0 -#define RTS_PIN_NUMBER 0 -#define HWFC false - -// Used as model string in OTA mode +//--------------------------------------------------------------------+ +// BLE OTA +//--------------------------------------------------------------------+ #define BLEDIS_MANUFACTURER "Particle Industries" #define BLEDIS_MODEL "Argon" +//--------------------------------------------------------------------+ +// USB +//--------------------------------------------------------------------+ #define UF2_PRODUCT_NAME "Particle Argon" #define UF2_VOLUME_LABEL "ARGONBOOT " #define UF2_BOARD_ID "nRF52840-Argon-v1" diff --git a/src/boards/particle_boron/board.h b/src/boards/particle_boron/board.h index 7d1d36d..b433cea 100644 --- a/src/boards/particle_boron/board.h +++ b/src/boards/particle_boron/board.h @@ -47,19 +47,15 @@ #define BUTTON_FRESET _PINNUM(0, 03) // A0 #define BUTTON_PULL NRF_GPIO_PIN_PULLUP -/*------------------------------------------------------------------*/ -/* UART (only used by nRF52832) - *------------------------------------------------------------------*/ -#define RX_PIN_NUMBER 8 -#define TX_PIN_NUMBER 6 -#define CTS_PIN_NUMBER 0 -#define RTS_PIN_NUMBER 0 -#define HWFC false - -// Used as model string in OTA mode +//--------------------------------------------------------------------+ +// BLE OTA +//--------------------------------------------------------------------+ #define BLEDIS_MANUFACTURER "Particle Industries" #define BLEDIS_MODEL "Boron" +//--------------------------------------------------------------------+ +// USB +//--------------------------------------------------------------------+ #define UF2_PRODUCT_NAME "Particle Boron" #define UF2_VOLUME_LABEL "BORONBOOT " #define UF2_BOARD_ID "nRF52840-Boron-v1" diff --git a/src/boards/particle_xenon/board.h b/src/boards/particle_xenon/board.h index 3f945b0..5fe2474 100644 --- a/src/boards/particle_xenon/board.h +++ b/src/boards/particle_xenon/board.h @@ -30,14 +30,14 @@ /*------------------------------------------------------------------*/ /* LED *------------------------------------------------------------------*/ - #define LEDS_NUMBER 1 - #define LED_PRIMARY_PIN _PINNUM(1, 12) - #define LED_STATE_ON 1 +#define LEDS_NUMBER 1 +#define LED_PRIMARY_PIN _PINNUM(1, 12) +#define LED_STATE_ON 1 - #define LED_RGB_RED_PIN _PINNUM(0, 13) - #define LED_RGB_GREEN_PIN _PINNUM(0, 14) - #define LED_RGB_BLUE_PIN _PINNUM(0, 15) - #define BOARD_RGB_BRIGHTNESS 0x202020 +#define LED_RGB_RED_PIN _PINNUM(0, 13) +#define LED_RGB_GREEN_PIN _PINNUM(0, 14) +#define LED_RGB_BLUE_PIN _PINNUM(0, 15) +#define BOARD_RGB_BRIGHTNESS 0x202020 /*------------------------------------------------------------------*/ /* BUTTON @@ -47,19 +47,15 @@ #define BUTTON_FRESET _PINNUM(0, 03) // A0 #define BUTTON_PULL NRF_GPIO_PIN_PULLUP -/*------------------------------------------------------------------*/ -/* UART (only used by nRF52832) - *------------------------------------------------------------------*/ -#define RX_PIN_NUMBER 8 -#define TX_PIN_NUMBER 6 -#define CTS_PIN_NUMBER 0 -#define RTS_PIN_NUMBER 0 -#define HWFC false - -// Used as model string in OTA mode +//--------------------------------------------------------------------+ +// BLE OTA +//--------------------------------------------------------------------+ #define BLEDIS_MANUFACTURER "Particle Industries" #define BLEDIS_MODEL "Xenon" +//--------------------------------------------------------------------+ +// USB +//--------------------------------------------------------------------+ #define UF2_PRODUCT_NAME "Particle Xenon" #define UF2_VOLUME_LABEL "XENONBOOT " #define UF2_BOARD_ID "nRF52840-Xenon-v1" diff --git a/src/boards/pca10056/board.h b/src/boards/pca10056/board.h index fbbdee6..a17d597 100644 --- a/src/boards/pca10056/board.h +++ b/src/boards/pca10056/board.h @@ -41,19 +41,15 @@ #define BUTTON_2 12 #define BUTTON_PULL NRF_GPIO_PIN_PULLUP -/*------------------------------------------------------------------*/ -/* UART (only used by nRF52832) - *------------------------------------------------------------------*/ -#define RX_PIN_NUMBER 8 -#define TX_PIN_NUMBER 6 -#define CTS_PIN_NUMBER 0 -#define RTS_PIN_NUMBER 0 -#define HWFC false - -// Used as model string in OTA mode +//--------------------------------------------------------------------+ +// BLE OTA +//--------------------------------------------------------------------+ #define BLEDIS_MANUFACTURER "Nordic" #define BLEDIS_MODEL "PCA10056" +//--------------------------------------------------------------------+ +// USB +//--------------------------------------------------------------------+ #define UF2_PRODUCT_NAME "Nordic nRF52840 DK" #define UF2_BOARD_ID "nRF52840-pca10056-v1" #define UF2_INDEX_URL "https://www.nordicsemi.com/Software-and-Tools/Development-Kits/nRF52840-DK" diff --git a/src/boards/pca10059/board.h b/src/boards/pca10059/board.h index 336b62f..6654b87 100644 --- a/src/boards/pca10059/board.h +++ b/src/boards/pca10059/board.h @@ -44,19 +44,15 @@ #define BUTTON_2 _PINNUM(1, 10) #define BUTTON_PULL NRF_GPIO_PIN_PULLUP -/*------------------------------------------------------------------*/ -/* UART (only used by nRF52832) - *------------------------------------------------------------------*/ -#define RX_PIN_NUMBER 20 -#define TX_PIN_NUMBER 17 -#define CTS_PIN_NUMBER 15 -#define RTS_PIN_NUMBER 13 -#define HWFC false - -// Used as model string in OTA mode +//--------------------------------------------------------------------+ +// BLE OTA +//--------------------------------------------------------------------+ #define BLEDIS_MANUFACTURER "Nordic" #define BLEDIS_MODEL "PCA10059" +//--------------------------------------------------------------------+ +// USB +//--------------------------------------------------------------------+ #define UF2_PRODUCT_NAME "Nordic nRF52840 Dongle" #define UF2_BOARD_ID "nRF52840-pca10059-v1" #define UF2_INDEX_URL "https://www.nordicsemi.com/Software-and-Tools/Development-Kits/nRF52840-Dongle" diff --git a/src/boards/pca10100/board.h b/src/boards/pca10100/board.h index 4e7dfbe..d82e2d1 100644 --- a/src/boards/pca10100/board.h +++ b/src/boards/pca10100/board.h @@ -41,19 +41,15 @@ #define BUTTON_2 12 #define BUTTON_PULL NRF_GPIO_PIN_PULLUP -/*------------------------------------------------------------------*/ -/* UART (only used by nRF52832) - *------------------------------------------------------------------*/ -#define RX_PIN_NUMBER 8 -#define TX_PIN_NUMBER 6 -#define CTS_PIN_NUMBER 0 -#define RTS_PIN_NUMBER 0 -#define HWFC false - -// Used as model string in OTA mode +//--------------------------------------------------------------------+ +// BLE OTA +//--------------------------------------------------------------------+ #define BLEDIS_MANUFACTURER "Nordic" #define BLEDIS_MODEL "PCA10100" +//--------------------------------------------------------------------+ +// USB +//--------------------------------------------------------------------+ #define UF2_PRODUCT_NAME "Nordic nRF52833 DK" #define UF2_BOARD_ID "nRF52833-pca10100-v1" #define UF2_INDEX_URL "https://www.nordicsemi.com/Software-and-Tools/Development-Kits/nRF52833-DK" diff --git a/src/boards/raytac_mdbt50q_rx/board.h b/src/boards/raytac_mdbt50q_rx/board.h index fb4bdef..18b7c90 100644 --- a/src/boards/raytac_mdbt50q_rx/board.h +++ b/src/boards/raytac_mdbt50q_rx/board.h @@ -43,15 +43,6 @@ #define BUTTON_2 _PINNUM(0, 10) #define BUTTON_PULL NRF_GPIO_PIN_PULLUP -/*------------------------------------------------------------------*/ -/* UART (only used by nRF52832) (only used by nRF52832) - *------------------------------------------------------------------*/ -#define RX_PIN_NUMBER 8 -#define TX_PIN_NUMBER 6 -#define CTS_PIN_NUMBER 0 -#define RTS_PIN_NUMBER 0 -#define HWFC false - //--------------------------------------------------------------------+ // BLE OTA //--------------------------------------------------------------------+ diff --git a/src/boards/waveshare_nrf52840_eval/board.h b/src/boards/waveshare_nrf52840_eval/board.h index d4f0efc..27991df 100644 --- a/src/boards/waveshare_nrf52840_eval/board.h +++ b/src/boards/waveshare_nrf52840_eval/board.h @@ -50,15 +50,6 @@ #define BUTTON_2 _PINNUM(0, 29) #define BUTTON_PULL NRF_GPIO_PIN_PULLUP -/*------------------------------------------------------------------*/ -/* UART (only used by nRF52832) - *------------------------------------------------------------------*/ -#define RX_PIN_NUMBER 8 -#define TX_PIN_NUMBER 6 -#define CTS_PIN_NUMBER 0 -#define RTS_PIN_NUMBER 0 -#define HWFC false - //--------------------------------------------------------------------+ // BLE OTA //--------------------------------------------------------------------+ diff --git a/src/nrfx_config.h b/src/nrfx_config.h index 419dc19..b626e00 100644 --- a/src/nrfx_config.h +++ b/src/nrfx_config.h @@ -6,11 +6,19 @@ #define NRFX_POWER_DEFAULT_CONFIG_IRQ_PRIORITY 7 #define NRFX_CLOCK_ENABLED 0 - #define NRFX_NVMC_ENABLED 1 +#define NRFX_PRS_ENABLED 0 + +#define NRFX_PWM_ENABLED 0 +#define NRFX_PWM0_ENABLED 0 +#define NRFX_PWM1_ENABLED 0 +#define NRFX_PWM2_ENABLED 0 +#define NRFX_PWM3_ENABLED 0 + // UART #ifdef NRF52832_XXAA + #define NRFX_UART_ENABLED 1 #define NRFX_UART0_ENABLED 1 diff --git a/src/sdk_config.h b/src/sdk_config.h index 51d9e92..50e8196 100644 --- a/src/sdk_config.h +++ b/src/sdk_config.h @@ -78,10 +78,12 @@ #define UART_DEFAULT_CONFIG_IRQ_PRIORITY 7 #define UART_EASY_DMA_SUPPORT 1 #define UART_LEGACY_SUPPORT 1 +#define UART_CONFIG_LOG_ENABLED 0 #define UART0_ENABLED 1 #define UART0_CONFIG_USE_EASY_DMA 0 + //========================================================== // APP_UART_ENABLED - app_uart - UART driver //========================================================== diff --git a/src/segger/Adafruit_nRF52_Bootloader.emProject b/src/segger/Adafruit_nRF52_Bootloader.emProject deleted file mode 100644 index 204a77e..0000000 --- a/src/segger/Adafruit_nRF52_Bootloader.emProject +++ /dev/null @@ -1,398 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/usb/uf2/compile_date.h b/src/usb/uf2/compile_date.h index d16f4ad..7175c83 100644 --- a/src/usb/uf2/compile_date.h +++ b/src/usb/uf2/compile_date.h @@ -50,22 +50,22 @@ SOFTWARE. : 12u /*Dec*/ ) #define __DAY_INT__ ( \ - (__DATE__ [4u] == ' ' ? 0u : __DATE__ [4u] - '0') * 10u \ + (__DATE__ [4u] == ' ' ? 0 : __DATE__ [4u] - '0') * 10u \ + (__DATE__ [5u] - '0') ) // __TIME__ expands to an eight-character string constant // "23:59:01", or (if cannot determine time) "??:??:??" #define __HOUR_INT__ ( \ - (__TIME__ [0u] == '?' ? 0u : __TIME__ [0u] - '0') * 10u \ - + (__TIME__ [1u] == '?' ? 0u : __TIME__ [1u] - '0') ) + (__TIME__ [0u] == '?' ? 0 : __TIME__ [0u] - '0') * 10u \ + + (__TIME__ [1u] == '?' ? 0 : __TIME__ [1u] - '0') ) #define __MINUTE_INT__ ( \ - (__TIME__ [3u] == '?' ? 0u : __TIME__ [3u] - '0') * 10u \ - + (__TIME__ [4u] == '?' ? 0u : __TIME__ [4u] - '0') ) + (__TIME__ [3u] == '?' ? 0 : __TIME__ [3u] - '0') * 10u \ + + (__TIME__ [4u] == '?' ? 0 : __TIME__ [4u] - '0') ) #define __SECONDS_INT__ ( \ - (__TIME__ [6u] == '?' ? 0u : __TIME__ [6u] - '0') * 10u \ - + (__TIME__ [7u] == '?' ? 0u : __TIME__ [7u] - '0') ) + (__TIME__ [6u] == '?' ? 0 : __TIME__ [6u] - '0') * 10u \ + + (__TIME__ [7u] == '?' ? 0 : __TIME__ [7u] - '0') ) #define __DOSDATE__ ( \ diff --git a/src/usb/uf2/ghostfat.c b/src/usb/uf2/ghostfat.c index 6f13980..e6a2c67 100644 --- a/src/usb/uf2/ghostfat.c +++ b/src/usb/uf2/ghostfat.c @@ -128,7 +128,7 @@ static FAT_BootBlock const BootBlock = { .SectorsPerFAT = SECTORS_PER_FAT, .SectorsPerTrack = 1, .Heads = 1, - .PhysicalDriveNum = 0x80, // to match MediaDescriptor of 0xF8 + .PhysicalDriveNum = 0x80, // to match MediaDescriptor of 0xF8 .ExtendedBootSig = 0x29, .VolumeSerialNumber = 0x00420042, .VolumeLabel = UF2_VOLUME_LABEL, @@ -172,6 +172,7 @@ static uint32_t current_flash_size(void) result = FLASH_SIZE; } } + flash_sz = result; // presumes atomic 32-bit read/write and static result } @@ -180,13 +181,14 @@ static uint32_t current_flash_size(void) void padded_memcpy (char *dst, char const *src, int len) { - for (int i = 0; i < len; ++i) { - if (*src) - *dst = *src++; - else - *dst = ' '; - dst++; - } + for ( int i = 0; i < len; ++i ) + { + if ( *src ) + *dst = *src++; + else + *dst = ' '; + dst++; + } } @@ -212,11 +214,11 @@ void read_block(uint32_t block_no, uint8_t *data) { // WARNING -- code presumes only one NULL .content for .UF2 file // and all non-NULL .content fit in one sector // and requires it be the last element of the array - for (int i = 1; i < NUM_FILES * 2 + 4; ++i) { + for (uint32_t i = 1; i < NUM_FILES * 2 + 4; ++i) { data[i] = 0xff; } } - for (int i = 0; i < 256; ++i) { // Generate the FAT chain for the firmware "file" + for (uint32_t i = 0; i < 256; ++i) { // Generate the FAT chain for the firmware "file" uint32_t v = sectionIdx * 256 + i; if (UF2_FIRST_SECTOR <= v && v <= UF2_LAST_SECTOR) ((uint16_t *)(void *)data)[i] = v == UF2_LAST_SECTOR ? 0xffff : v + 1; @@ -235,7 +237,7 @@ void read_block(uint32_t block_no, uint8_t *data) { remainingEntries--; } - for (int i = DIRENTRIES_PER_SECTOR * sectionIdx; + for (uint32_t i = DIRENTRIES_PER_SECTOR * sectionIdx; remainingEntries > 0 && i < NUM_FILES; i++, d++) { @@ -309,10 +311,7 @@ int write_block(uint32_t block_no, uint8_t *data, bool quiet, WriteState *state) if ((bl->flags & UF2_FLAG_NOFLASH) || bl->payloadSize > 256 || (bl->targetAddr & 0xff) || bl->targetAddr < USER_FLASH_START || bl->targetAddr + bl->payloadSize > USER_FLASH_END) { -#if USE_DBG_MSC - if (!quiet) - logval("invalid target addr", bl->targetAddr); -#endif + NRF_LOG_WARNING("Skip block at %x", bl->targetAddr); // this happens when we're trying to re-flash CURRENT.UF2 file previously // copied from a device; we still want to count these blocks to reset properly diff --git a/src/usb/usb_desc.c b/src/usb/usb_desc.c index d54b2b8..5d7779b 100644 --- a/src/usb/usb_desc.c +++ b/src/usb/usb_desc.c @@ -150,8 +150,10 @@ static uint16_t _desc_str[64+1]; // Invoked when received GET STRING DESCRIPTOR request // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete -uint16_t const* tud_descriptor_string_cb(uint8_t index) +uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid) { + (void) langid; + uint8_t chr_count; if ( index == 0) diff --git a/tools/build_all.py b/tools/build_all.py index 8dd3680..24640f5 100644 --- a/tools/build_all.py +++ b/tools/build_all.py @@ -1,33 +1,32 @@ import os import shutil +import glob import sys import subprocess import time -subprocess.run("rm -rf _build*", shell=True) -subprocess.run("rm -rf bin/*", shell=True) - -travis = False -if "TRAVIS" in os.environ and os.environ["TRAVIS"] == "true": - travis = True +subprocess.run("rm -rf _build/", shell=True) +subprocess.run("rm -rf bin/", shell=True) success_count = 0 fail_count = 0 exit_status = 0 -build_format = '| {:30} | {:9} ' -build_separator = '-' * 54 +build_format = '| {:32} | {:9} | {:5} | {:6} | {:6} |' +build_separator = '-' * 74 +# All supported boards all_boards = [] for entry in os.scandir("src/boards"): all_boards.append(entry.name) +all_boards.sort() #sha, version = build_info.get_version_info() total_time = time.monotonic() print(build_separator) -print((build_format + '| {:5} |').format('Board', 'Result', 'Time')) +print(build_format.format('Board', 'Result', 'Time', 'Flash', 'SRAM')) print(build_separator) for board in all_boards: @@ -35,31 +34,35 @@ for board in all_boards: os.makedirs(bin_directory, exist_ok=True) start_time = time.monotonic() - make_result = subprocess.run("make -j 4 BOARD={} combinehex genpkg".format(board), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + make_result = subprocess.run("make -j 4 BOARD={} all genpkg".format(board), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) build_duration = time.monotonic() - start_time + flash_size = "-" + sram_size = "-" + if make_result.returncode == 0: success = "\033[32msucceeded\033[0m" success_count += 1 + + out_file = glob.glob('_build/build-{}/*.out'.format(board))[0] + size_output = subprocess.run('size {}'.format(out_file), shell=True, stdout=subprocess.PIPE).stdout.decode("utf-8") + size_list = size_output.split('\n')[1].split('\t') + flash_size = int(size_list[0]) + sram_size = int(size_list[1]) + int(size_list[2]) else: exit_status = make_result.returncode success = "\033[31mfailed\033[0m " fail_count += 1 - for entry in os.scandir("_build-{}".format(board)): + for entry in os.scandir("_build/build-{}".format(board)): for extension in ["zip", "hex"]: if entry.name.endswith(extension) and "nosd" not in entry.name: shutil.copy(entry.path, bin_directory) - if travis: - print('travis_fold:start:build-{}\\r'.format(board)) - - print((build_format + '| {:.2f}s |').format(board, success, build_duration)) + print(build_format.format(board, success, "{:.2f}s".format(build_duration), flash_size, sram_size)) if make_result.returncode != 0: print(make_result.stdout.decode("utf-8")) - if travis: - print('travis_fold:end:build-{}\\r'.format(board)) # Build Summary total_time = time.monotonic() - total_time diff --git a/tools/upload_release_files.py b/tools/upload_release_files.py index 35abd50..b7ee02c 100755 --- a/tools/upload_release_files.py +++ b/tools/upload_release_files.py @@ -4,32 +4,31 @@ import os import os.path import sys import uritemplate +import glob sys.path.append("adabot") import github_requests as github exit_status = 0 -for dirpath, dirnames, filenames in os.walk("../bin"): - if not filenames: - continue - for filename in filenames: - full_filename = os.path.join(dirpath, filename) - label = filename.replace("adafruit-circuitpython-", "") - url_vars = {} - url_vars["name"] = filename - url_vars["label"] = label - url = uritemplate.expand(os.environ["UPLOAD_URL"], url_vars) - headers = {"content-type": "application/octet-stream"} - print(url) - with open(full_filename, "rb") as f: - response = github.post(url, data=f, headers=headers) - if not response.ok: - if response.status_code == 422 and response.json().get("errors", [{"code":""}])[0]["code"] == "already_exists": - print("File already uploaded. Skipping.") - continue - print("Upload of {} failed with {}.".format(filename, response.status_code)) - print(response.text) - sys.exit(response.status_code) +filepaths = list(glob.iglob('../bin/*/*', recursive=True)) +filepaths.sort() + +for full_filename in filepaths: + filename = os.path.basename(full_filename) + url_vars = {} + url_vars["name"] = filename + url = uritemplate.expand(os.environ["UPLOAD_URL"], url_vars) + headers = {"content-type": "application/octet-stream"} + print(url) + with open(full_filename, "rb") as f: + response = github.post(url, data=f, headers=headers) + if not response.ok: + if response.status_code == 422 and response.json().get("errors", [{"code":""}])[0]["code"] == "already_exists": + print("File already uploaded. Skipping.") + continue + print("Upload of {} failed with {}.".format(filename, response.status_code)) + print(response.text) + sys.exit(response.status_code) sys.exit(exit_status)