summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/atsam/libraries/libchip/include/trace.h
blob: 95a613f69aaa937779eb5cb21e812f4cf77caa77 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* ---------------------------------------------------------------------------- */
/*                  Atmel Microcontroller Software Support                      */
/*                       SAM Software Package License                           */
/* ---------------------------------------------------------------------------- */
/* Copyright (c) 2015, Atmel Corporation                                        */
/*                                                                              */
/* All rights reserved.                                                         */
/*                                                                              */
/* Redistribution and use in source and binary forms, with or without           */
/* modification, are permitted provided that the following condition is met:    */
/*                                                                              */
/* - Redistributions of source code must retain the above copyright notice,     */
/* this list of conditions and the disclaimer below.                            */
/*                                                                              */
/* Atmel's name may not be used to endorse or promote products derived from     */
/* this software without specific prior written permission.                     */
/*                                                                              */
/* DISCLAIMER:  THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR   */
/* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE   */
/* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,      */
/* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
/* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,  */
/* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    */
/* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING         */
/* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
/* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                           */
/* ---------------------------------------------------------------------------- */

/**
 *  \file
 *
 *  \par Purpose
 *
 *  Standard output methods for reporting debug information, warnings and
 *  errors, which can be easily be turned on/off.
 *
 *  \par Usage
 *  -# Initialize the DBGU using TRACE_CONFIGURE() if you intend to eventually
 *     disable ALL traces; otherwise use DBGU_Configure().
 *  -# Uses the TRACE_DEBUG(), TRACE_INFO(), TRACE_WARNING(), TRACE_ERROR()
 *     TRACE_FATAL() macros to output traces throughout the program.
 *  -# Each type of trace has a level : Debug 5, Info 4, Warning 3, Error 2
 *     and Fatal 1. Disable a group of traces by changing the value of
 *     TRACE_LEVEL during compilation; traces with a level bigger than TRACE_LEVEL
 *     are not generated. To generate no trace, use the reserved value 0.
 *  -# Trace disabling can be static or dynamic. If dynamic disabling is selected
 *     the trace level can be modified in runtime. If static disabling is selected
 *     the disabled traces are not compiled.
 *
 *  \par traceLevels Trace level description
 *  -# TRACE_DEBUG (5): Traces whose only purpose is for debugging the program,
 *     and which do not produce meaningful information otherwise.
 *  -# TRACE_INFO (4): Informational trace about the program execution. Should
 *     enable the user to see the execution flow.
 *  -# TRACE_WARNING (3): Indicates that a minor error has happened. In most case
 *     it can be discarded safely; it may even be expected.
 *  -# TRACE_ERROR (2): Indicates an error which may not stop the program execution,
 *     but which indicates there is a problem with the code.
 *  -# TRACE_FATAL (1): Indicates a major error which prevents the program from going
 *     any further.
 */

#ifndef _TRACE_
#define _TRACE_

/*
 *         Headers
 */

#include "pio.h"

#include <stdio.h>

/*
 *         Global Definitions
 */

/**  Softpack Version */
#define SOFTPACK_VERSION       "1.5"


#define TRACE_LEVEL_DEBUG      5
#define TRACE_LEVEL_INFO       4
#define TRACE_LEVEL_WARNING    3
#define TRACE_LEVEL_ERROR      2
#define TRACE_LEVEL_FATAL      1
#define TRACE_LEVEL_NO_TRACE   0

/* By default, all traces are output except the debug one. */
#if !defined(TRACE_LEVEL)
	#define TRACE_LEVEL TRACE_LEVEL_INFO
#endif

/* By default, trace level is static (not dynamic) */
#if !defined(DYN_TRACES)
	#define DYN_TRACES 0
#endif

#if defined(NOTRACE)
	#error "Error: NOTRACE has to be not defined !"
#endif

#undef NOTRACE
#if (DYN_TRACES==0)
	#if (TRACE_LEVEL == TRACE_LEVEL_NO_TRACE)
		#define NOTRACE
	#endif
#endif



/* ------------------------------------------------------------------------------
 *         Global Macros
 * ------------------------------------------------------------------------------
 */

extern void TRACE_CONFIGURE(uint32_t dwBaudRate, uint32_t dwMCk);

/**
 *  Initializes the DBGU for ISP project
 *
 *  \param mode  DBGU mode.
 *  \param baudrate  DBGU baudrate.
 *  \param mck  Master clock frequency.
 */
#ifndef DYNTRACE
	#define DYNTRACE 0
#endif

#if (TRACE_LEVEL==0) && (DYNTRACE==0)
#define TRACE_CONFIGURE_ISP(mode, baudrate, mck) {}
#else
#define TRACE_CONFIGURE_ISP(mode, baudrate, mck) { \
		const Pin pinsUART0[] = {PINS_UART}; \
		PIO_Configure(pinsUART0, PIO_LISTSIZE(pinsUART0)); \
		UART_Configure(baudrate, mck); \
	}
