Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usb_debug.c 2.9KB

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