/* * ---------------------------------------------------------------------------- * "THE BEER-WARE LICENSE" (Revision 42): * wrote this file. As long as you retain this notice you * can do whatever you want with this stuff. If we meet some day, and you think * this stuff is worth it, you can buy me a beer in return. Joerg Wunsch * ---------------------------------------------------------------------------- * * Stdio demo, UART implementation * * $Id: uart.c,v 1.1 2005/12/28 21:38:59 joerg_wunsch Exp $ * * Mod for mega644 Bruce Land Jan2009 * interrupt-driven getchar added by BL * Circular buffer/ISR driven putchar added by Jeff Melville */ #define UART_USE_RING_BUF_TX /* CPU frequency */ //#define F_CPU 16000000UL // UART baud rate defined in settings //#define UART_BAUD 9600 #include #include #include //jsm - add for interrupt handling #include #include "trtuart.h" //jsm - create the circular buffer with in/out index #define TX_BUF_SIZE 200 static volatile unsigned int tx_in; static volatile unsigned int tx_out; static volatile char tx_buff [TX_BUF_SIZE]; /* * Initialize the UART to 9600 Bd, tx/rx, 8N1. */ void trt_uart_init(void) { #if F_CPU < 2000000UL && defined(U2X) UCSR0A = _BV(U2X); /* improve baud rate error by using 2x clk */ UBRR0L = (F_CPU / (8UL * UART_BAUD)) - 1; #else UBRR0L = (F_CPU / (16UL * UART_BAUD)) - 1; #endif UCSR0B = _BV(TXEN0) | _BV(RXEN0); /* tx/rx enable */ //Set up circular buffer state variables tx_in = 0; tx_out = 0; //enable receive ISR -- added for TRT UCSR0B |= (1<= (uint8_t)' ' && c <= (uint8_t)'\x7e') || c >= (uint8_t)'\xa0') { if (cp == b + RX_BUFSIZE - 1) uart_putchar('\a', stream); else { *cp++ = c; uart_putchar(c, stream); } continue; } switch (c) { case 'c' & 0x1f: return -1; case '\b': case '\x7f': if (cp > b) { uart_putchar('\b', stream); uart_putchar(' ', stream); uart_putchar('\b', stream); cp--; } break; case 'r' & 0x1f: uart_putchar('\r', stream); for (cp2 = b; cp2 < cp; cp2++) uart_putchar(*cp2, stream); break; case 'u' & 0x1f: while (cp > b) { uart_putchar('\b', stream); uart_putchar(' ', stream); uart_putchar('\b', stream); cp--; } break; case 'w' & 0x1f: while (cp > b && cp[-1] != ' ') { uart_putchar('\b', stream); uart_putchar(' ', stream); uart_putchar('\b', stream); cp--; } break; } } c = *rxp++; if (c == '\n') rxp = 0; return c; }