tusb_debug.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2022, Ha Thach (tinyusb.org)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. * This file is part of the TinyUSB stack.
  25. */
  26. #ifndef _TUSB_DEBUG_H_
  27. #define _TUSB_DEBUG_H_
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. //--------------------------------------------------------------------+
  32. // Debug
  33. //--------------------------------------------------------------------+
  34. // CFG_TUSB_DEBUG for debugging
  35. // 0 : no debug
  36. // 1 : print error
  37. // 2 : print warning
  38. // 3 : print info
  39. #if CFG_TUSB_DEBUG
  40. // Enum to String for debugging purposes
  41. #if CFG_TUSB_DEBUG >= 2
  42. extern char const* const tu_str_speed[];
  43. extern char const* const tu_str_std_request[];
  44. #endif
  45. void tu_print_mem(void const *buf, uint32_t count, uint8_t indent);
  46. #ifdef CFG_TUSB_DEBUG_PRINTF
  47. extern int CFG_TUSB_DEBUG_PRINTF(const char *format, ...);
  48. #define tu_printf CFG_TUSB_DEBUG_PRINTF
  49. #else
  50. #define tu_printf printf
  51. #endif
  52. static inline void tu_print_arr(uint8_t const* buf, uint32_t bufsize)
  53. {
  54. for(uint32_t i=0; i<bufsize; i++) tu_printf("%02X ", buf[i]);
  55. }
  56. // Log with Level
  57. #define TU_LOG(n, ...) TU_XSTRCAT(TU_LOG, n)(__VA_ARGS__)
  58. #define TU_LOG_MEM(n, ...) TU_XSTRCAT3(TU_LOG, n, _MEM)(__VA_ARGS__)
  59. #define TU_LOG_ARR(n, ...) TU_XSTRCAT3(TU_LOG, n, _ARR)(__VA_ARGS__)
  60. #define TU_LOG_VAR(n, ...) TU_XSTRCAT3(TU_LOG, n, _VAR)(__VA_ARGS__)
  61. #define TU_LOG_INT(n, ...) TU_XSTRCAT3(TU_LOG, n, _INT)(__VA_ARGS__)
  62. #define TU_LOG_HEX(n, ...) TU_XSTRCAT3(TU_LOG, n, _HEX)(__VA_ARGS__)
  63. #define TU_LOG_LOCATION() tu_printf("%s: %d:\r\n", __PRETTY_FUNCTION__, __LINE__)
  64. #define TU_LOG_FAILED() tu_printf("%s: %d: Failed\r\n", __PRETTY_FUNCTION__, __LINE__)
  65. // Log Level 1: Error
  66. #define TU_LOG1 tu_printf
  67. #define TU_LOG1_MEM tu_print_mem
  68. #define TU_LOG1_ARR(_x, _n) tu_print_arr((uint8_t const*)(_x), _n)
  69. #define TU_LOG1_VAR(_x) tu_print_arr((uint8_t const*)(_x), sizeof(*(_x)))
  70. #define TU_LOG1_INT(_x) tu_printf(#_x " = %ld\r\n", (unsigned long) (_x) )
  71. #define TU_LOG1_HEX(_x) tu_printf(#_x " = %lX\r\n", (unsigned long) (_x) )
  72. // Log Level 2: Warn
  73. #if CFG_TUSB_DEBUG >= 2
  74. #define TU_LOG2 TU_LOG1
  75. #define TU_LOG2_MEM TU_LOG1_MEM
  76. #define TU_LOG2_ARR TU_LOG1_ARR
  77. #define TU_LOG2_VAR TU_LOG1_VAR
  78. #define TU_LOG2_INT TU_LOG1_INT
  79. #define TU_LOG2_HEX TU_LOG1_HEX
  80. #endif
  81. // Log Level 3: Info
  82. #if CFG_TUSB_DEBUG >= 3
  83. #define TU_LOG3 TU_LOG1
  84. #define TU_LOG3_MEM TU_LOG1_MEM
  85. #define TU_LOG3_ARR TU_LOG1_ARR
  86. #define TU_LOG3_VAR TU_LOG1_VAR
  87. #define TU_LOG3_INT TU_LOG1_INT
  88. #define TU_LOG3_HEX TU_LOG1_HEX
  89. #endif
  90. typedef struct
  91. {
  92. uint32_t key;
  93. const char* data;
  94. } tu_lookup_entry_t;
  95. typedef struct
  96. {
  97. uint16_t count;
  98. tu_lookup_entry_t const* items;
  99. } tu_lookup_table_t;
  100. static inline const char* tu_lookup_find(tu_lookup_table_t const* p_table, uint32_t key)
  101. {
  102. static char not_found[11];
  103. for(uint16_t i=0; i<p_table->count; i++)
  104. {
  105. if (p_table->items[i].key == key) return p_table->items[i].data;
  106. }
  107. // not found return the key value in hex
  108. snprintf(not_found, sizeof(not_found), "0x%08lX", (unsigned long) key);
  109. return not_found;
  110. }
  111. #endif // CFG_TUSB_DEBUG
  112. #ifndef TU_LOG
  113. #define TU_LOG(n, ...)
  114. #define TU_LOG_MEM(n, ...)
  115. #define TU_LOG_VAR(n, ...)
  116. #define TU_LOG_INT(n, ...)
  117. #define TU_LOG_HEX(n, ...)
  118. #define TU_LOG_LOCATION()
  119. #define TU_LOG_FAILED()
  120. #endif
  121. // TODO replace all TU_LOGn with TU_LOG(n)
  122. #define TU_LOG0(...)
  123. #define TU_LOG0_MEM(...)
  124. #define TU_LOG0_VAR(...)
  125. #define TU_LOG0_INT(...)
  126. #define TU_LOG0_HEX(...)
  127. #ifndef TU_LOG1
  128. #define TU_LOG1(...)
  129. #define TU_LOG1_MEM(...)
  130. #define TU_LOG1_VAR(...)
  131. #define TU_LOG1_INT(...)
  132. #define TU_LOG1_HEX(...)
  133. #endif
  134. #ifndef TU_LOG2
  135. #define TU_LOG2(...)
  136. #define TU_LOG2_MEM(...)
  137. #define TU_LOG2_VAR(...)
  138. #define TU_LOG2_INT(...)
  139. #define TU_LOG2_HEX(...)
  140. #endif
  141. #ifndef TU_LOG3
  142. #define TU_LOG3(...)
  143. #define TU_LOG3_MEM(...)
  144. #define TU_LOG3_VAR(...)
  145. #define TU_LOG3_INT(...)
  146. #define TU_LOG3_HEX(...)
  147. #endif
  148. #ifdef __cplusplus
  149. }
  150. #endif
  151. #endif /* _TUSB_DEBUG_H_ */