changeset 192:6ac92b8614c0

Fixing RAM calculator and reduced actual SRAM usage - Changed static variables to const that should have been const - Updated CMake files to prepare for MCHCK custom bootloader - Changed the USB ID numbers and ID number for bootloader - Only generate DFU or Teensy binary image, not both - Fixed RAM and FLASH calculator - Added missing license in delay.c/h (much of it was taken from Teensy source though I've changed a bunch of it) - Prepared mk20dx.c for upcoming bootloader addition - mk20dx.h cleanup - Reduced the MCHCK based flash size for the application image (bootloader changes requires more flash space) - Fixed bugs in macro.c - Added keyHold cli command - Added show pending events debug message for PartialMap macro module
author Jacob Alexander <haata@kiibohd.com>
date Fri, 15 Aug 2014 10:42:12 -0700
parents 3404be65670b
children b091bb09c55f
files CMakeLists.txt Debug/cli/cli.c Debug/cli/cli.h Lib/CMake/arm.cmake Lib/CMake/avr.cmake Lib/CMake/initialize.cmake Lib/CMake/modules.cmake Lib/CMake/sizeCalculator Lib/Interrupts.h Lib/delay.c Lib/delay.h Lib/mk20dx.c Lib/mk20dx.h Lib/mk20dx128vlf5.ld Macro/PartialMap/generatedKeymap.h Macro/PartialMap/macro.c Output/pjrcUSB/arm/usb_dev.c Output/pjrcUSB/output_com.c Output/uartOut/output_com.c Output/usbMuxUart/output_com.c Scan/ADCTest/scan_loop.c Scan/DPH/scan_loop.c Scan/MD1/scan_loop.c Scan/MatrixARM/matrix_scan.c
diffstat 24 files changed, 418 insertions(+), 110 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Thu Aug 07 20:03:39 2014 -0700
+++ b/CMakeLists.txt	Fri Aug 15 10:42:12 2014 -0700
@@ -31,7 +31,8 @@
 ###
 # Compiler Intialization
 #
-include( Lib/CMake/initialize.cmake )
+set ( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/Lib/CMake )
+include( initialize )
 
 
 
--- a/Debug/cli/cli.c	Thu Aug 07 20:03:39 2014 -0700
+++ b/Debug/cli/cli.c	Fri Aug 15 10:42:12 2014 -0700
@@ -35,8 +35,8 @@
 // ----- Variables -----
 
 // Basic command dictionary
-char*       basicCLIDictName = "General Commands";
-CLIDictItem basicCLIDict[] = {
+const char basicCLIDictName[] = "General Commands";
+const CLIDictItem basicCLIDict[] = {
 	{ "cliDebug", "Enables/Disables hex output of the most recent cli input.", cliFunc_cliDebug },
 	{ "help",     "You're looking at it :P", cliFunc_help },
 	{ "led",      "Enables/Disables indicator LED. Try a couple times just in case the LED is in an odd state.\r\n\t\t\033[33mWarning\033[0m: May adversely affect some modules...", cliFunc_led },
@@ -267,7 +267,7 @@
 }
 
 // Registers a command dictionary with the CLI
-void CLI_registerDictionary( CLIDictItem *cmdDict, char* dictName )
+void CLI_registerDictionary( const CLIDictItem *cmdDict, const char* dictName )
 {
 	// Make sure this max limit of dictionaries hasn't been reached
 	if ( CLIDictionariesUsed >= CLIMaxDictionaries )
@@ -277,8 +277,8 @@
 	}
 
 	// Add dictionary
-	CLIDictNames[CLIDictionariesUsed] = dictName;
-	CLIDict[CLIDictionariesUsed++] = cmdDict;
+	CLIDictNames[CLIDictionariesUsed] = (char*)dictName;
+	CLIDict[CLIDictionariesUsed++] = (CLIDictItem*)cmdDict;
 }
 
 inline void CLI_tabCompletion()
--- a/Debug/cli/cli.h	Thu Aug 07 20:03:39 2014 -0700
+++ b/Debug/cli/cli.h	Fri Aug 15 10:42:12 2014 -0700
@@ -68,7 +68,7 @@
 
 void CLI_init();
 void CLI_process();
-void CLI_registerDictionary( CLIDictItem *cmdDict, char* dictName );
+void CLI_registerDictionary( const CLIDictItem *cmdDict, const char* dictName );
 void CLI_argumentIsolation( char* string, char** first, char** second );
 
 void CLI_commandLookup();
--- a/Lib/CMake/arm.cmake	Thu Aug 07 20:03:39 2014 -0700
+++ b/Lib/CMake/arm.cmake	Fri Aug 15 10:42:12 2014 -0700
@@ -34,8 +34,13 @@
 
 
 #| Chip Size Database
+#| MCHCK Based
+if ( "${CHIP}" MATCHES "mk20dx128vlf5" )
+	set( SIZE_RAM    16384 )
+	set( SIZE_FLASH 126976 )
+
 #| Teensy 3.0
-if ( "${CHIP}" MATCHES "mk20dx128" OR "${CHIP}" MATCHES "mk20dx128vlf5" )
+elseif ( "${CHIP}" MATCHES "mk20dx128" )
 	set( SIZE_RAM    16384 )
 	set( SIZE_FLASH 131072 )
 
@@ -84,12 +89,18 @@
 
 
 #| USB Defines, this is how the loader programs detect which type of chip base is used
-if ( "${CHIP}" MATCHES "mk20dx128" OR "${CHIP}" MATCHES "mk20dx256" )
-	set( VENDOR_ID  "0x16C0" )
-	set( PRODUCT_ID "0x0487" )
-elseif ( "${CHIP}" MATCHES "mk20dx128vlf5" )
-	set( VENDOR_ID  "0x2323" )
-	set( PRODUCT_ID "0x0001" )
+if ( "${CHIP}" MATCHES "mk20dx128vlf5" )
+	set( VENDOR_ID       "0x1C11" )
+	set( PRODUCT_ID      "0xB04D" )
+	set( BOOT_VENDOR_ID  "0x1C11" )
+	set( BOOT_PRODUCT_ID "0xB007" )
+	set( DFU 1 )
+elseif ( "${CHIP}" MATCHES "mk20dx128" OR "${CHIP}" MATCHES "mk20dx256" )
+	set( VENDOR_ID       "0x1C11" )
+	set( PRODUCT_ID      "0xB04D" )
+	set( BOOT_VENDOR_ID  "0x16c0" ) # TODO Double check, this is likely incorrect
+	set( BOOT_PRODUCT_ID "0x0487" )
+	set( TEENSY 1 )
 endif ()
 
 
@@ -98,18 +109,24 @@
 #|     gnu89 = c89 plus GCC extensions
 #|     c99   = ISO C99 standard (not yet fully implemented)
 #|     gnu99 = c99 plus GCC extensions
-set( CSTANDARD "-std=gnu99" )
+#|     gnu11 = c11 plus GCC extensions
+set( CSTANDARD "-std=gnu11" )
 
 
 #| Warning Options
 #|  -Wall...:     warning level
-set( WARN "-Wall -g" )
+set( WARN "-Wall -ggdb3" )
 
 
 #| Tuning Options
 #|  -f...:        tuning, see GCC manual
 #| NOTE: -fshort-wchar is specified to allow USB strings be passed conveniently
-set( TUNING "-mthumb -nostdlib -fdata-sections -ffunction-sections -fshort-wchar -fno-builtin" )
+if( BOOTLOADER )
+	set( TUNING "-D_bootloader_ -Wno-main -msoft-float -mthumb -fplan9-extensions -ffunction-sections -fdata-sections -fno-builtin -fstrict-volatile-bitfields -flto -fno-use-linker-plugin" )
+	#set( TUNING "-mthumb -fdata-sections -ffunction-sections -fno-builtin -msoft-float -fstrict-volatile-bitfields -flto -fno-use-linker-plugin -fwhole-program -Wno-main -nostartfiles -fplan9-extensions -D_bootloader_" )
+else()
+	set( TUNING "-mthumb -nostdlib -fdata-sections -ffunction-sections -fshort-wchar -fno-builtin -nostartfiles" )
+endif()
 
 
 #| Optimization level, can be [0, 1, 2, 3, s].
@@ -136,7 +153,14 @@
 
 
 #| Linker Flags
-set( LINKER_FLAGS "-mcpu=${CPU} -Wl,-Map=link.map,--cref -Wl,--gc-sections -mthumb -Wl,--no-wchar-size-warning -T${CMAKE_CURRENT_SOURCE_DIR}/Lib/${CHIP}.ld -nostartfiles" )
+if( BOOTLOADER )
+	# Bootloader linker flags
+	set( LINKER_FLAGS "${TUNING} -Wl,--gc-sections -fwhole-program -T${CMAKE_CURRENT_SOURCE_DIR}/../Lib/${CHIP}.bootloader.ld -nostartfiles -Wl,-Map=link.map" )
+	#set( LINKER_FLAGS "-Wl,-Map=link.map,--cref -Wl,--gc-sections -Wl,--no-wchar-size-warning -T${CMAKE_CURRENT_SOURCE_DIR}/../Lib/${CHIP}.bootloader.ld" )
+else()
+	# Normal linker flags
+	set( LINKER_FLAGS "-Wl,-Map=link.map,--cref -Wl,--gc-sections -Wl,--no-wchar-size-warning -T${CMAKE_CURRENT_SOURCE_DIR}/Lib/${CHIP}.ld" )
+endif()
 
 
 #| Hex Flags (XXX, CMake seems to have issues if you quote the arguments for the custom commands...)
--- a/Lib/CMake/avr.cmake	Thu Aug 07 20:03:39 2014 -0700
+++ b/Lib/CMake/avr.cmake	Fri Aug 15 10:42:12 2014 -0700
@@ -36,6 +36,10 @@
 message( "${MCU}" )
 
 
+#| Indicate to later build step that this is a Teensy
+set( Teensy )
+
+
 #| Chip Size Database
 #| Teensy 1.0
 if ( "${CHIP}" MATCHES "at90usb162" )
@@ -80,8 +84,10 @@
 
 
 #| USB Defines
-set( VENDOR_ID  "0x16C0" )
-set( PRODUCT_ID "0x047D" )
+set( VENDOR_ID       "0x1C11" )
+set( PRODUCT_ID      "0xB04D" )
+set( BOOT_VENDOR_ID  "0x16C0" ) # TODO Double check, this is likely incorrect
+set( BOOT_PRODUCT_ID "0x047D" )
 
 
 #| Compiler flag to set the C Standard level.
--- a/Lib/CMake/initialize.cmake	Thu Aug 07 20:03:39 2014 -0700
+++ b/Lib/CMake/initialize.cmake	Fri Aug 15 10:42:12 2014 -0700
@@ -50,7 +50,7 @@
 endif ()
 
 #| Load the compiler family specific configurations
-include( Lib/CMake/${COMPILER_FAMILY}.cmake )
+include( ${COMPILER_FAMILY} )
 
 #| Binutils not set by CMake
 set( CMAKE_SIZE "${_CMAKE_TOOLCHAIN_PREFIX}size" )
--- a/Lib/CMake/modules.cmake	Thu Aug 07 20:03:39 2014 -0700
+++ b/Lib/CMake/modules.cmake	Fri Aug 15 10:42:12 2014 -0700
@@ -130,6 +130,14 @@
 
 
 ###
+# CMake Module Checking
+#
+find_package( Git REQUIRED )
+find_package( Ctags ) # Optional
+
+
+
+###
 # Generate USB Defines
 #
 
@@ -241,13 +249,6 @@
 
 
 ###
-# CMake Module Checking
-#
-find_package( Git REQUIRED )
-find_package( Ctags ) # Optional
-
-
-###
 # ctag Generation
 #
 
@@ -287,19 +288,23 @@
 
 
 #| Convert the .ELF into a .bin to load onto the McHCK
-set( TARGET_BIN ${TARGET}.bin.dfu )
-add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
-	COMMAND ${CMAKE_OBJCOPY} ${BIN_FLAGS} ${TARGET_ELF} ${TARGET_BIN}
-	COMMENT "Creating binary file to load:  ${TARGET_BIN}"
-)
+if( DEFINED DFU )
+	set( TARGET_BIN ${TARGET}.dfu.bin )
+	add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
+		COMMAND ${CMAKE_OBJCOPY} ${BIN_FLAGS} ${TARGET_ELF} ${TARGET_BIN}
+		COMMENT "Creating dfu binary file:      ${TARGET_BIN}"
+	)
+endif()
 
 
 #| Convert the .ELF into a .HEX to load onto the Teensy
