# HG changeset patch # User Jacob Alexander # Date 1420948426 28800 # Node ID 596d8e300a3732b6d16c22d48d2f5318b78dff24 # Parent 708af1b9bc5524add4bd8fb1a3b2c87269250926 Fixing device bricking bug - 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 diff -r 708af1b9bc55 -r 596d8e300a37 Bootloader/CMakeLists.txt --- a/Bootloader/CMakeLists.txt Wed Dec 31 19:43:10 2014 -0800 +++ b/Bootloader/CMakeLists.txt Sat Jan 10 19:53:46 2015 -0800 @@ -20,11 +20,26 @@ #| 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 # diff -r 708af1b9bc55 -r 596d8e300a37 Bootloader/flash.c --- a/Bootloader/flash.c Wed Dec 31 19:43:10 2014 -0800 +++ b/Bootloader/flash.c Sat Jan 10 19:53:46 2015 -0800 @@ -68,7 +68,6 @@ 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; diff -r 708af1b9bc55 -r 596d8e300a37 Lib/mk20dx.c --- a/Lib/mk20dx.c Wed Dec 31 19:43:10 2014 -0800 +++ b/Lib/mk20dx.c Sat Jan 10 19:53:46 2015 -0800 @@ -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 @@ 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 @@ // ----- 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 @@ __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 @@ 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 @@ 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 @@ // 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 ); }