]> 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 461ef0d4b612435f8c734a134951bec635be4546..d06f2f59e10c121d47f2ba1cc1672829c38127df 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2014 by Jacob Alexander
+/* Copyright (C) 2014-2015 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
  * THE SOFTWARE.
  */
 
-#include "uart_serial.h"
+// ----- Includes -----
+
+// Compiler Includes
+#include <string.h> // For memcpy
+
+// Project Includes
 #include <Lib/OutputLib.h>
 #include <Lib/Interrupts.h>
-#include <string.h> // For memcpy
+#include <print.h>
+#include <kll_defs.h>
+
+// Local Includes
+#include "uart_serial.h"
+
+
+
+// ----- Defines -----
+
+// UART Configuration
+#if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // UART0 Debug
+#define UART_BDH    UART0_BDH
+#define UART_BDL    UART0_BDL
+#define UART_C1     UART0_C1
+#define UART_C2     UART0_C2
+#define UART_C3     UART0_C3
+#define UART_C4     UART0_C4
+#define UART_CFIFO  UART0_CFIFO
+#define UART_D      UART0_D
+#define UART_PFIFO  UART0_PFIFO
+#define UART_RCFIFO UART0_RCFIFO
+#define UART_RWFIFO UART0_RWFIFO
+#define UART_S1     UART0_S1
+#define UART_S2     UART0_S2
+#define UART_SFIFO  UART0_SFIFO
+#define UART_TWFIFO UART0_TWFIFO
+
+#define SIM_SCGC4_UART  SIM_SCGC4_UART0
+#define IRQ_UART_STATUS IRQ_UART0_STATUS
+
+#elif defined(_mk20dx256vlh7_) // UART2 Debug
+#define UART_BDH    UART2_BDH
+#define UART_BDL    UART2_BDL
+#define UART_C1     UART2_C1
+#define UART_C2     UART2_C2
+#define UART_C3     UART2_C3
+#define UART_C4     UART2_C4
+#define UART_CFIFO  UART2_CFIFO
+#define UART_D      UART2_D
+#define UART_PFIFO  UART2_PFIFO
+#define UART_RCFIFO UART2_RCFIFO
+#define UART_RWFIFO UART2_RWFIFO
+#define UART_S1     UART2_S1
+#define UART_S2     UART2_S2
+#define UART_SFIFO  UART2_SFIFO
+#define UART_TWFIFO UART2_TWFIFO
+
+#define SIM_SCGC4_UART  SIM_SCGC4_UART2
+#define IRQ_UART_STATUS IRQ_UART2_STATUS
+
+#endif
+
+
 
 // ----- Variables -----
 
-#define uart0_buffer_size 128 // 128 byte buffer
-volatile uint8_t uart0_buffer_head = 0;
-volatile uint8_t uart0_buffer_tail = 0;
-volatile uint8_t uart0_buffer_items = 0;
-volatile uint8_t uart0_buffer[uart0_buffer_size];
+#define uart_buffer_size 128 // 128 byte buffer
+volatile uint8_t uart_buffer_head = 0;
+volatile uint8_t uart_buffer_tail = 0;
+volatile uint8_t uart_buffer_items = 0;
+volatile uint8_t uart_buffer[uart_buffer_size];
 
 volatile uint8_t uart_configured = 0;
 
@@ -38,51 +96,55 @@ volatile uint8_t uart_configured = 0;
 
 // ----- Interrupt Functions -----
 
+#if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // UART0 Debug
 void uart0_status_isr()