-set( TARGET_HEX ${TARGET}.teensy.hex )
-add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
-	COMMAND ${CMAKE_OBJCOPY} ${HEX_FLAGS} ${TARGET_ELF} ${TARGET_HEX}
-	COMMENT "Creating iHex file to load:    ${TARGET_HEX}"
-)
+if ( DEFINED TEENSY )
+	set( TARGET_HEX ${TARGET}.teensy.hex )
+	add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
+		COMMAND ${CMAKE_OBJCOPY} ${HEX_FLAGS} ${TARGET_ELF} ${TARGET_HEX}
+		COMMENT "Creating iHex file to load:    ${TARGET_HEX}"
+	)
+endif()
 
 
 #| Generate the Extended .LSS
@@ -331,8 +336,8 @@
 
 #| After Changes Size Information
 add_custom_target( SizeAfter ALL
-	COMMAND ${CMAKE_SOURCE_DIR}/Lib/CMake/sizeCalculator ${CMAKE_SIZE} ihex ${TARGET_ELF} ${SIZE_RAM}   " SRAM"
-	COMMAND ${CMAKE_SOURCE_DIR}/Lib/CMake/sizeCalculator ${CMAKE_SIZE} ihex ${TARGET_HEX} ${SIZE_FLASH} "Flash"
+	COMMAND ${CMAKE_SOURCE_DIR}/Lib/CMake/sizeCalculator ${CMAKE_SIZE} ram   ${TARGET_ELF} ${SIZE_RAM}   " SRAM"
+	COMMAND ${CMAKE_SOURCE_DIR}/Lib/CMake/sizeCalculator ${CMAKE_SIZE} flash ${TARGET_ELF} ${SIZE_FLASH} "Flash"
 	DEPENDS ${TARGET_ELF}
 	COMMENT "Chip usage for ${CHIP}"
 )
