]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Output/uartOut/arm/uart_serial.c
mk20dx256vlh7 working!
[kiibohd-controller.git] / Output / uartOut / arm / uart_serial.c
1 /* Copyright (C) 2014-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 // Compiler Includes
25 #include <string.h> // For memcpy
26
27 // Project Includes
28 #include <Lib/OutputLib.h>
29 #include <Lib/Interrupts.h>
30
31 // Local Includes
32 #include "uart_serial.h"
33
34
35
36 // ----- Defines -----
37
38 // UART Configuration
39 #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // UART0 Debug
40 #define UART_BDH    UART0_BDH
41 #define UART_BDL    UART0_BDL
42 #define UART_C1     UART0_C1
43 #define UART_C2     UART0_C2
44 #define UART_C3     UART0_C3
45 #define UART_C4     UART0_C4
46 #define UART_CFIFO  UART0_CFIFO
47 #define UART_D      UART0_D
48 #define UART_PFIFO  UART0_PFIFO
49 #define UART_RCFIFO UART0_RCFIFO
50 #define UART_RWFIFO UART0_RWFIFO
51 #define UART_S1     UART0_S1
52 #define UART_S2     UART0_S2
53 #define UART_SFIFO  UART0_SFIFO
54 #define UART_TWFIFO UART0_TWFIFO
55
56 #define SIM_SCGC4_UART  SIM_SCGC4_UART0
57 #define IRQ_UART_STATUS IRQ_UART0_STATUS
58
59 #elif defined(_mk20dx256vlh7_) // UART2 Debug
60 #define UART_BDH    UART2_BDH
61 #define UART_BDL    UART2_BDL
62 #define UART_C1     UART2_C1
63 #define UART_C2     UART2_C2
64 #define UART_C3     UART2_C3
65 #define UART_C4     UART2_C4
66 #define UART_CFIFO  UART2_CFIFO
67 #define UART_D      UART2_D
68 #define UART_PFIFO  UART2_PFIFO
69 #define UART_RCFIFO UART2_RCFIFO
70 #define UART_RWFIFO UART2_RWFIFO
71 #define UART_S1     UART2_S1
72 #define UART_S2     UART2_S2
73 #define UART_SFIFO  UART2_SFIFO
74 #define UART_TWFIFO UART2_TWFIFO
75
76 #define SIM_SCGC4_UART  SIM_SCGC4_UART2
77 #define IRQ_UART_STATUS IRQ_UART2_STATUS
78
79 #endif
80
81
82
83 // ----- Variables -----
84
85 #define uart_buffer_size 128 // 128 byte buffer
86 volatile uint8_t uart_buffer_head = 0;
87 volatile uint8_t uart_buffer_tail = 0;
88 volatile uint8_t uart_buffer_items = 0;
89 volatile uint8_t uart_buffer[uart_buffer_size];
90
91 volatile uint8_t uart_configured = 0;
92
93
94
95 // ----- Interrupt Functions -----
96
97 #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // UART0 Debug
98 void uart0_status_isr()
99 #elif defined(_mk20dx256vlh7_) // UART2 Debug
100 void uart2_status_isr()
101 #endif
102 {
103         cli(); // Disable Interrupts
104
105         // UART0_S1 must be read for the interrupt to be cleared
106         if ( UART_S1 & ( UART_S1_RDRF | UART_S1_IDLE ) )
107         {
108                 uint8_t available = UART_RCFIFO;
109
110                 // If there was actually nothing
111                 if ( available == 0 )
112                 {
113                         // Cleanup
114                         available = UART_D;
115                         UART_CFIFO = UART_CFIFO_RXFLUSH;
116                         goto done;
117                 }
118
119                 // Read UART0 into buffer until FIFO is empty
120                 while ( available-- > 0 )
121                 {
122                         uart_buffer[uart_buffer_tail++] = UART_D;
123                         uart_buffer_items++;
124
125                         // Wrap-around of tail pointer
126                         if ( uart_buffer_tail >= uart_buffer_size )
127                         {
128                                 uart_buffer_tail = 0;
129                         }
130
131                         // Make sure the head pointer also moves if circular buffer is overwritten
132                         if ( uart_buffer_head == uart_buffer_tail )
133                         {
134                                 uart_buffer_head++;
135                         }
136
137                         // Wrap-around of head pointer
138                         if ( uart_buffer_head >= uart_buffer_size )
139                         {
140                                 uart_buffer_head = 0;
141                         }
142                 }
143         }
144
145 done:
146         sei(); // Re-enable Interrupts
147 }
148
149
150
151 // ----- Functions -----
152
153 void uart_serial_setup()
154 {
155         // Indication that the UART is not ready yet
156         uart_configured = 0;
157
158         // Setup the the UART interface for keyboard data input
159         SIM_SCGC4 |= SIM_SCGC4_UART; // Disable clock gating
160
161 // MCHCK / Kiibohd-dfu
162 #if defined(_mk20dx128vlf5_)
163         // Pin Setup for UART0
164         PORTA_PCR1 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(2); // RX Pin
165         PORTA_PCR2 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(2); // TX Pin
166
167 // Kiibohd-dfu
168 #elif defined(_mk20dx256vlh7_)
169         // Pin Setup for UART2
170         PORTD_PCR2 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin
171         PORTD_PCR3 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
172
173 // Teensy
174 #else
175         // Pin Setup for UART0
176         PORTB_PCR16 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin
177         PORTB_PCR17 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
178 #endif
179
180
181 #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // UART0 Debug
182         // Setup baud rate - 115200 Baud
183         // 48 MHz / ( 16 * Baud ) = BDH/L
184         // Baud: 115200 -> 48 MHz / ( 16 * 115200 ) = 26.0416667
185         // Thus baud setting = 26
186         // NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
187         uint16_t baud = 26; // Max setting of 8191
188         UART_BDH = (uint8_t)(baud >> 8);
189         UART_BDL = (uint8_t)baud;
190         UART_C4 = 0x02;
191
192 #elif defined(_mk20dx256vlh7_) // UART2 Debug
193         // Setup baud rate - 115200 Baud
194         // Uses Bus Clock
195         // 36 MHz / ( 16 * Baud ) = BDH/L
196         // Baud: 115200 -> 36 MHz / ( 16 * 115200 ) = 19.53125
197         // Thus baud setting = 19
198         // NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
199         uint16_t baud = 19; // Max setting of 8191
200         UART_BDH = (uint8_t)(baud >> 8);
201         UART_BDL = (uint8_t)baud;
202         UART_C4 = 0x11;
203
204 #endif
205
206         // 8 bit, No Parity, Idle Character bit after stop
207         UART_C1 = UART_C1_ILT;
208
209         // Interrupt notification watermarks
210 #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // UART0 Debug
211         UART_TWFIFO = 2;
212         UART_RWFIFO = 4;
213 #elif defined(_mk20dx256vlh7_) // UART2 Debug
214         // UART2 has a single byte FIFO
215         UART_TWFIFO = 1;
216         UART_RWFIFO = 1;
217 #endif
218
219         // TX FIFO Enabled, TX FIFO Size 1 (Max 8 datawords), RX FIFO Enabled, RX FIFO Size 1 (Max 8 datawords)
220         // TX/RX FIFO Size:
221         //  0x0 - 1 dataword
222         //  0x1 - 4 dataword
223         //  0x2 - 8 dataword
224         UART_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
225
226         // Reciever Inversion Disabled, LSBF
227         // UART_S2_RXINV UART_S2_MSBF
228         UART_S2 |= 0x00;
229
230         // Transmit Inversion Disabled
231         // UART_C3_TXINV
232         UART_C3 |= 0x00;
233
234         // TX Enabled, RX Enabled, RX Interrupt Enabled, Generate idles
235         // UART_C2_TE UART_C2_RE UART_C2_RIE UART_C2_ILIE
236         UART_C2 = UART_C2_TE | UART_C2_RE | UART_C2_RIE | UART_C2_ILIE;
237
238         // Add interrupt to the vector table
239         NVIC_ENABLE_IRQ( IRQ_UART_STATUS );
240
241         // UART is now ready to use
242         uart_configured = 1;
243 }
244
245
246 // Get the next character, or -1 if nothing received
247 int uart_serial_getchar()
248 {
249         if ( !uart_configured )
250                 return -1;
251
252         unsigned int value = -1;
253
254         // Check to see if the FIFO has characters
255         if ( uart_buffer_items > 0 )
256         {
257                 value = uart_buffer[uart_buffer_head++];
258                 uart_buffer_items--;
259
260                 // Wrap-around of head pointer
261                 if ( uart_buffer_head >= uart_buffer_size )
262                 {
263                         uart_buffer_head = 0;
264                 }
265         }
266
267         return value;
268 }
269
270
271 // Number of bytes available in the receive buffer
272 int uart_serial_available()
273 {
274         return uart_buffer_items;
275 }
276
277
278 // Discard any buffered input
279 void uart_serial_flush_input()
280 {
281         uart_buffer_head = 0;
282         uart_buffer_tail = 0;
283         uart_buffer_items = 0;
284 }
285
286
287 // Transmit a character.  0 returned on success, -1 on error
288 int uart_serial_putchar( uint8_t c )
289 {
290         if ( !uart_configured )
291                 return -1;
292
293         while ( !( UART_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
294         UART_D = c;
295
296         return 0;
297 }
298
299
300 int uart_serial_write( const void *buffer, uint32_t size )
301 {
302         if ( !uart_configured )
303                 return -1;
304
305         const uint8_t *data = (const uint8_t *)buffer;
306         uint32_t position = 0;
307
308         // While buffer is not empty and transmit buffer is
309         while ( position < size )
310         {
311                 while ( !( UART_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
312                 UART_D = data[position++];
313         }
314
315         return 0;
316 }
317
318
319 void uart_serial_flush_output()
320 {
321         // Delay until buffer has been sent
322         while ( !( UART_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
323 }
324
325
326 void uart_device_reload()
327 {
328         asm volatile("bkpt");
329 }
330