From 6c60af4ba8b1b16548580de93cd45f02e69e8faf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Sun, 4 Jun 2017 23:32:48 +0200 Subject: [PATCH] Preserve state across resets by putting Wormhole to the newly added .noinit section. Rename The jump to bootloader USB command to reenumerate and generalize accordingly by handling the passed reenumeration mode. --- right/build/kds/MK22FN512xxx12_flash.ld | 21 +++++++++++++++++++-- right/src/main.c | 10 +++------- right/src/usb_composite_device.c | 2 +- right/src/usb_protocol_handler.c | 11 ++++++----- right/src/usb_protocol_handler.h | 2 +- right/src/wormhole.c | 2 +- right/src/wormhole.h | 2 +- 7 files changed, 32 insertions(+), 18 deletions(-) diff --git a/right/build/kds/MK22FN512xxx12_flash.ld b/right/build/kds/MK22FN512xxx12_flash.ld index 7249aa7..7226037 100644 --- a/right/build/kds/MK22FN512xxx12_flash.ld +++ b/right/build/kds/MK22FN512xxx12_flash.ld @@ -61,7 +61,8 @@ MEMORY m_interrupts (RX) : ORIGIN = 0x0000c000, LENGTH = 0x00000400 m_text (RX) : ORIGIN = 0x0000c400, LENGTH = 0x00073BF0 /* SGF Linker adjusted for app */ m_data (RW) : ORIGIN = 0x1FFF0000, LENGTH = 0x00010000 - m_data_2 (RW) : ORIGIN = 0x20000000, LENGTH = 0x00010000 + m_data_2 (RW) : ORIGIN = 0x20000000, LENGTH = 0x0000FF00 + m_noinit (RW) : ORIGIN = 0x2000FF00, LENGTH = 0x000000FF } /* Define output sections */ @@ -250,5 +251,21 @@ SECTIONS .ARM.attributes 0 : { *(.ARM.attributes) } ASSERT(__StackLimit >= __HeapLimit, "region m_data_2 overflowed with stack and heap") -} + .noinit (NOLOAD): + { + . = ALIGN(4); + _noinit = .; + + *(.noinit .noinit.*) + + . = ALIGN(4) ; + _end_noinit = .; + } > m_noinit + + /* Mandatory to be word aligned, _sbrk assumes this */ + PROVIDE ( end = _end_noinit ); /* was _ebss */ + PROVIDE ( _end = _end_noinit ); + PROVIDE ( __end = _end_noinit ); + PROVIDE ( __end__ = _end_noinit ); +} diff --git a/right/src/main.c b/right/src/main.c index 964f108..d992c96 100644 --- a/right/src/main.c +++ b/right/src/main.c @@ -72,16 +72,12 @@ void UpdateUsbReports() IsUsbBasicKeyboardReportSent = false; } -wormhole_t wormhole; - void main() { InitPeripherials(); InitClock(); - Wormhole = &wormhole; // TODO: Remove this as soon as the wormhole stabilizes. - Wormhole->enumerationMode = EnumerationMode_NormalKeyboard; -// Wormhole->enumerationMode = EnumerationMode_BusPal; - if (/*Wormhole->magicNumber == WORMHOLE_MAGIC_NUMBER && */ Wormhole->enumerationMode == EnumerationMode_BusPal) { - //Wormhole->magicNumber = 0; + + if (Wormhole.magicNumber == WORMHOLE_MAGIC_NUMBER && Wormhole.enumerationMode == EnumerationMode_BusPal) { + Wormhole.magicNumber = 0; init_hardware(); handleUsbBusPalCommand(); } else { diff --git a/right/src/usb_composite_device.c b/right/src/usb_composite_device.c index ce3617b..785b1f1 100644 --- a/right/src/usb_composite_device.c +++ b/right/src/usb_composite_device.c @@ -101,7 +101,7 @@ static usb_status_t UsbDeviceCallback(usb_device_handle handle, uint32_t event, void USB0_IRQHandler(void) { - USB_DeviceKhciIsrFunction(Wormhole->enumerationMode == EnumerationMode_BusPal + USB_DeviceKhciIsrFunction(Wormhole.enumerationMode == EnumerationMode_BusPal ? BuspalCompositeUsbDevice.device_handle : UsbCompositeDevice.deviceHandle); } diff --git a/right/src/usb_protocol_handler.c b/right/src/usb_protocol_handler.c index 2bee9f9..89d2aa4 100644 --- a/right/src/usb_protocol_handler.c +++ b/right/src/usb_protocol_handler.c @@ -16,7 +16,7 @@ void setError(uint8_t error); void setGenericError(); void usbProtocolHandler(); void getSystemProperty(); -void jumpToBootloader(); +void reenumerate(); void setTestLed(); void writeLedDriver(); void readLedDriver(); @@ -55,8 +55,8 @@ void usbProtocolHandler() case USB_COMMAND_GET_SYSTEM_PROPERTY: getSystemProperty(); break; - case USB_COMMAND_JUMP_TO_BOOTLOADER: - jumpToBootloader(); + case USB_COMMAND_REENUMERATE: + reenumerate(); break; case USB_COMMAND_SET_TEST_LED: setTestLed(); @@ -114,8 +114,9 @@ void getSystemProperty() { } } -void jumpToBootloader() { -// Wormhole->magicNumber = WORMHOLE_MAGIC_NUMBER; +void reenumerate() { + Wormhole.magicNumber = WORMHOLE_MAGIC_NUMBER; + Wormhole.enumerationMode = GenericHidInBuffer[1]; SCB->AIRCR = 0x5FA<