VivicoreSerial library
VivicoreSerialDebug.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2021 VIVIWARE JAPAN, Inc. All right reserved.
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License
6  as published by the Free Software Foundation; either version 2
7  of the License, or (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
24 #ifndef VIVICORESERIAL_DEBUG_H
25 #define VIVICORESERIAL_DEBUG_H
26 
27 //#define DBG_GPIO_ENABLED
28 //#define DBG_SERIAL_ENABLED
29 //#define DISABLE_VIVICORE
30 
31 #if defined(DBG_SERIAL_ENABLED)
32 # define SERIAL_DEBUG0_ENABLED
33 //#define SERIAL_DEBUG1_ENABLED
34 //#define SERIAL_DEBUG2_ENABLED
35 //#define SERIAL_DEBUG_CIBO_ENABLED
36 //#define SERIAL_DEBUG_COBI_ENABLED
37 #endif
38 
39 // GPIO Debug Macro definition
40 #if defined(DBG_GPIO_ENABLED)
41 # define DebugGPIODirectOut(DBG_GPIO_DDR, DBG_GPIO_BIT) DBG_GPIO_DDR = DBG_GPIO_DDR | (1 << DBG_GPIO_BIT)
42 # define DebugGPIOLow(DBG_GPIO_PORT, DBG_GPIO_BIT) DBG_GPIO_PORT = DBG_GPIO_PORT & ~(1 << DBG_GPIO_BIT)
43 # define DebugGPIOHigh(DBG_GPIO_PORT, DBG_GPIO_BIT) DBG_GPIO_PORT = DBG_GPIO_PORT | (1 << DBG_GPIO_BIT)
44 # define DebugGPIOWrite(DBG_GPIO_PORT, DBG_GPIO_BIT, BIT_VALUE) \
45  (BIT_VALUE ? DebugGPIOHigh(DBG_GPIO_PORT, DBG_GPIO_BIT) : DebugGPIOLow(DBG_GPIO_PORT, DBG_GPIO_BIT))
46 #else
47 # define DebugGPIODirectOut(DBG_GPIO_DDR, DBG_GPIO_BIT)
48 # define DebugGPIOLow(DBG_GPIO_PORT, DBG_GPIO_BIT)
49 # define DebugGPIOHigh(DBG_GPIO_PORT, DBG_GPIO_BIT)
50 # define DebugGPIOWrite(DBG_GPIO_PORT, DBG_GPIO_BIT, BIT_VALUE)
51 #endif
52 
53 // Debug Macro definition
54 
55 // Debug Serial Instance definition
56 #define HW_SERIAL_BAUD (250000)
57 #if (CORE_COMM_UART_PORT == 0)
58 # define DBG_SERIAL_INSTANCE Serial1
59 #elif (CORE_COMM_UART_PORT == 1)
60 # define DBG_SERIAL_INSTANCE Serial
61 #else
62 # error Not supported!
63 #endif
64 
65 // begin definition
66 #if defined(DBG_SERIAL_ENABLED)
67 # define DebugBegin() DBG_SERIAL_INSTANCE.begin(HW_SERIAL_BAUD)
68 #else
69 # define DebugBegin()
70 #endif
71 
72 // print definitions
73 #if defined(DBG_SERIAL_ENABLED)
74 # define DebugFlush(...) DBG_SERIAL_INSTANCE.flush()
75 # define DebugStackPointerPrint(...) \
76  DBG_SERIAL_INSTANCE.print("SP:"); \
77  DBG_SERIAL_INSTANCE.print((int)SP, HEX); \
78  DBG_SERIAL_INSTANCE.println()
79 #else
80 # define DebugFlush(...)
81 # define DebugStackPointerPrint(...)
82 #endif
83 #define DebugPrint(...) \
84  DBG_SERIAL_INSTANCE.print('['); \
85  DBG_SERIAL_INSTANCE.print(millis()); \
86  DBG_SERIAL_INSTANCE.print("] "); \
87  DBG_SERIAL_INSTANCE.print(__PRETTY_FUNCTION__); \
88  DBG_SERIAL_INSTANCE.print(' '); \
89  DBG_SERIAL_INSTANCE.print(__LINE__); \
90  DBG_SERIAL_INSTANCE.print(": "); \
91  DBG_SERIAL_INSTANCE.print(__VA_ARGS__)
92 #define DebugPrintln(...) \
93  DBG_SERIAL_INSTANCE.print('['); \
94  DBG_SERIAL_INSTANCE.print(millis()); \
95  DBG_SERIAL_INSTANCE.print("] "); \
96  DBG_SERIAL_INSTANCE.print(__PRETTY_FUNCTION__); \
97  DBG_SERIAL_INSTANCE.print(' '); \
98  DBG_SERIAL_INSTANCE.print(__LINE__); \
99  DBG_SERIAL_INSTANCE.print(": "); \
100  DBG_SERIAL_INSTANCE.println(__VA_ARGS__)
101 #define DebugPlainPrint(...) DBG_SERIAL_INSTANCE.print(__VA_ARGS__)
102 #define DebugPlainPrintln(...) DBG_SERIAL_INSTANCE.println(__VA_ARGS__)
103 
104 #define Debug1ByteHexPrint(v) \
105  if ((((v) >> 4) & 0x0F) > 9) { \
106  DBG_SERIAL_INSTANCE.print((char)('A' + ((uint8_t)(((v) >> 4) & 0x0F) - 10))); \
107  } else { \
108  DBG_SERIAL_INSTANCE.print((uint8_t)(((v) >> 4) & 0x0F)); \
109  } \
110  if (((v)&0x0F) > 9) { \
111  DBG_SERIAL_INSTANCE.print((char)('A' + ((uint8_t)((v)&0x0F) - 10))); \
112  } else { \
113  DBG_SERIAL_INSTANCE.print((uint8_t)((v)&0x0F)); \
114  }
115 
116 #define DebugHexPrint(v) Debug1ByteHexPrint(v)
117 
118 #define DebugBinPrint(v) \
119  { \
120  for (uint32_t _bit = 1UL << ((sizeof(v) * 8) - 1); _bit; _bit >>= 1) { \
121  DBG_SERIAL_INSTANCE.print(v &_bit ? '1' : '0'); \
122  } \
123  }
124 
125 #if defined(SERIAL_DEBUG0_ENABLED)
126 # define DebugPrint0(...) DebugPrint(__VA_ARGS__)
127 # define DebugPrintln0(...) DebugPrintln(__VA_ARGS__)
128 # define DebugPlainPrint0(...) DebugPlainPrint(__VA_ARGS__)
129 # define DebugPlainPrintln0(...) DebugPlainPrintln(__VA_ARGS__)
130 # define DebugHexPrint0(v) DebugHexPrint(v)
131 # define DebugBinPrint0(v) DebugBinPrint(v)
132 # define DebugStringPrint0(str) DebugPlainPrint(F(str))
133 # define DebugStringPrintln0(str) DebugPlainPrintln(F(str))
134 #else
135 # define DebugPrint0(...)
136 # define DebugPrintln0(...)
137 # define DebugPlainPrint0(...)
138 # define DebugPlainPrintln0(...)
139 # define DebugHexPrint0(v)
140 # define DebugBinPrint0(v)
141 # define DebugStringPrint0(str)
142 # define DebugStringPrintln0(str)
143 #endif
144 
145 #if defined(SERIAL_DEBUG1_ENABLED)
146 # define DebugPrint1(...) DebugPrint(__VA_ARGS__)
147 # define DebugPrintln1(...) DebugPrintln(__VA_ARGS__)
148 # define DebugPlainPrint1(...) DebugPlainPrint(__VA_ARGS__)
149 # define DebugPlainPrintln1(...) DebugPlainPrintln(__VA_ARGS__)
150 # define DebugHexPrint1(v) DebugHexPrint(v)
151 # define DebugBinPrint1(v) DebugBinPrint(v)
152 # define DebugStringPrint1(str) DebugPlainPrint(F(str))
153 # define DebugStringPrintln1(str) DebugPlainPrintln(F(str))
154 #else
155 # define DebugPrint1(...)
156 # define DebugPrintln1(...)
157 # define DebugPlainPrint1(...)
158 # define DebugPlainPrintln1(...)
159 # define DebugHexPrint1(v)
160 # define DebugBinPrint1(v)
161 # define DebugStringPrint1(str)
162 # define DebugStringPrintln1(str)
163 #endif
164 
165 #if defined(SERIAL_DEBUG2_ENABLED)
166 # define DebugPrint2(...) DebugPrint(__VA_ARGS__)
167 # define DebugPrintln2(...) DebugPrintln(__VA_ARGS__)
168 # define DebugPlainPrint2(...) DebugPlainPrint(__VA_ARGS__)
169 # define DebugPlainPrintln2(...) DebugPlainPrintln(__VA_ARGS__)
170 # define DebugHexPrint2(v) DebugHexPrint(v)
171 # define DebugBinPrint2(v) DebugBinPrint(v)
172 # define DebugStringPrint2(str) DebugPlainPrint(F(str))
173 # define DebugStringPrintln2(str) DebugPlainPrintln(F(str))
174 #else
175 # define DebugPrint2(...)
176 # define DebugPrintln2(...)
177 # define DebugPlainPrint2(...)
178 # define DebugPlainPrintln2(...)
179 # define DebugHexPrint2(v)
180 # define DebugBinPrint2(v)
181 # define DebugStringPrint2(str)
182 # define DebugStringPrintln2(str)
183 #endif
184 
185 #if defined(SERIAL_DEBUG_CIBO_ENABLED)
186 # define DebugPrintCIBO(str) DebugPlainPrint(F(str))
187 # define DebugPrintCIBOln(str) DebugPlainPrintln(F(str))
188 # define DebugHexPrintCIBO(v) DebugHexPrint(v)
189 #else
190 # define DebugPrintCIBO(str)
191 # define DebugPrintCIBOln(str)
192 # define DebugHexPrintCIBO(v)
193 #endif
194 
195 #if defined(SERIAL_DEBUG_COBI_ENABLED)
196 # define DebugPrintCOBI(str) DebugPlainPrint(F(str))
197 # define DebugPrintCOBIln(str) DebugPlainPrintln(F(str))
198 # define DebugHexPrintCOBI(v) DebugHexPrint(v)
199 #else
200 # define DebugPrintCOBI(str)
201 # define DebugPrintCOBIln(str)
202 # define DebugHexPrintCOBI(v)
203 #endif
204 
205 #endif