1 #include <avr/interrupt.h>
5 // the time remaining before we transmit any partially full
6 // packet, or send a zero length packet.
7 volatile uint8_t debug_flush_timer=0;
10 // transmit a character. 0 returned on success, -1 on error
11 int8_t usb_debug_putchar(uint8_t c)
13 static uint8_t previous_timeout=0;
14 uint8_t timeout, intr_state;
16 // if we're not online (enumerated and configured), error
17 if (!usb_configured()) return -1;
18 // interrupts are disabled so these functions can be
19 // used from the main program or interrupt context,
20 // even both in the same program!
23 UENUM = DEBUG_TX_ENDPOINT;
24 // if we gave up due to timeout before, don't wait again
25 if (previous_timeout) {
26 if (!(UEINTX & (1<<RWAL))) {
32 // wait for the FIFO to be ready to accept data
33 timeout = UDFNUML + 4;
35 // are we ready to transmit?
36 if (UEINTX & (1<<RWAL)) break;
38 // have we waited too long?
39 if (UDFNUML == timeout) {
43 // has the USB gone offline?
44 if (!usb_configured()) return -1;
45 // get ready to try checking again
48 UENUM = DEBUG_TX_ENDPOINT;
50 // actually write the byte into the FIFO
52 // if this completed a packet, transmit it now!
53 if (!(UEINTX & (1<<RWAL))) {
55 debug_flush_timer = 0;
57 debug_flush_timer = 2;
64 // immediately transmit any buffered output.
65 void usb_debug_flush_output(void)
71 if (debug_flush_timer) {
72 UENUM = DEBUG_TX_ENDPOINT;
73 while ((UEINTX & (1<<RWAL))) {
77 debug_flush_timer = 0;