root / trunk / code / lib / src / libdragonfly / serial.c @ 7
History | View | Annotate | Download (6.08 KB)
1 | 7 | bcoltin | /*
|
---|---|---|---|
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 | FILE *usb_fd; //For use with fprintf() and related stdio functions
|
||
13 | FILE *xbee_fd; //For use with fprintf() and related stdio functions
|
||
14 | |||
15 | /**
|
||
16 | * Initializes communication over the USB serial port.
|
||
17 | * This must be called before any other usb function
|
||
18 | * may be used.
|
||
19 | **/
|
||
20 | void usb_init() {
|
||
21 | //Set baud rate
|
||
22 | // - 115200 (both wired and wireless) is UBRR=8, U2X=1
|
||
23 | // - 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
|
||
35 | |||
36 | /*Enable receiver and transmitter */
|
||
37 | UCSR0B |= (1<<RXEN0)|(1<<TXEN0); |
||
38 | |||
39 | /* Set frame format: 8data, 1stop bit, asynchronous normal mode */
|
||
40 | UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01); |
||
41 | |||
42 | // 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
|
||
47 | } |
||
48 | |||
49 | /**
|
||
50 | * Initializes communication over the XBee.
|
||
51 | * This must be called before any other xbee function
|
||
52 | * may be used.
|
||
53 | **/
|
||
54 | void xbee_init() {
|
||
55 | //Set baud rate
|
||
56 | // - 115200 (both wired and wireless) is UBRR=8, U2X=1
|
||
57 | // - 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
|
||
69 | |||
70 | //Enable receiver and transmitter
|
||
71 | UCSR1B |= (1<<RXEN1)|(1<<TXEN1); |
||
72 | |||
73 | // Set frame format: 8data, 1stop bit, asynchronous normal mode
|
||
74 | UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11); |
||
75 | |||
76 | // 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
|
||
81 | } |
||
82 | |||
83 | /**
|
||
84 | * Sends a character over USB.
|
||
85 | *
|
||
86 | * @param c the character to send
|
||
87 | * @return 0 for success, nonzero for failure
|
||
88 | **/
|
||
89 | int usb_putc(char c) |
||
90 | { |
||
91 | // Wait until buffer is clear for sending
|
||
92 | loop_until_bit_is_set(UCSR0A, UDRE0); |
||
93 | |||
94 | // Load buffer with your character
|
||
95 | UDR0 = c; |
||
96 | return 0; |
||
97 | } |
||
98 | |||
99 | /**
|
||
100 | * Sends a character to the XBee.
|
||
101 | *
|
||
102 | * @param c the character to send
|
||
103 | * @return 0 for success, nonzero for failure
|
||
104 | **/
|
||
105 | int xbee_putc(char c) |
||
106 | { |
||
107 | // Wait until buffer is clear for sending
|
||
108 | loop_until_bit_is_set(UCSR1A, UDRE1); |
||
109 | |||
110 | // Load buffer with your character
|
||
111 | UDR1 = c; |
||
112 | return 0; |
||
113 | } |
||
114 | |||
115 | /**
|
||
116 | * Sends a sequence of characters over USB.
|
||
117 | *
|
||
118 | * @param s the string to send
|
||
119 | * @return 0 for success, nonzero for failure
|
||
120 | **/
|
||
121 | int usb_puts(char *s) |
||
122 | { |
||
123 | char *t = s;
|
||
124 | while (*t != 0) |
||
125 | { |
||
126 | usb_putc(*t); |
||
127 | t++; |
||
128 | } |
||
129 | return 0; |
||
130 | } |
||
131 | |||
132 | /**
|
||
133 | * Returns the first character in the buffer received from USB.
|
||
134 | * This function blocks execution until a character has been received.
|
||
135 | * xbee_init must be called before this function may be used.
|
||
136 | *
|
||
137 | * @return the first character in the usb buffer
|
||
138 | *
|
||
139 | * @see usb_init, usb_getc_nb
|
||
140 | **/
|
||
141 | int usb_getc(void) |
||
142 | { |
||
143 | // Wait for the receive buffer to be filled
|
||
144 | loop_until_bit_is_set(UCSR0A, RXC0); |
||
145 | |||
146 | // Read the receive buffer
|
||
147 | return UDR0;
|
||
148 | } |
||
149 | |||
150 | /**
|
||
151 | * Returns the first character in the buffer received from USB.
|
||
152 | * This function blocks execution until a character has been
|
||
153 | * received. xbee_init must be called before this function
|
||
154 | * may be used.
|
||
155 | *
|
||
156 | * @return the first character in the xbee buffer
|
||
157 | *
|
||
158 | * @see xbee_init, xbee_getc_nb
|
||
159 | **/
|
||
160 | int xbee_getc(void) |
||
161 | { |
||
162 | // Wait for the receive buffer to be filled
|
||
163 | loop_until_bit_is_set(UCSR1A, RXC1); |
||
164 | |||
165 | // Read the receive buffer
|
||
166 | return UDR1;
|
||
167 | } |
||
168 | |||
169 | /**
|
||
170 | * Non blocking version of usb_getc. If a character is present in the buffer,
|
||
171 | * it is returned, otherwise -1 is returned immediately. usb_init must be
|
||
172 | * called before this function can be used.
|
||
173 | *
|
||
174 | * @param c the received character. This will be set if a character has
|
||
175 | * been received.
|
||
176 | *
|
||
177 | * @return -1 if no character is available, 0 otherwise
|
||
178 | *
|
||
179 | * @see usb_init, usb_getc
|
||
180 | **/
|
||
181 | int usb_getc_nb(char *c) |
||
182 | { |
||
183 | // check if the receive buffer is filled
|
||
184 | if (UCSR0A & _BV(RXC0)) {
|
||
185 | // Read the receive buffer
|
||
186 | (*c) = UDR0; |
||
187 | return 0; |
||
188 | } |
||
189 | else {
|
||
190 | // Return empty
|
||
191 | return -1; |
||
192 | } |
||
193 | } |
||
194 | |||
195 | /**
|
||
196 | * Non blocking version of xbee_getc. If a character is present in the buffer,
|
||
197 | * it is returned, otherwise -1 is returned immediately. xbee_init
|
||
198 | * must be called before this function can be used.
|
||
199 | *
|
||
200 | * @param c the received character. This will be set if a character has
|
||
201 | * been received.
|
||
202 | *
|
||
203 | * @return -1 if no character is available, 0 otherwise
|
||
204 | *
|
||
205 | * @see xbee_init, xbee_getc
|
||
206 | **/
|
||
207 | int xbee_getc_nb(char *c) |
||
208 | { |
||
209 | // check if the receive buffer is filled
|
||
210 | if (UCSR1A & _BV(RXC1)) {
|
||
211 | // Read the receive buffer
|
||
212 | (*c) = UDR1; |
||
213 | return 0; |
||
214 | } |
||
215 | else {
|
||
216 | // Return empty
|
||
217 | return -1; |
||
218 | } |
||
219 | } |
||
220 | |||
221 | |||
222 | /*
|
||
223 | prints an int to serial
|
||
224 | |||
225 | code adapted from Chris Efstathiou's code (hendrix@otenet.gr)
|
||
226 | uses usb_putc
|
||
227 | */
|
||
228 | /**
|
||
229 | * Prints an integer, converted to ASCII, to usb. usb_init must be called
|
||
230 | * before this function can be used.
|
||
231 | *
|
||
232 | * @param value the integer to print
|
||
233 | *
|
||
234 | * @return 0 if successful, nonzero otherwise
|
||
235 | *
|
||
236 | * @see usb_init, usb_putc
|
||
237 | **/
|
||
238 | int usb_puti(int value ) { |
||
239 | unsigned char usb_data[6]={'0','0','0','0','0','0' }, position=sizeof(usb_data), radix=10; |
||
240 | |||
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); |
||
244 | |||
245 | |||
246 | /* start displaying the number */
|
||
247 | for(;position<=(sizeof(usb_data)-1);position++) |
||
248 | { |
||
249 | |||
250 | usb_putc(usb_data[position]); |
||
251 | } |
||
252 | |||
253 | return 0; |
||
254 | } |
||
255 |