root / trunk / code / projects / colonet / utilities / robot_slave / serial.c @ 13
History | View | Annotate | Download (4.8 KB)
1 | 13 | emarinel | /*
|
---|---|---|---|
2 | serial.c - Functions for using the RS232 serial port
|
||
3 | |||
4 | authors: Robotics Club, Colony Project, pkv
|
||
5 | much code taken from FWR's library, author: Tom Lauwers
|
||
6 | */
|
||
7 | |||
8 | #include <avr/io.h> |
||
9 | #include <stdio.h> |
||
10 | #include "serial.h" |
||
11 | |||
12 | /* usb_init() - sets up AVR for communication over the usb serial port */
|
||
13 | void usb_init() {
|
||
14 | //Set baud rate
|
||
15 | // - 115200 (both wired and wireless) is UBRR=8, U2X=1
|
||
16 | // - 9600 is U2X =1, UBRR = 107.
|
||
17 | #if (USB_BAUD == 115200) |
||
18 | UBRR0H = 0x00;
|
||
19 | UBRR0L = 8;
|
||
20 | UCSR0A |= _BV(U2X0); |
||
21 | #elif (USB_BAUD == 9600) |
||
22 | UBRR0H = 0x00;
|
||
23 | UBRR0L = 107;
|
||
24 | UCSR0A |= _BV(U2X0); |
||
25 | #else //Baud rate is defined in the header file, we should not get here |
||
26 | return;
|
||
27 | #endif
|
||
28 | |||
29 | /*Enable receiver and transmitter */
|
||
30 | UCSR0B |= (1<<RXEN0)|(1<<TXEN0); |
||
31 | |||
32 | /* Set frame format: 8data, 1stop bit, asynchronous normal mode */
|
||
33 | UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01); |
||
34 | |||
35 | // if we have enabled the stdio stuff, then we init it here
|
||
36 | #ifdef USE_STDIO
|
||
37 | /* Open the stdio stream corresponding to this port */
|
||
38 | usb_fd = fdevopen(usb_putc, usb_getc); |
||
39 | #endif
|
||
40 | } |
||
41 | |||
42 | /* xbee_init() - sets up AVR for communication over the xbee serial port */
|
||
43 | void xbee_init() {
|
||
44 | //Set baud rate
|
||
45 | // - 115200 (both wired and wireless) is UBRR=8, U2X=1
|
||
46 | // - 9600 is U2X =1, UBRR = 107.
|
||
47 | #if (XBEE_BAUD == 115200) |
||
48 | UBRR1H = 0x00;
|
||
49 | UBRR1L = 8;
|
||
50 | UCSR1A |= _BV(U2X1); |
||
51 | #elif (XBEE_BAUD == 9600) |
||
52 | UBRR1H = 0x00;
|
||
53 | UBRR1L = 107;
|
||
54 | UCSR1A |= _BV(U2X1); |
||
55 | #else //Baud rate is defined in the header file, we should not get here |
||
56 | return;
|
||
57 | #endif
|
||
58 | |||
59 | //Enable receiver and transmitter
|
||
60 | UCSR1B |= (1<<RXEN1)|(1<<TXEN1); |
||
61 | |||
62 | // Set frame format: 8data, 1stop bit, asynchronous normal mode
|
||
63 | UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11); |
||
64 | |||
65 | // if we have enabled the stdio stuff, then we init it here
|
||
66 | #ifdef USE_STDIO
|
||
67 | /* Open the stdio stream corresponding to this port */
|
||
68 | xbee_fd = fdevopen(xbee_putc, xbee_getc); |
||
69 | #endif
|
||
70 | } |
||
71 | |||
72 | /* usb_putc(c) - waits until the usb serial tx buffer is clear, then
|
||
73 | puts the character c into the buffer */
|
||
74 | int usb_putc(char c) |
||
75 | { |
||
76 | // Wait until buffer is clear for sending
|
||
77 | loop_until_bit_is_set(UCSR0A, UDRE0); |
||
78 | |||
79 | // Load buffer with your character
|
||
80 | UDR0 = c; |
||
81 | return 0; |
||
82 | } |
||
83 | |||
84 | /* xbee_putc(c) - waits until the xbee serial tx buffer is clear, then
|
||
85 | puts the character c into the buffer */
|
||
86 | int xbee_putc(char c) |
||
87 | { |
||
88 | // Wait until buffer is clear for sending
|
||
89 | loop_until_bit_is_set(UCSR1A, UDRE1); |
||
90 | |||
91 | // Load buffer with your character
|
||
92 | UDR1 = c; |
||
93 | return 0; |
||
94 | } |
||
95 | |||
96 | /* usb_puts(c) - writes each character from the string to the usb */
|
||
97 | int usb_puts(char *s) |
||
98 | { |
||
99 | char *t = s;
|
||
100 | while (*t != 0) |
||
101 | { |
||
102 | usb_putc(*t); |
||
103 | t++; |
||
104 | } |
||
105 | return 0; |
||
106 | } |
||
107 | |||
108 | |||
109 | /* usb_getc() - waits until the usb serial rx buffer is not empty, then
|
||
110 | returns the first character in the buffer */
|
||
111 | int usb_getc(void) |
||
112 | { |
||
113 | // Wait for the receive buffer to be filled
|
||
114 | loop_until_bit_is_set(UCSR0A, RXC0); |
||
115 | |||
116 | // Read the receive buffer
|
||
117 | return UDR0;
|
||
118 | } |
||
119 | |||
120 | /* xbee_getc() - waits until the xbee serial rx buffer is not empty, then
|
||
121 | returns the first character in the buffer */
|
||
122 | int xbee_getc(void) |
||
123 | { |
||
124 | // Wait for the receive buffer to be filled
|
||
125 | loop_until_bit_is_set(UCSR1A, RXC1); |
||
126 | |||
127 | // Read the receive buffer
|
||
128 | return UDR1;
|
||
129 | } |
||
130 | |||
131 | /* usb_getc_nb(*c) - checks if the usb serial rx buffer is empty or not:
|
||
132 | -If it is empty, c is unchanged and -1 is returned.
|
||
133 | -If is not empty, the first character in the buffer is loaded into c,
|
||
134 | and 0 is returned */
|
||
135 | int usb_getc_nb(char *c) |
||
136 | { |
||
137 | // check if the receive buffer is filled
|
||
138 | if (UCSR0A & _BV(RXC0)) {
|
||
139 | // Read the receive buffer
|
||
140 | (*c) = UDR0; |
||
141 | return 0; |
||
142 | } |
||
143 | else {
|
||
144 | // Return empty
|
||
145 | return -1; |
||
146 | } |
||
147 | } |
||
148 | |||
149 | /* xbee_getc_nb(*c) - checks if the xbee serial rx buffer is empty or not:
|
||
150 | -If it is empty, c is unchanged and -1 is returned.
|
||
151 | -If is not empty, the first character in the buffer is loaded into c,
|
||
152 | and 0 is returned */
|
||
153 | int xbee_getc_nb(char *c) |
||
154 | { |
||
155 | // check if the receive buffer is filled
|
||
156 | if (UCSR1A & _BV(RXC1)) {
|
||
157 | // Read the receive buffer
|
||
158 | (*c) = UDR1; |
||
159 | return 0; |
||
160 | } |
||
161 | else {
|
||
162 | // Return empty
|
||
163 | return -1; |
||
164 | } |
||
165 | } |
||
166 | |||
167 | |||
168 | /*
|
||
169 | prints an int to serial
|
||
170 | |||
171 | code adapted from Chris Efstathiou's code (hendrix@otenet.gr)
|
||
172 | uses usb_putc
|
||
173 | */
|
||
174 | int usb_puti(int value ) { |
||
175 | unsigned char usb_data[6]={'0','0','0','0','0','0' }, position=sizeof(usb_data), radix=10; |
||
176 | |||
177 | /* convert int to ascii */
|
||
178 | if(value<0) { usb_putc('-'); value=-value; } |
||
179 | do { position--; *(usb_data+position)=(value%radix)+'0'; value/=radix; } while(value); |
||
180 | |||
181 | |||
182 | /* start displaying the number */
|
||
183 | for(;position<=(sizeof(usb_data)-1);position++) |
||
184 | { |
||
185 | |||
186 | usb_putc(usb_data[position]); |
||
187 | } |
||
188 | |||
189 | return 0; |
||
190 | } |