diff options
Diffstat (limited to 'common/uart.c')
| -rw-r--r-- | common/uart.c | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/common/uart.c b/common/uart.c new file mode 100644 index 000000000..c17649b08 --- /dev/null +++ b/common/uart.c | |||
| @@ -0,0 +1,129 @@ | |||
| 1 | // TODO: Teensy support(ATMega32u4/AT90USB128) | ||
| 2 | // Fixed for Arduino Duemilanove ATmega168p by Jun Wako | ||
| 3 | /* UART Example for Teensy USB Development Board | ||
| 4 | * http://www.pjrc.com/teensy/ | ||
| 5 | * Copyright (c) 2009 PJRC.COM, LLC | ||
| 6 | * | ||
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 8 | * of this software and associated documentation files (the "Software"), to deal | ||
| 9 | * in the Software without restriction, including without limitation the rights | ||
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 11 | * copies of the Software, and to permit persons to whom the Software is | ||
| 12 | * furnished to do so, subject to the following conditions: | ||
| 13 | * | ||
| 14 | * The above copyright notice and this permission notice shall be included in | ||
| 15 | * all copies or substantial portions of the Software. | ||
| 16 | * | ||
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| 23 | * THE SOFTWARE. | ||
| 24 | */ | ||
| 25 | |||
| 26 | // Version 1.0: Initial Release | ||
| 27 | // Version 1.1: Add support for Teensy 2.0, minor optimizations | ||
| 28 | |||
| 29 | |||
| 30 | #include <avr/io.h> | ||
| 31 | #include <avr/interrupt.h> | ||
| 32 | |||
| 33 | #include "uart.h" | ||
| 34 | |||
| 35 | // These buffers may be any size from 2 to 256 bytes. | ||
| 36 | #define RX_BUFFER_SIZE 64 | ||
| 37 | #define TX_BUFFER_SIZE 40 | ||
| 38 | |||
| 39 | static volatile uint8_t tx_buffer[TX_BUFFER_SIZE]; | ||
| 40 | static volatile uint8_t tx_buffer_head; | ||
| 41 | static volatile uint8_t tx_buffer_tail; | ||
| 42 | static volatile uint8_t rx_buffer[RX_BUFFER_SIZE]; | ||
| 43 | static volatile uint8_t rx_buffer_head; | ||
| 44 | static volatile uint8_t rx_buffer_tail; | ||
| 45 | |||
| 46 | // Initialize the UART | ||
| 47 | void uart_init(uint32_t baud) | ||
| 48 | { | ||
| 49 | cli(); | ||
| 50 | UBRR0 = (F_CPU / 4 / baud - 1) / 2; | ||
| 51 | UCSR0A = (1<<U2X0); | ||
| 52 | UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0); | ||
| 53 | UCSR0C = (1<<UCSZ01) | (1<<UCSZ00); | ||
| 54 | tx_buffer_head = tx_buffer_tail = 0; | ||
| 55 | rx_buffer_head = rx_buffer_tail = 0; | ||
| 56 | sei(); | ||
| 57 | } | ||
| 58 | |||
| 59 | // Transmit a byte | ||
| 60 | void uart_putchar(uint8_t c) | ||
| 61 | { | ||
| 62 | uint8_t i; | ||
| 63 | |||
| 64 | i = tx_buffer_head + 1; | ||
| 65 | if (i >= TX_BUFFER_SIZE) i = 0; | ||
| 66 | while (tx_buffer_tail == i) ; // wait until space in buffer | ||
| 67 | //cli(); | ||
| 68 | tx_buffer[i] = c; | ||
| 69 | tx_buffer_head = i; | ||
| 70 | UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0) | (1<<UDRIE0); | ||
| 71 | //sei(); | ||
| 72 | } | ||
| 73 | |||
| 74 | // Receive a byte | ||
| 75 | uint8_t uart_getchar(void) | ||
| 76 | { | ||
| 77 | uint8_t c, i; | ||
| 78 | |||
| 79 | while (rx_buffer_head == rx_buffer_tail) ; // wait for character | ||
| 80 | i = rx_buffer_tail + 1; | ||
| 81 | if (i >= RX_BUFFER_SIZE) i = 0; | ||
| 82 | c = rx_buffer[i]; | ||
| 83 | rx_buffer_tail = i; | ||
| 84 | return c; | ||
| 85 | } | ||
| 86 | |||
| 87 | // Return the number of bytes waiting in the receive buffer. | ||
| 88 | // Call this before uart_getchar() to check if it will need | ||
| 89 | // to wait for a byte to arrive. | ||
| 90 | uint8_t uart_available(void) | ||
| 91 | { | ||
| 92 | uint8_t head, tail; | ||
| 93 | |||
| 94 | head = rx_buffer_head; | ||
| 95 | tail = rx_buffer_tail; | ||
| 96 | if (head >= tail) return head - tail; | ||
| 97 | return RX_BUFFER_SIZE + head - tail; | ||
| 98 | } | ||
| 99 | |||
| 100 | // Transmit Interrupt | ||
| 101 | ISR(USART_UDRE_vect) | ||
| 102 | { | ||
| 103 | uint8_t i; | ||
| 104 | |||
| 105 | if (tx_buffer_head == tx_buffer_tail) { | ||
| 106 | // buffer is empty, disable transmit interrupt | ||
| 107 | UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0); | ||
| 108 | } else { | ||
| 109 | i = tx_buffer_tail + 1; | ||
| 110 | if (i >= TX_BUFFER_SIZE) i = 0; | ||
| 111 | UDR0 = tx_buffer[i]; | ||
| 112 | tx_buffer_tail = i; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | // Receive Interrupt | ||
| 117 | ISR(USART_RX_vect) | ||
| 118 | { | ||
| 119 | uint8_t c, i; | ||
| 120 | |||
| 121 | c = UDR0; | ||
| 122 | i = rx_buffer_head + 1; | ||
| 123 | if (i >= RX_BUFFER_SIZE) i = 0; | ||
| 124 | if (i != rx_buffer_tail) { | ||
| 125 | rx_buffer[i] = c; | ||
| 126 | rx_buffer_head = i; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
