24 #include <avr/interrupt.h> 27 #include <util/delay.h> 49 static USART_t *usart[NUM_USARTS] = { &USARTC0, &USARTC1, &USARTD0, &USARTD1, &USARTE0, &USARTF0 };
54 static int USART_putchar (
char c, FILE *stream ) __attribute__((noinline));
55 static int USART_getchar ( FILE *stream ) __attribute__((noinline));
74 FILE usart_out = FDEV_SETUP_STREAM(USART_putchar, NULL, _FDEV_SETUP_WRITE);
75 FILE usartout = FDEV_SETUP_STREAM(USART_putchar, NULL, _FDEV_SETUP_WRITE);
76 FILE usartin = FDEV_SETUP_STREAM(NULL, USART_getchar, _FDEV_SETUP_READ);
82 static int usartsel_in = eUSARTC0;
83 static int usartsel_out = eUSARTC0;
84 static int usartsel_err = eUSARTC0;
94 static volatile unsigned char inptr[NUM_USARTS] = { 0, 0, 0, 0, 0, 0 };
95 static volatile unsigned char outptr[NUM_USARTS] = { 0, 0, 0, 0, 0, 0 };
100 static volatile unsigned char queue[NUM_USARTS][QUEUE_SIZE];
105 static void _rxcisr (
int usartnum );
114 void USART_Init ( uint8_t usartnum, uint8_t bauda, uint8_t baudb )
119 if ( usartnum >= NUM_USARTS )
return;
121 pusart = usart[usartnum];
126 PORTC_OUTSET |= PIN3_bm;
127 PORTC_DIRSET |= PIN3_bm;
128 PORTC_OUTCLR = PIN2_bm;
129 PORTC_DIRCLR = PIN2_bm;
130 PORTC_PIN3CTRL |= 0x18;
134 PORTC_OUTSET |= PIN7_bm;
135 PORTC_DIRSET |= PIN7_bm;
136 PORTC_PIN7CTRL |= 0x18;
140 PORTD_OUTSET |= PIN3_bm;
141 PORTD_DIRSET |= PIN3_bm;
142 PORTD_PIN3CTRL |= 0x18;
146 PORTD_OUTSET |= PIN7_bm;
147 PORTD_DIRSET |= PIN7_bm;
148 PORTD_PIN7CTRL |= 0x18;
152 PORTE_OUTSET |= PIN3_bm;
153 PORTE_DIRSET |= PIN3_bm;
154 PORTE_PIN3CTRL |= 0x18;
158 PORTF_OUTSET |= PIN3_bm;
159 PORTF_DIRSET |= PIN3_bm;
160 PORTF_OUTCLR = PIN2_bm;
161 PORTF_DIRCLR = PIN2_bm;
162 PORTF_PIN3CTRL |= 0x18;
169 pusart->CTRLA = USART_RXCINTLVL_HI_gc;
171 if ( DOUBLESPEED == 1 )
173 pusart->CTRLB = USART_CLK2X_bm;
177 pusart->CTRLB &= ~USART_CLK2X_bm;
180 pusart->CTRLC = USART_CHSIZE0_bm + USART_CHSIZE1_bm;
182 pusart->BAUDCTRLB = baudb;
183 pusart->BAUDCTRLA = bauda;
185 pusart->CTRLB |= (USART_RXEN_bm | USART_TXEN_bm);
191 void USART_Connect (
int usartnum,
int streamsel )
194 if ( usartnum >= NUM_USARTS )
return;
196 if ( streamsel == IN_FILENO )
197 usartsel_in = usartnum;
198 else if ( streamsel == OUT_FILENO )
199 usartsel_out = usartnum;
200 else if ( streamsel == ERR_FILENO )
201 usartsel_err = usartnum;
212 static int USART_putchar (
char c, FILE *stream )
216 if ( stream == &usartout )
217 ptr = usart[usartsel_out];
221 while ( (ptr->STATUS & USART_DREIF_bm) == 0 )
232 static int USART_getchar ( FILE *stream )
237 if ( stream != &usartin )
return 0;
240 while ( inptr[n] == outptr[n] )
242 c = queue[n][outptr[n]];
244 outptr[n] = outptr[n] & (QUEUE_SIZE - 1);
245 if ( c ==
'\r' ) c =
'\n';
260 static void _rxcisr (
int usartnum )
264 pusart = usart[usartnum];
265 queue[usartnum][inptr[usartnum]] = pusart->DATA;
267 inptr[usartnum] = inptr[usartnum] & (QUEUE_SIZE - 1);
268 if ( inptr[usartnum] == outptr[usartnum] )
271 outptr[usartnum] = outptr[usartnum] & (QUEUE_SIZE - 1);
283 ISR(USARTC0_RXC_vect)
285 _rxcisr ( eUSARTC0 );
288 ISR(USARTC1_RXC_vect)
290 _rxcisr ( eUSARTC1 );
293 ISR(USARTD0_RXC_vect)
295 _rxcisr ( eUSARTD0 );
298 ISR(USARTD1_RXC_vect)
300 _rxcisr ( eUSARTD1 );
303 ISR(USARTE0_RXC_vect)
305 _rxcisr ( eUSARTE0 );
308 ISR(USARTF0_RXC_vect)
310 _rxcisr ( eUSARTF0 );
313 #ifndef BAUD_SETTINGS 314 #define F_CPU 32000000UL 316 #define BAUDRATE 57600 318 #define BSEL_USART ((F_PER/(8*BAUDRATE))-1) 319 #define BAUD_A ((BSEL_USART) & 0xff) 320 #define BAUD_B ((BSEL_USART >> 8) | 0) Standardheader und Auswahl der Übertragungsgeschwindigkeit.