1 // vim: set sw=8 ts=8 si et:
2 /********************************************
3 * UART interface without interrupt
6 **********************************************/
7 #include <avr/interrupt.h>
11 #define F_CPU 8000000UL // 8 MHz
14 #define SENDSTACKSIZE 12
15 static volatile char ustack[SENDSTACKSIZE];
16 static volatile uint8_t stackpointer_end=0;
17 static uint8_t stackpointer_start=0;
21 unsigned int baud=51; // 9600 baud at 8MHz
23 UBRR0H=(unsigned char) (baud >>8);
24 UBRR0L=(unsigned char) (baud & 0xFF);
25 // enable tx/rx and no interrupt on tx/rx
26 UCSR0B = (1<<RXEN0) | (1<<TXEN0);
27 // format: asynchronous, 8data, no parity, 1stop bit
28 UCSR0C = (1<<UCSZ01)|(1<<UCSZ00);
30 UBRRH=(unsigned char) (baud >>8);
31 UBRRL=(unsigned char) (baud & 0xFF);
32 // enable tx/rx and no interrupt on tx/rx
33 UCSRB = (1<<RXEN) | (1<<TXEN);
34 // format: asynchronous, 8data, no parity, 1stop bit
35 UCSRC = (1<<URSEL)|(3<<UCSZ0);
39 // send one character to the rs232
40 void uart_sendchar(char c)
43 // wait for empty transmit buffer
44 while (!(UCSR0A & (1<<UDRE0)));
47 // wait for empty transmit buffer
48 while (!(UCSRA & (1<<UDRE)));
52 // send string to the rs232
53 void uart_sendstr(char *s)
61 void uart_sendstr_p(const prog_char *progmem_s)
62 // print string from program memory on rs232
65 while ((c = pgm_read_byte(progmem_s++))) {
71 // call this function from interrupt to fill the
72 // ustack reveiver buffer. We need this big buffer
73 // to handle fast copy/paste of strings comming
75 void uart_poll_getchar_isr(void)
78 if(!(UCSR0A & (1<<RXC0))) return;
79 ustack[stackpointer_end]=UDR0;
81 if(!(UCSRA & (1<<RXC))) return;
82 ustack[stackpointer_end]=UDR;
84 stackpointer_end=(stackpointer_end+1) % SENDSTACKSIZE;
87 // get the characters out of the buffer which is filled by
88 // the above interrupt function
89 unsigned char uart_getchar_isr_noblock(char *returnval)
91 if (stackpointer_start!=stackpointer_end){
92 *returnval=ustack[stackpointer_start];
93 stackpointer_start=(stackpointer_start+1) % SENDSTACKSIZE;
100 // get a byte from rs232
101 // this function does a blocking read
102 char uart_getchar(void)
105 while(!(UCSR0A & (1<<RXC0)));
108 while(!(UCSRA & (1<<RXC)));
113 // get a byte from rs232
114 // this function does a non blocking read
115 // returns 1 if a character was read
116 unsigned char uart_getchar_noblock(char *returnval)
119 if(UCSR0A & (1<<RXC0)){
124 if(UCSRA & (1<<RXC)){
132 // read and discard any data in the receive buffer
133 void uart_flushRXbuf(void)
137 while(UCSR0A & (1<<RXC0)){
141 while(UCSRA & (1<<RXC)){