comparison cons.c @ 4:095216e8453d

Hide register name abstraction in a separate file.
author darius@Inchoate
date Thu, 12 Mar 2009 16:30:47 +1030
parents 15d89caaf516
children b46f0c742316
comparison
equal deleted inserted replaced
3:15d89caaf516 4:095216e8453d
1 /* 1 /*
2 * Console code for AVR board 2 * Console code for AVR board
3 * 3 *
4 * Copyright (c) 2008 4 * Copyright (c) 2008-2009
5 * Daniel O'Connor <darius@dons.net.au>. All rights reserved. 5 * Daniel O'Connor <darius@dons.net.au>. All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
31 #include <stdint.h> 31 #include <stdint.h>
32 #include <stdlib.h> 32 #include <stdlib.h>
33 #include <avr/interrupt.h> 33 #include <avr/interrupt.h>
34 #include <avr/pgmspace.h> 34 #include <avr/pgmspace.h>
35 #include "cons.h" 35 #include "cons.h"
36 #include "cons-reg.h"
36 37
37 #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1) 38 #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)
38
39 #ifdef UBRR0
40 #define DUALUART
41 #endif
42 39
43 /* Receive buffer storage */ 40 /* Receive buffer storage */
44 consbuf_t cmd; 41 consbuf_t cmd;
45 42
46 /* 43 /*
62 return(cons_getc()); 59 return(cons_getc());
63 } 60 }
64 61
65 void 62 void
66 cons_init(void) { 63 cons_init(void) {
67 #ifdef DUALUART 64 _SETBAUD(38400);
68 UBRR0 = UART_BAUD_SELECT(38400, F_CPU);
69 65
70 /* Enable receiver and transmitter. Turn on rx interrupts */ 66 /* Enable receiver and transmitter. Turn on rx interrupts */
71 UCSR0A = 0; 67 _INITREG();
72 UCSR0B = _BV(RXEN0) | _BV(TXEN0) | _BV(RXCIE0); 68
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
83
84 fdevopen(_putc, NULL); /* Open stdout */ 69 fdevopen(_putc, NULL); /* Open stdout */
85 fdevopen(NULL, _getc); /* Open stdin */ 70 fdevopen(NULL, _getc); /* Open stdin */
86 } 71 }
87 72
88 int 73 int
89 cons_putc(char c) { 74 cons_putc(char c) {
90 #ifdef DUALUART 75 loop_until_bit_is_set(_UCSRA, _UDRE);
91 loop_until_bit_is_set(UCSR0A, UDRE0); 76 _UDR = c;
92 UDR0 = c;
93 #else
94 loop_until_bit_is_set(UCSRA, UDRE);
95 UDR = c;
96 #endif
97 77
98 return(0); 78 return(0);
99 } 79 }
100 80
101 void 81 void
131 cons_puts(utoa(a, s, 16)); 111 cons_puts(utoa(a, s, 16));
132 } 112 }
133 113
134 char 114 char
135 cons_getc(void) { 115 cons_getc(void) {
136 #ifdef DUALUART 116 while (!(_UCSRA & _BV(_RXC)))
137 while (!(UCSR0A & _BV(RXC0)))
138 ; 117 ;
139 return (UDR0); 118 return (_UDR);
140 #else
141 while (!(UCSRA & _BV(RXC)))
142 ;
143 return (UDR);
144 #endif
145 } 119 }
146 120
147 /* Rx complete */ 121 /* Rx complete */
148 #ifdef DUALUART 122 ISR(_RXVECT) {
149 ISR(USART0_RX_vect) {
150 #else
151 ISR(USART_RXC_vect) {
152 #endif
153 char c; 123 char c;
154 124
155 #ifdef DUALUART 125 while (_UCSRA & _BV(_RXC)) {
156 while (UCSR0A & _BV(RXC0)) { 126 c = _UDR;
157 #else
158 while (UCSRA & _BV(RXC)) {
159 #endif
160
161 #ifdef DUALUART
162 c = UDR0;
163 #else
164 c = UDR;
165 #endif
166 /* 255 means we're waiting for main to process the command, 127 /* 255 means we're waiting for main to process the command,
167 * just throw stuff away 128 * just throw stuff away
168 */ 129 */
169 if (cmd.state == 255) 130 if (cmd.state == 255)
170 continue; 131 continue;
205 } 166 }
206 } 167 }
207 } 168 }
208 169
209 /* Tx complete */ 170 /* Tx complete */
210 #ifdef DUALUART 171 ISR(_TXVECT) {
211 ISR(USART0_TX_vect) {
212 #else
213 ISR(USART_TXC_vect) {
214 #endif
215 } 172 }
216 173