Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / libdragonfly / serial.c @ 491

History | View | Annotate | Download (7.16 KB)

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
}