comparison 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
comparison
equal deleted inserted replaced
2:43d3b2bef999 3:15d89caaf516
34 #include <avr/pgmspace.h> 34 #include <avr/pgmspace.h>
35 #include "cons.h" 35 #include "cons.h"
36 36
37 #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1) 37 #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)
38 38
39 #ifdef UBRR0
40 #define DUALUART
41 #endif
42
39 /* Receive buffer storage */ 43 /* Receive buffer storage */
40 consbuf_t cmd; 44 consbuf_t cmd;
41 45
42 /* 46 /*
43 * Stub to use with fdevopen 47 * Stub to use with fdevopen
58 return(cons_getc()); 62 return(cons_getc());
59 } 63 }
60 64
61 void 65 void
62 cons_init(void) { 66 cons_init(void) {
67 #ifdef DUALUART
63 UBRR0 = UART_BAUD_SELECT(38400, F_CPU); 68 UBRR0 = UART_BAUD_SELECT(38400, F_CPU);
64 69
65 /* Enable receiver and transmitter. Turn on rx interrupts */ 70 /* Enable receiver and transmitter. Turn on rx interrupts */
66 UCSR0A = 0; 71 UCSR0A = 0;
67 UCSR0B = _BV(RXEN0) | _BV(TXEN0) | _BV(RXCIE0); 72 UCSR0B = _BV(RXEN0) | _BV(TXEN0) | _BV(RXCIE0);
68 UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); 73 UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
74 #else
75 UBRRH = UART_BAUD_SELECT(38400, F_CPU) >> 8;
76 UBRRL = (uint8_t)UART_BAUD_SELECT(38400, F_CPU);
77
78 /* Enable receiver and transmitter. Turn on rx interrupts */
79 UCSRA = 0;
80 UCSRB = _BV(RXEN) | _BV(TXEN) | _BV(RXCIE);
81 UCSRC = _BV(URSEL) | _BV(UCSZ1) | _BV(UCSZ0);
82 #endif
69 83
70 fdevopen(_putc, NULL); /* Open stdout */ 84 fdevopen(_putc, NULL); /* Open stdout */
71 fdevopen(NULL, _getc); /* Open stdin */ 85 fdevopen(NULL, _getc); /* Open stdin */
72 } 86 }
73 87
74 int 88 int
75 cons_putc(char c) { 89 cons_putc(char c) {
90 #ifdef DUALUART
76 loop_until_bit_is_set(UCSR0A, UDRE0); 91 loop_until_bit_is_set(UCSR0A, UDRE0);
77 UDR0 = c; 92 UDR0 = c;
93 #else
94 loop_until_bit_is_set(UCSRA, UDRE);
95 UDR = c;
96 #endif
78 97
79 return(0); 98 return(0);
80 } 99 }
81 100
82 void 101 void
112 cons_puts(utoa(a, s, 16)); 131 cons_puts(utoa(a, s, 16));
113 } 132 }
114 133
115 char 134 char
116 cons_getc(void) { 135 cons_getc(void) {
136 #ifdef DUALUART
117 while (!(UCSR0A & _BV(RXC0))) 137 while (!(UCSR0A & _BV(RXC0)))
118 ; 138 ;
119
120 return (UDR0); 139 return (UDR0);
140 #else
141 while (!(UCSRA & _BV(RXC)))
142 ;
143 return (UDR);
144 #endif
121 } 145 }
122 146
123 /* Rx complete */ 147 /* Rx complete */
148 #ifdef DUALUART
124 ISR(USART0_RX_vect) { 149 ISR(USART0_RX_vect) {
125 volatile char pit; 150 #else
151 ISR(USART_RXC_vect) {
152 #endif
126 char c; 153 char c;
127 154
155 #ifdef DUALUART
128 while (UCSR0A & _BV(RXC0)) { 156 while (UCSR0A & _BV(RXC0)) {
157 #else
158 while (UCSRA & _BV(RXC)) {
159 #endif
160
161 #ifdef DUALUART
162 c = UDR0;
163 #else
164 c = UDR;
165 #endif
129 /* 255 means we're waiting for main to process the command, 166 /* 255 means we're waiting for main to process the command,
130 just throw stuff away 167 * just throw stuff away
131 */ 168 */
132 if (cmd.state == 255) { 169 if (cmd.state == 255)
133 pit = UDR0; 170 continue;
134 continue;
135 }
136 c = UDR0;
137 171
138 /* End of line? */ 172 /* End of line? */
139 if (c == '\n' || c == '\r') { 173 if (c == '\n' || c == '\r') {
140 cmd.buf[cmd.state] = '\0'; 174 cmd.buf[cmd.state] = '\0';
141 printf_P(PSTR("\r\n")); 175 printf_P(PSTR("\r\n"));
171 } 205 }
172 } 206 }
173 } 207 }
174 208
175 /* Tx complete */ 209 /* Tx complete */
210 #ifdef DUALUART
176 ISR(USART0_TX_vect) { 211 ISR(USART0_TX_vect) {
177 212 #else
178 } 213 ISR(USART_TXC_vect) {
179 214 #endif
215 }
216