Revision 1345
Final spline code for master/slave
updated outdated libdragonfly and libwireless
serial.c | ||
---|---|---|
1 |
/* |
|
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 |
*/ |
|
1 |
/** |
|
2 |
* Copyright (c) 2007 Colony Project |
|
3 |
* |
|
4 |
* Permission is hereby granted, free of charge, to any person |
|
5 |
* obtaining a copy of this software and associated documentation |
|
6 |
* files (the "Software"), to deal in the Software without |
|
7 |
* restriction, including without limitation the rights to use, |
|
8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
9 |
* copies of the Software, and to permit persons to whom the |
|
10 |
* Software is furnished to do so, subject to the following |
|
11 |
* conditions: |
|
12 |
* |
|
13 |
* The above copyright notice and this permission notice shall be |
|
14 |
* included in all copies or substantial portions of the Software. |
|
15 |
* |
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
23 |
* OTHER DEALINGS IN THE SOFTWARE. |
|
24 |
**/ |
|
7 | 25 |
|
26 |
|
|
27 |
/** |
|
28 |
* @file serial.c |
|
29 |
* @brief Serial Input and Output |
|
30 |
* |
|
31 |
* Implementation of functions for serial input and output. |
|
32 |
* |
|
33 |
* @author Colony Project, CMU Robotics Club |
|
34 |
**/ |
|
35 |
|
|
8 | 36 |
#include <avr/io.h> |
9 | 37 |
#include <stdio.h> |
10 | 38 |
#include "serial.h" |
... | ... | |
21 | 49 |
//Set baud rate |
22 | 50 |
// - 115200 (both wired and wireless) is UBRR=8, U2X=1 |
23 | 51 |
// - 9600 is U2X =1, UBRR = 107. |
24 |
#if (USB_BAUD == 115200)
|
|
25 |
UBRR0H = 0x00;
|
|
26 |
UBRR0L = 8;
|
|
27 |
UCSR0A |= _BV(U2X0);
|
|
28 |
#elif (USB_BAUD == 9600)
|
|
29 |
UBRR0H = 0x00;
|
|
30 |
UBRR0L = 103;
|
|
31 |
UCSR0A |= _BV(U2X0);
|
|
32 |
#else //Baud rate is defined in the header file, we should not get here
|
|
33 |
return;
|
|
34 |
#endif
|
|
52 |
#if (USB_BAUD == 115200) |
|
53 |
UBRR0H = 0x00; |
|
54 |
UBRR0L = 8; |
|
55 |
UCSR0A |= _BV(U2X0); |
|
56 |
#elif (USB_BAUD == 9600) |
|
57 |
UBRR0H = 0x00; |
|
58 |
UBRR0L = 103; |
|
59 |
UCSR0A |= _BV(U2X0); |
|
60 |
#else //Baud rate is defined in the header file, we should not get here |
|
61 |
return; |
|
62 |
#endif |
|
35 | 63 |
|
36 | 64 |
/*Enable receiver and transmitter */ |
37 |
UCSR0B |= (1<<RXEN0)|(1<<TXEN0);
|
|
65 |
UCSR0B |= (1<<RXEN0)|(1<<TXEN0);
|
|
38 | 66 |
|
39 |
/* Set frame format: 8data, 1stop bit, asynchronous normal mode */
|
|
40 |
UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01);
|
|
67 |
/* Set frame format: 8data, 1stop bit, asynchronous normal mode */
|
|
68 |
UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01);
|
|
41 | 69 |
|
42 | 70 |
// if we have enabled the stdio stuff, then we init it here |
43 |
#ifdef USE_STDIO
|
|
44 |
/* Open the stdio stream corresponding to this port */
|
|
45 |
usb_fd = fdevopen(usb_putc, usb_getc);
|
|
46 |
#endif
|
|
71 |
#ifdef USE_STDIO |
|
72 |
/* Open the stdio stream corresponding to this port */ |
|
73 |
usb_fd = fdevopen(usb_putc, usb_getc); |
|
74 |
#endif |
|
47 | 75 |
} |
48 | 76 |
|
49 | 77 |
/** |
... | ... | |
55 | 83 |
//Set baud rate |
56 | 84 |
// - 115200 (both wired and wireless) is UBRR=8, U2X=1 |
57 | 85 |
// - 9600 is U2X =1, UBRR = 107. |
58 |
#if (XBEE_BAUD == 115200)
|
|
59 |
UBRR1H = 0x00;
|
|
60 |
UBRR1L = 8;
|
|
61 |
UCSR1A |= _BV(U2X1);
|
|
62 |
#elif (XBEE_BAUD == 9600)
|
|
63 |
UBRR1H = 0x00;
|
|
64 |
UBRR1L = 103;
|
|
65 |
UCSR1A |= _BV(U2X1);
|
|
66 |
#else //Baud rate is defined in the header file, we should not get here
|
|
67 |
return;
|
|
68 |
#endif
|
|
86 |
#if (XBEE_BAUD == 115200) |
|
87 |
UBRR1H = 0x00; |
|
88 |
UBRR1L = 8; |
|
89 |
UCSR1A |= _BV(U2X1); |
|
90 |
#elif (XBEE_BAUD == 9600) |
|
91 |
UBRR1H = 0x00; |
|
92 |
UBRR1L = 103; |
|
93 |
UCSR1A |= _BV(U2X1); |
|
94 |
#else //Baud rate is defined in the header file, we should not get here |
|
95 |
return; |
|
96 |
#endif |
|
69 | 97 |
|
70 |
//Enable receiver and transmitter
|
|
71 |
UCSR1B |= (1<<RXEN1)|(1<<TXEN1);
|
|
98 |
//Enable receiver and transmitter
|
|
99 |
UCSR1B |= (1<<RXEN1)|(1<<TXEN1);
|
|
72 | 100 |
|
73 |
// Set frame format: 8data, 1stop bit, asynchronous normal mode
|
|
74 |
UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11);
|
|
101 |
// Set frame format: 8data, 1stop bit, asynchronous normal mode
|
|
102 |
UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11);
|
|
75 | 103 |
|
76 | 104 |
// if we have enabled the stdio stuff, then we init it here |
77 |
#ifdef USE_STDIO
|
|
78 |
/* Open the stdio stream corresponding to this port */
|
|
79 |
xbee_fd = fdevopen(xbee_putc, xbee_getc);
|
|
80 |
#endif
|
|
105 |
#ifdef USE_STDIO |
|
106 |
/* Open the stdio stream corresponding to this port */ |
|
107 |
xbee_fd = fdevopen(xbee_putc, xbee_getc); |
|
108 |
#endif |
|
81 | 109 |
} |
82 | 110 |
|
83 | 111 |
/** |
... | ... | |
86 | 114 |
* @param c the character to send |
87 | 115 |
* @return 0 for success, nonzero for failure |
88 | 116 |
**/ |
89 |
int usb_putc(char c) |
|
90 |
{ |
|
117 |
int usb_putc(char c) { |
|
91 | 118 |
// Wait until buffer is clear for sending |
92 | 119 |
loop_until_bit_is_set(UCSR0A, UDRE0); |
93 | 120 |
|
... | ... | |
102 | 129 |
* @param c the character to send |
103 | 130 |
* @return 0 for success, nonzero for failure |
104 | 131 |
**/ |
105 |
int xbee_putc(char c) |
|
106 |
{ |
|
132 |
int xbee_putc(char c) { |
|
107 | 133 |
// Wait until buffer is clear for sending |
108 | 134 |
loop_until_bit_is_set(UCSR1A, UDRE1); |
109 | 135 |
|
... | ... | |
118 | 144 |
* @param s the string to send |
119 | 145 |
* @return 0 for success, nonzero for failure |
120 | 146 |
**/ |
121 |
int usb_puts(char *s) |
|
122 |
{ |
|
123 |
char *t = s; |
|
124 |
while (*t != 0) |
|
125 |
{ |
|
126 |
usb_putc(*t); |
|
127 |
t++; |
|
128 |
} |
|
147 |
int usb_puts(char *s) { |
|
148 |
char *t = s; |
|
149 |
while (*t != 0) { |
|
150 |
usb_putc(*t); |
|
151 |
t++; |
|
152 |
} |
|
129 | 153 |
return 0; |
130 | 154 |
} |
131 | 155 |
|
132 | 156 |
/** |
157 |
* Sends a sequence of characters from program space over USB. |
|
158 |
* |
|
159 |
* @param s the string to send |
|
160 |
**/ |
|
161 |
void usb_puts_P (PGM_P s) { |
|
162 |
char buf; |
|
163 |
|
|
164 |
while (memcpy_P (&buf, s, sizeof (char)), buf!=0) { |
|
165 |
usb_putc (buf); |
|
166 |
s++; |
|
167 |
} |
|
168 |
} |
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
/** |
|
133 | 173 |
* Returns the first character in the buffer received from USB. |
134 | 174 |
* This function blocks execution until a character has been received. |
135 | 175 |
* xbee_init must be called before this function may be used. |
... | ... | |
138 | 178 |
* |
139 | 179 |
* @see usb_init, usb_getc_nb |
140 | 180 |
**/ |
141 |
int usb_getc(void) |
|
142 |
{ |
|
181 |
int usb_getc(void) { |
|
143 | 182 |
// Wait for the receive buffer to be filled |
144 | 183 |
loop_until_bit_is_set(UCSR0A, RXC0); |
145 | 184 |
|
146 |
// Read the receive buffer
|
|
147 |
return UDR0;
|
|
185 |
// Read the receive buffer
|
|
186 |
return UDR0;
|
|
148 | 187 |
} |
149 | 188 |
|
150 | 189 |
/** |
... | ... | |
157 | 196 |
* |
158 | 197 |
* @see xbee_init, xbee_getc_nb |
159 | 198 |
**/ |
160 |
int xbee_getc(void) |
|
161 |
{ |
|
162 |
// Wait for the receive buffer to be filled |
|
163 |
loop_until_bit_is_set(UCSR1A, RXC1); |
|
199 |
int xbee_getc(void) { |
|
200 |
// Wait for the receive buffer to be filled |
|
201 |
loop_until_bit_is_set(UCSR1A, RXC1); |
|
164 | 202 |
|
165 |
// Read the receive buffer
|
|
166 |
return UDR1;
|
|
203 |
// Read the receive buffer
|
|
204 |
return UDR1;
|
|
167 | 205 |
} |
168 | 206 |
|
169 | 207 |
/** |
... | ... | |
178 | 216 |
* |
179 | 217 |
* @see usb_init, usb_getc |
180 | 218 |
**/ |
181 |
int usb_getc_nb(char *c) |
|
182 |
{ |
|
183 |
// check if the receive buffer is filled |
|
219 |
int usb_getc_nb(char *c) { |
|
220 |
// check if the receive buffer is filled |
|
184 | 221 |
if (UCSR0A & _BV(RXC0)) { |
185 | 222 |
// Read the receive buffer |
186 | 223 |
(*c) = UDR0; |
187 | 224 |
return 0; |
188 |
} |
|
189 |
else { |
|
225 |
} else { |
|
190 | 226 |
// Return empty |
191 | 227 |
return -1; |
192 |
}
|
|
228 |
}
|
|
193 | 229 |
} |
194 | 230 |
|
195 | 231 |
/** |
... | ... | |
204 | 240 |
* |
205 | 241 |
* @see xbee_init, xbee_getc |
206 | 242 |
**/ |
207 |
int xbee_getc_nb(char *c) |
|
208 |
{ |
|
209 |
// check if the receive buffer is filled |
|
243 |
int xbee_getc_nb(char *c) { |
|
244 |
// check if the receive buffer is filled |
|
210 | 245 |
if (UCSR1A & _BV(RXC1)) { |
211 | 246 |
// Read the receive buffer |
212 | 247 |
(*c) = UDR1; |
213 | 248 |
return 0; |
214 |
} |
|
215 |
else { |
|
249 |
} else { |
|
216 | 250 |
// Return empty |
217 | 251 |
return -1; |
218 |
}
|
|
252 |
}
|
|
219 | 253 |
} |
220 | 254 |
|
221 | 255 |
|
222 | 256 |
/* |
223 |
prints an int to serial |
|
257 |
prints an int to serial
|
|
224 | 258 |
|
225 |
code adapted from Chris Efstathiou's code (hendrix@otenet.gr) |
|
226 |
uses usb_putc |
|
259 |
code adapted from Chris Efstathiou's code (hendrix@otenet.gr)
|
|
260 |
uses usb_putc
|
|
227 | 261 |
*/ |
228 | 262 |
/** |
229 | 263 |
* Prints an integer, converted to ASCII, to usb. usb_init must be called |
... | ... | |
236 | 270 |
* @see usb_init, usb_putc |
237 | 271 |
**/ |
238 | 272 |
int usb_puti(int value ) { |
239 |
unsigned char usb_data[6]={'0','0','0','0','0','0' }, position=sizeof(usb_data), radix=10;
|
|
273 |
unsigned char usb_data[6]={'0','0','0','0','0','0' }, position=sizeof(usb_data), radix=10;
|
|
240 | 274 |
|
241 |
/* convert int to ascii */ |
|
242 |
if(value<0) { usb_putc('-'); value=-value; } |
|
243 |
do { position--; *(usb_data+position)=(value%radix)+'0'; value/=radix; } while(value); |
|
275 |
/* convert int to ascii */ |
|
276 |
if(value<0) { |
|
277 |
usb_putc('-'); |
|
278 |
value=-value; |
|
279 |
} |
|
280 |
do { |
|
281 |
position--; |
|
282 |
*(usb_data+position)=(value%radix)+'0'; |
|
283 |
value/=radix; |
|
284 |
} while(value); |
|
244 | 285 |
|
245 | 286 |
|
246 |
/* start displaying the number */ |
|
247 |
for(;position<=(sizeof(usb_data)-1);position++) |
|
248 |
{ |
|
287 |
/* start displaying the number */ |
|
288 |
for(;position<=(sizeof(usb_data)-1);position++) { |
|
249 | 289 |
|
250 |
usb_putc(usb_data[position]);
|
|
251 |
}
|
|
290 |
usb_putc(usb_data[position]); |
|
291 |
} |
|
252 | 292 |
|
253 |
return 0;
|
|
293 |
return 0;
|
|
254 | 294 |
} |
255 | 295 |
|
296 |
/** |
|
297 |
* Determines a hexadecimal digit in ASCII code. |
|
298 |
* |
|
299 |
* @param value the value of the digit (0<=value<=15) |
|
300 |
* |
|
301 |
* @return the hexadecimal digit in ASCII code, or '?' |
|
302 |
* if the input is invalid. |
|
303 |
**/ |
|
304 |
uint8_t hex_digit (uint8_t value) |
|
305 |
{ |
|
306 |
if (value>15) return '?'; |
|
307 |
// Postcondition: 0<=x<=15 |
|
256 | 308 |
|
309 |
return "0123456789ABCDEF"[value]; |
|
310 |
} |
|
311 |
|
|
312 |
/** |
|
313 |
* Prints a fixed width hexadecimal representation of an unsigned |
|
314 |
* 16 bit integer in ASCII code to USB. |
|
315 |
* usb_init must be called before this function can be used. |
|
316 |
* |
|
317 |
* @param value the value to print |
|
318 |
* |
|
319 |
* @see usb_init, usb_puti, usb_puts, usb_puth8, hex_digit |
|
320 |
**/ |
|
321 |
void usb_puth16 (uint16_t value) |
|
322 |
{ |
|
323 |
usb_putc (hex_digit((value >>12)&0xF)); |
|
324 |
usb_putc (hex_digit((value >>8 )&0xF)); |
|
325 |
usb_putc (hex_digit((value >>4 )&0xF)); |
|
326 |
usb_putc (hex_digit( value &0xF)); |
|
327 |
} |
|
328 |
|
|
329 |
/** |
|
330 |
* Prints a fixed width hexadecimal representation of an unsigned |
|
331 |
* 8 bit integer in ASCII code to USB. |
|
332 |
* usb_init must be called before this function can be used. |
|
333 |
* |
|
334 |
* @param value the value to print |
|
335 |
* |
|
336 |
* @see usb_init, usb_puti, usb_puts, usb_puth16, hex_digit |
|
337 |
**/ |
|
338 |
void usb_puth8(uint8_t value) |
|
339 |
{ |
|
340 |
usb_putc (hex_digit ((value)>>4 &0xF)); |
|
341 |
usb_putc (hex_digit ( value &0xF)); |
|
342 |
} |
|
343 |
|
Also available in: Unified diff