Revision 1496
Reverted "libdragonfly" folder back to version before Init Checking was implemented and did "make dist" to recompile the library. BOM LEDs now shine
correctly.
serial.c | ||
---|---|---|
34 | 34 |
**/ |
35 | 35 |
|
36 | 36 |
#include <avr/io.h> |
37 |
|
|
38 |
#include "dragonfly_defs.h" |
|
39 | 37 |
#include "serial.h" |
40 | 38 |
|
41 |
unsigned char usb_initd=0; |
|
42 |
unsigned char xbee_initd=0; |
|
43 |
|
|
44 | 39 |
#ifdef USE_STDIO |
45 | 40 |
|
46 | 41 |
#include <stdio.h> |
... | ... | |
62 | 57 |
* This must be called before any other usb function |
63 | 58 |
* may be used. |
64 | 59 |
**/ |
65 |
int usb_init() {
|
|
60 |
void usb_init() {
|
|
66 | 61 |
//Set baud rate |
67 | 62 |
// - 115200 (both wired and wireless) is UBRR=8, U2X=1 |
68 | 63 |
// - 9600 is U2X =1, UBRR = 107. |
69 |
|
|
70 |
if(usb_initd) { |
|
71 |
return ERROR_INIT_ALREADY_INITD; |
|
72 |
} |
|
73 |
|
|
74 | 64 |
#if (USB_BAUD == 115200) |
75 | 65 |
UBRR0H = 0x00; |
76 | 66 |
UBRR0L = 8; |
... | ... | |
80 | 70 |
UBRR0L = 103; |
81 | 71 |
UCSR0A |= _BV(U2X0); |
82 | 72 |
#else //Baud rate is defined in the header file, we should not get here |
83 |
return 0;
|
|
73 |
return; |
|
84 | 74 |
#endif |
85 | 75 |
|
86 | 76 |
/*Enable receiver and transmitter */ |
... | ... | |
94 | 84 |
/* Open the stdio stream corresponding to this port */ |
95 | 85 |
usb_fd = fdevopen(usb_putc, usb_getc); |
96 | 86 |
#endif |
97 |
|
|
98 |
usb_initd = 1; |
|
99 |
return 0; |
|
100 |
|
|
101 | 87 |
} |
102 | 88 |
|
103 | 89 |
/** |
... | ... | |
105 | 91 |
* This must be called before any other xbee function |
106 | 92 |
* may be used. |
107 | 93 |
**/ |
108 |
int xbee_init() { |
|
109 |
|
|
110 |
if(xbee_initd) { |
|
111 |
return ERROR_INIT_ALREADY_INITD; |
|
112 |
} |
|
113 |
|
|
94 |
void xbee_init() { |
|
114 | 95 |
//Set baud rate |
115 | 96 |
// - 115200 (both wired and wireless) is UBRR=8, U2X=1 |
116 | 97 |
// - 9600 is U2X =1, UBRR = 107. |
... | ... | |
123 | 104 |
UBRR1L = 103; |
124 | 105 |
UCSR1A |= _BV(U2X1); |
125 | 106 |
#else //Baud rate is defined in the header file, we should not get here |
126 |
return 0;
|
|
107 |
return; |
|
127 | 108 |
#endif |
128 | 109 |
|
129 | 110 |
//Enable receiver and transmitter |
... | ... | |
137 | 118 |
/* Open the stdio stream corresponding to this port */ |
138 | 119 |
xbee_fd = fdevopen(xbee_putc, xbee_getc); |
139 | 120 |
#endif |
140 |
|
|
141 |
xbee_initd = 1; |
|
142 |
return 0; |
|
143 |
|
|
144 | 121 |
} |
145 | 122 |
|
146 | 123 |
/** |
... | ... | |
150 | 127 |
* @return 0 for success, nonzero for failure |
151 | 128 |
**/ |
152 | 129 |
int usb_putc(char c) { |
153 |
|
|
154 |
if(!usb_initd) |
|
155 |
return ERROR_LIBRARY_NOT_INITD; |
|
156 |
|
|
157 | 130 |
// Wait until buffer is clear for sending |
158 | 131 |
loop_until_bit_is_set(UCSR0A, UDRE0); |
159 | 132 |
|
... | ... | |
169 | 142 |
* @return 0 for success, nonzero for failure |
170 | 143 |
**/ |
171 | 144 |
int xbee_putc(char c) { |
172 |
|
|
173 |
if(!xbee_initd) |
|
174 |
return ERROR_LIBRARY_NOT_INITD; |
|
175 |
|
|
176 | 145 |
// Wait until buffer is clear for sending |
177 | 146 |
loop_until_bit_is_set(UCSR1A, UDRE1); |
178 | 147 |
|
... | ... | |
189 | 158 |
**/ |
190 | 159 |
int usb_puts(char *s) { |
191 | 160 |
char *t = s; |
192 |
|
|
193 |
if(!usb_initd) |
|
194 |
return ERROR_LIBRARY_NOT_INITD; |
|
195 |
|
|
196 | 161 |
while (*t != 0) { |
197 | 162 |
usb_putc(*t); |
198 | 163 |
t++; |
... | ... | |
204 | 169 |
* Sends a sequence of characters from program space over USB. |
205 | 170 |
* |
206 | 171 |
* @param s the string to send |
207 |
* |
|
208 |
* @return 0 if init succesfull, an error code otherwise |
|
209 | 172 |
**/ |
210 |
int usb_puts_P (PGM_P s) {
|
|
173 |
void usb_puts_P (PGM_P s) {
|
|
211 | 174 |
char buf; |
212 |
|
|
213 |
if(!usb_initd) |
|
214 |
return ERROR_LIBRARY_NOT_INITD; |
|
215 | 175 |
|
216 | 176 |
while (memcpy_P (&buf, s, sizeof (char)), buf!=0) { |
217 | 177 |
usb_putc (buf); |
218 | 178 |
s++; |
219 | 179 |
} |
220 |
|
|
221 |
return 0; |
|
222 | 180 |
} |
223 | 181 |
|
224 | 182 |
|
... | ... | |
228 | 186 |
* This function blocks execution until a character has been received. |
229 | 187 |
* xbee_init must be called before this function may be used. |
230 | 188 |
* |
231 |
* @return the first character in the usb buffer, -1 on error
|
|
189 |
* @return the first character in the usb buffer |
|
232 | 190 |
* |
233 | 191 |
* @see usb_init, usb_getc_nb |
234 | 192 |
**/ |
235 | 193 |
int usb_getc(void) { |
236 |
|
|
237 |
if(!usb_initd) |
|
238 |
return -1; |
|
239 |
|
|
240 | 194 |
// Wait for the receive buffer to be filled |
241 | 195 |
loop_until_bit_is_set(UCSR0A, RXC0); |
242 | 196 |
|
... | ... | |
250 | 204 |
* received. xbee_init must be called before this function |
251 | 205 |
* may be used. |
252 | 206 |
* |
253 |
* @return the first character in the xbee buffer, -1 on error
|
|
207 |
* @return the first character in the xbee buffer |
|
254 | 208 |
* |
255 | 209 |
* @see xbee_init, xbee_getc_nb |
256 | 210 |
**/ |
257 | 211 |
int xbee_getc(void) { |
258 |
|
|
259 |
if(!usb_initd) |
|
260 |
return -1; |
|
261 |
|
|
262 | 212 |
// Wait for the receive buffer to be filled |
263 | 213 |
loop_until_bit_is_set(UCSR1A, RXC1); |
264 | 214 |
|
... | ... | |
274 | 224 |
* @param c the received character. This will be set if a character has |
275 | 225 |
* been received. |
276 | 226 |
* |
277 |
* @return -1 if no character is available, 0 otherwise, positive for error
|
|
227 |
* @return -1 if no character is available, 0 otherwise |
|
278 | 228 |
* |
279 | 229 |
* @see usb_init, usb_getc |
280 | 230 |
**/ |
281 | 231 |
int usb_getc_nb(char *c) { |
282 |
|
|
283 |
if(!usb_initd) |
|
284 |
return ERROR_LIBRARY_NOT_INITD; |
|
285 |
|
|
286 | 232 |
// check if the receive buffer is filled |
287 | 233 |
if (UCSR0A & _BV(RXC0)) { |
288 | 234 |
// Read the receive buffer |
... | ... | |
302 | 248 |
* @param c the received character. This will be set if a character has |
303 | 249 |
* been received. |
304 | 250 |
* |
305 |
* @return -1 if no character is available, 0 otherwise, positive for error
|
|
251 |
* @return -1 if no character is available, 0 otherwise |
|
306 | 252 |
* |
307 | 253 |
* @see xbee_init, xbee_getc |
308 | 254 |
**/ |
309 | 255 |
int xbee_getc_nb(char *c) { |
310 |
if(!xbee_initd) |
|
311 |
return ERROR_LIBRARY_NOT_INITD; |
|
312 |
|
|
313 | 256 |
// check if the receive buffer is filled |
314 | 257 |
if (UCSR1A & _BV(RXC1)) { |
315 | 258 |
// Read the receive buffer |
... | ... | |
341 | 284 |
int usb_puti(int value ) { |
342 | 285 |
unsigned char usb_data[6]={'0','0','0','0','0','0' }, position=sizeof(usb_data), radix=10; |
343 | 286 |
|
344 |
if(!usb_initd) |
|
345 |
return ERROR_LIBRARY_NOT_INITD; |
|
346 |
|
|
347 |
|
|
348 | 287 |
/* convert int to ascii */ |
349 | 288 |
if(value<0) { |
350 | 289 |
usb_putc('-'); |
... | ... | |
390 | 329 |
* @param value the value to print |
391 | 330 |
* |
392 | 331 |
* @see usb_init, usb_puti, usb_puts, usb_puth8, hex_digit |
393 |
* |
|
394 |
* @return 0 if init succesfull, an error code otherwise |
|
395 | 332 |
**/ |
396 |
int usb_puth16 (uint16_t value)
|
|
333 |
void usb_puth16 (uint16_t value)
|
|
397 | 334 |
{ |
398 |
if(!usb_initd) |
|
399 |
return ERROR_LIBRARY_NOT_INITD; |
|
400 |
|
|
401 | 335 |
usb_putc (hex_digit((value >>12)&0xF)); |
402 | 336 |
usb_putc (hex_digit((value >>8 )&0xF)); |
403 | 337 |
usb_putc (hex_digit((value >>4 )&0xF)); |
404 | 338 |
usb_putc (hex_digit( value &0xF)); |
405 |
|
|
406 |
return 0; |
|
407 | 339 |
} |
408 | 340 |
|
409 | 341 |
/** |
... | ... | |
414 | 346 |
* @param value the value to print |
415 | 347 |
* |
416 | 348 |
* @see usb_init, usb_puti, usb_puts, usb_puth16, hex_digit |
417 |
* |
|
418 |
* @return 0 if init succesfull, an error code otherwise |
|
419 | 349 |
**/ |
420 |
int usb_puth8(uint8_t value)
|
|
350 |
void usb_puth8(uint8_t value)
|
|
421 | 351 |
{ |
422 |
if(!usb_initd) |
|
423 |
return ERROR_LIBRARY_NOT_INITD; |
|
424 |
|
|
425 | 352 |
usb_putc (hex_digit ((value)>>4 &0xF)); |
426 | 353 |
usb_putc (hex_digit ( value &0xF)); |
427 |
|
|
428 |
return 0; |
|
429 | 354 |
} |
430 | 355 |
|
Also available in: Unified diff