1 /* Copyright (C) 2014 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 #include "uart_serial.h"
23 #include <Lib/OutputLib.h>
24 #include <Lib/Interrupts.h>
25 #include <string.h> // For memcpy
27 // ----- Variables -----
29 #define uart0_buffer_size 128 // 128 byte buffer
30 volatile uint8_t uart0_buffer_head = 0;
31 volatile uint8_t uart0_buffer_tail = 0;
32 volatile uint8_t uart0_buffer_items = 0;
33 volatile uint8_t uart0_buffer[uart0_buffer_size];
35 volatile uint8_t uart_configured = 0;
39 // ----- Interrupt Functions -----
41 void uart0_status_isr()
43 cli(); // Disable Interrupts
45 // UART0_S1 must be read for the interrupt to be cleared
46 if ( UART0_S1 & ( UART_S1_RDRF | UART_S1_IDLE ) )
48 uint8_t available = UART0_RCFIFO;
50 // If there was actually nothing
55 UART0_CFIFO = UART_CFIFO_RXFLUSH;
60 // Read UART0 into buffer until FIFO is empty
61 while ( available-- > 0 )
63 uart0_buffer[uart0_buffer_tail++] = UART0_D;
66 // Wrap-around of tail pointer
67 if ( uart0_buffer_tail >= uart0_buffer_size )
69 uart0_buffer_tail = 0;
72 // Make sure the head pointer also moves if circular buffer is overwritten
73 if ( uart0_buffer_head == uart0_buffer_tail )
78 // Wrap-around of head pointer
79 if ( uart0_buffer_head >= uart0_buffer_size )
81 uart0_buffer_head = 0;
86 sei(); // Re-enable Interrupts
91 // ----- Functions -----
93 void uart_serial_setup()
95 // Indication that the UART is not ready yet
98 // Setup the the UART interface for keyboard data input
99 SIM_SCGC4 |= SIM_SCGC4_UART0; // Disable clock gating
102 #if defined(_mk20dx128vlf5_)
103 // Pin Setup for UART0
104 PORTA_PCR1 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(2); // RX Pin
105 PORTA_PCR2 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(2); // TX Pin
109 // Pin Setup for UART0
110 PORTB_PCR16 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin
111 PORTB_PCR17 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
114 // Setup baud rate - 115200 Baud
115 // 48 MHz / ( 16 * Baud ) = BDH/L
116 // Baud: 115200 -> 48 MHz / ( 16 * 115200 ) = 26.0416667
117 // Thus baud setting = 26
118 // NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
119 uint16_t baud = 26; // Max setting of 8191
120 UART0_BDH = (uint8_t)(baud >> 8);
121 UART0_BDL = (uint8_t)baud;
124 // 8 bit, No Parity, Idle Character bit after stop
125 UART0_C1 = UART_C1_ILT;
127 // Interrupt notification watermarks
131 // TX FIFO Disabled, TX FIFO Size 1 (Max 8 datawords), RX FIFO Enabled, RX FIFO Size 1 (Max 8 datawords)
136 UART0_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
138 // Reciever Inversion Disabled, LSBF
139 // UART_S2_RXINV UART_S2_MSBF
142 // Transmit Inversion Disabled
146 // TX Enabled, RX Enabled, RX Interrupt Enabled, Generate idles
147 // UART_C2_TE UART_C2_RE UART_C2_RIE UART_C2_ILIE
148 UART0_C2 = UART_C2_TE | UART_C2_RE | UART_C2_RIE | UART_C2_ILIE;
150 // Add interrupt to the vector table
151 NVIC_ENABLE_IRQ( IRQ_UART0_STATUS );
153 // UART is now ready to use
158 // Get the next character, or -1 if nothing received
159 int uart_serial_getchar()
161 if ( !uart_configured )
164 unsigned int value = -1;
166 // Check to see if the FIFO has characters
167 if ( uart0_buffer_items > 0 )
169 value = uart0_buffer[uart0_buffer_head++];
170 uart0_buffer_items--;
172 // Wrap-around of head pointer
173 if ( uart0_buffer_head >= uart0_buffer_size )
175 uart0_buffer_head = 0;
183 // Number of bytes available in the receive buffer
184 int uart_serial_available()
186 return uart0_buffer_items;
190 // Discard any buffered input
191 void uart_serial_flush_input()
193 uart0_buffer_head = 0;
194 uart0_buffer_tail = 0;
195 uart0_buffer_items = 0;
199 // Transmit a character. 0 returned on success, -1 on error
200 int uart_serial_putchar( uint8_t c )
202 if ( !uart_configured )
205 while ( !( UART0_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
212 int uart_serial_write( const void *buffer, uint32_t size )
214 if ( !uart_configured )
217 const uint8_t *data = (const uint8_t *)buffer;
218 uint32_t position = 0;
220 // While buffer is not empty and transmit buffer is
221 while ( position < size )
223 while ( !( UART0_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
224 UART0_D = data[position++];
231 void uart_serial_flush_output()
233 // Delay until buffer has been sent
234 while ( !( UART0_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
238 void uart_device_reload()
240 asm volatile("bkpt");