+#elif defined(_mk20dx256vlh7_) // UART2 Debug
+void uart2_status_isr()
+#endif
 {
        cli(); // Disable Interrupts
 
        // UART0_S1 must be read for the interrupt to be cleared
-       if ( UART0_S1 & ( UART_S1_RDRF | UART_S1_IDLE ) )
+       if ( UART_S1 & ( UART_S1_RDRF | UART_S1_IDLE ) )
        {
-               uint8_t available = UART0_RCFIFO;
+               uint8_t available = UART_RCFIFO;
 
                // If there was actually nothing
                if ( available == 0 )
                {
                        // Cleanup
-                       available = UART0_D;
-                       UART0_CFIFO = UART_CFIFO_RXFLUSH;
-                       sei();
-                       return;
+                       available = UART_D;
+                       UART_CFIFO = UART_CFIFO_RXFLUSH;
+                       goto done;
                }
 
                // Read UART0 into buffer until FIFO is empty
                while ( available-- > 0 )
                {
-                       uart0_buffer[uart0_buffer_tail++] = UART0_D;
-                       uart0_buffer_items++;
+                       uart_buffer[uart_buffer_tail++] = UART_D;
+                       uart_buffer_items++;
 
                        // Wrap-around of tail pointer
-                       if ( uart0_buffer_tail >= uart0_buffer_size )
+                       if ( uart_buffer_tail >= uart_buffer_size )
                        {
-                               uart0_buffer_tail = 0;
+                               uart_buffer_tail = 0;
                        }
 
                        // Make sure the head pointer also moves if circular buffer is overwritten
-                       if ( uart0_buffer_head == uart0_buffer_tail )
+                       if ( uart_buffer_head == uart_buffer_tail )
                        {
-                               uart0_buffer_head++;
+                               uart_buffer_head++;
                        }
 
                        // Wrap-around of head pointer
-                       if ( uart0_buffer_head >= uart0_buffer_size )
+                       if ( uart_buffer_head >= uart_buffer_size )
                        {
-                               uart0_buffer_head = 0;
+                               uart_buffer_head = 0;
                        }
                }
        }
 
+done:
        sei(); // Re-enable Interrupts
 }
 
@@ -96,50 +158,87 @@ void uart_serial_setup()
        uart_configured = 0;
 
        // Setup the the UART interface for keyboard data input
-       SIM_SCGC4 |= SIM_SCGC4_UART0; // Disable clock gating
+       SIM_SCGC4 |= SIM_SCGC4_UART; // Disable clock gating
 
+// MCHCK / Kiibohd-dfu
+#if defined(_mk20dx128vlf5_)
+       // Pin Setup for UART0
+       PORTA_PCR1 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(2); // RX Pin
+       PORTA_PCR2 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(2); // TX Pin
+
+// Kiibohd-dfu
+#elif defined(_mk20dx256vlh7_)
+       // Pin Setup for UART2
+       PORTD_PCR2 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin
+       PORTD_PCR3 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
+
+// Teensy
+#else
        // Pin Setup for UART0
        PORTB_PCR16 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin
        PORTB_PCR17 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
+#endif
+
 
+#if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // UART0 Debug
        // Setup baud rate - 115200 Baud
        // 48 MHz / ( 16 * Baud ) = BDH/L
        // Baud: 115200 -> 48 MHz / ( 16 * 115200 ) = 26.0416667
        // Thus baud setting = 26
        // NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
        uint16_t baud = 26; // Max setting of 8191
-       UART0_BDH = (uint8_t)(baud >> 8);
-       UART0_BDL = (uint8_t)baud;
-       UART0_C4 = 0x02;
+       UART_BDH = (uint8_t)(baud >> 8);
+       UART_BDL = (uint8_t)baud;
+       UART_C4 = 0x02;
 
-       // 8 bit, No Parity, Idle Character bit after stop
-       UART0_C1 = UART_C1_ILT;
+#elif defined(_mk20dx256vlh7_) // UART2 Debug
+       // Setup baud rate - 115200 Baud
+       // Uses Bus Clock
+       // 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 = 19; // Max setting of 8191
+       UART_BDH = (uint8_t)(baud >> 8);
+       UART_BDL = (uint8_t)baud;
+       UART_C4 = 0x11;
 
-       // Interrupt notification watermark
-       UART0_TWFIFO = 2;
-       UART0_RWFIFO = 4;
+#endif
 
