Revision 278
more cleanup
serial.c | ||
---|---|---|
49 | 49 |
//Set baud rate |
50 | 50 |
// - 115200 (both wired and wireless) is UBRR=8, U2X=1 |
51 | 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
|
|
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 | 63 |
|
64 | 64 |
/*Enable receiver and transmitter */ |
65 |
UCSR0B |= (1<<RXEN0)|(1<<TXEN0);
|
|
65 |
UCSR0B |= (1<<RXEN0)|(1<<TXEN0);
|
|
66 | 66 |
|
67 |
/* Set frame format: 8data, 1stop bit, asynchronous normal mode */
|
|
68 |
UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01);
|
|
67 |
/* Set frame format: 8data, 1stop bit, asynchronous normal mode */
|
|
68 |
UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01);
|
|
69 | 69 |
|
70 | 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
|
|
71 |
#ifdef USE_STDIO |
|
72 |
/* Open the stdio stream corresponding to this port */ |
|
73 |
usb_fd = fdevopen(usb_putc, usb_getc); |
|
74 |
#endif |
|
75 | 75 |
} |
76 | 76 |
|
77 | 77 |
/** |
... | ... | |
83 | 83 |
//Set baud rate |
84 | 84 |
// - 115200 (both wired and wireless) is UBRR=8, U2X=1 |
85 | 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
|
|
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 | 97 |
|
98 |
//Enable receiver and transmitter
|
|
99 |
UCSR1B |= (1<<RXEN1)|(1<<TXEN1);
|
|
98 |
//Enable receiver and transmitter
|
|
99 |
UCSR1B |= (1<<RXEN1)|(1<<TXEN1);
|
|
100 | 100 |
|
101 |
// Set frame format: 8data, 1stop bit, asynchronous normal mode
|
|
102 |
UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11);
|
|
101 |
// Set frame format: 8data, 1stop bit, asynchronous normal mode
|
|
102 |
UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11);
|
|
103 | 103 |
|
104 | 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
|
|
105 |
#ifdef USE_STDIO |
|
106 |
/* Open the stdio stream corresponding to this port */ |
|
107 |
xbee_fd = fdevopen(xbee_putc, xbee_getc); |
|
108 |
#endif |
|
109 | 109 |
} |
110 | 110 |
|
111 | 111 |
/** |
... | ... | |
148 | 148 |
**/ |
149 | 149 |
int usb_puts(char *s) |
150 | 150 |
{ |
151 |
char *t = s; |
|
152 |
while (*t != 0) |
|
153 |
{ |
|
154 |
usb_putc(*t); |
|
155 |
t++; |
|
156 |
} |
|
151 |
char *t = s; |
|
152 |
while (*t != 0) { |
|
153 |
usb_putc(*t); |
|
154 |
t++; |
|
155 |
} |
|
157 | 156 |
return 0; |
158 | 157 |
} |
159 | 158 |
|
... | ... | |
168 | 167 |
**/ |
169 | 168 |
int usb_getc(void) |
170 | 169 |
{ |
171 |
// Wait for the receive buffer to be filled
|
|
172 |
loop_until_bit_is_set(UCSR0A, RXC0);
|
|
170 |
// Wait for the receive buffer to be filled
|
|
171 |
loop_until_bit_is_set(UCSR0A, RXC0);
|
|
173 | 172 |
|
174 |
// Read the receive buffer
|
|
175 |
return UDR0;
|
|
173 |
// Read the receive buffer
|
|
174 |
return UDR0;
|
|
176 | 175 |
} |
177 | 176 |
|
178 | 177 |
/** |
... | ... | |
187 | 186 |
**/ |
188 | 187 |
int xbee_getc(void) |
189 | 188 |
{ |
190 |
// Wait for the receive buffer to be filled
|
|
191 |
loop_until_bit_is_set(UCSR1A, RXC1);
|
|
189 |
// Wait for the receive buffer to be filled
|
|
190 |
loop_until_bit_is_set(UCSR1A, RXC1); |
|
192 | 191 |
|
193 |
// Read the receive buffer
|
|
194 |
return UDR1;
|
|
192 |
// Read the receive buffer
|
|
193 |
return UDR1;
|
|
195 | 194 |
} |
196 | 195 |
|
197 | 196 |
/** |
... | ... | |
208 | 207 |
**/ |
209 | 208 |
int usb_getc_nb(char *c) |
210 | 209 |
{ |
211 |
// check if the receive buffer is filled
|
|
210 |
// check if the receive buffer is filled
|
|
212 | 211 |
if (UCSR0A & _BV(RXC0)) { |
213 | 212 |
// Read the receive buffer |
214 | 213 |
(*c) = UDR0; |
215 | 214 |
return 0; |
216 |
} |
|
217 |
else { |
|
215 |
} else { |
|
218 | 216 |
// Return empty |
219 | 217 |
return -1; |
220 |
}
|
|
218 |
}
|
|
221 | 219 |
} |
222 | 220 |
|
223 | 221 |
/** |
... | ... | |
234 | 232 |
**/ |
235 | 233 |
int xbee_getc_nb(char *c) |
236 | 234 |
{ |
237 |
// check if the receive buffer is filled
|
|
235 |
// check if the receive buffer is filled
|
|
238 | 236 |
if (UCSR1A & _BV(RXC1)) { |
239 | 237 |
// Read the receive buffer |
240 | 238 |
(*c) = UDR1; |
241 | 239 |
return 0; |
242 |
} |
|
243 |
else { |
|
240 |
} else { |
|
244 | 241 |
// Return empty |
245 | 242 |
return -1; |
246 |
}
|
|
243 |
}
|
|
247 | 244 |
} |
248 | 245 |
|
249 | 246 |
|
... | ... | |
263 | 260 |
* |
264 | 261 |
* @see usb_init, usb_putc |
265 | 262 |
**/ |
266 |
int usb_puti(int value ) {
|
|
267 |
unsigned char usb_data[6]={'0','0','0','0','0','0' }, position=sizeof(usb_data), radix=10;
|
|
263 |
int usb_puti(int value) { |
|
264 |
unsigned char usb_data[6]={'0','0','0','0','0','0' }, position=sizeof(usb_data), radix=10;
|
|
268 | 265 |
|
269 |
/* convert int to ascii */
|
|
270 |
if(value<0) { usb_putc('-'); value=-value; }
|
|
271 |
do { position--; *(usb_data+position)=(value%radix)+'0'; value/=radix; } while(value);
|
|
266 |
/* convert int to ascii */ |
|
267 |
if(value<0) { usb_putc('-'); value=-value; } |
|
268 |
do { position--; *(usb_data+position)=(value%radix)+'0'; value/=radix; } while(value);
|
|
272 | 269 |
|
270 |
/* start displaying the number */ |
|
271 |
for(; position <= (sizeof(usb_data)-1); position++) { |
|
272 |
usb_putc(usb_data[position]); |
|
273 |
} |
|
273 | 274 |
|
274 |
/* start displaying the number */ |
|
275 |
for(;position<=(sizeof(usb_data)-1);position++) |
|
276 |
{ |
|
277 |
|
|
278 |
usb_putc(usb_data[position]); |
|
279 |
} |
|
280 |
|
|
281 |
return 0; |
|
275 |
return 0; |
|
282 | 276 |
} |
283 |
|
|
284 |
|
Also available in: Unified diff