serial.c
Go to the documentation of this file.00001 00036 #include <avr/io.h> 00037 #include "serial.h" 00038 00039 #ifdef USE_STDIO 00040 00041 #include <stdio.h> 00042 00046 FILE *usb_fd; 00047 00051 FILE *xbee_fd; 00052 00053 #endif 00054 00060 void usb_init() { 00061 //Set baud rate 00062 // - 115200 (both wired and wireless) is UBRR=8, U2X=1 00063 // - 9600 is U2X =1, UBRR = 107. 00064 #if (USB_BAUD == 115200) 00065 UBRR0H = 0x00; 00066 UBRR0L = 8; 00067 UCSR0A |= _BV(U2X0); 00068 #elif (USB_BAUD == 9600) 00069 UBRR0H = 0x00; 00070 UBRR0L = 103; 00071 UCSR0A |= _BV(U2X0); 00072 #else //Baud rate is defined in the header file, we should not get here 00073 return; 00074 #endif 00075 00076 /*Enable receiver and transmitter */ 00077 UCSR0B |= (1<<RXEN0)|(1<<TXEN0); 00078 00079 /* Set frame format: 8data, 1stop bit, asynchronous normal mode */ 00080 UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01); 00081 00082 // if we have enabled the stdio stuff, then we init it here 00083 #ifdef USE_STDIO 00084 /* Open the stdio stream corresponding to this port */ 00085 usb_fd = fdevopen(usb_putc, usb_getc); 00086 #endif 00087 } 00088 00094 void xbee_init() { 00095 //Set baud rate 00096 // - 115200 (both wired and wireless) is UBRR=8, U2X=1 00097 // - 9600 is U2X =1, UBRR = 107. 00098 #if (XBEE_BAUD == 115200) 00099 UBRR1H = 0x00; 00100 UBRR1L = 8; 00101 UCSR1A |= _BV(U2X1); 00102 #elif (XBEE_BAUD == 9600) 00103 UBRR1H = 0x00; 00104 UBRR1L = 103; 00105 UCSR1A |= _BV(U2X1); 00106 #else //Baud rate is defined in the header file, we should not get here 00107 return; 00108 #endif 00109 00110 //Enable receiver and transmitter 00111 UCSR1B |= (1<<RXEN1)|(1<<TXEN1); 00112 00113 // Set frame format: 8data, 1stop bit, asynchronous normal mode 00114 UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11); 00115 00116 // if we have enabled the stdio stuff, then we init it here 00117 #ifdef USE_STDIO 00118 /* Open the stdio stream corresponding to this port */ 00119 xbee_fd = fdevopen(xbee_putc, xbee_getc); 00120 #endif 00121 } 00122 00129 int usb_putc(char c) { 00130 // Wait until buffer is clear for sending 00131 loop_until_bit_is_set(UCSR0A, UDRE0); 00132 00133 // Load buffer with your character 00134 UDR0 = c; 00135 return 0; 00136 } 00137 00144 int xbee_putc(char c) { 00145 // Wait until buffer is clear for sending 00146 loop_until_bit_is_set(UCSR1A, UDRE1); 00147 00148 // Load buffer with your character 00149 UDR1 = c; 00150 return 0; 00151 } 00152 00159 int usb_puts(char *s) { 00160 char *t = s; 00161 while (*t != 0) { 00162 usb_putc(*t); 00163 t++; 00164 } 00165 return 0; 00166 } 00167 00173 void usb_puts_P (PGM_P s) { 00174 char buf; 00175 00176 while (memcpy_P (&buf, s, sizeof (char)), buf!=0) { 00177 usb_putc (buf); 00178 s++; 00179 } 00180 } 00181 00182 00183 00193 int usb_getc(void) { 00194 // Wait for the receive buffer to be filled 00195 loop_until_bit_is_set(UCSR0A, RXC0); 00196 00197 // Read the receive buffer 00198 return UDR0; 00199 } 00200 00211 int xbee_getc(void) { 00212 // Wait for the receive buffer to be filled 00213 loop_until_bit_is_set(UCSR1A, RXC1); 00214 00215 // Read the receive buffer 00216 return UDR1; 00217 } 00218 00231 int usb_getc_nb(char *c) { 00232 // check if the receive buffer is filled 00233 if (UCSR0A & _BV(RXC0)) { 00234 // Read the receive buffer 00235 (*c) = UDR0; 00236 return 0; 00237 } else { 00238 // Return empty 00239 return -1; 00240 } 00241 } 00242 00255 int xbee_getc_nb(char *c) { 00256 // check if the receive buffer is filled 00257 if (UCSR1A & _BV(RXC1)) { 00258 // Read the receive buffer 00259 (*c) = UDR1; 00260 return 0; 00261 } else { 00262 // Return empty 00263 return -1; 00264 } 00265 } 00266 00267 00268 /* 00269 prints an int to serial 00270 00271 code adapted from Chris Efstathiou's code (hendrix@otenet.gr) 00272 uses usb_putc 00273 */ 00284 int usb_puti(int value ) { 00285 unsigned char usb_data[6]={'0','0','0','0','0','0' }, position=sizeof(usb_data), radix=10; 00286 00287 /* convert int to ascii */ 00288 if(value<0) { 00289 usb_putc('-'); 00290 value=-value; 00291 } 00292 do { 00293 position--; 00294 *(usb_data+position)=(value%radix)+'0'; 00295 value/=radix; 00296 } while(value); 00297 00298 00299 /* start displaying the number */ 00300 for(;position<=(sizeof(usb_data)-1);position++) { 00301 00302 usb_putc(usb_data[position]); 00303 } 00304 00305 return 0; 00306 } 00307 00316 uint8_t hex_digit (uint8_t value) 00317 { 00318 if (value>15) return '?'; 00319 // Postcondition: 0<=x<=15 00320 00321 return "0123456789ABCDEF"[value]; 00322 } 00323 00333 void usb_puth16 (uint16_t value) 00334 { 00335 usb_putc (hex_digit((value >>12)&0xF)); 00336 usb_putc (hex_digit((value >>8 )&0xF)); 00337 usb_putc (hex_digit((value >>4 )&0xF)); 00338 usb_putc (hex_digit( value &0xF)); 00339 } 00340 00350 void usb_puth8(uint8_t value) 00351 { 00352 usb_putc (hex_digit ((value)>>4 &0xF)); 00353 usb_putc (hex_digit ( value &0xF)); 00354 } 00355
Generated on Thu May 17 00:00:09 2012 for libdragonfly by
1.6.1