Mercurial > louis > kiibohd-controller
comparison Bootloader/debug.c @ 339:2f415c0b4b28
Adding missing debug header.
author | Jacob Alexander <haata@kiibohd.com> |
---|---|
date | Tue, 09 Jun 2015 10:02:05 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
338:80bca7f4808b | 339:2f415c0b4b28 |
---|---|
1 /* Copyright (C) 2015 by Jacob Alexander | |
2 * | |
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: | |
9 * | |
10 * The above copyright notice and this permission notice shall be included in | |
11 * all copies or substantial portions of the Software. | |
12 * | |
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 | |
19 * THE SOFTWARE. | |
20 */ | |
21 | |
22 // ----- Includes ----- | |
23 | |
24 // Local Includes | |
25 #include "mchck.h" | |
26 | |
27 | |
28 | |
29 // ----- Defines ----- | |
30 | |
31 // UART Configuration | |
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 | |
48 | |
49 #define SIM_SCGC4_UART SIM_SCGC4_UART2 | |
50 #define IRQ_UART_STATUS IRQ_UART2_STATUS | |
51 | |
52 #else | |
53 #error "Bootloader UART Debug unsupported" | |
54 #endif | |
55 | |
56 | |
57 | |
58 // ----- Functions ----- | |
59 | |
60 #if defined(_mk20dx256vlh7_) | |
61 | |
62 void uart_serial_setup() | |
63 { | |
64 // Setup the the UART interface for keyboard data input | |
65 SIM_SCGC4 |= SIM_SCGC4_UART; // Disable clock gating | |
66 | |
67 // Kiibohd-dfu | |
68 #if defined(_mk20dx256vlh7_) | |
69 // Pin Setup for UART2 | |
70 PORTD_PCR3 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin | |
71 #endif | |
72 | |
73 | |
74 #if defined(_mk20dx256vlh7_) // UART2 Debug | |
75 // Setup baud rate - 115200 Baud | |
76 // Uses Bus Clock | |
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; | |
84 UART_C4 = 0x11; | |
85 | |
86 #endif | |
87 | |
88 // 8 bit, No Parity, Idle Character bit after stop | |
89 UART_C1 = UART_C1_ILT; | |
90 | |
91 // TX FIFO Enabled, TX FIFO Size 1 (Max 8 datawords) | |
92 // TX/RX FIFO Size: | |
93 // 0x0 - 1 dataword | |
94 // 0x1 - 4 dataword | |
95 // 0x2 - 8 dataword | |
96 UART_PFIFO = UART_PFIFO_TXFE; | |
97 | |
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; | |
101 } | |
102 | |
103 | |
104 int uart_serial_write( const void *buffer, uint32_t size ) | |
105 { | |
106 const uint8_t *data = (const uint8_t *)buffer; | |
107 uint32_t position = 0; | |
108 | |
109 // While buffer is not empty and transmit buffer is | |
110 while ( position < size ) | |
111 { | |
112 while ( !( UART_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send | |
113 UART_D = data[position++]; | |
114 } | |
115 | |
116 return 0; | |
117 } | |
118 | |
119 | |
120 int Output_putstr( char* str ) | |
121 { | |
122 uint32_t count = 0; | |
123 | |
124 // Count characters until NULL character, then send the amount counted | |
125 while ( str[count] != '\0' ) | |
126 count++; | |
127 | |
128 return uart_serial_write( str, count ); | |
129 } | |
130 | |
131 | |
132 uint16_t lenStr( char* in ) | |
133 { | |
134 // Iterator | |
135 char *pos; | |
136 | |
137 // Loop until null is found | |
138 for ( pos = in; *pos; pos++ ); | |
139 | |
140 // Return the difference between the pointers of in and pos (which is the string length) | |
141 return (pos - in); | |
142 } | |
143 | |
144 | |
145 void revsStr( char* in ) | |
146 { | |
147 // Iterators | |
148 int i, j; | |
149 | |
150 // Temp storage | |
151 char c; | |
152 | |
153 // Loop through the string, and reverse the order of the characters | |
154 for ( i = 0, j = lenStr( in ) - 1; i < j; i++, j-- ) | |
155 { | |
156 c = in[i]; | |
157 in[i] = in[j]; | |
158 in[j] = c; | |
159 } | |
160 } | |
161 | |
162 | |
163 void hexToStr_op( uint32_t in, char* out, uint8_t op ) | |
164 { | |
165 // Position container | |
166 uint32_t pos = 0; | |
167 | |
168 // Evaluate through digits as hex | |
169 do | |
170 { | |
171 uint32_t cur = in % 16; | |
172 out[pos++] = cur + (( cur < 10 ) ? '0' : 'A' - 10); | |
173 } | |
174 while ( (in /= 16) > 0 ); | |
175 | |
176 // Output formatting options | |
177 switch ( op ) | |
178 { | |
179 case 1: // Add 0x | |
180 out[pos++] = 'x'; | |
181 out[pos++] = '0'; | |
182 break; | |
183 case 2: // 8-bit padding | |
184 case 4: // 16-bit padding | |
185 case 8: // 32-bit padding | |
186 while ( pos < op ) | |
187 out[pos++] = '0'; | |
188 break; | |
189 } | |
190 | |
191 // Append null | |
192 out[pos] = '\0'; | |
193 | |
194 // Reverse the string to the correct order | |
195 revsStr( out ); | |
196 } | |
197 | |
198 | |
199 void printHex_op( uint32_t in, uint8_t op ) | |
200 { | |
201 // With an op of 1, the max number of characters is 6 + 1 for null | |
202 // e.g. "0xFFFF\0" | |
203 // op 2 and 4 require fewer characters (2+1 and 4+1 respectively) | |
204 char tmpStr[7]; | |
205 | |
206 // Convert number | |
207 hexToStr_op( in, tmpStr, op ); | |
208 | |
209 // Print number | |
210 Output_putstr( tmpStr ); | |
211 } | |
212 | |
213 #endif | |
214 |