-       // TX FIFO Disabled, TX FIFO Size 1 (Max 8 datawords), RX FIFO Enabled, RX FIFO Size 1 (Max 8 datawords)
+       // 8 bit, No Parity, Idle Character bit after stop
+       UART_C1 = UART_C1_ILT;
+
+       // Interrupt notification watermarks
+#if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // UART0 Debug
+       UART_TWFIFO = 2;
+       UART_RWFIFO = 4;
+#elif defined(_mk20dx256vlh7_) // UART2 Debug
+       // UART2 has a single byte FIFO
+       UART_TWFIFO = 1;
+       UART_RWFIFO = 1;
+#endif
+
+       // TX FIFO Enabled, TX FIFO Size 1 (Max 8 datawords), RX FIFO Enabled, RX FIFO Size 1 (Max 8 datawords)
        // TX/RX FIFO Size:
        //  0x0 - 1 dataword
        //  0x1 - 4 dataword
        //  0x2 - 8 dataword
-       UART0_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
+       UART_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
 
        // Reciever Inversion Disabled, LSBF
        // UART_S2_RXINV UART_S2_MSBF
-       UART0_S2 |= 0x00;
+       UART_S2 |= 0x00;
 
        // Transmit Inversion Disabled
        // UART_C3_TXINV
-       UART0_C3 |= 0x00;
+       UART_C3 |= 0x00;
 
        // TX Enabled, RX Enabled, RX Interrupt Enabled, Generate idles
        // UART_C2_TE UART_C2_RE UART_C2_RIE UART_C2_ILIE
-       UART0_C2 = UART_C2_TE | UART_C2_RE | UART_C2_RIE | UART_C2_ILIE;
+       UART_C2 = UART_C2_TE | UART_C2_RE | UART_C2_RIE | UART_C2_ILIE;
 
        // Add interrupt to the vector table
-       NVIC_ENABLE_IRQ( IRQ_UART0_STATUS );
+       NVIC_ENABLE_IRQ( IRQ_UART_STATUS );
 
        // UART is now ready to use
        uart_configured = 1;
@@ -155,15 +254,15 @@ int uart_serial_getchar()
        unsigned int value = -1;
 
        // Check to see if the FIFO has characters
-       if ( uart0_buffer_items > 0 )
+       if ( uart_buffer_items > 0 )
        {
-               value = uart0_buffer[uart0_buffer_head++];
-               uart0_buffer_items--;
+               value = uart_buffer[uart_buffer_head++];
+               uart_buffer_items--;
 
                // Wrap-around of head pointer
-               if ( uart0_buffer_head >= uart0_buffer_size )
+               if ( uart_buffer_head >= uart_buffer_size )
                {
-                       uart0_buffer_head = 0;
+                       uart_buffer_head = 0;
                }
        }
 
@@ -174,16 +273,16 @@ int uart_serial_getchar()
 // Number of bytes available in the receive buffer
 int uart_serial_available()
 {
-       return uart0_buffer_items;
+       return uart_buffer_items;
 }
 
 
 // Discard any buffered input
 void uart_serial_flush_input()
 {
-       uart0_buffer_head = 0;
-       uart0_buffer_tail = 0;
-       uart0_buffer_items = 0;
+       uart_buffer_head = 0;
+       uart_buffer_tail = 0;
+       uart_buffer_items = 0;
 }
 
 
@@ -193,8 +292,8 @@ int uart_serial_putchar( uint8_t c )
        if ( !uart_configured )
                return -1;
 
-       while ( !( UART0_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
-       UART0_D = c;
+       while ( !( UART_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
+       UART_D = c;
 
        return 0;
 }
@@ -211,8 +310,8 @@ int uart_serial_write( const void *buffer, uint32_t size )
        // While buffer is not empty and transmit buffer is
        while ( position < size )
        {
-               while ( !( UART0_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
-               UART0_D = data[position++];
+               while ( !( UART_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
+               UART_D = data[position++];
        }
 
        return 0;
@@ -222,12 +321,54 @@ int uart_serial_write( const void *buffer, uint32_t size )
 void uart_serial_flush_output()
 {
        // Delay until buffer has been sent
-       while ( !( UART0_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
+       while ( !( UART_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
 }
 
 
 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
 }