1 /* Copyright (C) 2015 by Jacob Alexander
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // ----- Includes -----
29 // ----- Defines -----
32 #if defined(_mk20dx256vlh7_) // UART2 Debug
33 #define UART_BDH UART2_BDH
34 #define UART_BDL UART2_BDL
35 #define UART_C1 UART2_C1
36 #define UART_C2 UART2_C2
37 #define UART_C3 UART2_C3
38 #define UART_C4 UART2_C4
39 #define UART_CFIFO UART2_CFIFO
40 #define UART_D UART2_D
41 #define UART_PFIFO UART2_PFIFO
42 #define UART_RCFIFO UART2_RCFIFO
43 #define UART_RWFIFO UART2_RWFIFO
44 #define UART_S1 UART2_S1
45 #define UART_S2 UART2_S2
46 #define UART_SFIFO UART2_SFIFO
47 #define UART_TWFIFO UART2_TWFIFO
49 #define SIM_SCGC4_UART SIM_SCGC4_UART2
50 #define IRQ_UART_STATUS IRQ_UART2_STATUS
53 #error "Bootloader UART Debug unsupported"
58 // ----- Functions -----
60 #if defined(_mk20dx256vlh7_)
62 void uart_serial_setup()
64 // Setup the the UART interface for keyboard data input
65 SIM_SCGC4 |= SIM_SCGC4_UART; // Disable clock gating
68 #if defined(_mk20dx256vlh7_)
69 // Pin Setup for UART2
70 PORTD_PCR3 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
74 #if defined(_mk20dx256vlh7_) // UART2 Debug
75 // Setup baud rate - 115200 Baud
77 // 36 MHz / ( 16 * Baud ) = BDH/L
78 // Baud: 115200 -> 36 MHz / ( 16 * 115200 ) = 19.53125
79 // Thus baud setting = 19
80 // NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
81 uint16_t baud = 19; // Max setting of 8191
82 UART_BDH = (uint8_t)(baud >> 8);
83 UART_BDL = (uint8_t)baud;
88 // 8 bit, No Parity, Idle Character bit after stop
89 UART_C1 = UART_C1_ILT;
91 // TX FIFO Enabled, TX FIFO Size 1 (Max 8 datawords)
96 UART_PFIFO = UART_PFIFO_TXFE;
98 // TX Enabled, RX Enabled, RX Interrupt Enabled, Generate idles
99 // UART_C2_TE UART_C2_RE UART_C2_RIE UART_C2_ILIE
100 UART_C2 = UART_C2_TE | UART_C2_ILIE;
104 int uart_serial_write( const void *buffer, uint32_t size )
106 const uint8_t *data = (const uint8_t *)buffer;
107 uint32_t position = 0;
109 // While buffer is not empty and transmit buffer is
110 while ( position < size )
112 while ( !( UART_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
113 UART_D = data[position++];
120 int Output_putstr( char* str )
124 // Count characters until NULL character, then send the amount counted
125 while ( str[count] != '\0' )
128 return uart_serial_write( str, count );
132 uint16_t lenStr( char* in )
137 // Loop until null is found
138 for ( pos = in; *pos; pos++ );
140 // Return the difference between the pointers of in and pos (which is the string length)
145 void revsStr( char* in )
153 // Loop through the string, and reverse the order of the characters
154 for ( i = 0, j = lenStr( in ) - 1; i < j; i++, j-- )
163 void hexToStr_op( uint32_t in, char* out, uint8_t op )
165 // Position container
168 // Evaluate through digits as hex
171 uint32_t cur = in % 16;
172 out[pos++] = cur + (( cur < 10 ) ? '0' : 'A' - 10);
174 while ( (in /= 16) > 0 );
176 // Output formatting options
183 case 2: // 8-bit padding
184 case 4: // 16-bit padding
185 case 8: // 32-bit padding
194 // Reverse the string to the correct order
199 void printHex_op( uint32_t in, uint8_t op )
201 // With an op of 1, the max number of characters is 6 + 1 for null
203 // op 2 and 4 require fewer characters (2+1 and 4+1 respectively)
207 hexToStr_op( in, tmpStr, op );
210 Output_putstr( tmpStr );