X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=tv_i2c_hack;a=blobdiff_plain;f=serial.c;fp=serial.c;h=0000000000000000000000000000000000000000;hp=3ca641a020c4fd78c51b8a26fea55e9a7093bc41;hb=3b128b470c001555ba1c69ba8b0c96994182f8a1;hpb=efb5b9c4333f6034595b30d7d549fab2b120d9c9 diff --git a/serial.c b/serial.c deleted file mode 100644 index 3ca641a..0000000 --- a/serial.c +++ /dev/null @@ -1,103 +0,0 @@ -#ifndef F_CPU -#ifdef XTAL -#define F_CPU XTAL -#else -#warning "compiled for 1mhz internal rc osc. serial comms won't work" -#define F_CPU 1000000 -#endif -#endif - -#include -#include -#include -#include -#include - -static int uart_send_char(char c, FILE *fp); -static int uart_get_char(FILE *fp); - -#define BUF_SZ 16 -#define BUF_IDX_MASK (BUF_SZ - 1) -#define NEXT_IDX(x) (((x) + 1) & BUF_IDX_MASK) -static char outbuf[BUF_SZ]; -static volatile unsigned char out_rd, out_wr; -static char inbuf[BUF_SZ]; -static volatile unsigned char in_rd, in_wr; - -static FILE std_stream = FDEV_SETUP_STREAM(uart_send_char, uart_get_char, _FDEV_SETUP_RW); - - - -void init_serial(long baud) -{ - unsigned int ubrr_val = F_CPU / 16 / baud - 1; - - power_usart0_enable(); - - /* set baud generator timer reset value */ - UBRR0H = (unsigned char)(ubrr_val >> 8); - UBRR0L = (unsigned char)ubrr_val; - - /* enable rx/tx and recv interrupt */ - UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0); - /* set frame format: 8n1 */ - UCSR0C = 3 << UCSZ00; - - stdin = stdout = stderr = &std_stream; -} - -int have_input(void) -{ - return in_wr != in_rd; -} - -static int uart_send_char(char c, FILE *fp) -{ - /*int next;*/ - if(c == '\n') uart_send_char('\r', fp); - - while((UCSR0A & (1 << UDRE0)) == 0); - UDR0 = (unsigned char)c; -#if 0 - next = NEXT_IDX(out_wr); - while(next == out_rd); - - outbuf[out_wr] = c; - out_wr = next; - - /* enable the Tx data register empty interrupt */ - UCSR0B |= 1 << UDRIE0; -#endif - return 0; -} - -static int uart_get_char(FILE *fp) -{ - char c; - - while(in_rd == in_wr); - - c = inbuf[in_rd]; - in_rd = NEXT_IDX(in_rd); - return c; -} - -ISR(USART_RX_vect) -{ - char c = UDR0; - - inbuf[in_wr] = c; - in_wr = NEXT_IDX(in_wr); -} - -/* USART Tx data register empty (can send more data) */ -ISR(USART_UDRE_vect) -{ - if(out_rd != out_wr) { - UDR0 = outbuf[out_rd]; - out_rd = NEXT_IDX(out_rd); - } else { - /* no more data to send for now, disable the interrupt */ - UCSR0B &= ~(1 << UDRIE0); - } -}