Open FFBoard
Open source force feedback firmware
tusb_debug.h
Go to the documentation of this file.
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 >= CFG_TUH_LOG_LEVEL || CFG_TUSB_DEBUG >= CFG_TUD_LOG_LEVEL
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_buf(uint8_t const* buf, uint32_t bufsize) {
62 for(uint32_t i=0; i<bufsize; i++) tu_printf("%02X ", buf[i]);
63 tu_printf("\r\n");
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_BUF(n, ...) TU_XSTRCAT3(TU_LOG, n, _BUF)(__VA_ARGS__)
70#define TU_LOG_INT(n, ...) TU_XSTRCAT3(TU_LOG, n, _INT)(__VA_ARGS__)
71#define TU_LOG_HEX(n, ...) TU_XSTRCAT3(TU_LOG, n, _HEX)(__VA_ARGS__)
72#define TU_LOG_LOCATION() tu_printf("%s: %d:\r\n", __PRETTY_FUNCTION__, __LINE__)
73#define TU_LOG_FAILED() tu_printf("%s: %d: Failed\r\n", __PRETTY_FUNCTION__, __LINE__)
74
75// Log Level 1: Error
76#define TU_LOG1 tu_printf
77#define TU_LOG1_MEM tu_print_mem
78#define TU_LOG1_BUF(_x, _n) tu_print_buf((uint8_t const*)(_x), _n)
79#define TU_LOG1_INT(_x) tu_printf(#_x " = %ld\r\n", (unsigned long) (_x) )
80#define TU_LOG1_HEX(_x) tu_printf(#_x " = 0x%lX\r\n", (unsigned long) (_x) )
81
82// Log Level 2: Warn
83#if CFG_TUSB_DEBUG >= 2
84 #define TU_LOG2 TU_LOG1
85 #define TU_LOG2_MEM TU_LOG1_MEM
86 #define TU_LOG2_BUF TU_LOG1_BUF
87 #define TU_LOG2_INT TU_LOG1_INT
88 #define TU_LOG2_HEX TU_LOG1_HEX
89#endif
90
91// Log Level 3: Info
92#if CFG_TUSB_DEBUG >= 3
93 #define TU_LOG3 TU_LOG1
94 #define TU_LOG3_MEM TU_LOG1_MEM
95 #define TU_LOG3_BUF TU_LOG1_BUF
96 #define TU_LOG3_INT TU_LOG1_INT
97 #define TU_LOG3_HEX TU_LOG1_HEX
98#endif
99
100typedef struct {
101 uint32_t key;
102 const char* data;
104
105typedef struct {
106 uint16_t count;
109
110static inline const char* tu_lookup_find(tu_lookup_table_t const* p_table, uint32_t key) {
111 tu_static char not_found[11];
112
113 for(uint16_t i=0; i<p_table->count; i++) {
114 if (p_table->items[i].key == key) return p_table->items[i].data;
115 }
116
117 // not found return the key value in hex
118 snprintf(not_found, sizeof(not_found), "0x%08lX", (unsigned long) key);
119
120 return not_found;
121}
122
123#endif // CFG_TUSB_DEBUG
124
125#ifndef TU_LOG
126 #define TU_LOG(n, ...)
127 #define TU_LOG_MEM(n, ...)
128 #define TU_LOG_BUF(n, ...)
129 #define TU_LOG_INT(n, ...)
130 #define TU_LOG_HEX(n, ...)
131 #define TU_LOG_LOCATION()
132 #define TU_LOG_FAILED()
133#endif
134
135// TODO replace all TU_LOGn with TU_LOG(n)
136
137#define TU_LOG0(...)
138#define TU_LOG0_MEM(...)
139#define TU_LOG0_BUF(...)
140#define TU_LOG0_INT(...)
141#define TU_LOG0_HEX(...)
142
143#ifndef TU_LOG1
144 #define TU_LOG1(...)
145 #define TU_LOG1_MEM(...)
146 #define TU_LOG1_BUF(...)
147 #define TU_LOG1_INT(...)
148 #define TU_LOG1_HEX(...)
149#endif
150
151#ifndef TU_LOG2
152 #define TU_LOG2(...)
153 #define TU_LOG2_MEM(...)
154 #define TU_LOG2_BUF(...)
155 #define TU_LOG2_INT(...)
156 #define TU_LOG2_HEX(...)
157#endif
158
159#ifndef TU_LOG3
160 #define TU_LOG3(...)
161 #define TU_LOG3_MEM(...)
162 #define TU_LOG3_BUF(...)
163 #define TU_LOG3_INT(...)
164 #define TU_LOG3_HEX(...)
165#endif
166
167#ifdef __cplusplus
168 }
169#endif
170
171#endif /* _TUSB_DEBUG_H_ */
uint32_t bufsize
Definition: midi_device.h:95
Definition: tusb_debug.h:100
uint32_t key
Definition: tusb_debug.h:101
const char * data
Definition: tusb_debug.h:102
tu_lookup_entry_t const * items
Definition: tusb_debug.h:107
char const *const tu_str_speed[]
Definition: tusb.c:475
static void tu_print_buf(uint8_t const *buf, uint32_t bufsize)
Definition: tusb_debug.h:61
char const *const tu_str_std_request[]
Definition: tusb.c:476
void tu_print_mem(void const *buf, uint32_t count, uint8_t indent)
Definition: tusb.c:512
static const char * tu_lookup_find(tu_lookup_table_t const *p_table, uint32_t key)
Definition: tusb_debug.h:110
char const *const tu_str_xfer_result[]
Definition: tusb.c:492
int CFG_TUSB_DEBUG_PRINTF(const char *format,...)