diff cons.c @ 3:15d89caaf516

Add support for single UART devices, although untested apart from a compile. Doesn't break dual UART ones :) Also checks for PRR before setting it.
author darius@Inchoate
date Wed, 11 Mar 2009 17:28:39 +1030
parents 3879f487b661
children 095216e8453d
line wrap: on
line diff
--- a/cons.c	Wed Mar 11 17:10:13 2009 +1030
+++ b/cons.c	Wed Mar 11 17:28:39 2009 +1030
@@ -36,6 +36,10 @@
 
 #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)
 
+#ifdef UBRR0
+#define DUALUART
+#endif
+
 /* Receive buffer storage */
 consbuf_t cmd;
 
@@ -60,12 +64,22 @@
 
 void
 cons_init(void) {
+#ifdef DUALUART
     UBRR0 = UART_BAUD_SELECT(38400, F_CPU);
     
     /* Enable receiver and transmitter. Turn on rx interrupts */
     UCSR0A = 0;
     UCSR0B = _BV(RXEN0) | _BV(TXEN0) | _BV(RXCIE0);
     UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
+#else
+    UBRRH = UART_BAUD_SELECT(38400, F_CPU) >> 8;
+    UBRRL = (uint8_t)UART_BAUD_SELECT(38400, F_CPU);
+
+    /* Enable receiver and transmitter. Turn on rx interrupts */
+    UCSRA = 0;
+    UCSRB = _BV(RXEN) | _BV(TXEN) | _BV(RXCIE);
+    UCSRC = _BV(URSEL) | _BV(UCSZ1) | _BV(UCSZ0);
+#endif
 
     fdevopen(_putc, NULL); /* Open stdout */
     fdevopen(NULL, _getc); /* Open stdin */
@@ -73,8 +87,13 @@
 
 int
 cons_putc(char c) {
+#ifdef DUALUART
     loop_until_bit_is_set(UCSR0A, UDRE0);
     UDR0 = c;
+#else
+    loop_until_bit_is_set(UCSRA, UDRE);
+    UDR = c;
+#endif
 
     return(0);
 }
@@ -114,26 +133,41 @@
 
 char
 cons_getc(void) {
+#ifdef DUALUART
     while (!(UCSR0A & _BV(RXC0)))
 	;
-    
     return (UDR0);
+#else
+    while (!(UCSRA & _BV(RXC)))
+	;
+    return (UDR);
+#endif
 }
 
 /* Rx complete */
+#ifdef DUALUART
 ISR(USART0_RX_vect) {
-    volatile char pit;
+#else
+ISR(USART_RXC_vect) {
+#endif
     char c;
 
+#ifdef DUALUART
     while (UCSR0A & _BV(RXC0)) {
+#else
+    while (UCSRA & _BV(RXC)) {
+#endif
+
+#ifdef DUALUART
+	c = UDR0;
+#else
+	c = UDR;
+#endif
 	/* 255 means we're waiting for main to process the command,
-	   just throw stuff away
-	*/
-	if (cmd.state == 255) {
-	    pit = UDR0;
+	 * just throw stuff away
+	 */
+	if (cmd.state == 255)
 	    continue;
-	}
-	c = UDR0;
 	
 	/* End of line? */
 	if (c == '\n' || c == '\r') {
@@ -173,7 +207,10 @@
 }
 
 /* Tx complete */
+#ifdef DUALUART
 ISR(USART0_TX_vect) {
-	
+#else
+ISR(USART_TXC_vect) {
+#endif	
 }