]> git.donarmstrong.com Git - kiibohd-controller.git/commitdiff
Fixing device bricking bug
authorJacob Alexander <haata@kiibohd.com>
Sun, 11 Jan 2015 03:53:46 +0000 (19:53 -0800)
committerJacob Alexander <haata@kiibohd.com>
Sun, 11 Jan 2015 03:53:46 +0000 (19:53 -0800)
- When loading a garbage image onto the device, the chip hangs
  and refuses to jump to the bootloader and start dfu

Fix
- Check if chip was in locked state and jump to dfu
- Check if watchdog timer fired and jump to dfu

Bootloader/CMakeLists.txt
Bootloader/flash.c
Lib/mk20dx.c

index ebe5ff4603a99351e1d7460d977ca6ca7ec4590d..704115db663a92adea330460918d10f087d23e60 100644 (file)
 #| You _MUST_ clean the build directory if you change this value
 #|
 set( CHIP
-       "mk20dx128vlf5"    # McHCK    mk20dx128vlf5
-#      "mk20dx256vlh7"    # Kiibohd-dfu
+       "mk20dx128vlf5"    # McHCK       mk20dx128vlf5
+#      "mk20dx256vlh7"    # Kiibohd-dfu mk20dx256vlh7
 )
 
 
+
+###
+# Compiler Selection
+#
+
+#| *** EXPERIMENTAL ***
+#| Stick with gcc unless you know what you're doing
+#| Currently only arm is supported with clang
+set( COMPILER
+       "gcc"   # arm-none-eabi-gcc / avr-gcc - Default
+#      "clang" # arm-none-eabi
+       CACHE STRING "Compiler Type" )
+
+
+
 ###
 # Bootloader Configuration
 #
index 9ae6dae704052363c3f70f4eab065e5d30aac06b..db2ef8fbc30079d0b52265332d2ecb046d806f6f 100644 (file)
@@ -68,7 +68,6 @@ int flash_erase_sector(uintptr_t addr)
 
 int flash_program_section(uintptr_t addr, size_t num_words)
 {
-       GPIOA_PSOR |= (1<<19);
         FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
         FTFL.fccob.program_section.addr = addr;
         FTFL.fccob.program_section.num_words = num_words;
index 9b319495a46bc00109c12c5a3a6794cd6753ffc5..d7a8cdef89885064f17740b4b41a4d0a4c203a8f 100644 (file)
@@ -1,7 +1,7 @@
 /* Teensyduino Core Library
  * http://www.pjrc.com/teensy/
  * Copyright (c) 2013 PJRC.COM, LLC.
- * Modifications by Jacob Alexander 2014
+ * Modifications by Jacob Alexander 2014-2015
  *
  * Permission is hereby granted, free of charge, to any person obtaining
  * a copy of this software and associated documentation files (the
@@ -29,6 +29,8 @@
  * SOFTWARE.
  */
 
+// ----- Includes -----
+
 // Local Includes
 #include "mk20dx.h"
 
@@ -246,7 +248,7 @@ void (* const gVectors[])() =
        portd_isr,                                      // 59 Pin detect (Port D)
        porte_isr,                                      // 60 Pin detect (Port E)
        software_isr,                                   // 61 Software interrupt
-#elif defined(_mk20dx256_)
+#elif defined(_mk20dx256_) || defined(_mk20dx256vlh7_)
        dma_ch0_isr,                                    // 16 DMA channel 0 transfer complete
        dma_ch1_isr,                                    // 17 DMA channel 1 transfer complete
        dma_ch2_isr,                                    // 18 DMA channel 2 transfer complete
@@ -404,7 +406,7 @@ const uint8_t flashconfigbytes[16] = {
 
 // ----- Functions -----
 
-#if defined(_mk20dx128vlf5_) && defined(_bootloader_) // Bootloader Section
+#if ( defined(_mk20dx128vlf5_) || defined(_mk20dx256vlh7_) ) && defined(_bootloader_) // Bootloader Section
 __attribute__((noreturn))
 static inline void jump_to_app( uintptr_t addr )
 {
@@ -453,18 +455,20 @@ void *memcpy( void *dst, const void *src, unsigned int len )
 __attribute__ ((section(".startup")))
 void ResetHandler()
 {
-       // Disable Watchdog
-       WDOG_UNLOCK = WDOG_UNLOCK_SEQ1;
-       WDOG_UNLOCK = WDOG_UNLOCK_SEQ2;
-       WDOG_STCTRLH = WDOG_STCTRLH_ALLOWUPDATE;
-
-#if defined(_mk20dx128vlf5_) && defined(_bootloader_) // Bootloader Section
+#if ( defined(_mk20dx128vlf5_) || defined(_mk20dx256vlh7_) ) && defined(_bootloader_) // Bootloader Section
        extern uint32_t _app_rom;
 
        // We treat _app_rom as pointer to directly read the stack
        // pointer and check for valid app code.  This is no fool
        // proof method, but it should help for the first flash.
-       if ( RCM_SRS0 & 0x40 || _app_rom == 0xffffffff ||
+       //
+       // Purposefully disabling the watchdog *after* the reset check this way
+       // if the chip goes into an odd state we'll reset to the bootloader (invalid firmware image)
+       // RCM_SRS0 & 0x20
+       //
+       // Also checking for ARM lock-up signal (invalid firmware image)
+       // RCM_SRS1 & 0x02
+       if ( RCM_SRS0 & 0x40 || RCM_SRS0 & 0x20 || RCM_SRS1 & 0x02 || _app_rom == 0xffffffff ||
          memcmp( (uint8_t*)&VBAT, sys_reset_to_loader_magic, sizeof(sys_reset_to_loader_magic) ) == 0 ) // Check for soft reload
        {
                memset( (uint8_t*)&VBAT, 0, sizeof(VBAT) );
@@ -476,6 +480,10 @@ void ResetHandler()
                jump_to_app( addr );
        }
 #endif
+       // Disable Watchdog
+       WDOG_UNLOCK = WDOG_UNLOCK_SEQ1;
+       WDOG_UNLOCK = WDOG_UNLOCK_SEQ2;
+       WDOG_STCTRLH = WDOG_STCTRLH_ALLOWUPDATE;
 
        uint32_t *src = (uint32_t*)&_etext;
        uint32_t *dest = (uint32_t*)&_sdata;
@@ -510,7 +518,7 @@ void ResetHandler()
        dest = (uint32_t*)&_sbss;
        while ( dest < (uint32_t*)&_ebss ) *dest++ = 0;
 
-// MCHCK
+// MCHCK / Kiibohd-dfu
 #if defined(_mk20dx128vlf5_)
        // Default all interrupts to medium priority level
        for ( unsigned int i = 0; i < NVIC_NUM_INTERRUPTS; i++ )
@@ -524,14 +532,12 @@ void ResetHandler()
        // USB Clock and FLL select
        SIM_SOPT2 = SIM_SOPT2_USBSRC | SIM_SOPT2_TRACECLKSEL;
 
-// Teensy 3.0 and 3.1
+// Teensy 3.0 and 3.1 and Kiibohd-dfu (mk20dx256vlh7)
 #else
-       unsigned int i;
-
        SCB_VTOR = 0;   // use vector table in flash
 
        // default all interrupts to medium priority level
-       for ( i = 0; i < NVIC_NUM_INTERRUPTS; i++ )
+       for ( unsigned int i = 0; i < NVIC_NUM_INTERRUPTS; i++ )
        {
                NVIC_SET_PRIORITY( i, 128 );
        }