Revision 643
Copied reset and serial code from dragonfly, have not modified it yet
branches/autonomous_recharging/code/projects/libbayboard/serial.c | ||
---|---|---|
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 |
**/ |
|
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 |
|
|
36 |
#include <avr/io.h> |
|
37 |
#include <stdio.h> |
|
38 |
#include "serial.h" |
|
39 |
|
|
40 |
FILE *usb_fd; //For use with fprintf() and related stdio functions |
|
41 |
FILE *xbee_fd; //For use with fprintf() and related stdio functions |
|
42 |
|
|
43 |
/** |
|
44 |
* Initializes communication over the USB serial port. |
|
45 |
* This must be called before any other usb function |
|
46 |
* may be used. |
|
47 |
**/ |
|
48 |
void usb_init() { |
|
49 |
//Set baud rate |
|
50 |
// - 115200 (both wired and wireless) is UBRR=8, U2X=1 |
|
51 |
// - 9600 is U2X =1, UBRR = 107. |
|
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 |
|
63 |
|
|
64 |
/*Enable receiver and transmitter */ |
|
65 |
UCSR0B |= (1<<RXEN0)|(1<<TXEN0); |
|
66 |
|
|
67 |
/* Set frame format: 8data, 1stop bit, asynchronous normal mode */ |
|
68 |
UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01); |
|
69 |
|
|
70 |
// if we have enabled the stdio stuff, then we init it here |
|
71 |
#ifdef USE_STDIO |
|
72 |
/* Open the stdio stream corresponding to this port */ |
|
73 |
usb_fd = fdevopen(usb_putc, usb_getc); |
|
74 |
#endif |
|
75 |
} |
|
76 |
|
|
77 |
/** |
|
78 |
* Initializes communication over the XBee. |
|
79 |
* This must be called before any other xbee function |
|
80 |
* may be used. |
|
81 |
**/ |
|
82 |
void xbee_init() { |
|
83 |
//Set baud rate |
|
84 |
// - 115200 (both wired and wireless) is UBRR=8, U2X=1 |
|
85 |
// - 9600 is U2X =1, UBRR = 107. |
|
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 |
|
97 |
|
|
98 |
//Enable receiver and transmitter |
|
99 |
UCSR1B |= (1<<RXEN1)|(1<<TXEN1); |
|
100 |
|
|
101 |
// Set frame format: 8data, 1stop bit, asynchronous normal mode |
|
102 |
UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11); |
|
103 |
|
|
104 |
// if we have enabled the stdio stuff, then we init it here |
|
105 |
#ifdef USE_STDIO |
|
106 |
/* Open the stdio stream corresponding to this port */ |
|
107 |
xbee_fd = fdevopen(xbee_putc, xbee_getc); |
|
108 |
#endif |
|
109 |
} |
|
110 |
|
|
111 |
/** |
|
112 |
* Sends a character over USB. |
|
113 |
* |
|
114 |
* @param c the character to send |
|
115 |
* @return 0 for success, nonzero for failure |
|
116 |
**/ |
|
117 |
int usb_putc(char c) { |
|
118 |
// Wait until buffer is clear for sending |
|
119 |
loop_until_bit_is_set(UCSR0A, UDRE0); |
|
120 |
|
|
121 |
// Load buffer with your character |
|
122 |
UDR0 = c; |
|
123 |
return 0; |
|
124 |
} |
|
125 |
|
|
126 |
/** |
|
127 |
* Sends a character to the XBee. |
|
128 |
* |
|
129 |
* @param c the character to send |
|
130 |
* @return 0 for success, nonzero for failure |
|
131 |
**/ |
|
132 |
int xbee_putc(char c) { |
|
133 |
// Wait until buffer is clear for sending |
|
134 |
loop_until_bit_is_set(UCSR1A, UDRE1); |
|
135 |
|
|
136 |
// Load buffer with your character |
|
137 |
UDR1 = c; |
|
138 |
return 0; |
|
139 |
} |
|
140 |
|
|
141 |
/** |
|
142 |
* Sends a sequence of characters over USB. |
|
143 |
* |
|
144 |
* @param s the string to send |
|
145 |
* @return 0 for success, nonzero for failure |
|
146 |
**/ |
|
147 |
int usb_puts(char *s) { |
|
148 |
char *t = s; |
|
149 |
while (*t != 0) { |
|
150 |
usb_putc(*t); |
|
151 |
t++; |
|
152 |
} |
|
153 |
return 0; |
|
154 |
} |
|
155 |
|
|
156 |
/** |
|
157 |
* Returns the first character in the buffer received from USB. |
|
158 |
* This function blocks execution until a character has been received. |
|
159 |
* xbee_init must be called before this function may be used. |
|
160 |
* |
|
161 |
* @return the first character in the usb buffer |
|
162 |
* |
|
163 |
* @see usb_init, usb_getc_nb |
|
164 |
**/ |
|
165 |
int usb_getc(void) { |
|
166 |
// Wait for the receive buffer to be filled |
|
167 |
loop_until_bit_is_set(UCSR0A, RXC0); |
|
168 |
|
|
169 |
// Read the receive buffer |
|
170 |
return UDR0; |
|
171 |
} |
|
172 |
|
|
173 |
/** |
|
174 |
* Returns the first character in the buffer received from USB. |
|
175 |
* This function blocks execution until a character has been |
|
176 |
* received. xbee_init must be called before this function |
|
177 |
* may be used. |
|
178 |
* |
|
179 |
* @return the first character in the xbee buffer |
|
180 |
* |
|
181 |
* @see xbee_init, xbee_getc_nb |
|
182 |
**/ |
|
183 |
int xbee_getc(void) { |
|
184 |
// Wait for the receive buffer to be filled |
|
185 |
loop_until_bit_is_set(UCSR1A, RXC1); |
|
186 |
|
|
187 |
// Read the receive buffer |
|
188 |
return UDR1; |
|
189 |
} |
|
190 |
|
|
191 |
/** |
|
192 |
* Non blocking version of usb_getc. If a character is present in the buffer, |
|
193 |
* it is returned, otherwise -1 is returned immediately. usb_init must be |
|
194 |
* called before this function can be used. |
|
195 |
* |
|
196 |
* @param c the received character. This will be set if a character has |
|
197 |
* been received. |
|
198 |
* |
|
199 |
* @return -1 if no character is available, 0 otherwise |
|
200 |
* |
|
201 |
* @see usb_init, usb_getc |
|
202 |
**/ |
|
203 |
int usb_getc_nb(char *c) { |
|
204 |
// check if the receive buffer is filled |
|
205 |
if (UCSR0A & _BV(RXC0)) { |
|
206 |
// Read the receive buffer |
|
207 |
(*c) = UDR0; |
|
208 |
return 0; |
|
209 |
} else { |
|
210 |
// Return empty |
|
211 |
return -1; |
|
212 |
} |
|
213 |
} |
|
214 |
|
|
215 |
/** |
|
216 |
* Non blocking version of xbee_getc. If a character is present in the buffer, |
|
217 |
* it is returned, otherwise -1 is returned immediately. xbee_init |
|
218 |
* must be called before this function can be used. |
|
219 |
* |
|
220 |
* @param c the received character. This will be set if a character has |
|
221 |
* been received. |
|
222 |
* |
|
223 |
* @return -1 if no character is available, 0 otherwise |
|
224 |
* |
|
225 |
* @see xbee_init, xbee_getc |
|
226 |
**/ |
|
227 |
int xbee_getc_nb(char *c) { |
|
228 |
// check if the receive buffer is filled |
|
229 |
if (UCSR1A & _BV(RXC1)) { |
|
230 |
// Read the receive buffer |
|
231 |
(*c) = UDR1; |
|
232 |
return 0; |
|
233 |
} else { |
|
234 |
// Return empty |
|
235 |
return -1; |
|
236 |
} |
|
237 |
} |
|
238 |
|
|
239 |
|
|
240 |
/* |
|
241 |
prints an int to serial |
|
242 |
|
|
243 |
code adapted from Chris Efstathiou's code (hendrix@otenet.gr) |
|
244 |
uses usb_putc |
|
245 |
*/ |
|
246 |
/** |
|
247 |
* Prints an integer, converted to ASCII, to usb. usb_init must be called |
|
248 |
* before this function can be used. |
|
249 |
* |
|
250 |
* @param value the integer to print |
|
251 |
* |
|
252 |
* @return 0 if successful, nonzero otherwise |
|
253 |
* |
|
254 |
* @see usb_init, usb_putc |
|
255 |
**/ |
|
256 |
int usb_puti(int value ) { |
|
257 |
unsigned char usb_data[6]={'0','0','0','0','0','0' }, position=sizeof(usb_data), radix=10; |
|
258 |
|
|
259 |
/* convert int to ascii */ |
|
260 |
if(value<0) { |
|
261 |
usb_putc('-'); |
|
262 |
value=-value; |
|
263 |
} |
|
264 |
do { |
|
265 |
position--; |
|
266 |
*(usb_data+position)=(value%radix)+'0'; |
|
267 |
value/=radix; |
|
268 |
} while(value); |
|
269 |
|
|
270 |
|
|
271 |
/* start displaying the number */ |
|
272 |
for(;position<=(sizeof(usb_data)-1);position++) { |
|
273 |
|
|
274 |
usb_putc(usb_data[position]); |
|
275 |
} |
|
276 |
|
|
277 |
return 0; |
|
278 |
} |
branches/autonomous_recharging/code/projects/libbayboard/reset.c | ||
---|---|---|
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 |
**/ |
|
25 |
|
|
26 |
|
|
27 |
/** |
|
28 |
* @file reset |
|
29 |
* @brief Reset the Robot |
|
30 |
* |
|
31 |
* Implementation of a function to reset the robot. |
|
32 |
* |
|
33 |
* @author Colony Project, CMU Robotics Club |
|
34 |
**/ |
|
35 |
#include <avr/interrupt.h> |
|
36 |
#include <util/delay.h> |
|
37 |
#include <reset.h> |
|
38 |
|
|
39 |
/** |
|
40 |
* @defgroup reset Reset |
|
41 |
* Functions for manipulation the ADC on the dragonfly board. |
|
42 |
* All definitions may be found in analog.h. |
|
43 |
* |
|
44 |
* @{ |
|
45 |
**/ |
|
46 |
|
|
47 |
/** |
|
48 |
* Resets the AVR when called. Sets the watchdog timer and lets it overflow |
|
49 |
* to reset the AVR. The reset clears all memory and resets all registers |
|
50 |
* with the exception of the Watchdog Reset Flag (WDRF) in the MCU Control |
|
51 |
* and Status Register (MCUCSR). |
|
52 |
**/ |
|
53 |
void reset(void) { |
|
54 |
WDTCR &= 0xF8; |
|
55 |
WDTCR |= 0x08; |
|
56 |
_delay_ms(15); |
|
57 |
} |
|
58 |
|
|
59 |
/**@}**/ //end defgroup |
|
60 |
|
branches/autonomous_recharging/code/projects/libbayboard/serial.h | ||
---|---|---|
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 |
**/ |
|
25 |
|
|
26 |
|
|
27 |
/** |
|
28 |
* @file serial.h |
|
29 |
* @brief Contains declarations for serial input and output |
|
30 |
* |
|
31 |
* Contains definitions for serial input and output. |
|
32 |
* |
|
33 |
* @author Colony Project, CMU Robotics Club |
|
34 |
* Based on Tom Lauwer's Firefly Library |
|
35 |
* |
|
36 |
**/ |
|
37 |
|
|
38 |
/* |
|
39 |
serial.h - Contains definitions and function prototypes for the RS232 serial port |
|
40 |
author(s): pkv |
|
41 |
|
|
42 |
Directions: |
|
43 |
Call the initialization function for the serial port you wish to use. Then, use |
|
44 |
either the provided functions or the stdio functions (fprintf, etc) to read and |
|
45 |
write characters to the serial ports. |
|
46 |
|
|
47 |
UART Mapping: |
|
48 |
usb_*() -> UART0 |
|
49 |
xbee_*() -> UART1 |
|
50 |
|
|
51 |
Options: (Add the following defines to your code to configure this library) |
|
52 |
#define USB_BAUD { 115200 | 9600 } <= pick ONE value from in here |
|
53 |
#define XBEE_BAUD { 115200 | 9600 } <= pick ONE value from in here |
|
54 |
#define USE_STDIO |
|
55 |
|
|
56 |
Note: If you enable USE_STDIO, the first init function that is called will |
|
57 |
automatically be linked to stdin, stdout, and stderr. To use the baud rate |
|
58 |
commands, add something like the following to your code: |
|
59 |
|
|
60 |
#define FOO_BAUD 9600 |
|
61 |
|
|
62 |
**UNLESS YOU KNOW WHAT YOU ARE DOING, PLEASE DO NOT CHANGE THIS FILE** |
|
63 |
Many, many other people use this file in their code. If you change it, you will |
|
64 |
probably break all of their nice code. You should not need to change anything in |
|
65 |
here, except to accomodate new hardware. |
|
66 |
*/ |
|
67 |
|
|
68 |
#ifndef _SERIAL_H |
|
69 |
#define _SERIAL_H |
|
70 |
|
|
71 |
/** |
|
72 |
* @defgroup usb USB Input / Output |
|
73 |
* @brief Functions for USB input / output |
|
74 |
* |
|
75 |
* Low level functions for USB input and output. |
|
76 |
* |
|
77 |
* @{ |
|
78 |
**/ |
|
79 |
|
|
80 |
// if no baud rate is defined for usb, default is set here |
|
81 |
#ifndef USB_BAUD |
|
82 |
/** @brief the USB baud rate **/ |
|
83 |
#define USB_BAUD 115200 |
|
84 |
#endif |
|
85 |
|
|
86 |
/** @brief Initialize the USB **/ |
|
87 |
void usb_init(void); |
|
88 |
/** @brief Print a character to USB **/ |
|
89 |
int usb_putc(char c); |
|
90 |
/** @brief Read a character from USB **/ |
|
91 |
int usb_getc(void); |
|
92 |
/** @brief Read a character from USB without blocking **/ |
|
93 |
int usb_getc_nb(char *c); |
|
94 |
/** @brief Print a string to USB **/ |
|
95 |
int usb_puts(char *s); |
|
96 |
/** @brief Print an integer to USB **/ |
|
97 |
int usb_puti(int value); |
|
98 |
|
|
99 |
/** @} **/ //end addtogroup |
|
100 |
|
|
101 |
/** |
|
102 |
* @defgroup xbee XBee Input / Output |
|
103 |
* @brief Functions for XBee input / output |
|
104 |
* |
|
105 |
* Low level functions for XBee input and output. |
|
106 |
* |
|
107 |
* @{ |
|
108 |
**/ |
|
109 |
|
|
110 |
// if no baud rate is defined for usb, default is set here |
|
111 |
|
|
112 |
// if no baud rate is defined for xbee, default is set here |
|
113 |
#ifndef XBEE_BAUD |
|
114 |
/** @brief the XBee baud rate **/ |
|
115 |
#define XBEE_BAUD 9600 |
|
116 |
#endif |
|
117 |
|
|
118 |
/** @brief Initialize the XBee **/ |
|
119 |
void xbee_init(void); |
|
120 |
/** @brief Print a character to the XBee **/ |
|
121 |
int xbee_putc(char c); |
|
122 |
/** @brief Read a character from the XBee **/ |
|
123 |
int xbee_getc(void); |
|
124 |
/** @brief Read a character from the XBee without blocking **/ |
|
125 |
int xbee_getc_nb(char *c); |
|
126 |
|
|
127 |
/** @} **/ //end addtogroup |
|
128 |
|
|
129 |
#endif |
|
130 |
|
branches/autonomous_recharging/code/projects/libbayboard/reset.h | ||
---|---|---|
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 |
**/ |
|
25 |
|
|
26 |
|
|
27 |
/** |
|
28 |
* |
|
29 |
* @file reset.h |
|
30 |
* @brief Contains function(s) for resetting the robots |
|
31 |
* |
|
32 |
* @author Colony Project, CMU Robotics Club, James Kong |
|
33 |
*/ |
|
34 |
|
|
35 |
#ifndef _RESET_H_ |
|
36 |
#define _RESET_H_ |
|
37 |
|
|
38 |
/** |
|
39 |
* @addtogroup reset |
|
40 |
* @{ |
|
41 |
**/ |
|
42 |
|
|
43 |
/** @brief Reset the robot **/ |
|
44 |
void reset(void); |
|
45 |
|
|
46 |
/**@}**/ //end group |
|
47 |
|
|
48 |
#endif |
|
49 |
|
Also available in: Unified diff