root / branches / simulator / projects / libdragonfly / serial.c @ 1076
History | View | Annotate | Download (4.88 KB)
1 | 241 | bcoltin | /**
|
---|---|---|---|
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 | 8 | bcoltin | |
26 | 241 | bcoltin | |
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 | 8 | bcoltin | #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 | void usb_init() {
|
||
44 | //Set baud rate
|
||
45 | // - 115200 (both wired and wireless) is UBRR=8, U2X=1
|
||
46 | // - 9600 is U2X =1, UBRR = 107.
|
||
47 | 380 | jknichel | #if (USB_BAUD == 115200) |
48 | UBRR0H = 0x00;
|
||
49 | UBRR0L = 8;
|
||
50 | UCSR0A |= _BV(U2X0); |
||
51 | #elif (USB_BAUD == 9600) |
||
52 | UBRR0H = 0x00;
|
||
53 | UBRR0L = 103;
|
||
54 | UCSR0A |= _BV(U2X0); |
||
55 | #else //Baud rate is defined in the header file, we should not get here |
||
56 | return;
|
||
57 | #endif
|
||
58 | 8 | bcoltin | |
59 | /*Enable receiver and transmitter */
|
||
60 | 380 | jknichel | UCSR0B |= (1<<RXEN0)|(1<<TXEN0); |
61 | 8 | bcoltin | |
62 | 380 | jknichel | /* Set frame format: 8data, 1stop bit, asynchronous normal mode */
|
63 | UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01); |
||
64 | 8 | bcoltin | |
65 | // if we have enabled the stdio stuff, then we init it here
|
||
66 | 380 | jknichel | #ifdef USE_STDIO
|
67 | /* Open the stdio stream corresponding to this port */
|
||
68 | usb_fd = fdevopen(usb_putc, usb_getc); |
||
69 | #endif
|
||
70 | 8 | bcoltin | } |
71 | |||
72 | void xbee_init() {
|
||
73 | //Set baud rate
|
||
74 | // - 115200 (both wired and wireless) is UBRR=8, U2X=1
|
||
75 | // - 9600 is U2X =1, UBRR = 107.
|
||
76 | 380 | jknichel | #if (XBEE_BAUD == 115200) |
77 | UBRR1H = 0x00;
|
||
78 | UBRR1L = 8;
|
||
79 | UCSR1A |= _BV(U2X1); |
||
80 | #elif (XBEE_BAUD == 9600) |
||
81 | UBRR1H = 0x00;
|
||
82 | UBRR1L = 103;
|
||
83 | UCSR1A |= _BV(U2X1); |
||
84 | #else //Baud rate is defined in the header file, we should not get here |
||
85 | return;
|
||
86 | #endif
|
||
87 | 8 | bcoltin | |
88 | 380 | jknichel | //Enable receiver and transmitter
|
89 | UCSR1B |= (1<<RXEN1)|(1<<TXEN1); |
||
90 | 8 | bcoltin | |
91 | 380 | jknichel | // Set frame format: 8data, 1stop bit, asynchronous normal mode
|
92 | UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11); |
||
93 | 8 | bcoltin | |
94 | // if we have enabled the stdio stuff, then we init it here
|
||
95 | 380 | jknichel | #ifdef USE_STDIO
|
96 | /* Open the stdio stream corresponding to this port */
|
||
97 | xbee_fd = fdevopen(xbee_putc, xbee_getc); |
||
98 | #endif
|
||
99 | 8 | bcoltin | } |
100 | |||
101 | 380 | jknichel | int usb_putc(char c) { |
102 | 8 | bcoltin | // Wait until buffer is clear for sending
|
103 | loop_until_bit_is_set(UCSR0A, UDRE0); |
||
104 | |||
105 | // Load buffer with your character
|
||
106 | UDR0 = c; |
||
107 | return 0; |
||
108 | } |
||
109 | |||
110 | 380 | jknichel | int xbee_putc(char c) { |
111 | 8 | bcoltin | // Wait until buffer is clear for sending
|
112 | loop_until_bit_is_set(UCSR1A, UDRE1); |
||
113 | |||
114 | // Load buffer with your character
|
||
115 | UDR1 = c; |
||
116 | return 0; |
||
117 | } |
||
118 | |||
119 | 380 | jknichel | int usb_puts(char *s) { |
120 | char *t = s;
|
||
121 | while (*t != 0) { |
||
122 | usb_putc(*t); |
||
123 | t++; |
||
124 | } |
||
125 | 8 | bcoltin | return 0; |
126 | } |
||
127 | |||
128 | 380 | jknichel | int usb_getc(void) { |
129 | // Wait for the receive buffer to be filled
|
||
130 | loop_until_bit_is_set(UCSR0A, RXC0); |
||
131 | 8 | bcoltin | |
132 | 380 | jknichel | // Read the receive buffer
|
133 | return UDR0;
|
||
134 | 8 | bcoltin | } |
135 | |||
136 | 380 | jknichel | int xbee_getc(void) { |
137 | // Wait for the receive buffer to be filled
|
||
138 | loop_until_bit_is_set(UCSR1A, RXC1); |
||
139 | 8 | bcoltin | |
140 | 380 | jknichel | // Read the receive buffer
|
141 | return UDR1;
|
||
142 | 8 | bcoltin | } |
143 | |||
144 | 380 | jknichel | int usb_getc_nb(char *c) { |
145 | // check if the receive buffer is filled
|
||
146 | 8 | bcoltin | if (UCSR0A & _BV(RXC0)) {
|
147 | // Read the receive buffer
|
||
148 | (*c) = UDR0; |
||
149 | return 0; |
||
150 | 380 | jknichel | } else {
|
151 | 8 | bcoltin | // Return empty
|
152 | return -1; |
||
153 | 380 | jknichel | } |
154 | 8 | bcoltin | } |
155 | |||
156 | 380 | jknichel | int xbee_getc_nb(char *c) { |
157 | // check if the receive buffer is filled
|
||
158 | 8 | bcoltin | if (UCSR1A & _BV(RXC1)) {
|
159 | // Read the receive buffer
|
||
160 | (*c) = UDR1; |
||
161 | return 0; |
||
162 | 380 | jknichel | } else {
|
163 | 8 | bcoltin | // Return empty
|
164 | return -1; |
||
165 | 380 | jknichel | } |
166 | 8 | bcoltin | } |
167 | |||
168 | |||
169 | /*
|
||
170 | 380 | jknichel | prints an int to serial
|
171 | 8 | bcoltin | |
172 | 380 | jknichel | code adapted from Chris Efstathiou's code (hendrix@otenet.gr)
|
173 | uses usb_putc
|
||
174 | 8 | bcoltin | */
|
175 | int usb_puti(int value ) { |
||
176 | 380 | jknichel | unsigned char usb_data[6]={'0','0','0','0','0','0' }, position=sizeof(usb_data), radix=10; |
177 | 8 | bcoltin | |
178 | 380 | jknichel | /* convert int to ascii */
|
179 | if(value<0) { |
||
180 | usb_putc('-');
|
||
181 | value=-value; |
||
182 | } |
||
183 | do {
|
||
184 | position--; |
||
185 | *(usb_data+position)=(value%radix)+'0';
|
||
186 | value/=radix; |
||
187 | } while(value);
|
||
188 | 8 | bcoltin | |
189 | |||
190 | 380 | jknichel | /* start displaying the number */
|
191 | for(;position<=(sizeof(usb_data)-1);position++) { |
||
192 | 8 | bcoltin | |
193 | 380 | jknichel | usb_putc(usb_data[position]); |
194 | } |
||
195 | 8 | bcoltin | |
196 | 380 | jknichel | return 0; |
197 | 8 | bcoltin | } |