Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

103 linhas
2.3KB

  1. #ifndef pauls_ugly_debug_included_yadda_yadda__
  2. #define pauls_ugly_debug_included_yadda_yadda__
  3. //#define SPEW_ALL_THIS_CRAP_TO_THE_SERIAL_PORT
  4. #ifdef SPEW_ALL_THIS_CRAP_TO_THE_SERIAL_PORT
  5. #define BAUD_RATE 115200UL
  6. #include <avr/pgmspace.h>
  7. #define print(s) print_P(PSTR(s))
  8. #define print_hex(s, n) print_P_hex(PSTR(s), (n))
  9. #define print_hex16(s, n) print_P_hex16(PSTR(s), (n))
  10. #define print_hex32(s, n) print_P_hex32(PSTR(s), (n))
  11. static void cout(char c) __attribute__((unused));
  12. static void cout(char c)
  13. {
  14. if (!(UCSR1B & (1<<TXEN1))) {
  15. UBRR1 = ((F_CPU / 8 + BAUD_RATE / 2) / BAUD_RATE - 1);
  16. UCSR1A = (1<<U2X1);
  17. UCSR1B = (1<<TXEN1);
  18. }
  19. while (!(UCSR1A & (1 << UDRE1))) ;
  20. UDR1 = c;
  21. }
  22. static void print_P(const char *str) __attribute__((unused));
  23. static void print_P(const char *str)
  24. {
  25. char c;
  26. while (1) {
  27. c = pgm_read_byte(str++);
  28. if (!c) return;
  29. if (c == '\n') {
  30. cout('\r');
  31. cout('\n');
  32. } else {
  33. cout(c);
  34. }
  35. }
  36. }
  37. static void phex1(uint8_t n) __attribute__((unused));
  38. static void phex1(uint8_t n)
  39. {
  40. if (n < 10) cout(n + '0');
  41. else cout(n - 10 + 'A');
  42. }
  43. static void phex(uint8_t n) __attribute__((unused));
  44. static void phex(uint8_t n)
  45. {
  46. phex1(n >> 4);
  47. phex1(n & 15);
  48. }
  49. static void phex16(uint16_t n) __attribute__((unused));
  50. static void phex16(uint16_t n)
  51. {
  52. phex(n >> 8);
  53. phex(n & 255);
  54. }
  55. static void phex32(uint32_t n) __attribute__((unused));
  56. static void phex32(uint32_t n)
  57. {
  58. phex(n >> 24);
  59. phex(n >> 16);
  60. phex(n >> 8);
  61. phex(n & 255);
  62. }
  63. static void print_P_hex(const char *s, uint8_t n) __attribute__((unused));
  64. static void print_P_hex(const char *s, uint8_t n)
  65. {
  66. print_P(s);
  67. phex(n);
  68. cout('\r');
  69. cout('\n');
  70. }
  71. static void print_P_hex16(const char *s, uint16_t n) __attribute__((unused));
  72. static void print_P_hex16(const char *s, uint16_t n)
  73. {
  74. print_P(s);
  75. phex16(n);
  76. cout('\r');
  77. cout('\n');
  78. }
  79. static void print_P_hex32(const char *s, uint32_t n) __attribute__((unused));
  80. static void print_P_hex32(const char *s, uint32_t n)
  81. {
  82. print_P(s);
  83. phex32(n);
  84. cout('\r');
  85. cout('\n');
  86. }
  87. #else // do not SPEW_ALL_THIS_CRAP_TO_THE_SERIAL_PORT
  88. #define cout(c)
  89. #define print(s)
  90. #define phex1(n)
  91. #define phex(n)
  92. #define phex16(n)
  93. #define phex32(n)
  94. #define print_hex(s, n)
  95. #define print_hex16(s, n)
  96. #define print_hex32(s, n)
  97. #endif
  98. #endif