From b7dd33b76edee78e5da8ea856f4fcac3d44e254a Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 21 Aug 2018 15:15:44 +0700 Subject: [PATCH] add touch 1200 for dfu-flash --- lib/tinyusb | 2 +- ports/nrf/Makefile | 10 ++++++---- ports/nrf/usb/usb.c | 25 +++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/tinyusb b/lib/tinyusb index 6d96b12e27..7b35cd0203 160000 --- a/lib/tinyusb +++ b/lib/tinyusb @@ -1 +1 @@ -Subproject commit 6d96b12e27ae60ca500365ee2137105145ada9dd +Subproject commit 7b35cd0203bc409d7c1aefc075672103cb4a913e diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index b62eff3dad..2ab44dbd07 100755 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -314,10 +314,12 @@ endif ##################### .phony: dfu-gen dfu-flash dfu-bootloader -ifeq ($(OS),Windows_NT) - NRFUTIL = ../../lib/nrfutil/binaries/win32/nrfutil.exe +NRFUTIL = adafruit-nrfutil + +ifeq ($(MCU_SUB_VARIANT),nrf52840) + DFU_TOUCH = --touch 1200 else - NRFUTIL = nrfutil + DFU_TOUCH = endif check_defined = \ @@ -332,7 +334,7 @@ dfu-gen: $(BUILD)/$(OUTPUT_FILENAME).hex dfu-flash: $(BUILD)/dfu-package.zip @:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyUSB0) - $(NRFUTIL) --verbose dfu serial --package $^ -p $(SERIAL) -b 115200 --singlebank + $(NRFUTIL) --verbose dfu serial --package $^ -p $(SERIAL) -b 115200 --singlebank $(DFU_TOUCH) dfu-bootloader: @:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyACM0) diff --git a/ports/nrf/usb/usb.c b/ports/nrf/usb/usb.c index 953b61e396..7168cf53d1 100644 --- a/ports/nrf/usb/usb.c +++ b/ports/nrf/usb/usb.c @@ -94,9 +94,12 @@ void usb_init(void) { //--------------------------------------------------------------------+ // tinyusb callbacks //--------------------------------------------------------------------+ + +// Invoked when device is mounted void tud_mount_cb(void) { } +// Invoked when device is unmounted void tud_umount_cb(void) { } @@ -107,6 +110,28 @@ uint32_t tusb_hal_millis(void) { return (uint32_t) ms; } + +// Invoked when cdc when line state changed e.g connected/disconnected +// Use to reset to DFU when disconnect with 1200 bps +void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) { + (void) itf; // interface ID, not used + + // disconnected event + if ( !dtr && !rts ) + { + cdc_line_coding_t coding; + tud_cdc_get_line_coding(&coding); + + if ( coding.bit_rate == 1200 ) + { + enum { DFU_MAGIC_SERIAL = 0x4e }; + + NRF_POWER->GPREGRET = DFU_MAGIC_SERIAL; + NVIC_SystemReset(); + } + } +} + #if MICROPY_KBD_EXCEPTION /**