mikroSDK Reference Manual
tusb_debug.h
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
27#ifndef _TUSB_DEBUG_H_
28#define _TUSB_DEBUG_H_
29
30#ifdef __cplusplus
31 extern "C" {
32#endif
33
34//--------------------------------------------------------------------+
35// Debug
36//--------------------------------------------------------------------+
37
38// CFG_TUSB_DEBUG for debugging
39// 0 : no debug
40// 1 : print error
41// 2 : print warning
42// 3 : print info
43#if CFG_TUSB_DEBUG
44
45// Enum to String for debugging purposes
46#if CFG_TUSB_DEBUG >= 2
47extern char const* const tu_str_speed[];
48extern char const* const tu_str_std_request[];
49extern char const* const tu_str_xfer_result[];
50#endif
51
52void tu_print_mem(void const *buf, uint32_t count, uint8_t indent);
53
54#ifdef CFG_TUSB_DEBUG_PRINTF
55 extern int CFG_TUSB_DEBUG_PRINTF(const char *format, ...);
56 #define tu_printf CFG_TUSB_DEBUG_PRINTF
57#else
58 #define tu_printf printf
59#endif
60
61static inline void tu_print_arr(uint8_t const* buf, uint32_t bufsize)
62{
63 for(uint32_t i=0; i<bufsize; i++) tu_printf("%02X ", buf[i]);
64}
65
66// Log with Level
67#define TU_LOG(n, ...) TU_XSTRCAT(TU_LOG, n)(__VA_ARGS__)
68#define TU_LOG_MEM(n, ...) TU_XSTRCAT3(TU_LOG, n, _MEM)(__VA_ARGS__)
69#define TU_LOG_ARR(n, ...) TU_XSTRCAT3(TU_LOG, n, _ARR)(__VA_ARGS__)
70#define TU_LOG_PTR(n, ...) TU_XSTRCAT3(TU_LOG, n, _PTR)(__VA_ARGS__)
71#define TU_LOG_INT(n, ...) TU_XSTRCAT3(TU_LOG, n, _INT)(__VA_ARGS__)
72#define TU_LOG_HEX(n, ...) TU_XSTRCAT3(TU_LOG, n, _HEX)(__VA_ARGS__)
73#define TU_LOG_LOCATION() tu_printf("%s: %d:\r\n", __PRETTY_FUNCTION__, __LINE__)
74#define TU_LOG_FAILED() tu_printf("%s: %d: Failed\r\n", __PRETTY_FUNCTION__, __LINE__)
75
76// Log Level 1: Error
77#define TU_LOG1 tu_printf
78#define TU_LOG1_MEM tu_print_mem
79#define TU_LOG1_ARR(_x, _n) tu_print_arr((uint8_t const*)(_x), _n)
80#define TU_LOG1_PTR(_x) tu_print_arr((uint8_t const*)(_x), sizeof(*(_x)))
81#define TU_LOG1_INT(_x) tu_printf(#_x " = %ld\r\n", (unsigned long) (_x) )
82#define TU_LOG1_HEX(_x) tu_printf(#_x " = %lX\r\n", (unsigned long) (_x) )
83
84// Log Level 2: Warn
85#if CFG_TUSB_DEBUG >= 2
86 #define TU_LOG2 TU_LOG1
87 #define TU_LOG2_MEM TU_LOG1_MEM
88 #define TU_LOG2_ARR TU_LOG1_ARR
89 #define TU_LOG2_PTR TU_LOG1_PTR
90 #define TU_LOG2_INT TU_LOG1_INT
91 #define TU_LOG2_HEX TU_LOG1_HEX
92#endif
93
94// Log Level 3: Info
95#if CFG_TUSB_DEBUG >= 3
96 #define TU_LOG3 TU_LOG1
97 #define TU_LOG3_MEM TU_LOG1_MEM
98 #define TU_LOG3_ARR TU_LOG1_ARR
99 #define TU_LOG3_PTR TU_LOG1_PTR
100 #define TU_LOG3_INT TU_LOG1_INT
101 #define TU_LOG3_HEX TU_LOG1_HEX
102#endif
103
104typedef struct
105{
106 uint32_t key;
107 const char* data;
108} tu_lookup_entry_t;
109
110typedef struct
111{
112 uint16_t count;
113 tu_lookup_entry_t const* items;
114} tu_lookup_table_t;
115
116static inline const char* tu_lookup_find(tu_lookup_table_t const* p_table, uint32_t key)
117{
118 tu_static char not_found[11];
119
120 for(uint16_t i=0; i<p_table->count; i++)
121 {
122 if (p_table->items[i].key == key) return p_table->items[i].data;
123 }
124
125 // not found return the key value in hex
126 snprintf(not_found, sizeof(not_found), "0x%08lX", (unsigned long) key);
127
128 return not_found;
129}
130
131#endif // CFG_TUSB_DEBUG
132
133#ifndef TU_LOG
134 #define TU_LOG(n, ...)
135 #define TU_LOG_MEM(n, ...)
136 #define TU_LOG_PTR(n, ...)
137 #define TU_LOG_INT(n, ...)
138 #define TU_LOG_HEX(n, ...)
139 #define TU_LOG_LOCATION()
140 #define TU_LOG_FAILED()
141#endif
142
143// TODO replace all TU_LOGn with TU_LOG(n)
144
145#define TU_LOG0(...)
146#define TU_LOG0_MEM(...)
147#define TU_LOG0_PTR(...)
148#define TU_LOG0_INT(...)
149#define TU_LOG0_HEX(...)
150
151#ifndef TU_LOG1
152 #define TU_LOG1(...)
153 #define TU_LOG1_MEM(...)
154 #define TU_LOG1_PTR(...)
155 #define TU_LOG1_INT(...)
156 #define TU_LOG1_HEX(...)
157#endif
158
159#ifndef TU_LOG2
160 #define TU_LOG2(...)
161 #define TU_LOG2_MEM(...)
162 #define TU_LOG2_PTR(...)
163 #define TU_LOG2_INT(...)
164 #define TU_LOG2_HEX(...)
165#endif
166
167#ifndef TU_LOG3
168 #define TU_LOG3(...)
169 #define TU_LOG3_MEM(...)
170 #define TU_LOG3_PTR(...)
171 #define TU_LOG3_INT(...)
172 #define TU_LOG3_HEX(...)
173#endif
174
175#ifdef __cplusplus
176 }
177#endif
178
179#endif /* _TUSB_DEBUG_H_ */