Project

General

Profile

Statistics
| Revision:

root / branches / simulator / projects / libdragonfly / serial.c @ 891

History | View | Annotate | Download (4.88 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
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
#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

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

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

    
101
int usb_putc(char c) {
102
  // 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
int xbee_putc(char c) {
111
  // 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
int usb_puts(char *s) {
120
  char *t = s;
121
  while (*t != 0) {
122
    usb_putc(*t);
123
    t++;
124
  }
125
  return 0;
126
}
127

    
128
int usb_getc(void) {
129
  // Wait for the receive buffer to be filled
130
  loop_until_bit_is_set(UCSR0A, RXC0);
131
        
132
  // Read the receive buffer
133
  return UDR0;
134
}
135

    
136
int xbee_getc(void) {
137
  // Wait for the receive buffer to be filled
138
  loop_until_bit_is_set(UCSR1A, RXC1);
139
        
140
  // Read the receive buffer
141
  return UDR1;
142
}
143

    
144
int usb_getc_nb(char *c) {
145
  // check if the receive buffer is filled
146
  if (UCSR0A & _BV(RXC0)) {
147
    // Read the receive buffer
148
    (*c) = UDR0;
149
    return 0;
150
  } else {
151
    // Return empty
152
    return -1;
153
  }
154
}
155

    
156
int xbee_getc_nb(char *c) {
157
  // check if the receive buffer is filled
158
  if (UCSR1A & _BV(RXC1)) {
159
    // Read the receive buffer
160
    (*c) = UDR1;
161
    return 0;
162
  } else {
163
    // Return empty
164
    return -1;
165
  }
166
}
167

    
168

    
169
/*
170
  prints an int to serial
171

172
  code adapted from Chris Efstathiou's code (hendrix@otenet.gr)
173
  uses usb_putc
174
*/
175
int usb_puti(int value ) {
176
  unsigned char usb_data[6]={'0','0','0','0','0','0' }, position=sizeof(usb_data), radix=10; 
177

    
178
  /* 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

    
189
    
190
  /* start displaying the number */
191
  for(;position<=(sizeof(usb_data)-1);position++) {
192
            
193
    usb_putc(usb_data[position]);
194
  }
195

    
196
  return 0;
197
}