summaryrefslogtreecommitdiff
path: root/tmk_core/common/uart.c
diff options
context:
space:
mode:
Diffstat (limited to 'tmk_core/common/uart.c')
-rw-r--r--tmk_core/common/uart.c132
1 files changed, 63 insertions, 69 deletions
diff --git a/tmk_core/common/uart.c b/tmk_core/common/uart.c
index c17649b082..f2e4bc4f34 100644
--- a/tmk_core/common/uart.c
+++ b/tmk_core/common/uart.c
@@ -3,17 +3,17 @@
/* UART Example for Teensy USB Development Board
* http://www.pjrc.com/teensy/
* Copyright (c) 2009 PJRC.COM, LLC
- *
+ *
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
- *
+ *
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
- *
+ *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -26,7 +26,6 @@
// Version 1.0: Initial Release
// Version 1.1: Add support for Teensy 2.0, minor optimizations
-
#include <avr/io.h>
#include <avr/interrupt.h>
@@ -44,86 +43,81 @@ static volatile uint8_t rx_buffer_head;
static volatile uint8_t rx_buffer_tail;
// Initialize the UART
-void uart_init(uint32_t baud)
-{
- cli();
- UBRR0 = (F_CPU / 4 / baud - 1) / 2;
- UCSR0A = (1<<U2X0);
- UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0);
- UCSR0C = (1<<UCSZ01) | (1<<UCSZ00);
- tx_buffer_head = tx_buffer_tail = 0;
- rx_buffer_head = rx_buffer_tail = 0;
- sei();
+void uart_init(uint32_t baud) {
+ cli();
+ UBRR0 = (F_CPU / 4 / baud - 1) / 2;
+ UCSR0A = (1 << U2X0);
+ UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);
+ UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
+ tx_buffer_head = tx_buffer_tail = 0;
+ rx_buffer_head = rx_buffer_tail = 0;
+ sei();
}
// Transmit a byte
-void uart_putchar(uint8_t c)
-{
- uint8_t i;
-
- i = tx_buffer_head + 1;
- if (i >= TX_BUFFER_SIZE) i = 0;
- while (tx_buffer_tail == i) ; // wait until space in buffer
- //cli();
- tx_buffer[i] = c;
- tx_buffer_head = i;
- UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0) | (1<<UDRIE0);
- //sei();
+void uart_putchar(uint8_t c) {
+ uint8_t i;
+
+ i = tx_buffer_head + 1;
+ if (i >= TX_BUFFER_SIZE) i = 0;
+ while (tx_buffer_tail == i)
+ ; // wait until space in buffer
+ // cli();
+ tx_buffer[i] = c;
+ tx_buffer_head = i;
+ UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0) | (1 << UDRIE0);
+ // sei();
}
// Receive a byte
-uint8_t uart_getchar(void)
-{
- uint8_t c, i;
-
- while (rx_buffer_head == rx_buffer_tail) ; // wait for character
- i = rx_buffer_tail + 1;
- if (i >= RX_BUFFER_SIZE) i = 0;
- c = rx_buffer[i];
- rx_buffer_tail = i;
- return c;
+uint8_t uart_getchar(void) {
+ uint8_t c, i;
+
+ while (rx_buffer_head == rx_buffer_tail)
+ ; // wait for character
+ i = rx_buffer_tail + 1;
+ if (i >= RX_BUFFER_SIZE) i = 0;
+ c = rx_buffer[i];
+ rx_buffer_tail = i;
+ return c;
}
// Return the number of bytes waiting in the receive buffer.
// Call this before uart_getchar() to check if it will need
// to wait for a byte to arrive.
-uint8_t uart_available(void)
-{
- uint8_t head, tail;
-
- head = rx_buffer_head;
- tail = rx_buffer_tail;
- if (head >= tail) return head - tail;
- return RX_BUFFER_SIZE + head - tail;
+uint8_t uart_available(void) {
+ uint8_t head, tail;
+
+ head = rx_buffer_head;
+ tail = rx_buffer_tail;
+ if (head >= tail) return head - tail;
+ return RX_BUFFER_SIZE + head - tail;
}
// Transmit Interrupt
-ISR(USART_UDRE_vect)
-{
- uint8_t i;
-
- if (tx_buffer_head == tx_buffer_tail) {
- // buffer is empty, disable transmit interrupt
- UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0);
- } else {
- i = tx_buffer_tail + 1;
- if (i >= TX_BUFFER_SIZE) i = 0;
- UDR0 = tx_buffer[i];
- tx_buffer_tail = i;
- }
+ISR(USART_UDRE_vect) {
+ uint8_t i;
+
+ if (tx_buffer_head == tx_buffer_tail) {
+ // buffer is empty, disable transmit interrupt
+ UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);
+ } else {
+ i = tx_buffer_tail + 1;
+ if (i >= TX_BUFFER_SIZE) i = 0;
+ UDR0 = tx_buffer[i];
+ tx_buffer_tail = i;
+ }
}
// Receive Interrupt
-ISR(USART_RX_vect)
-{
- uint8_t c, i;
-
- c = UDR0;
- i = rx_buffer_head + 1;
- if (i >= RX_BUFFER_SIZE) i = 0;
- if (i != rx_buffer_tail) {
- rx_buffer[i] = c;
- rx_buffer_head = i;
- }
+ISR(USART_RX_vect) {
+ uint8_t c, i;
+
+ c = UDR0;
+ i = rx_buffer_head + 1;
+ if (i >= RX_BUFFER_SIZE) i = 0;
+ if (i != rx_buffer_tail) {
+ rx_buffer[i] = c;
+ rx_buffer_head = i;
+ }
}
-