]> git.donarmstrong.com Git - kiibohd-controller.git/blobdiff - Output/uartOut/arm/uart_serial.c
Fixing default ErgoDox layout and adding FlashMode button
[kiibohd-controller.git] / Output / uartOut / arm / uart_serial.c
index e92bbd1fc4b564d89c48da640f0e8c3d087fd74d..d06f2f59e10c121d47f2ba1cc1672829c38127df 100644 (file)
@@ -27,6 +27,8 @@
 // Project Includes
 #include <Lib/OutputLib.h>
 #include <Lib/Interrupts.h>
+#include <print.h>
+#include <kll_defs.h>
 
 // Local Includes
 #include "uart_serial.h"
@@ -113,8 +115,7 @@ void uart2_status_isr()
                        // Cleanup
                        available = UART_D;
                        UART_CFIFO = UART_CFIFO_RXFLUSH;
-                       sei();
-                       return;
+                       goto done;
                }
 
                // Read UART0 into buffer until FIFO is empty
@@ -143,6 +144,7 @@ void uart2_status_isr()
                }
        }
 
+done:
        sei(); // Re-enable Interrupts
 }
 
@@ -192,14 +194,14 @@ void uart_serial_setup()
 #elif defined(_mk20dx256vlh7_) // UART2 Debug
        // Setup baud rate - 115200 Baud
        // Uses Bus Clock
-       // 24 MHz / ( 16 * Baud ) = BDH/L
-       // Baud: 115200 -> 24 MHz / ( 16 * 115200 ) = 13.021
-       // Thus baud setting = 13
+       // 36 MHz / ( 16 * Baud ) = BDH/L
+       // Baud: 115200 -> 36 MHz / ( 16 * 115200 ) = 19.53125
+       // Thus baud setting = 19
        // NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
-       uint16_t baud = 13; // Max setting of 8191
+       uint16_t baud = 19; // Max setting of 8191
        UART_BDH = (uint8_t)(baud >> 8);
        UART_BDL = (uint8_t)baud;
-       UART_C4 = 0x01;
+       UART_C4 = 0x11;
 
 #endif
 
@@ -325,6 +327,48 @@ void uart_serial_flush_output()
 
 void uart_device_reload()
 {
+       if ( flashModeEnabled_define == 0 )
+       {
+               print( NL );
+               warn_print("flashModeEnabled not set, cancelling firmware reload...");
+               info_msg("Set flashModeEnabled to 1 in your kll configuration.");
+               return;
+       }
+
+// MCHCK
+#if defined(_mk20dx128vlf5_)
+
+       // MCHCK Kiibohd Variant
+       // Check to see if PTA3 (has a pull-up) is connected to GND (usually via jumper)
+       // Only allow reload if the jumper is present (security)
+       GPIOA_PDDR &= ~(1<<3); // Input
+       PORTA_PCR3 = PORT_PCR_PFE | PORT_PCR_MUX(1); // Internal pull-up
+
+       // Check for jumper
+       if ( GPIOA_PDIR & (1<<3) && flashModeEnabled_define != 0 )
+       {
+               print( NL );
+               warn_print("Security jumper not present, cancelling firmware reload...");
+               info_msg("Replace jumper on middle 2 pins, or manually press the firmware reload button.");
+       }
+       else
+       {
+               // 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();
+       }
+
+// Kiibohd mk20dx256vlh7
+#elif defined(_mk20dx256vlh7_)
+       // 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();
+
+// Teensy 3.0 and 3.1
+#else
        asm volatile("bkpt");
+#endif
 }