--- a/Lib/CMake/sizeCalculator	Thu Aug 07 20:03:39 2014 -0700
+++ b/Lib/CMake/sizeCalculator	Fri Aug 15 10:42:12 2014 -0700
@@ -2,15 +2,24 @@
 #| Jacob Alexander 2014
 #| Arg List
 #| 1 - size binary   (e.g. avr-size)
-#| 2 - target binary (e.g. ihex)
+#| 2 - measurement type (flash or ram)
 #| 3 - binary file   (e.g. kiibohd.hex)
 #| 4 - total available (flash/ram) in bytes
-#| 5 - flash/ram
+#| 5 - flash/ram text
 
 
-# Looks for the data column, to get the flash/ram used (in bytes)
-# <size binary> --target=<target binary> <binary file> | cut -f2 | tail -n 1
-USED=$("$1" --target="$2" "$3" | cut -f2 | tail -n 1)
+case "$2" in
+"flash")
+	USED=$("$1" "$3" | tail -n-1 | awk '{ print $1+$2 }')
+	;;
+"ram")
+	USED=$("$1" "$3" | tail -n-1 | awk '{ print $2+$3 }')
+	;;
+*)
+	echo "INVALID Measurement type: $2"
+	exit 1
+esac
+
 
 # Calculates the total flash/ram used
 TOTAL="$4"
@@ -35,7 +44,7 @@
 
 # Displays Results
 NAME="$5"
-echo -e "\t\033[1m${NAME}\033[m: ${COLOR}${PERCENTAGE}%\033[m ${USED}/${TOTAL}\tbytes"
+echo -e "\t\033[1m${NAME}\033[m: ${COLOR}${PERCENTAGE}%\033[m \t${USED}/${TOTAL}\tbytes"
 
 exit 0
 
--- a/Lib/Interrupts.h	Thu Aug 07 20:03:39 2014 -0700
+++ b/Lib/Interrupts.h	Fri Aug 15 10:42:12 2014 -0700
@@ -1,15 +1,15 @@
 /* Copyright (C) 2013-2014 by Jacob Alexander
- * 
+ *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
  * in the Software without restriction, including without limitation the rights
  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  * copies of the Software, and to permit persons to whom the Software is
  * furnished to do so, subject to the following conditions:
- * 
+ *
  * The above copyright notice and this permission notice shall be included in
  * all copies or substantial portions of the Software.
- * 
+ *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
--- a/Lib/delay.c	Thu Aug 07 20:03:39 2014 -0700
+++ b/Lib/delay.c	Fri Aug 15 10:42:12 2014 -0700
@@ -1,10 +1,50 @@
+/* Teensyduino Core Library
+ * http://www.pjrc.com/teensy/
+ * Copyright (c) 2013 PJRC.COM, LLC.
+ * Modifications by Jacob Alexander 2013-2014
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * 1. The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * 2. If the Software is incorporated into a build system that allows
+ * selection among a list of target devices, then similar target
+ * devices manufactured by PJRC.COM must be included in the list of
+ * target devices and selectable in the same manner.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+// ----- Local Includes -----
 
 #include "delay.h"
 #include "mk20dx.h"
 
+
+
+// ----- Variables -----
+
 // the systick interrupt is supposed to increment this at 1 kHz rate
 volatile uint32_t systick_millis_count = 0;
 
+
+
+// ----- Functions -----
+
 void yield(void) {};
 
 uint32_t micros(void)
--- a/Lib/delay.h	Thu Aug 07 20:03:39 2014 -0700
+++ b/Lib/delay.h	Fri Aug 15 10:42:12 2014 -0700
@@ -1,14 +1,53 @@
+/* Teensyduino Core Library
+ * http://www.pjrc.com/teensy/
+ * Copyright (c) 2013 PJRC.COM, LLC.
+ * Modifications by Jacob Alexander 2013-2014
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * 1. The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * 2. If the Software is incorporated into a build system that allows
+ * selection among a list of target devices, then similar target
+ * devices manufactured by PJRC.COM must be included in the list of
+ * target devices and selectable in the same manner.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
 
 #ifndef __DELAY_H
 #define __DELAY_H
 
+// ----- System Includes -----
+
 #include <stdint.h>
 
+
+
+// ----- Macros -----
+
 // Convenience Macros, for delay compatibility with AVR-GCC
 #define _delay_ms(val) delay( val )
 #define _delay_us(val) delayMicroseconds( val )
 
 
+
+// ----- Functions -----
+
 // the systick interrupt is supposed to increment this at 1 kHz rate
 extern volatile uint32_t systick_millis_count;
 
--- a/Lib/mk20dx.c	Thu Aug 07 20:03:39 2014 -0700
+++ b/Lib/mk20dx.c	Fri Aug 15 10:42:12 2014 -0700
@@ -31,7 +31,6 @@
 
 // Local Includes
 #include "mk20dx.h"
-#include <print.h>
 
 
 
@@ -45,6 +44,8 @@
 extern unsigned long _ebss;
 extern unsigned long _estack;
 
+const uint8_t sys_reset_to_loader_magic[22] = "\xff\x00\x7fRESET TO LOADER\x7f\x00\xff";
+
 
 
 // ----- Function Declarations -----
@@ -354,23 +355,98 @@
 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
 	0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF
 };
+#elif defined(_mk20dx128vlf5_) && defined(_bootloader_)
+// XXX Byte labels may be in incorrect positions, double check before modifying
+//     FSEC is in correct location -Jacob
+__attribute__ ((section(".flashconfig"), used))
+const uint8_t flashconfigbytes[16] = {
+	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // Backdoor Verif Key 28.3.1
+	0xFF, 0xFF, 0xFF, 0xFF, // Program Flash Protection Bytes FPROT0-3
+	0xBE, // Flash security byte FSEC
+	0x03, // Flash nonvolatile option byte FOPT
+	0xFF, // EEPROM Protection Byte FEPROT
+	0xFF, // Data Flash Protection Byte FDPROT
+};
 #endif
 
 
 
+// ----- Functions -----
+
+__attribute__((noreturn))
+static inline void jump_to_app( uintptr_t addr )
+{
+        // addr is in r0
+        __asm__("ldr sp, [%[addr], #0]\n"
+                "ldr pc, [%[addr], #4]"
+                :: [addr] "r" (addr));
+        // NOTREACHED
+        __builtin_unreachable();
+}
+
+void *memset( void *addr, int val, unsigned int len )
+{
+	char *buf = addr;
+
+	for (; len > 0; --len, ++buf)
+		*buf = val;
+	return (addr);
+}
+
+int memcmp( const void *a, const void *b, unsigned int len )
+{
+	const uint8_t *ap = a, *bp = b;
+	int val = 0;
+
+	for (; len > 0 && (val = *ap - *bp) == 0; --len, ++ap, ++bp)
+		/* NOTHING */;
+	return (val);
+}
+
+void *memcpy( void *dst, const void *src, unsigned int len )
+{
+	char *dstbuf = dst;
+	const char *srcbuf = src;
+
+	for (; len > 0; --len, ++dstbuf, ++srcbuf)
+		*dstbuf = *srcbuf;
+	return (dst);
+}
+
+
+
 // ----- Chip Entry Point -----
 
 __attribute__ ((section(".startup")))
 void ResetHandler()
 {
-	uint32_t *src = &_etext;
-	uint32_t *dest = &_sdata;
-
 	// Disable Watchdog
 	WDOG_UNLOCK = WDOG_UNLOCK_SEQ1;
 	WDOG_UNLOCK = WDOG_UNLOCK_SEQ2;
 	WDOG_STCTRLH = WDOG_STCTRLH_ALLOWUPDATE;
 
+#if defined(_mk20dx128vlf5_) && 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 ||
+	  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) );
+	}
+	else
+	{
+		uint32_t addr = (uintptr_t)&_app_rom;
+		SCB_VTOR = addr; // relocate vector table
+		jump_to_app( addr );
+	}
+#endif
+
+	uint32_t *src = &_etext;
+	uint32_t *dest = &_sdata;
+
 	// Enable clocks to always-used peripherals
 	SIM_SCGC5 = 0x00043F82; // Clocks active to all GPIO
 	SIM_SCGC6 = SIM_SCGC6_FTM0 | SIM_SCGC6_FTM1 | SIM_SCGC6_ADC0 | SIM_SCGC6_FTFL;
