1 //*********************************************/
2 //
3 //  File:   debug.h
4 //
5 //  Author:  Domenico La Fauci
6 //
7 //********************************************/
8 
9 
10 #ifndef Debug_H
11 #define Debug_H
12 
13 #include <stdio.h>
14 #include <string.h>
15 
16 #define INFO_INIT_FLAG 	1
17 #define INFO_TCP_FLAG 	2
18 #define INFO_SPI_FLAG 	4
19 #define INFO_CM_FLAG 	8
20 #define INFO_UTIL_FLAG 16
21 
22 #define INFO_D (1<<0xD)     // Debug
23 #define INFO_E (1<<0xE)     // Error
24 #define INFO_WARN_FLAG (1<<0xF)     // Warning
25 #define DEFAULT_INFO_FLAG 0 //INFO_WARN_FLAG
26 
27 #ifdef _DEBUG_
28 #define DEFINE_DEBUG_VARIABLES()							\
29 uint16_t enableDebug = DEFAULT_INFO_FLAG | INFO_WARN_FLAG;	\
30 uint16_t verboseDebug = 0;									\
31 uint16_t dumpDebug = 0;										\
32 uint16_t pollDebug = 0;
33 #else
34 #define DEFINE_DEBUG_VARIABLES()							\
35 uint16_t enableDebug = DEFAULT_INFO_FLAG;					\
36 uint16_t verboseDebug = 0;									\
37 uint16_t dumpDebug = 0;										\
38 uint16_t pollDebug = 0;
39 #endif
40 
41 #define INIT_DEBUG_VARIABLES()	\
42 		enableDebug = DEFAULT_INFO_FLAG | INFO_WARN_FLAG;	\
43 		verboseDebug = 0;									\
44 		dumpDebug = 0; pollDebug = 0;
45 
46 
47 #define PRINT_DEBUG_VARIABLES()	\
48 		printk("Debug 	enabled: 0x%x\n", enableDebug);	\
49         printk("Verbose enabled: 0x%x\n", verboseDebug);	\
50         printk("Dump 	enabled: 0x%x\n", dumpDebug);	\
51         printk("POoll 	enabled: 0x%x\n", pollDebug);
52 
53 #define TURNON_DEBUG_VARIABLES()	\
54 		enableDebug = 0xff;
55 
56 extern uint16_t enableDebug;
57 extern uint16_t verboseDebug;
58 extern uint16_t dumpDebug;
59 extern uint16_t pollDebug;
60 
61 #define ENABLE_DEBUG_LEVEL 	1
62 #define VERBOSE_DEBUG_LEVEL 2
63 #define DUMP_DEBUG_LEVEL 	3
64 #define POLL_DEBUG_LEVEL 	4
65 
66 #define CHECK_DEBUG(VAR, LEVEL, LEVEL_LIMIT, FLAG)  		\
67 	do{										\
68 		if (LEVEL >= LEVEL_LIMIT) VAR |= FLAG;		\
69 		else VAR &= ~FLAG;			\
70 		}while(0);
71 
72 #define CHECK_ENA_DEBUG(LEVEL, FLAG)	\
73 		CHECK_DEBUG(enableDebug, LEVEL, ENABLE_DEBUG_LEVEL, FLAG)
74 #define CHECK_VERB_DEBUG(LEVEL, FLAG) 	\
75 		CHECK_DEBUG(verboseDebug, LEVEL, VERBOSE_DEBUG_LEVEL, FLAG)
76 #define CHECK_DUMP_DEBUG(LEVEL, FLAG) 	\
77 		CHECK_DEBUG(dumpDebug, LEVEL, DUMP_DEBUG_LEVEL, FLAG)
78 #define CHECK_POLL_DEBUG(LEVEL, FLAG) 	\
79 		CHECK_DEBUG(pollDebug, LEVEL, POLL_DEBUG_LEVEL, FLAG)
80 
81 
82 #define CHECK_DEBUG_LEVEL(LEVEL, INFO_FLAG)		\
83 		CHECK_ENA_DEBUG(LEVEL, INFO_FLAG) 		\
84 		CHECK_VERB_DEBUG(LEVEL, INFO_FLAG) 		\
85 		CHECK_DUMP_DEBUG(LEVEL, INFO_FLAG)		\
86 		CHECK_POLL_DEBUG(LEVEL, INFO_FLAG)
87 
88 #ifdef _INFO_DEBUG_
89 #define PRINT_DEBUG(msg, args...) do { 			\
90 	printk("[%s] " msg , __func__ , ##args );	\
91 } while (0)
92 
93 #define INFO_DEBUG(msg, args...) do { 			\
94 	printk("I-[%s] " msg , __func__ , ##args );	\
95 } while (0)
96 
97 #define WARN_DEBUG(msg, args...) do { 			\
98 	printk("W-[%s] " msg , __func__ , ##args );	\
99 } while (0)
100 
101 #else
102 do { }while(0);
103 #endif
104 
105 #define IF_DEBUG(X,Y) do { 			\
106 if (enableDebug & INFO_##X##_FLAG) \
107 Y;	\
108 } while (0)
109 
110 #define IF_DEBUG_VER(X,Y) do { 			\
111 if (verboseDebug & INFO_##X##_FLAG) \
112 Y;	\
113 } while (0)
114 
115 #define IF_DEBUG_DUMP(X,Y) do { 			\
116 if (dumpDebug & INFO_##X##_FLAG) \
117 Y;	\
118 } while (0)
119 
120 #define IF_DEBUG_POLL(X,Y) do { 			\
121 if (pollDebug & INFO_##X##_FLAG) {\
122 Y;	\
123 }} while (0)
124 
125 
126 
127 #define IF_WARN(Y) IF_DEBUG(WARN,Y)
128 #define IF_WARN_VER(Y) IF_DEBUG_VER(WARN,Y)
129 #define IF_TCP(Y) IF_DEBUG(TCP,Y)
130 #define IF_TCP_VER(Y) IF_DEBUG_VER(TCP,Y)
131 #define IF_TCP_POLL(Y) IF_DEBUG_POLL(TCP,Y)
132 #define IF_TCP_DUMP(Y) IF_DEBUG_DUMP(TCP,Y)
133 #define IF_SPI(Y) IF_DEBUG(SPI,Y)
134 #define IF_SPI_VER(Y) IF_DEBUG_VER(SPI,Y)
135 #define IF_SPI_DUMP(Y) IF_DEBUG_DUMP(SPI,Y)
136 #define IF_SPI_POLL(Y) IF_DEBUG_POLL(SPI,Y)
137 #define IF_UTIL(Y) IF_DEBUG(UTIL,Y)
138 #define IF_UTIL_VER(Y) IF_DEBUG_VER(UTIL,Y)
139 
140 #define WARN(msg, args...)			IF_DEBUG(WARN,WARN_DEBUG(msg, ##args))
141 #define WARN_VER(msg, args...)		IF_DEBUG_VER(WARN,WARN_DEBUG(msg, ##args))
142 #define WARN_POLL(msg, args...)		IF_DEBUG_POLL(WARN,WARN_DEBUG(msg, ##args))
143 #if 0	// disable to reduce the size of binary
144 #define INFO_INIT(msg, args...) 	IF_DEBUG(INIT,PRINT_DEBUG(msg, ##args))
145 #define INFO_INIT_VER(msg, args...) IF_DEBUG_VER(INIT,PRINT_DEBUG(msg, ##args))
146 #else
147 #define INFO_INIT(msg, args...)
148 #define INFO_INIT_VER(msg, args...)
149 #endif
150 #define INFO_TCP(msg, args...) 		IF_DEBUG(TCP,PRINT_DEBUG(msg, ##args))
151 #define INFO_TCP_VER(msg, args...) 	IF_DEBUG_VER(TCP,PRINT_DEBUG(msg, ##args))
152 #define INFO_TCP_DUMP(msg, args...) IF_DEBUG_DUMP(TCP,PRINT_DEBUG(msg, ##args))
153 #define INFO_TCP_POLL(msg, args...) IF_DEBUG_POLL(TCP,PRINT_DEBUG(msg, ##args))
154 #define INFO_SPI(msg, args...) 		IF_DEBUG(SPI,PRINT_DEBUG(msg, ##args))
155 #define INFO_SPI_VER(msg, args...) 	IF_DEBUG_VER(SPI,PRINT_DEBUG(msg, ##args))
156 #define INFO_SPI_DUMP(msg, args...) IF_DEBUG_DUMP(SPI,PRINT_DEBUG(msg, ##args))
157 #define INFO_SPI_POLL(msg, args...) IF_DEBUG_POLL(SPI,PRINT_DEBUG(msg, ##args))
158 #define INFO_UTIL(msg, args...) 	IF_DEBUG(UTIL,PRINT_DEBUG(msg, ##args))
159 #define INFO_UTIL_VER(msg, args...) IF_DEBUG_VER(UTIL,PRINT_DEBUG(msg, ##args))
160 #define CM_DPRINTF(msg, args...) 	IF_DEBUG(CM,PRINT_DEBUG(msg, ##args))
161 
162 extern void dump(char* _buf, uint16_t _count);
163 
164 #define _DUMP(BUF, COUNT) do {		\
165 	printk("[%s]: ", __func__);		\
166 	dump((char*)BUF, COUNT);				\
167 	} while (0)
168 
169 #ifdef _APP_DEBUG_
170 #define DUMP(BUF, COUNT) _DUMP(BUF, COUNT)
171 #else
172 #define DUMP(BUF, COUNT) do {} while (0)
173 #endif
174 #endif
175 
176 #define DUMP_TCP(BUF, COUNT) IF_TCP_DUMP(_DUMP(BUF, COUNT))
177 #define DUMP_SPI(BUF, COUNT) IF_SPI_DUMP(_DUMP(BUF, COUNT))
178 
179 #define DUMP_SPI_CMD(BUF) do {				\
180 	if (dumpDebug & INFO_SPI_FLAG) {		\
181 	int i = 0;								\
182 	for (; i < CMD_MAX_LEN; ++i) 			\
183 	{										\
184 		printk("0x%x ", BUF[i]);			\
185 		if (BUF[i] == END_CMD)				\
186 			break;							\
187 	}										\
188 	printk("\n");							\
189 	}										\
190 }while(0);
191 
192