eb7c4e1a325bb867e180214b8e6fa5d035214225
[digitaldcpower] / uart.c
1 // vim: set sw=8 ts=8 si et: 
2 /********************************************
3 * UART interface without interrupt
4 * Author: Guido Socher
5 * Copyright: GPL
6 **********************************************/
7 #include <avr/interrupt.h>
8 #include <string.h>
9 #include <avr/io.h>
10 #include "uart.h"
11 #define F_CPU 8000000UL  // 8 MHz
12
13 // a receiver stack:
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;
18
19 void uart_init(void) 
20 {
21         unsigned int baud=51;   // 9600 baud at 8MHz
22 #ifdef VAR_88CHIP
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);
29 #else
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);
36 #endif
37 }
38
39 // send one character to the rs232 
40 void uart_sendchar(char c) 
41 {
42 #ifdef VAR_88CHIP
43         // wait for empty transmit buffer 
44         while (!(UCSR0A & (1<<UDRE0)));
45         UDR0=c;
46 #else
47         // wait for empty transmit buffer 
48         while (!(UCSRA & (1<<UDRE)));
49         UDR=c;
50 #endif
51 }
52 // send string to the rs232 
53 void uart_sendstr(char *s) 
54 {
55         while (*s){
56                 uart_sendchar(*s);
57                 s++;
58         }
59 }
60
61 void uart_sendstr_p(const prog_char *progmem_s)
62 // print string from program memory on rs232 
63 {
64         char c;
65         while ((c = pgm_read_byte(progmem_s++))) {
66                 uart_sendchar(c);
67         }
68
69 }
70
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
74 // into the UART
75 void uart_poll_getchar_isr(void)
76 {
77 #ifdef VAR_88CHIP
78         if(!(UCSR0A & (1<<RXC0))) return;
79         ustack[stackpointer_end]=UDR0;
80 #else
81         if(!(UCSRA & (1<<RXC))) return;
82         ustack[stackpointer_end]=UDR;
83 #endif
84         stackpointer_end=(stackpointer_end+1) % SENDSTACKSIZE;
85 }
86
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)  
90 {
91         if (stackpointer_start!=stackpointer_end){
92                 *returnval=ustack[stackpointer_start];
93                 stackpointer_start=(stackpointer_start+1) % SENDSTACKSIZE;
94                 return(1);
95         }
96         return(0);
97 }
98
99 /*
100 // get a byte from rs232
101 // this function does a blocking read 
102 char uart_getchar(void)  
103 {
104 #ifdef VAR_88CHIP
105         while(!(UCSR0A & (1<<RXC0)));
106         return(UDR0);
107 #else
108         while(!(UCSRA & (1<<RXC)));
109         return(UDR);
110 #endif
111 }
112
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)  
117 {
118 #ifdef VAR_88CHIP
119         if(UCSR0A & (1<<RXC0)){
120                 *returnval=UDR0;
121                 return(1);
122         }
123 #else
124         if(UCSRA & (1<<RXC)){
125                 *returnval=UDR;
126                 return(1);
127         }
128 #endif
129         return(0);
130 }
131
132 // read and discard any data in the receive buffer 
133 void uart_flushRXbuf(void)  
134 {
135         unsigned char tmp;
136 #ifdef VAR_88CHIP
137         while(UCSR0A & (1<<RXC0)){
138                 tmp=UDR0;
139         }
140 #else
141         while(UCSRA & (1<<RXC)){
142                 tmp=UDR;
143         }
144 #endif
145 }
146
147 */