tweak voltage divider for my config
[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 <avr/pgmspace.h>
11 #include "uart.h"
12 #define F_CPU 8000000UL  // 8 MHz
13
14 // a receiver stack:
15 #define SENDSTACKSIZE 12
16 static volatile char ustack[SENDSTACKSIZE];
17 static volatile uint8_t stackpointer_end=0;
18 static uint8_t stackpointer_start=0;
19
20 void uart_init(void) 
21 {
22         unsigned int baud=51;   // 9600 baud at 8MHz
23 #ifdef VAR_88CHIP
24         UBRR0H=(unsigned char) (baud >>8);
25         UBRR0L=(unsigned char) (baud & 0xFF);
26         // enable tx/rx and no interrupt on tx/rx 
27         UCSR0B =  (1<<RXEN0) | (1<<TXEN0);
28         // format: asynchronous, 8data, no parity, 1stop bit 
29         UCSR0C = (1<<UCSZ01)|(1<<UCSZ00);
30 #else
31         UBRRH=(unsigned char) (baud >>8);
32         UBRRL=(unsigned char) (baud & 0xFF);
33         // enable tx/rx and no interrupt on tx/rx 
34         UCSRB =  (1<<RXEN) | (1<<TXEN);
35         // format: asynchronous, 8data, no parity, 1stop bit 
36         UCSRC = (1<<URSEL)|(3<<UCSZ0);
37 #endif
38 }
39
40 // send one character to the rs232 
41 void uart_sendchar(char c) 
42 {
43 #ifdef VAR_88CHIP
44         // wait for empty transmit buffer 
45         while (!(UCSR0A & (1<<UDRE0)));
46         UDR0=c;
47 #else
48         // wait for empty transmit buffer 
49         while (!(UCSRA & (1<<UDRE)));
50         UDR=c;
51 #endif
52 }
53 // send string to the rs232 
54 void uart_sendstr(char *s) 
55 {
56         while (*s){
57                 uart_sendchar(*s);
58                 s++;
59         }
60 }
61
62 void uart_sendstr_p(const char *progmem_s )
63 // print string from program memory on rs232 
64 {
65         char c;
66         while ((c = pgm_read_byte(progmem_s++))) {
67                 uart_sendchar(c);
68         }
69
70 }
71
72 // call this function from interrupt to fill the
73 // ustack reveiver buffer. We need this big buffer
74 // to handle fast copy/paste of strings comming
75 // into the UART
76 void uart_poll_getchar_isr(void)
77 {
78 #ifdef VAR_88CHIP
79         if(!(UCSR0A & (1<<RXC0))) return;
80         ustack[stackpointer_end]=UDR0;
81 #else
82         if(!(UCSRA & (1<<RXC))) return;
83         ustack[stackpointer_end]=UDR;
84 #endif
85         stackpointer_end=(stackpointer_end+1) % SENDSTACKSIZE;
86 }
87
88 // get the characters out of the buffer which is filled by
89 // the above interrupt function
90 unsigned char uart_getchar_isr_noblock(char *returnval)  
91 {
92         if (stackpointer_start!=stackpointer_end){
93                 *returnval=ustack[stackpointer_start];
94                 stackpointer_start=(stackpointer_start+1) % SENDSTACKSIZE;
95                 return(1);
96         }
97         return(0);
98 }
99
100 /*
101 // get a byte from rs232
102 // this function does a blocking read 
103 char uart_getchar(void)  
104 {
105 #ifdef VAR_88CHIP
106         while(!(UCSR0A & (1<<RXC0)));
107         return(UDR0);
108 #else
109         while(!(UCSRA & (1<<RXC)));
110         return(UDR);
111 #endif
112 }
113
114 // get a byte from rs232
115 // this function does a non blocking read 
116 // returns 1 if a character was read
117 unsigned char uart_getchar_noblock(char *returnval)  
118 {
119 #ifdef VAR_88CHIP
120         if(UCSR0A & (1<<RXC0)){
121                 *returnval=UDR0;
122                 return(1);
123         }
124 #else
125         if(UCSRA & (1<<RXC)){
126                 *returnval=UDR;
127                 return(1);
128         }
129 #endif
130         return(0);
131 }
132
133 // read and discard any data in the receive buffer 
134 void uart_flushRXbuf(void)  
135 {
136         unsigned char tmp;
137 #ifdef VAR_88CHIP
138         while(UCSR0A & (1<<RXC0)){
139                 tmp=UDR0;
140         }
141 #else
142         while(UCSRA & (1<<RXC)){
143                 tmp=UDR;
144         }
145 #endif
146 }
147
148 */