@@ -486,11 +562,17 @@
 	SIM_SOPT2 = SIM_SOPT2_USBSRC | SIM_SOPT2_PLLFLLSEL | SIM_SOPT2_TRACECLKSEL | SIM_SOPT2_CLKOUTSEL( 6 );
 
 #endif
+
+#if !defined(_bootloader_)
 	// Initialize the SysTick counter
 	SYST_RVR = (F_CPU / 1000) - 1;
 	SYST_CSR = SYST_CSR_CLKSOURCE | SYST_CSR_TICKINT | SYST_CSR_ENABLE;
 
 	__enable_irq();
+#else
+	// Disable Watchdog for bootloader
+	WDOG_STCTRLH &= ~WDOG_STCTRLH_WDOGEN;
+#endif
 
 	main();
 	while ( 1 ); // Shouldn't get here...
--- a/Lib/mk20dx.h	Thu Aug 07 20:03:39 2014 -0700
+++ b/Lib/mk20dx.h	Fri Aug 15 10:42:12 2014 -0700
@@ -1,6 +1,7 @@
 /* Teensyduino Core Library
  * http://www.pjrc.com/teensy/
  * Copyright (c) 2013 PJRC.COM, LLC.
+ * Modified by Jacob Alexander 2014
  *
  * Permission is hereby granted, free of charge, to any person obtaining
  * a copy of this software and associated documentation files (the
@@ -31,12 +32,7 @@
 #ifndef _mk20dx_h_
 #define _mk20dx_h_
 
-//#define F_CPU 96000000
-//#define F_CPU 48000000
-//#define F_CPU 24000000
-//#define F_BUS 48000000
-//#define F_BUS 24000000
-//#define F_MEM 24000000
+// ----- Defines -----
 
 #if (F_CPU == 96000000)
  #define F_BUS 48000000
@@ -54,8 +50,16 @@
 #define NULL ((void *)0)
 #endif
 
+
+
+// ----- Includes -----
+
 #include <stdint.h>
 
+
+
+// ----- Registers -----
+
 // chapter 11: Port control and interrupts (PORT)
 #define PORT_PCR_ISF			(uint32_t)0x01000000		// Interrupt Status Flag
 #define PORT_PCR_IRQC(n)		(uint32_t)(((n) & 15) << 16)	// Interrupt Configuration
@@ -249,7 +253,7 @@
 #define SIM_SOPT1               *(volatile uint32_t *)0x40047000 // System Options Register 1
 #define SIM_SOPT1CFG            *(volatile uint32_t *)0x40047004 // SOPT1 Configuration Register
 #define SIM_SOPT2               *(volatile uint32_t *)0x40048004 // System Options Register 2
-#define SIM_SOPT2_USBSRC		(uint32_t)0x00040000		// 0=USB_CLKIN, 1=FFL/PLL 
+#define SIM_SOPT2_USBSRC		(uint32_t)0x00040000		// 0=USB_CLKIN, 1=FFL/PLL
 #define SIM_SOPT2_PLLFLLSEL		(uint32_t)0x00010000		// 0=FLL, 1=PLL
 #define SIM_SOPT2_TRACECLKSEL		(uint32_t)0x00001000		// 0=MCGOUTCLK, 1=CPU
 #define SIM_SOPT2_PTD7PAD		(uint32_t)0x00000800		// 0=normal, 1=double drive PTD7
@@ -703,7 +707,7 @@
 #define MCG_C2                  *(volatile uint8_t  *)0x40064001 // MCG Control 2 Register
 #define MCG_C2_IRCS			(uint8_t)0x01			// Internal Reference Clock Select, Selects between the fast or slow internal reference clock source.
 #define MCG_C2_LP			(uint8_t)0x02			// Low Power Select, Controls whether the FLL or PLL is disabled in BLPI and BLPE modes.
-#define MCG_C2_EREFS			(uint8_t)0x04			// External Reference Select, Selects the source for the external reference clock. 
+#define MCG_C2_EREFS			(uint8_t)0x04			// External Reference Select, Selects the source for the external reference clock.
 #define MCG_C2_HGO0			(uint8_t)0x08			// High Gain Oscillator Select, Controls the crystal oscillator mode of operation
 #define MCG_C2_RANGE0(n)		(uint8_t)(((n) & 0x03) << 4)	// Frequency Range Select, Selects the frequency range for the crystal oscillator
 #define MCG_C2_LOCRE0			(uint8_t)0x80			// Loss of Clock Reset Enable, Determines whether an interrupt or a reset request is made following a loss of OSC0 
@@ -721,7 +725,7 @@
 #define MCG_C6                  *(volatile uint8_t  *)0x40064005 // MCG Control 6 Register
 #define MCG_C6_VDIV0(n)			(uint8_t)((n) & 0x1F)		// VCO 0 Divider
 #define MCG_C6_CME0			(uint8_t)0x20			// Clock Monitor Enable
-#define MCG_C6_PLLS			(uint8_t)0x40			// PLL Select, Controls whether the PLL or FLL output is selected as the MCG source when CLKS[1:0]=00. 
+#define MCG_C6_PLLS			(uint8_t)0x40			// PLL Select, Controls whether the PLL or FLL output is selected as the MCG source when CLKS[1:0]=00.
 #define MCG_C6_LOLIE0			(uint8_t)0x80			// Loss of Lock Interrrupt Enable
 #define MCG_S                   *(volatile uint8_t  *)0x40064006 // MCG Status Register
 #define MCG_S_IRCST			(uint8_t)0x01			// Internal Reference Clock Status
@@ -1960,8 +1964,32 @@
 // Other
 #define VBAT                    *(volatile uint8_t *)0x4003E000 // Register available in all power states
 
+
+
+// ----- Macros -----
+
+#define SOFTWARE_RESET()        SCB_AIRCR = 0x5FA0004
+
+
+
+// ----- Variables -----
+
+extern const uint8_t sys_reset_to_loader_magic[22];
+
+
+
+// ----- Functions -----
+
+void *memset( void *addr, int val, unsigned int len );
+void *memcpy( void *dst, const void *src, unsigned int len );
+int memcmp( const void *a, const void *b, unsigned int len );
+
 extern int nvic_execution_priority(void);
 
+
+
+// ----- Interrupts -----
+
 extern void nmi_isr(void);
 extern void hard_fault_isr(void);
 extern void memmanage_fault_isr(void);
@@ -2053,7 +2081,5 @@
 extern void porte_isr(void);
 extern void software_isr(void);
 
-#define SOFTWARE_RESET()        SCB_AIRCR = 0x5FA0004
-
 #endif
 
--- a/Lib/mk20dx128vlf5.ld	Thu Aug 07 20:03:39 2014 -0700
+++ b/Lib/mk20dx128vlf5.ld	Fri Aug 15 10:42:12 2014 -0700
@@ -31,7 +31,7 @@
 
 MEMORY
 {
-	FLASH (rx) : ORIGIN = 3K, LENGTH = 128K-3K
+	FLASH (rx) : ORIGIN = 4K, LENGTH = 128K-4K
 	RAM  (rwx) : ORIGIN = 0x20000000 - 16K / 2, LENGTH = 16K
 }
 
--- a/Macro/PartialMap/generatedKeymap.h	Thu Aug 07 20:03:39 2014 -0700
+++ b/Macro/PartialMap/generatedKeymap.h	Fri Aug 15 10:42:12 2014 -0700
@@ -21,7 +21,11 @@
 
 // ----- Includes -----
 
+// Project Includes
 #include <print.h>
+#include <scan_loop.h>
+#include <macro.h>
+#include <output_com.h>
 
 // USB HID Keymap list
 #include <usb_hid.h>
@@ -44,7 +48,7 @@
 
 // ResultMacro struct, one is created per ResultMacro, no duplicates
 typedef struct ResultMacro {
-	uint8_t *guide;
+	const uint8_t *guide;
 	unsigned int pos;
 	uint8_t  state;
 	uint8_t  stateType;
@@ -91,7 +95,7 @@
 
 // TriggerMacro struct, one is created per TriggerMacro, no duplicates
 typedef struct TriggerMacro {
-	uint8_t *guide;
+	const uint8_t *guide;
 	unsigned int result;
 	unsigned int pos;
 	TriggerMacroState state;
@@ -156,13 +160,15 @@
 } Capability;
 
 // Total Number of Capabilities
-#define CapabilitiesNum sizeof( void* ) / 4 + sizeof( uint8_t )
+#define CapabilitiesNum sizeof( CapabilitiesList ) / sizeof( Capability )
 
 // Indexed Capabilities Table
-// TODO Should be moved to the Scan Module
-Capability CapabilitiesList[] = {
+// TODO Generated from .kll files in each module
+const Capability CapabilitiesList[] = {
 	{ debugPrint_capability, 1 },
 	{ debugPrint2_capability, 2 },
+	{ Macro_layerStateToggle_capability, sizeof(unsigned int) + 1 },
+	{ Output_usbCodeSend_capability, 1 },
 };
 
 
@@ -175,7 +181,7 @@
 // Define_RM( index );
 //  * index  - Result Macro index number
 //  Must be used after Guide_RM
-#define Guide_RM( index ) static uint8_t rm##index##_guide[]
+#define Guide_RM( index ) const uint8_t rm##index##_guide[]
 #define Define_RM( index ) { rm##index##_guide, 0, 0, 0 }
 
 Guide_RM( 0 ) = { 1, 0, 0xDA, 0 };
@@ -208,13 +214,13 @@
 // Define_TM( index, result );
 //  * index   - Trigger Macro index number
 //  * result  - Result Macro index number which is triggered by this Trigger Macro
-#define Guide_TM( index ) static uint8_t tm##index##_guide[]
-#define Define_TM( index, result ) { tm##index##_guide, result, 0 }
+#define Guide_TM( index ) const uint8_t tm##index##_guide[]
+#define Define_TM( index, result ) { tm##index##_guide, result, 0, TriggerMacro_Waiting }
 
-Guide_TM( 0 ) = { 1, 0x10, 0x01, 0x73, 0 };
-Guide_TM( 1 ) = { 1, 0x0F, 0x01, 0x73, 1, 0x00, 0x01, 0x75, 0 };
-Guide_TM( 2 ) = { 2, 0xF0, 0x01, 0x73, 0x00, 0x01, 0x74, 0 };
-Guide_TM( 3 ) = { 1, 0x10, 0x01, 0x76, 0 };
+Guide_TM( 0 ) = { 1, 0x00, 0x01, 0x73, 0 };
+Guide_TM( 1 ) = { 1, 0x00, 0x01, 0x73, 1, 0x00, 0x01, 0x75, 0 };
+Guide_TM( 2 ) = { 2, 0x00, 0x01, 0x73, 0x00, 0x01, 0x74, 0 };
+Guide_TM( 3 ) = { 1, 0x00, 0x01, 0x76, 0 };
 
 
 // -- Trigger Macro List
@@ -246,7 +252,7 @@
 //  * layer       - basename of the layer
 //  * scanCode    - Hex value of the scanCode
 //  * triggerList - Trigger List (see Trigger Lists)
-#define Define_TL( layer, scanCode ) static unsigned int layer##_tl_##scanCode[]
+#define Define_TL( layer, scanCode ) const unsigned int layer##_tl_##scanCode[]
 
 // -- Trigger Lists
 //
@@ -538,17 +544,17 @@
 //                 -
 
 // Default Map for ScanCode Lookup
-static unsigned int *default_scanMap[] = {
+const unsigned int *default_scanMap[] = {
 default_tl_0x00, default_tl_0x01, default_tl_0x02, default_tl_0x03, default_tl_0x04, default_tl_0x05, default_tl_0x06, default_tl_0x07, default_tl_0x08, default_tl_0x09, default_tl_0x0A, default_tl_0x0B, default_tl_0x0C, default_tl_0x0D, default_tl_0x0E, default_tl_0x0F, default_tl_0x10, default_tl_0x11, default_tl_0x12, default_tl_0x13, default_tl_0x14, default_tl_0x15, default_tl_0x16, default_tl_0x17, default_tl_0x18, default_tl_0x19, default_tl_0x1A, default_tl_0x1B, default_tl_0x1C, default_tl_0x1D, default_tl_0x1E, default_tl_0x1F, default_tl_0x20, default_tl_0x21, default_tl_0x22, default_tl_0x23, default_tl_0x24, default_tl_0x25, default_tl_0x26, default_tl_0x27, default_tl_0x28, default_tl_0x29, default_tl_0x2A, default_tl_0x2B, default_tl_0x2C, default_tl_0x2D, default_tl_0x2E, default_tl_0x2F, default_tl_0x30, default_tl_0x31, default_tl_0x32, default_tl_0x33, default_tl_0x34, default_tl_0x35, default_tl_0x36, default_tl_0x37, default_tl_0x38, default_tl_0x39, default_tl_0x3A, default_tl_0x3B, default_tl_0x3C, default_tl_0x3D, default_tl_0x3E, default_tl_0x3F, default_tl_0x40, default_tl_0x41, default_tl_0x42, default_tl_0x43, default_tl_0x44, default_tl_0x45, default_tl_0x46, default_tl_0x47, default_tl_0x48, default_tl_0x49, default_tl_0x4A, default_tl_0x4B, default_tl_0x4C, default_tl_0x4D, default_tl_0x4E, default_tl_0x4F, default_tl_0x50, default_tl_0x51, default_tl_0x52, default_tl_0x53, default_tl_0x54, default_tl_0x55, default_tl_0x56, default_tl_0x57, default_tl_0x58, default_tl_0x59, default_tl_0x5A, default_tl_0x5B, default_tl_0x5C, default_tl_0x5D, default_tl_0x5E, default_tl_0x5F, default_tl_0x60, default_tl_0x61, default_tl_0x62, default_tl_0x63, default_tl_0x64, default_tl_0x65, default_tl_0x66, default_tl_0x67, default_tl_0x68, default_tl_0x69, default_tl_0x6A, default_tl_0x6B, default_tl_0x6C, default_tl_0x6D, default_tl_0x6E, default_tl_0x6F, default_tl_0x70, default_tl_0x71, default_tl_0x72, default_tl_0x73, default_tl_0x74, default_tl_0x75, default_tl_0x76, default_tl_0x77, default_tl_0x78, default_tl_0x79, default_tl_0x7A, default_tl_0x7B, default_tl_0x7C, default_tl_0x7D, default_tl_0x7E, default_tl_0x7F, default_tl_0x80, default_tl_0x81, default_tl_0x82, default_tl_0x83, default_tl_0x84, default_tl_0x85, default_tl_0x86, default_tl_0x87, default_tl_0x88, default_tl_0x89, default_tl_0x8A, default_tl_0x8B, default_tl_0x8C, default_tl_0x8D, default_tl_0x8E, default_tl_0x8F, default_tl_0x90, default_tl_0x91, default_tl_0x92, default_tl_0x93, default_tl_0x94, default_tl_0x95, default_tl_0x96, default_tl_0x97, default_tl_0x98, default_tl_0x99, default_tl_0x9A, default_tl_0x9B, default_tl_0x9C, default_tl_0x9D, default_tl_0x9E, default_tl_0x9F, default_tl_0xA0, default_tl_0xA1, default_tl_0xA2, default_tl_0xA3, default_tl_0xA4, default_tl_0xA5, default_tl_0xA6, default_tl_0xA7, default_tl_0xA8, default_tl_0xA9, default_tl_0xAA, default_tl_0xAB, default_tl_0xAC, default_tl_0xAD, default_tl_0xAE, default_tl_0xAF, default_tl_0xB0, default_tl_0xB1, default_tl_0xB2, default_tl_0xB3, default_tl_0xB4, default_tl_0xB5, default_tl_0xB6, default_tl_0xB7, default_tl_0xB8, default_tl_0xB9, default_tl_0xBA, default_tl_0xBB, default_tl_0xBC, default_tl_0xBD, default_tl_0xBE, default_tl_0xBF, default_tl_0xC0, default_tl_0xC1, default_tl_0xC2, default_tl_0xC3, default_tl_0xC4, default_tl_0xC5, default_tl_0xC6, default_tl_0xC7, default_tl_0xC8, default_tl_0xC9, default_tl_0xCA, default_tl_0xCB, default_tl_0xCC, default_tl_0xCD, default_tl_0xCE, default_tl_0xCF, default_tl_0xD0, default_tl_0xD1, default_tl_0xD2, default_tl_0xD3, default_tl_0xD4, default_tl_0xD5, default_tl_0xD6, default_tl_0xD7, default_tl_0xD8, default_tl_0xD9, default_tl_0xDA, default_tl_0xDB, default_tl_0xDC, default_tl_0xDD, default_tl_0xDE, default_tl_0xDF, default_tl_0xE0, default_tl_0xE1, default_tl_0xE2, default_tl_0xE3, default_tl_0xE4, default_tl_0xE5, default_tl_0xE6, default_tl_0xE7, default_tl_0xE8, default_tl_0xE9, default_tl_0xEA, default_tl_0xEB, default_tl_0xEC, default_tl_0xED, default_tl_0xEE, default_tl_0xEF, default_tl_0xF0, default_tl_0xF1, default_tl_0xF2, default_tl_0xF3, default_tl_0xF4, default_tl_0xF5, default_tl_0xF6, default_tl_0xF7, default_tl_0xF8, default_tl_0xF9, default_tl_0xFA, default_tl_0xFB, default_tl_0xFC, default_tl_0xFD, default_tl_0xFE, default_tl_0xFF,
 };
 
 // Layer <name> for ScanCode Lookup
-static unsigned int *myname_scanMap[] = {
+const unsigned int *myname_scanMap[] = {
 0, 0, 0, 0, myname_tl_0x05, myname_tl_0x06, myname_tl_0x07
 };
 
 // Layer <name> for ScanCode Lookup
-static unsigned int *myname2_scanMap[] = {
+const unsigned int *myname2_scanMap[] = {
 0, 0, 0, myname2_tl_0x04, myname2_tl_0x05, myname2_tl_0x06
 };
 
@@ -574,9 +580,9 @@
 // The name is defined for cli debugging purposes (Null terminated string)
 
 typedef struct Layer {
-	unsigned int **triggerMap;
-	char *name;
-	uint8_t max;
+	const unsigned int **triggerMap;
+	const char *name;
+	const uint8_t max;
 	uint8_t state;
 } Layer;
 
--- a/Macro/PartialMap/macro.c	Thu Aug 07 20:03:39 2014 -0700
+++ b/Macro/PartialMap/macro.c	Fri Aug 15 10:42:12 2014 -0700
@@ -39,6 +39,7 @@
 
 void cliFunc_capList   ( char* args );
 void cliFunc_capSelect ( char* args );
+void cliFunc_keyHold   ( char* args );
 void cliFunc_keyPress  ( char* args );
 void cliFunc_keyRelease( char* args );
 void cliFunc_layerList ( char* args );
@@ -80,12 +81,13 @@
 // ----- Variables -----
 
 // Macro Module command dictionary
-char*       macroCLIDictName = "Macro Module Commands";
-CLIDictItem macroCLIDict[] = {
+const char macroCLIDictName[] = "Macro Module Commands";
+const CLIDictItem macroCLIDict[] = {
 	{ "capList",     "Prints an indexed list of all non USB keycode capabilities.", cliFunc_capList },
 	{ "capSelect",   "Triggers the specified capabilities. First two args are state and stateType." NL "\t\t\033[35mK11\033[0m Keyboard Capability 0x0B", cliFunc_capSelect },
-	{ "keyPress",    "Send key-presses to the macro module. Held until released. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyPress },
-	{ "keyRelease",  "Release a key-press from the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyRelease },
+	{ "keyHold",     "Send key-hold events to the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyHold },
+	{ "keyPress",    "Send key-press events to the macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyPress },
+	{ "keyRelease",  "Send key-release event to macro module. Duplicates have undefined behaviour." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_keyRelease },
 	{ "layerList",   "List available layers.", cliFunc_layerList },
 	{ "layerState",  "Modify specified indexed layer state <layer> <state byte>." NL "\t\t\033[35mL2\033[0m Indexed Layer 0x02" NL "\t\t0 Off, 1 Shift, 2 Latch, 4 Lock States", cliFunc_layerState },
 	{ "macroDebug",  "Disables/Enables sending USB keycodes to the Output Module and prints U/K codes.", cliFunc_macroDebug },
@@ -146,7 +148,8 @@
 	}
 
 	// Get layer index from arguments
-	unsigned int layer = (unsigned int)(&args[0]);
+	// Cast pointer to uint8_t to unsigned int then access that memory location
+	unsigned int layer = *(unsigned int*)(&args[0]);
 
 	// Get layer toggle byte
 	uint8_t toggleByte = args[ sizeof(unsigned int) ];
@@ -229,7 +232,7 @@
 		if ( (layer->state & 0x01) ^ (latch>>1) ^ ((layer->state & 0x04)>>2) )
 		{
 			// Lookup layer
-			unsigned int **map = layer->triggerMap;
+			unsigned int **map = (unsigned int**)layer->triggerMap;
 
 			// Determine if layer has key defined
 			if ( map != 0 && *map[ scanCode ] != 0 )
@@ -238,7 +241,7 @@
 	}
 
 	// Do lookup on default layer
-	unsigned int **map = LayerIndex[0].triggerMap;
+	unsigned int **map = (unsigned int**)LayerIndex[0].triggerMap;
 
 	// Determine if layer has key defined
 	if ( map == 0 && *map[ scanCode ] == 0 )
@@ -615,6 +618,7 @@
 
 		// Proceed, decrementing the step counter
 		macroStepCounter--;
+		dbug_print("Macro Step");
 	}
 
 	// Update pending trigger list, before processing TriggerMacros
@@ -713,7 +717,6 @@
 	// Initialize TriggerMacro states
 	for ( unsigned int macro = 0; macro < TriggerMacroNum; macro++ )
 	{
-		TriggerMacroList[ macro ].result = 0;
 		TriggerMacroList[ macro ].pos    = 0;
 		TriggerMacroList[ macro ].state  = TriggerMacro_Waiting;
 	}
@@ -734,6 +737,7 @@
 {
 	print( NL );
 	info_msg("Capabilities List");
+	printHex( CapabilitiesNum );
 
 	// Iterate through all of the capabilities and display them
 	for ( unsigned int cap = 0; cap < CapabilitiesNum; cap++ )
@@ -812,6 +816,34 @@
 	}
 }
 
+void cliFunc_keyHold( char* args )
+{
+	// Parse codes from arguments
+	char* curArgs;
+	char* arg1Ptr;
+	char* arg2Ptr = args;
+
+	// Process all args
+	for ( ;; )
+	{
+		curArgs = arg2Ptr;
+		CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
+
+		// Stop processing args if no more are found
+		if ( *arg1Ptr == '\0' )
+			break;
+
+		// Ignore non-Scancode numbers
+		switch ( arg1Ptr[0] )
+		{
+		// Scancode
+		case 'S':
+			Macro_keyState( (uint8_t)decToInt( &arg1Ptr[1] ), 0x02 ); // Hold scancode
+			break;
+		}
+	}
+}
+
 void cliFunc_keyPress( char* args )
 {
 	// Parse codes from arguments
@@ -881,7 +913,7 @@
 		print(" - ");
 
 		// Display layer name
-		dPrint( LayerIndex[ layer ].name );
+		dPrint( (char*)LayerIndex[ layer ].name );
 
 		// Default map
 		if ( layer == 0 )
@@ -956,6 +988,39 @@
 
 void cliFunc_macroList( char* args )
 {
+	// Show pending key events
+	print( NL );
+	info_msg("Pending Key Events: ");
+	printInt16( (uint16_t)macroTriggerListBufferSize );
+	print(" : ");
+	for ( uint8_t key = 0; key < macroTriggerListBufferSize; key++ )
+	{
+		printHex( macroTriggerListBuffer[ key ].scanCode );
+		print(" ");
+	}
+
+	// Show pending trigger macros
+	print( NL );
+	info_msg("Pending Trigger Macros: ");
+	printInt16( (uint16_t)macroTriggerMacroPendingListSize );
+	print(" : ");
+	for ( unsigned int macro = 0; macro < macroTriggerMacroPendingListSize; macro++ )
+	{
+		printHex( macroTriggerMacroPendingList[ macro ] );
+		print(" ");
+	}
+
+	// Show pending result macros
+	print( NL );
+	info_msg("Pending Result Macros: ");
+	printInt16( (uint16_t)macroResultMacroPendingListSize );
+	print(" : ");
+	for ( unsigned int macro = 0; macro < macroResultMacroPendingListSize; macro++ )
+	{
+		printHex( macroResultMacroPendingList[ macro ] );
+		print(" ");
+	}
+
 	// Show available trigger macro indices
 	print( NL );
 	info_msg("Trigger Macros Range: T0 -> T");
@@ -1176,7 +1241,13 @@
 	char* arg2Ptr;
 	CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
 
+	// Default to 1, if no argument given
+	unsigned int count = (unsigned int)decToInt( arg1Ptr );
+
+	if ( count == 0 )
+		count = 1;
+
 	// Set the macro step counter, negative int's are cast to uint
-	macroStepCounter = (unsigned int)decToInt( arg1Ptr );
+	macroStepCounter = count;
 }
 
--- a/Output/pjrcUSB/arm/usb_dev.c	Thu Aug 07 20:03:39 2014 -0700
+++ b/Output/pjrcUSB/arm/usb_dev.c	Fri Aug 15 10:42:12 2014 -0700
@@ -700,8 +700,7 @@
 	}
 	else
 	{
-		// This line must be exactly the same in the bootloader
-		const uint8_t sys_reset_to_loader_magic[] = "\xff\x00\x7fRESET TO LOADER\x7f\x00\xff";
+		// Copies variable into the VBAT register, must be identical to the variable in the bootloader to jump to the bootloader flash mode
 		for ( int pos = 0; pos < sizeof(sys_reset_to_loader_magic); pos++ )(&VBAT)[pos] = sys_reset_to_loader_magic[ pos ];
 		SOFTWARE_RESET();
 	}
--- a/Output/pjrcUSB/output_com.c	Thu Aug 07 20:03:39 2014 -0700
+++ b/Output/pjrcUSB/output_com.c	Fri Aug 15 10:42:12 2014 -0700
@@ -56,8 +56,8 @@
 // ----- Variables -----
 
 // Output Module command dictionary
-char*       outputCLIDictName = "USB Module Commands";
-CLIDictItem outputCLIDict[] = {
+const char outputCLIDictName[] = "USB Module Commands";
+const CLIDictItem outputCLIDict[] = {
 	{ "kbdProtocol", "Keyboard Protocol Mode: 0 - Boot, 1 - OS/NKRO Mode", cliFunc_kbdProtocol },
 	{ "readLEDs",    "Read LED byte:" NL "\t\t1 NumLck, 2 CapsLck, 4 ScrlLck, 16 Kana, etc.", cliFunc_readLEDs },
 	{ "sendKeys",    "Send the prepared list of USB codes and modifier byte.", cliFunc_sendKeys },
--- a/Output/uartOut/output_com.c	Thu Aug 07 20:03:39 2014 -0700
+++ b/Output/uartOut/output_com.c	Fri Aug 15 10:42:12 2014 -0700
@@ -52,8 +52,8 @@
 // ----- Variables -----
 
 // Output Module command dictionary
-char*       outputCLIDictName = "USB Module Commands - NOT WORKING";
-CLIDictItem outputCLIDict[] = {
+const char outputCLIDictName[] = "USB Module Commands - NOT WORKING";
+const CLIDictItem outputCLIDict[] = {
 	{ "kbdProtocol", "Keyboard Protocol Mode: 0 - Boot, 1 - OS/NKRO Mode", cliFunc_kbdProtocol },
 	{ "readLEDs",    "Read LED byte:" NL "\t\t1 NumLck, 2 CapsLck, 4 ScrlLck, 16 Kana, etc.", cliFunc_readLEDs },
 	{ "sendKeys",    "Send the prepared list of USB codes and modifier byte.", cliFunc_sendKeys },
--- a/Output/usbMuxUart/output_com.c	Thu Aug 07 20:03:39 2014 -0700
+++ b/Output/usbMuxUart/output_com.c	Fri Aug 15 10:42:12 2014 -0700
@@ -57,8 +57,8 @@
 // ----- Variables -----
 
 // Output Module command dictionary
-char*       outputCLIDictName = "USB Module Commands - NOT WORKING";
-CLIDictItem outputCLIDict[] = {
+const char outputCLIDictName[] = "USB Module Commands - NOT WORKING";
+const CLIDictItem outputCLIDict[] = {
 	{ "kbdProtocol", "Keyboard Protocol Mode: 0 - Boot, 1 - OS/NKRO Mode", cliFunc_kbdProtocol },
 	{ "readLEDs",    "Read LED byte:" NL "\t\t1 NumLck, 2 CapsLck, 4 ScrlLck, 16 Kana, etc.", cliFunc_readLEDs },
 	{ "readUART",    "Read UART buffer until empty.", cliFunc_readUART },
--- a/Scan/ADCTest/scan_loop.c	Thu Aug 07 20:03:39 2014 -0700
+++ b/Scan/ADCTest/scan_loop.c	Fri Aug 15 10:42:12 2014 -0700
@@ -65,8 +65,8 @@
 
 
 // Scan Module command dictionary
-char*       scanCLIDictName = "ADC Test Module Commands";
-CLIDictItem scanCLIDict[] = {
+char scanCLIDictName[] = "ADC Test Module Commands";
+const CLIDictItem scanCLIDict[] = {
 #if defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
 	{ "adc",     "Read the specified number of values from the ADC at the given pin: <pin> [# of reads]"
 	          NL "\t\t See \033[35mLib/pin_map.teensy3\033[0m for ADC0 channel number.", cliFunc_adc },
--- a/Scan/DPH/scan_loop.c	Thu Aug 07 20:03:39 2014 -0700
+++ b/Scan/DPH/scan_loop.c	Fri Aug 15 10:42:12 2014 -0700
@@ -149,8 +149,8 @@
 
 
 // Scan Module command dictionary
-char*       scanCLIDictName = "DPH Module Commands";
-CLIDictItem scanCLIDict[] = {
+const char scanCLIDictName[] = "DPH Module Commands";
+const CLIDictItem scanCLIDict[] = {
 	{ "echo",        "Example command, echos the arguments.", cliFunc_echo },
 	{ "avgDebug",    "Enables/Disables averaging results." NL "\t\tDisplays each average, starting from Key 0x00, ignoring 0 valued averages.", cliFunc_avgDebug },
 	{ "keyDebug",    "Enables/Disables long debug for each keypress." NL "\t\tkeycode - [strobe:mux] : sense val : threshold+delta=total : margin", cliFunc_keyDebug },
--- a/Scan/MD1/scan_loop.c	Thu Aug 07 20:03:39 2014 -0700
+++ b/Scan/MD1/scan_loop.c	Fri Aug 15 10:42:12 2014 -0700
@@ -46,8 +46,8 @@
 // ----- Variables -----
 
 // Scan Module command dictionary
-char*       scanCLIDictName = "Scan Module Commands";
-CLIDictItem scanCLIDict[] = {
+const char scanCLIDictName[] = "Scan Module Commands";
+const CLIDictItem scanCLIDict[] = {
 	{ "echo",        "Example command, echos the arguments.", cliFunc_echo },
 	{ 0, 0, 0 } // Null entry for dictionary end
 };
--- a/Scan/MatrixARM/matrix_scan.c	Thu Aug 07 20:03:39 2014 -0700
+++ b/Scan/MatrixARM/matrix_scan.c	Fri Aug 15 10:42:12 2014 -0700
@@ -49,8 +49,8 @@
 // ----- Variables -----
 
 // Scan Module command dictionary
-char*       matrixCLIDictName = "Matrix Module Commands";
-CLIDictItem matrixCLIDict[] = {
+const char matrixCLIDictName[] = "Matrix Module Commands";
+const CLIDictItem matrixCLIDict[] = {
 	{ "matrixDebug",  "Enables matrix debug mode, prints out each scan code." NL "\t\tIf argument \033[35mT\033[0m is given, prints out each scan code state transition.", cliFunc_matrixDebug },
 	{ "matrixState",  "Prints out the current scan table N times." NL "\t\t \033[1mO\033[0m - Off, \033[1;33mP\033[0m - Press, \033[1;32mH\033[0m - Hold, \033[1;35mR\033[0m - Release, \033[1;31mI\033[0m - Invalid", cliFunc_matrixState },
 	{ 0, 0, 0 } // Null entry for dictionary end