From 3c67ba97955922e6047deae561ac7106bea14a91 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 22 Feb 2012 21:27:27 +0000 Subject: moved cmx589 sample code software directory git-svn-id: https://svn.spreadspace.org/mur.sat@248 7de4ea59-55d0-425e-a1af-a3118ea81d4c --- software/cmx589a_teensy_test/uart.c | 134 ++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 software/cmx589a_teensy_test/uart.c (limited to 'software/cmx589a_teensy_test/uart.c') diff --git a/software/cmx589a_teensy_test/uart.c b/software/cmx589a_teensy_test/uart.c new file mode 100644 index 0000000..94afe7e --- /dev/null +++ b/software/cmx589a_teensy_test/uart.c @@ -0,0 +1,134 @@ +/* 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 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +// Version 1.0: Initial Release +// Version 1.1: Add support for Teensy 2.0, minor optimizations + + +#include +#include + +#include "uart.h" + +// These buffers may be any size from 2 to 256 bytes. +#define RX_BUFFER_SIZE 64 +#define TX_BUFFER_SIZE 40 + +static volatile uint8_t tx_buffer[TX_BUFFER_SIZE]; +static volatile uint8_t tx_buffer_head; +static volatile uint8_t tx_buffer_tail; +static volatile uint8_t rx_buffer[RX_BUFFER_SIZE]; +static volatile uint8_t rx_buffer_head; +static volatile uint8_t rx_buffer_tail; + +// Initialize the UART +void uart_init(uint32_t baud) +{ + cli(); + UBRR1 = (F_CPU / 4 / baud - 1) / 2; + UCSR1A = (1<= TX_BUFFER_SIZE) i = 0; + while (tx_buffer_tail == i) ; // wait until space in buffer + //cli(); + tx_buffer[i] = c; + tx_buffer_head = i; + UCSR1B = (1<= 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; +} + +// Transmit Interrupt +ISR(USART1_UDRE_vect) +{ + uint8_t i; + + if (tx_buffer_head == tx_buffer_tail) { + // buffer is empty, disable transmit interrupt + UCSR1B = (1<= TX_BUFFER_SIZE) i = 0; + UDR1 = tx_buffer[i]; + tx_buffer_tail = i; + } +} + +// Receive Interrupt +ISR(USART1_RX_vect) +{ + uint8_t c, i; + + c = UDR1; + 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; + } +} + -- cgit v1.2.3