annotate Output/uartOut/arm/uart_serial.c @ 334:9ac304aa1ab5

Adding initial dfu-upload code and debugging for Bootloader.
author Jacob Alexander <haata@kiibohd.com>
date Mon, 27 Apr 2015 00:57:34 -0700
parents 5b66d1da5377
children e8841d3c6db5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
272
5b66d1da5377 Updating setup.cmake files to use Module hierarchy implicitly
Jacob Alexander <haata@kiibohd.com>
parents: 267
diff changeset
1 /* Copyright (C) 2014-2015 by Jacob Alexander
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
2 *
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
4 * of this software and associated documentation files (the "Software"), to deal
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
5 * in the Software without restriction, including without limitation the rights
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
7 * copies of the Software, and to permit persons to whom the Software is
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
8 * furnished to do so, subject to the following conditions:
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
9 *
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
10 * The above copyright notice and this permission notice shall be included in
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
11 * all copies or substantial portions of the Software.
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
12 *
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
19 * THE SOFTWARE.
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
20 */
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
21
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
22 // ----- Includes -----
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
23
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
24 // Compiler Includes
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
25 #include <string.h> // For memcpy
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
26
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
27 // Project Includes
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
28 #include <Lib/OutputLib.h>
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
29 #include <Lib/Interrupts.h>
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
30
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
31 // Local Includes
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
32 #include "uart_serial.h"
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
33
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
34
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
35
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
36 // ----- Defines -----
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
37
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
38 // UART Configuration
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
39 #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // UART0 Debug
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
40 #define UART_BDH UART0_BDH
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
41 #define UART_BDL UART0_BDL
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
42 #define UART_C1 UART0_C1
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
43 #define UART_C2 UART0_C2
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
44 #define UART_C3 UART0_C3
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
45 #define UART_C4 UART0_C4
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
46 #define UART_CFIFO UART0_CFIFO
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
47 #define UART_D UART0_D
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
48 #define UART_PFIFO UART0_PFIFO
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
49 #define UART_RCFIFO UART0_RCFIFO
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
50 #define UART_RWFIFO UART0_RWFIFO
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
51 #define UART_S1 UART0_S1
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
52 #define UART_S2 UART0_S2
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
53 #define UART_SFIFO UART0_SFIFO
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
54 #define UART_TWFIFO UART0_TWFIFO
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
55
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
56 #define SIM_SCGC4_UART SIM_SCGC4_UART0
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
57 #define IRQ_UART_STATUS IRQ_UART0_STATUS
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
58
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
59 #elif defined(_mk20dx256vlh7_) // UART2 Debug
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
60 #define UART_BDH UART2_BDH
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
61 #define UART_BDL UART2_BDL
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
62 #define UART_C1 UART2_C1
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
63 #define UART_C2 UART2_C2
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
64 #define UART_C3 UART2_C3
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
65 #define UART_C4 UART2_C4
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
66 #define UART_CFIFO UART2_CFIFO
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
67 #define UART_D UART2_D
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
68 #define UART_PFIFO UART2_PFIFO
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
69 #define UART_RCFIFO UART2_RCFIFO
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
70 #define UART_RWFIFO UART2_RWFIFO
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
71 #define UART_S1 UART2_S1
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
72 #define UART_S2 UART2_S2
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
73 #define UART_SFIFO UART2_SFIFO
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
74 #define UART_TWFIFO UART2_TWFIFO
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
75
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
76 #define SIM_SCGC4_UART SIM_SCGC4_UART2
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
77 #define IRQ_UART_STATUS IRQ_UART2_STATUS
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
78
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
79 #endif
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
80
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
81
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
82
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
83 // ----- Variables -----
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
84
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
85 #define uart_buffer_size 128 // 128 byte buffer
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
86 volatile uint8_t uart_buffer_head = 0;
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
87 volatile uint8_t uart_buffer_tail = 0;
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
88 volatile uint8_t uart_buffer_items = 0;
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
89 volatile uint8_t uart_buffer[uart_buffer_size];
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
90
173
afd0f94023ff Fixed bug with standalone UART CLI
Jacob Alexander <haata@kiibohd.com>
parents: 172
diff changeset
91 volatile uint8_t uart_configured = 0;
afd0f94023ff Fixed bug with standalone UART CLI
Jacob Alexander <haata@kiibohd.com>
parents: 172
diff changeset
92
afd0f94023ff Fixed bug with standalone UART CLI
Jacob Alexander <haata@kiibohd.com>
parents: 172
diff changeset
93
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
94
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
95 // ----- Interrupt Functions -----
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
96
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
97 #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // UART0 Debug
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
98 void uart0_status_isr()
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
99 #elif defined(_mk20dx256vlh7_) // UART2 Debug
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
100 void uart2_status_isr()
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
101 #endif
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
102 {
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
103 cli(); // Disable Interrupts
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
104
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
105 // UART0_S1 must be read for the interrupt to be cleared
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
106 if ( UART_S1 & ( UART_S1_RDRF | UART_S1_IDLE ) )
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
107 {
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
108 uint8_t available = UART_RCFIFO;
172
a30bddad4115 Adding UART and UART + USB muxed CLI output.
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
109
a30bddad4115 Adding UART and UART + USB muxed CLI output.
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
110 // If there was actually nothing
a30bddad4115 Adding UART and UART + USB muxed CLI output.
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
111 if ( available == 0 )
a30bddad4115 Adding UART and UART + USB muxed CLI output.
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
112 {
a30bddad4115 Adding UART and UART + USB muxed CLI output.
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
113 // Cleanup
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
114 available = UART_D;
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
115 UART_CFIFO = UART_CFIFO_RXFLUSH;
172
a30bddad4115 Adding UART and UART + USB muxed CLI output.
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
116 sei();
a30bddad4115 Adding UART and UART + USB muxed CLI output.
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
117 return;
a30bddad4115 Adding UART and UART + USB muxed CLI output.
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
118 }
a30bddad4115 Adding UART and UART + USB muxed CLI output.
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
119
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
120 // Read UART0 into buffer until FIFO is empty
172
a30bddad4115 Adding UART and UART + USB muxed CLI output.
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
121 while ( available-- > 0 )
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
122 {
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
123 uart_buffer[uart_buffer_tail++] = UART_D;
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
124 uart_buffer_items++;
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
125
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
126 // Wrap-around of tail pointer
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
127 if ( uart_buffer_tail >= uart_buffer_size )
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
128 {
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
129 uart_buffer_tail = 0;
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
130 }
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
131
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
132 // Make sure the head pointer also moves if circular buffer is overwritten
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
133 if ( uart_buffer_head == uart_buffer_tail )
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
134 {
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
135 uart_buffer_head++;
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
136 }
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
137
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
138 // Wrap-around of head pointer
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
139 if ( uart_buffer_head >= uart_buffer_size )
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
140 {
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
141 uart_buffer_head = 0;
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
142 }
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
143 }
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
144 }
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
145
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
146 sei(); // Re-enable Interrupts
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
147 }
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
148
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
149
173
afd0f94023ff Fixed bug with standalone UART CLI
Jacob Alexander <haata@kiibohd.com>
parents: 172
diff changeset
150
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
151 // ----- Functions -----
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
152
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
153 void uart_serial_setup()
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
154 {
173
afd0f94023ff Fixed bug with standalone UART CLI
Jacob Alexander <haata@kiibohd.com>
parents: 172
diff changeset
155 // Indication that the UART is not ready yet
afd0f94023ff Fixed bug with standalone UART CLI
Jacob Alexander <haata@kiibohd.com>
parents: 172
diff changeset
156 uart_configured = 0;
afd0f94023ff Fixed bug with standalone UART CLI
Jacob Alexander <haata@kiibohd.com>
parents: 172
diff changeset
157
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
158 // Setup the the UART interface for keyboard data input
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
159 SIM_SCGC4 |= SIM_SCGC4_UART; // Disable clock gating
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
160
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
161 // MCHCK / Kiibohd-dfu
174
848e1ef8966c McHCK now working with UART.
Jacob Alexander <haata@kiibohd.com>
parents: 173
diff changeset
162 #if defined(_mk20dx128vlf5_)
848e1ef8966c McHCK now working with UART.
Jacob Alexander <haata@kiibohd.com>
parents: 173
diff changeset
163 // Pin Setup for UART0
848e1ef8966c McHCK now working with UART.
Jacob Alexander <haata@kiibohd.com>
parents: 173
diff changeset
164 PORTA_PCR1 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(2); // RX Pin
848e1ef8966c McHCK now working with UART.
Jacob Alexander <haata@kiibohd.com>
parents: 173
diff changeset
165 PORTA_PCR2 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(2); // TX Pin
848e1ef8966c McHCK now working with UART.
Jacob Alexander <haata@kiibohd.com>
parents: 173
diff changeset
166
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
167 // Kiibohd-dfu
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
168 #elif defined(_mk20dx256vlh7_)
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
169 // Pin Setup for UART2
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
170 PORTD_PCR2 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
171 PORTD_PCR3 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
172
174
848e1ef8966c McHCK now working with UART.
Jacob Alexander <haata@kiibohd.com>
parents: 173
diff changeset
173 // Teensy
848e1ef8966c McHCK now working with UART.
Jacob Alexander <haata@kiibohd.com>
parents: 173
diff changeset
174 #else
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
175 // Pin Setup for UART0
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
176 PORTB_PCR16 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
177 PORTB_PCR17 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
174
848e1ef8966c McHCK now working with UART.
Jacob Alexander <haata@kiibohd.com>
parents: 173
diff changeset
178 #endif
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
179
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
180
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
181 #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // UART0 Debug
172
a30bddad4115 Adding UART and UART + USB muxed CLI output.
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
182 // Setup baud rate - 115200 Baud
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
183 // 48 MHz / ( 16 * Baud ) = BDH/L
172
a30bddad4115 Adding UART and UART + USB muxed CLI output.
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
184 // Baud: 115200 -> 48 MHz / ( 16 * 115200 ) = 26.0416667
a30bddad4115 Adding UART and UART + USB muxed CLI output.
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
185 // Thus baud setting = 26
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
186 // NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
172
a30bddad4115 Adding UART and UART + USB muxed CLI output.
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
187 uint16_t baud = 26; // Max setting of 8191
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
188 UART_BDH = (uint8_t)(baud >> 8);
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
189 UART_BDL = (uint8_t)baud;
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
190 UART_C4 = 0x02;
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
191
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
192 #elif defined(_mk20dx256vlh7_) // UART2 Debug
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
193 // Setup baud rate - 115200 Baud
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
194 // Uses Bus Clock
334
9ac304aa1ab5 Adding initial dfu-upload code and debugging for Bootloader.
Jacob Alexander <haata@kiibohd.com>
parents: 272
diff changeset
195 // 36 MHz / ( 16 * Baud ) = BDH/L
9ac304aa1ab5 Adding initial dfu-upload code and debugging for Bootloader.
Jacob Alexander <haata@kiibohd.com>
parents: 272
diff changeset
196 // Baud: 115200 -> 36 MHz / ( 16 * 115200 ) = 19.53125
9ac304aa1ab5 Adding initial dfu-upload code and debugging for Bootloader.
Jacob Alexander <haata@kiibohd.com>
parents: 272
diff changeset
197 // Thus baud setting = 19
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
198 // NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
334
9ac304aa1ab5 Adding initial dfu-upload code and debugging for Bootloader.
Jacob Alexander <haata@kiibohd.com>
parents: 272
diff changeset
199 uint16_t baud = 19; // Max setting of 8191
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
200 UART_BDH = (uint8_t)(baud >> 8);
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
201 UART_BDL = (uint8_t)baud;
334
9ac304aa1ab5 Adding initial dfu-upload code and debugging for Bootloader.
Jacob Alexander <haata@kiibohd.com>
parents: 272
diff changeset
202 UART_C4 = 0x11;
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
203
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
204 #endif
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
205
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
206 // 8 bit, No Parity, Idle Character bit after stop
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
207 UART_C1 = UART_C1_ILT;
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
208
174
848e1ef8966c McHCK now working with UART.
Jacob Alexander <haata@kiibohd.com>
parents: 173
diff changeset
209 // Interrupt notification watermarks
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
210 #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // UART0 Debug
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
211 UART_TWFIFO = 2;
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
212 UART_RWFIFO = 4;
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
213 #elif defined(_mk20dx256vlh7_) // UART2 Debug
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
214 // UART2 has a single byte FIFO
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
215 UART_TWFIFO = 1;
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
216 UART_RWFIFO = 1;
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
217 #endif
172
a30bddad4115 Adding UART and UART + USB muxed CLI output.
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
218
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
219 // TX FIFO Enabled, TX FIFO Size 1 (Max 8 datawords), RX FIFO Enabled, RX FIFO Size 1 (Max 8 datawords)
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
220 // TX/RX FIFO Size:
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
221 // 0x0 - 1 dataword
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
222 // 0x1 - 4 dataword
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
223 // 0x2 - 8 dataword
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
224 UART_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
225
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
226 // Reciever Inversion Disabled, LSBF
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
227 // UART_S2_RXINV UART_S2_MSBF
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
228 UART_S2 |= 0x00;
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
229
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
230 // Transmit Inversion Disabled
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
231 // UART_C3_TXINV
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
232 UART_C3 |= 0x00;
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
233
172
a30bddad4115 Adding UART and UART + USB muxed CLI output.
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
234 // TX Enabled, RX Enabled, RX Interrupt Enabled, Generate idles
a30bddad4115 Adding UART and UART + USB muxed CLI output.
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
235 // UART_C2_TE UART_C2_RE UART_C2_RIE UART_C2_ILIE
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
236 UART_C2 = UART_C2_TE | UART_C2_RE | UART_C2_RIE | UART_C2_ILIE;
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
237
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
238 // Add interrupt to the vector table
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
239 NVIC_ENABLE_IRQ( IRQ_UART_STATUS );
173
afd0f94023ff Fixed bug with standalone UART CLI
Jacob Alexander <haata@kiibohd.com>
parents: 172
diff changeset
240
afd0f94023ff Fixed bug with standalone UART CLI
Jacob Alexander <haata@kiibohd.com>
parents: 172
diff changeset
241 // UART is now ready to use
afd0f94023ff Fixed bug with standalone UART CLI
Jacob Alexander <haata@kiibohd.com>
parents: 172
diff changeset
242 uart_configured = 1;
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
243 }
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
244
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
245
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
246 // Get the next character, or -1 if nothing received
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
247 int uart_serial_getchar()
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
248 {
173
afd0f94023ff Fixed bug with standalone UART CLI
Jacob Alexander <haata@kiibohd.com>
parents: 172
diff changeset
249 if ( !uart_configured )
afd0f94023ff Fixed bug with standalone UART CLI
Jacob Alexander <haata@kiibohd.com>
parents: 172
diff changeset
250 return -1;
afd0f94023ff Fixed bug with standalone UART CLI
Jacob Alexander <haata@kiibohd.com>
parents: 172
diff changeset
251
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
252 unsigned int value = -1;
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
253
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
254 // Check to see if the FIFO has characters
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
255 if ( uart_buffer_items > 0 )
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
256 {
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
257 value = uart_buffer[uart_buffer_head++];
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
258 uart_buffer_items--;
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
259
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
260 // Wrap-around of head pointer
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
261 if ( uart_buffer_head >= uart_buffer_size )
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
262 {
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
263 uart_buffer_head = 0;
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
264 }
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
265 }
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
266
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
267 return value;
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
268 }
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
269
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
270
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
271 // Number of bytes available in the receive buffer
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
272 int uart_serial_available()
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
273 {
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
274 return uart_buffer_items;
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
275 }
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
276
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
277
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
278 // Discard any buffered input
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
279 void uart_serial_flush_input()
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
280 {
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
281 uart_buffer_head = 0;
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
282 uart_buffer_tail = 0;
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
283 uart_buffer_items = 0;
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
284 }
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
285
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
286
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
287 // Transmit a character. 0 returned on success, -1 on error
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
288 int uart_serial_putchar( uint8_t c )
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
289 {
173
afd0f94023ff Fixed bug with standalone UART CLI
Jacob Alexander <haata@kiibohd.com>
parents: 172
diff changeset
290 if ( !uart_configured )
afd0f94023ff Fixed bug with standalone UART CLI
Jacob Alexander <haata@kiibohd.com>
parents: 172
diff changeset
291 return -1;
afd0f94023ff Fixed bug with standalone UART CLI
Jacob Alexander <haata@kiibohd.com>
parents: 172
diff changeset
292
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
293 while ( !( UART_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
294 UART_D = c;
172
a30bddad4115 Adding UART and UART + USB muxed CLI output.
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
295
a30bddad4115 Adding UART and UART + USB muxed CLI output.
Jacob Alexander <haata@kiibohd.com>
parents: 171
diff changeset
296 return 0;
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
297 }
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
298
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
299
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
300 int uart_serial_write( const void *buffer, uint32_t size )
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
301 {
173
afd0f94023ff Fixed bug with standalone UART CLI
Jacob Alexander <haata@kiibohd.com>
parents: 172
diff changeset
302 if ( !uart_configured )
afd0f94023ff Fixed bug with standalone UART CLI
Jacob Alexander <haata@kiibohd.com>
parents: 172
diff changeset
303 return -1;
afd0f94023ff Fixed bug with standalone UART CLI
Jacob Alexander <haata@kiibohd.com>
parents: 172
diff changeset
304
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
305 const uint8_t *data = (const uint8_t *)buffer;
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
306 uint32_t position = 0;
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
307
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
308 // While buffer is not empty and transmit buffer is
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
309 while ( position < size )
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
310 {
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
311 while ( !( UART_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
312 UART_D = data[position++];
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
313 }
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
314
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
315 return 0;
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
316 }
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
317
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
318
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
319 void uart_serial_flush_output()
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
320 {
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
321 // Delay until buffer has been sent
267
4739cb8920b8 More preparation for mk20dx256vlh7
Jacob Alexander <haata@kiibohd.com>
parents: 174
diff changeset
322 while ( !( UART_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
171
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
323 }
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
324
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
325
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
326 void uart_device_reload()
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
327 {
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
328 asm volatile("bkpt");
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
329 }
aeac00164f2e Initial code for ARM UART output module (mainly for CLI)
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
330