Project

General

Profile

Statistics
| Revision:

root / branches / autonomous_recharging / code / projects / libbayboard / serial.c @ 649

History | View | Annotate | Download (7.09 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 for Bayboard
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 = 103.
52
#if (USB_BAUD == 115200)
53
  UBRR0 = 8;
54
  UCSR0A |= _BV(U2X0);
55
#elif (USB_BAUD == 9600)
56
  UBRR0 = 103;
57
  UCSR0A |= _BV(U2X0);
58
#else //Baud rate is defined in the header file, we should not get here
59
  return;
60
#endif
61

    
62
  /*Enable receiver and transmitter */
63
  UCSR0B |= (1<<RXEN0)|(1<<TXEN0);
64
        
65
  /* Set frame format: 8data, 1stop bit, asynchronous normal mode */
66
  UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01);
67
  
68
  // if we have enabled the stdio stuff, then we init it here
69
#ifdef USE_STDIO
70
  /* Open the stdio stream corresponding to this port */
71
  usb_fd = fdevopen(usb_putc, usb_getc);
72
#endif
73
}
74

    
75
/**
76
 * Initializes communication over the XBee.
77
 * This must be called before any other xbee function
78
 * may be used.
79
 **/
80
void xbee_init() {
81
  //Set baud rate
82
  // - 115200 (both wired and wireless) is UBRR=8, U2X=1
83
  // - 9600 is U2X =1, UBRR = 103.
84
#if (XBEE_BAUD == 115200)
85
  UBRR1 = 8;
86
  UCSR1A |= _BV(U2X1);
87
#elif (XBEE_BAUD == 9600)
88
  UBRR1 = 103;
89
  UCSR1A |= _BV(U2X1);
90
#else //Baud rate is defined in the header file, we should not get here
91
  return;
92
#endif
93

    
94
  //Enable receiver and transmitter
95
  UCSR1B |= (1<<RXEN1)|(1<<TXEN1);
96
        
97
  // Set frame format: 8data, 1stop bit, asynchronous normal mode
98
  UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11);
99
  
100
  // if we have enabled the stdio stuff, then we init it here
101
#ifdef USE_STDIO
102
  /* Open the stdio stream corresponding to this port */
103
  xbee_fd = fdevopen(xbee_putc, xbee_getc);
104
#endif
105
}
106

    
107
/**
108
 * Sends a character over USB.
109
 *
110
 * @param c the character to send
111
 * @return 0 for success, nonzero for failure
112
 **/
113
int usb_putc(char c) {
114
  // Wait until buffer is clear for sending
115
  loop_until_bit_is_set(UCSR0A, UDRE0);
116
        
117
  // Load buffer with your character
118
  UDR0 = c;
119
  return 0;
120
}
121

    
122
/**
123
 * Sends a character to the XBee.
124
 *
125
 * @param c the character to send
126
 * @return 0 for success, nonzero for failure
127
 **/
128
int xbee_putc(char c) {
129
  // Wait until buffer is clear for sending
130
  loop_until_bit_is_set(UCSR1A, UDRE1);
131
        
132
  // Load buffer with your character
133
  UDR1 = c;
134
  return 0;
135
}
136

    
137
/**
138
 * Sends a sequence of characters over USB.
139
 *
140
 * @param s the string to send
141
 * @return 0 for success, nonzero for failure
142
 **/
143
int usb_puts(char *s) {
144
  char *t = s;
145
  while (*t != 0) {
146
    usb_putc(*t);
147
    t++;
148
  }
149
  return 0;
150
}
151

    
152
/**
153
 * Returns the first character in the buffer received from USB.
154
 * This function blocks execution until a character has been received.
155
 * xbee_init must be called before this function may be used.
156
 * 
157
 * @return the first character in the usb buffer
158
 *
159
 * @see usb_init, usb_getc_nb
160
 **/
161
int usb_getc(void) {
162
  // Wait for the receive buffer to be filled
163
  loop_until_bit_is_set(UCSR0A, RXC0);
164
        
165
  // Read the receive buffer
166
  return UDR0;
167
}
168

    
169
/**
170
 * Returns the first character in the buffer received from USB.
171
 * This function blocks execution until a character has been
172
 * received. xbee_init must be called before this function
173
 * may be used.
174
 * 
175
 * @return the first character in the xbee buffer
176
 * 
177
 * @see xbee_init, xbee_getc_nb
178
 **/
179
int xbee_getc(void) {
180
  // Wait for the receive buffer to be filled
181
  loop_until_bit_is_set(UCSR1A, RXC1);
182
        
183
  // Read the receive buffer
184
  return UDR1;
185
}
186

    
187
/**
188
 * Non blocking version of usb_getc. If a character is present in the buffer,
189
 * it is returned, otherwise -1 is returned immediately. usb_init must be
190
 * called before this function can be used.
191
 *
192
 * @param c the received character. This will be set if a character has
193
 * been received.
194
 * 
195
 * @return -1 if no character is available, 0 otherwise
196
 * 
197
 * @see usb_init, usb_getc
198
 **/
199
int usb_getc_nb(char *c) {
200
  // check if the receive buffer is filled
201
  if (UCSR0A & _BV(RXC0)) {
202
    // Read the receive buffer
203
    (*c) = UDR0;
204
    return 0;
205
  } else {
206
    // Return empty
207
    return -1;
208
  }
209
}
210

    
211
/**
212
 * Non blocking version of xbee_getc. If a character is present in the buffer,
213
 * it is returned, otherwise -1 is returned immediately. xbee_init
214
 * must be called before this function can be used.
215
 *
216
 * @param c the received character. This will be set if a character has
217
 * been received.
218
 * 
219
 * @return -1 if no character is available, 0 otherwise
220
 *
221
 * @see xbee_init, xbee_getc
222
 **/
223
int xbee_getc_nb(char *c) {
224
  // check if the receive buffer is filled
225
  if (UCSR1A & _BV(RXC1)) {
226
    // Read the receive buffer
227
    (*c) = UDR1;
228
    return 0;
229
  } else {
230
    // Return empty
231
    return -1;
232
  }
233
}
234

    
235

    
236
/*
237
  prints an int to serial
238

239
  code adapted from Chris Efstathiou's code (hendrix@otenet.gr)
240
  uses usb_putc
241
*/
242
/**
243
 * Prints an integer, converted to ASCII, to usb. usb_init must be called
244
 * before this function can be used.
245
 *
246
 * @param value the integer to print
247
 * 
248
 * @return 0 if successful, nonzero otherwise
249
 *
250
 * @see usb_init, usb_putc
251
 **/
252
int usb_puti(int value ) {
253
  unsigned char usb_data[6]={'0','0','0','0','0','0' }, position=sizeof(usb_data), radix=10; 
254

    
255
  /* convert int to ascii  */ 
256
  if(value<0) { 
257
    usb_putc('-'); 
258
    value=-value; 
259
  }    
260
  do { 
261
    position--; 
262
    *(usb_data+position)=(value%radix)+'0'; 
263
    value/=radix;  
264
  } while(value); 
265

    
266
    
267
  /* start displaying the number */
268
  for(;position<=(sizeof(usb_data)-1);position++) {
269
            
270
    usb_putc(usb_data[position]);
271
  }
272

    
273
  return 0;
274
}