]> git.gir.st - tmk_keyboard.git/blob - protocol/pjrc/usb_debug.c
Change TOP_DIR to TMK_DIR in makefiles
[tmk_keyboard.git] / protocol / pjrc / usb_debug.c
1 /* USB Keyboard Plus Debug Channel Example for Teensy USB Development Board
2 * http://www.pjrc.com/teensy/usb_keyboard.html
3 * Copyright (c) 2009 PJRC.COM, LLC
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24 #include <avr/interrupt.h>
25 #include "sendchar.h"
26 #include "usb_debug.h"
27
28
29 // the time remaining before we transmit any partially full
30 // packet, or send a zero length packet.
31 volatile uint8_t debug_flush_timer=0;
32
33
34 int8_t sendchar(uint8_t c)
35 {
36 static uint8_t previous_timeout=0;
37 uint8_t timeout, intr_state;
38
39 // if we're not online (enumerated and configured), error
40 if (!usb_configured()) return -1;
41 // interrupts are disabled so these functions can be
42 // used from the main program or interrupt context,
43 // even both in the same program!
44 intr_state = SREG;
45 cli();
46 UENUM = DEBUG_TX_ENDPOINT;
47 // if we gave up due to timeout before, don't wait again
48 if (previous_timeout) {
49 if (!(UEINTX & (1<<RWAL))) {
50 SREG = intr_state;
51 return -1;
52 }
53 previous_timeout = 0;
54 }
55 // wait for the FIFO to be ready to accept data
56 timeout = UDFNUML + 4;
57 while (1) {
58 // are we ready to transmit?
59 if (UEINTX & (1<<RWAL)) break;
60 SREG = intr_state;
61 // have we waited too long?
62 if (UDFNUML == timeout) {
63 previous_timeout = 1;
64 return -1;
65 }
66 // has the USB gone offline?
67 if (!usb_configured()) return -1;
68 // get ready to try checking again
69 intr_state = SREG;
70 cli();
71 UENUM = DEBUG_TX_ENDPOINT;
72 }
73 // actually write the byte into the FIFO
74 UEDATX = c;
75 // if this completed a packet, transmit it now!
76 if (!(UEINTX & (1<<RWAL))) {
77 UEINTX = 0x3A;
78 debug_flush_timer = 0;
79 } else {
80 debug_flush_timer = 2;
81 }
82 SREG = intr_state;
83 return 0;
84 }
85
86 // immediately transmit any buffered output.
87 void usb_debug_flush_output(void)
88 {
89 uint8_t intr_state;
90
91 intr_state = SREG;
92 cli();
93 if (debug_flush_timer) {
94 UENUM = DEBUG_TX_ENDPOINT;
95 while ((UEINTX & (1<<RWAL))) {
96 UEDATX = 0;
97 }
98 UEINTX = 0x3A;
99 debug_flush_timer = 0;
100 }
101 SREG = intr_state;
102 }
Imprint / Impressum