usb_debug.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #include <avr/interrupt.h>
  24. #include "sendchar.h"
  25. #include "usb_debug.h"
  26. // the time remaining before we transmit any partially full
  27. // packet, or send a zero length packet.
  28. volatile uint8_t debug_flush_timer = 0;
  29. int8_t sendchar(uint8_t c) {
  30. static uint8_t previous_timeout = 0;
  31. uint8_t timeout, intr_state;
  32. // if we're not online (enumerated and configured), error
  33. if (!usb_configured()) return -1;
  34. // interrupts are disabled so these functions can be
  35. // used from the main program or interrupt context,
  36. // even both in the same program!
  37. intr_state = SREG;
  38. cli();
  39. UENUM = DEBUG_TX_ENDPOINT;
  40. // if we gave up due to timeout before, don't wait again
  41. if (previous_timeout) {
  42. if (!(UEINTX & (1 << RWAL))) {
  43. SREG = intr_state;
  44. return -1;
  45. }
  46. previous_timeout = 0;
  47. }
  48. // wait for the FIFO to be ready to accept data
  49. timeout = UDFNUML + 4;
  50. while (1) {
  51. // are we ready to transmit?
  52. if (UEINTX & (1 << RWAL)) break;
  53. SREG = intr_state;
  54. // have we waited too long?
  55. if (UDFNUML == timeout) {
  56. previous_timeout = 1;
  57. return -1;
  58. }
  59. // has the USB gone offline?
  60. if (!usb_configured()) return -1;
  61. // get ready to try checking again
  62. intr_state = SREG;
  63. cli();
  64. UENUM = DEBUG_TX_ENDPOINT;
  65. }
  66. // actually write the byte into the FIFO
  67. UEDATX = c;
  68. // if this completed a packet, transmit it now!
  69. if (!(UEINTX & (1 << RWAL))) {
  70. UEINTX = 0x3A;
  71. debug_flush_timer = 0;
  72. } else {
  73. debug_flush_timer = 2;
  74. }
  75. SREG = intr_state;
  76. return 0;
  77. }
  78. // immediately transmit any buffered output.
  79. void usb_debug_flush_output(void) {
  80. uint8_t intr_state;
  81. intr_state = SREG;
  82. cli();
  83. if (debug_flush_timer) {
  84. UENUM = DEBUG_TX_ENDPOINT;
  85. while ((UEINTX & (1 << RWAL))) {
  86. UEDATX = 0;
  87. }
  88. UEINTX = 0x3A;
  89. debug_flush_timer = 0;
  90. }
  91. SREG = intr_state;
  92. }