From bd1a3910f10db349dd492c50495c73ed677cd217 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Wed, 4 Apr 2018 11:56:57 +0200 Subject: [PATCH] [ion] USB::Device::Calculator::PollAndReset Change-Id: Ib847b5f8a0fd559f98c1c130c0b0daffaf4c6468 --- ion/src/device/boot/dfu.ld | 2 +- ion/src/device/usb/calculator.cpp | 4 +++- ion/src/device/usb/calculator.h | 2 +- ion/src/device/usb_dfu_relocated_ram.cpp | 13 ++++++++++--- ion/src/device/usb_dfu_xip_flash.cpp | 5 ++++- 5 files changed, 19 insertions(+), 7 deletions(-) diff --git a/ion/src/device/boot/dfu.ld b/ion/src/device/boot/dfu.ld index 470b7ebd9..12b71c55d 100644 --- a/ion/src/device/boot/dfu.ld +++ b/ion/src/device/boot/dfu.ld @@ -28,7 +28,7 @@ MEMORY { SECTIONS { .text : { . = ALIGN(4); - KEEP(*(.text._ZN3Ion3USB6Device10Calculator4PollEv)) + KEEP(*(.text._ZN3Ion3USB6Device10Calculator12PollAndResetEv)) *(.text) *(.text.*) } >RAM_BUFFER diff --git a/ion/src/device/usb/calculator.cpp b/ion/src/device/usb/calculator.cpp index 55944cea2..3ab07250f 100644 --- a/ion/src/device/usb/calculator.cpp +++ b/ion/src/device/usb/calculator.cpp @@ -7,7 +7,7 @@ namespace Ion { namespace USB { namespace Device { -void Calculator::Poll() { +bool Calculator::PollAndReset() { Calculator c; /* Leave DFU mode if the Back key is pressed, the calculator unplugged or the @@ -24,6 +24,8 @@ void Calculator::Poll() { if (!c.isSoftDisconnected()) { c.detach(); } + + return false; } Descriptor * Calculator::descriptor(uint8_t type, uint8_t index) { diff --git a/ion/src/device/usb/calculator.h b/ion/src/device/usb/calculator.h index 750182713..47fbb15a8 100644 --- a/ion/src/device/usb/calculator.h +++ b/ion/src/device/usb/calculator.h @@ -24,7 +24,7 @@ namespace Device { class Calculator : public Device { public: - static void Poll(); + static bool PollAndReset(); // Return true if reset is needed Calculator() : Device(&m_dfuInterface), m_deviceDescriptor( diff --git a/ion/src/device/usb_dfu_relocated_ram.cpp b/ion/src/device/usb_dfu_relocated_ram.cpp index 98376d348..1fe2795f2 100644 --- a/ion/src/device/usb_dfu_relocated_ram.cpp +++ b/ion/src/device/usb_dfu_relocated_ram.cpp @@ -10,7 +10,7 @@ extern char _dfu_bootloader_flash_end; namespace Ion { namespace USB { -typedef void (*FunctionPointer)(); +typedef bool (*PollFunctionPointer)(void); void DFU() { @@ -51,7 +51,7 @@ void DFU() { /* 4- Jump to DFU bootloader code. We made sure in the linker script that the * first function we want to call is at the beginning of the DFU code. */ - FunctionPointer dfu_bootloader_entry = reinterpret_cast(dfu_bootloader_ram_start); + PollFunctionPointer dfu_bootloader_entry = reinterpret_cast(dfu_bootloader_ram_start); /* To have the right debug symbols for the reallocated code, break here and: * - Get the address of the new .text section @@ -62,7 +62,14 @@ void DFU() { * add-symbol-file ion/src/device/usb/dfu.elf 0x20038000 */ - dfu_bootloader_entry(); + if (dfu_bootloader_entry()) { + /* We don't perform a core reset because at this point in time the USB cable + * is most likely plugged in. Doing a full core reset would be the clean + * thing to do but would therefore result in the device entering the ROMed + * DFU bootloader, which we want to avoid. By performing a jump-reset, we + * will enter the newly flashed firmware. */ + Ion::Device::jumpReset(); + } /* 5- That's all. The DFU bootloader on the stack is now dead code that will * be overwritten when the stack grows. */ diff --git a/ion/src/device/usb_dfu_xip_flash.cpp b/ion/src/device/usb_dfu_xip_flash.cpp index ffde4e5b6..59fd0b1f0 100644 --- a/ion/src/device/usb_dfu_xip_flash.cpp +++ b/ion/src/device/usb_dfu_xip_flash.cpp @@ -1,10 +1,13 @@ #include "usb/calculator.h" +#include namespace Ion { namespace USB { void DFU() { - Ion::USB::Device::Calculator::Poll(); + if (Ion::USB::Device::Calculator::PollAndReset()) { + Ion::Device::jumpReset(); + } } }