#endif

/**
 *  Outputs a formatted string using 'printf' if the log level is high
 *  enough. Can be disabled by defining TRACE_LEVEL=0 during compilation.
 *  \param ...  Additional parameters depending on formatted string.
 */
#if defined(NOTRACE)

	/* Empty macro */
	#define TRACE_DEBUG(...)      { }
	#define TRACE_INFO(...)       { }
	#define TRACE_WARNING(...)    { }
	#define TRACE_ERROR(...)      { }
	#define TRACE_FATAL(...)      { while (1); }

	#define TRACE_DEBUG_WP(...)   { }
	#define TRACE_INFO_WP(...)    { }
	#define TRACE_WARNING_WP(...) { }
	#define TRACE_ERROR_WP(...)   { }
	#define TRACE_FATAL_WP(...)   { while (1); }

#elif (DYN_TRACES == 1)

	/* Trace output depends on dwTraceLevel value */
	#define TRACE_DEBUG(...)      { if (dwTraceLevel >= TRACE_LEVEL_DEBUG)   { printf("-D- " __VA_ARGS__); } }
	#define TRACE_INFO(...)       { if (dwTraceLevel >= TRACE_LEVEL_INFO)    { printf("-I- " __VA_ARGS__); } }
	#define TRACE_WARNING(...)    { if (dwTraceLevel >= TRACE_LEVEL_WARNING) { printf("-W- " __VA_ARGS__); } }
	#define TRACE_ERROR(...)      { if (dwTraceLevel >= TRACE_LEVEL_ERROR)   { printf("-E- " __VA_ARGS__); } }
	#define TRACE_FATAL(...)      { if (dwTraceLevel >= TRACE_LEVEL_FATAL)   { printf("-F- " __VA_ARGS__); while (1); } }

	#define TRACE_DEBUG_WP(...)   { if (dwTraceLevel >= TRACE_LEVEL_DEBUG)   { printf(__VA_ARGS__); } }
	#define TRACE_INFO_WP(...)    { if (dwTraceLevel >= TRACE_LEVEL_INFO)    { printf(__VA_ARGS__); } }
	#define TRACE_WARNING_WP(...) { if (dwTraceLevel >= TRACE_LEVEL_WARNING) { printf(__VA_ARGS__); } }
	#define TRACE_ERROR_WP(...)   { if (dwTraceLevel >= TRACE_LEVEL_ERROR)   { printf(__VA_ARGS__); } }
	#define TRACE_FATAL_WP(...)   { if (dwTraceLevel >= TRACE_LEVEL_FATAL)   { printf(__VA_ARGS__); while (1); } }

#else

	/* Trace compilation depends on TRACE_LEVEL value */
	#if (TRACE_LEVEL >= TRACE_LEVEL_DEBUG)
		#define TRACE_DEBUG(...)      { printf("-D- " __VA_ARGS__); }
		#define TRACE_DEBUG_WP(...)   { printf(__VA_ARGS__); }
	#else
		#define TRACE_DEBUG(...)      { }
		#define TRACE_DEBUG_WP(...)   { }
	#endif

	#if (TRACE_LEVEL >= TRACE_LEVEL_INFO)
		#define TRACE_INFO(...)       { printf("-I- " __VA_ARGS__); }
		#define TRACE_INFO_WP(...)    { printf(__VA_ARGS__); }
	#else
		#define TRACE_INFO(...)       { }
		#define TRACE_INFO_WP(...)    { }
	#endif

	#if (TRACE_LEVEL >= TRACE_LEVEL_WARNING)
		#define TRACE_WARNING(...)    { printf("-W- " __VA_ARGS__); }
		#define TRACE_WARNING_WP(...) { printf(__VA_ARGS__); }
	#else
		#define TRACE_WARNING(...)    { }
		#define TRACE_WARNING_WP(...) { }
	#endif

	#if (TRACE_LEVEL >= TRACE_LEVEL_ERROR)
		#define TRACE_ERROR(...)      { printf("-E- " __VA_ARGS__); }
		#define TRACE_ERROR_WP(...)   { printf(__VA_ARGS__); }
	#else
		#define TRACE_ERROR(...)      { }
		#define TRACE_ERROR_WP(...)   { }
	#endif

	#if (TRACE_LEVEL >= TRACE_LEVEL_FATAL)
		#define TRACE_FATAL(...)      { printf("-F- " __VA_ARGS__); while (1); }
		#define TRACE_FATAL_WP(...)   { printf(__VA_ARGS__); while (1); }
	#else
		#define TRACE_FATAL(...)      { while (1); }
		#define TRACE_FATAL_WP(...)   { while (1); }
	#endif

#endif


/**
 *        Exported variables
 */
/** Depending on DYN_TRACES, dwTraceLevel is a modifiable runtime variable or a define */
#if !defined(NOTRACE) && (DYN_TRACES == 1)
	extern uint32_t dwTraceLevel;
#endif

#endif //#ifndef TRACE_H