yincheng.zhong
2024-08-20 7744fffacb03dc81cc9dbaf9f5d86a0f21e79c03
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * Copyright (c) 2019-2023 Beijing Hanwei Innovation Technology Ltd. Co. and
 * its subsidiaries and affiliates (collectly called MKSEMI).
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into an MKSEMI
 *    integrated circuit in a product or a software update for such product,
 *    must reproduce the above copyright notice, this list of conditions and
 *    the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of MKSEMI nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    MKSEMI integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be
 *    reverse engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY MKSEMI "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL MKSEMI OR CONTRIBUTORS 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.
 */
 
#ifndef MK_TRACE_H_
#define MK_TRACE_H_
 
#include "mk_common.h"
#include "mk_uart.h"
#include "stdarg.h"
 
/**
 * @addtogroup MK8000_Trace
 * @{
 */
 
#ifndef UART_CONSOLE_EN
#define UART_CONSOLE_EN (0)
#endif
 
#ifndef TRACE_OVER_UCI_EN
#define TRACE_OVER_UCI_EN (0)
#endif
 
#define TRACE_NEW_LINE "\r\n"
 
#define TRACE_STACK_DUMP_LEN 32
 
#define TRACE_BACKTRACE_COUNT 50
#define TRACE_BACKTRACE_DEPTH 2048
 
#define TRACE_ASSERT_ID 0x4D4B1111
#define TRACE_EXCEPTION_ID 0x4D4B2222
 
#define TRACE_NO_TIMESTAMP 0x0100
#define TRACE_NO_MODULE_NAME 0x0200
#define TRACE_NO_LEVEL_TAG 0x0400
#define TRACE_NO_REPORT_HOST 0x0800
#define TRACE_NO_OPTION 0xff00
 
/** Trace module ID */
enum TRACE_MODULE_ID_T
{
    TRACE_MODULE_MAC = 0,
    TRACE_MODULE_PHY,
    TRACE_MODULE_DRIVER,
    TRACE_MODULE_APP,
    TRACE_MODULE_UWB,
    TRACE_MODULE_UCI,
    TRACE_MODULE_TEST,
    TRACE_MODULE_BOOT,
    TRACE_MODULE_OS,
    TRACE_MODULE_FIRA,
    TRACE_MODULE_CCC,
    TRACE_MODULE_SE,
    TRACE_MODULE_SCP03,
    TRACE_MODULE_NUM,
};
 
/** Trace information level */
enum TRACE_LEVEL_T
{
    TRACE_LEVEL_OFF = 0,     /* Output no tracing and debugging messages. */
    TRACE_LEVEL_ERROR = 1,   /* Output error-handling messages. */
    TRACE_LEVEL_WARNING = 2, /* Output warnings and error-handling messages. */
    TRACE_LEVEL_INFO = 3,    /* Output informational messages, warnings, and error-handling messages. */
    TRACE_LEVEL_VERBOSE = 4, /* Output all debugging and tracing messages. */
    TRACE_LEVEL_NUM,
};
 
/** Trace port */
enum TRACE_PORT_T
{
    TRACE_PORT_UART0,
    TRACE_PORT_UART1,
    TRACE_PORT_SPI0,
    TRACE_PORT_SPI1,
    TRACE_PORT_NUM,
};
 
typedef void (*TRACE_CRASH_DUMP_CB_T)(void);
 
#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
#endif
 
#if TRACE_EN
 
#if TRACE_OVER_UCI_EN
#define TRACE(module, level, str, ...) trace_upto_host((module), (level), (str), ##__VA_ARGS__)
#define TRACE_IF(condition, module, level, str, ...) \
    if (condition)                                   \
    trace_upto_host((module), (level), (str), ##__VA_ARGS__)
#else
#define TRACE(module, level, str, ...) trace_printf((module), (level), (str), ##__VA_ARGS__)
#define TRACE_IF(condition, module, level, str, ...) \
    if (condition)                                   \
    trace_printf((module), (level), (str), ##__VA_ARGS__)
#endif
 
#define TRACE_DUMP8(module, level, str, buf, cnt) trace_dump((module), (level), (str), sizeof(uint8_t), (buf), (cnt))
#define TRACE_DUMP16(module, level, str, buf, cnt) trace_dump((module), (level), (str), sizeof(uint16_t), (buf), (cnt))
#define TRACE_DUMP32(module, level, str, buf, cnt) trace_dump((module), (level), (str), sizeof(uint32_t), (buf), (cnt))
 
#define ASSERT(cond, str, ...) \
    if (!(cond))               \
    trace_assert_dump(__FILE__, __FUNCTION__, __LINE__, str, ##__VA_ARGS__)
 
#else
 
#define TRACE(module, level, str, ...)
#define TRACE_IF(condition, module, level, str, ...)
#define TRACE_DUMP8(module, level, str, buf, cnt)
#define TRACE_DUMP16(module, level, str, buf, cnt)
#define TRACE_DUMP32(module, level, str, buf, cnt)
 
#define ASSERT(cond, str, ...)
 
#endif
 
int trace_printf(uint16_t module, uint8_t level, const char *fmt, ...) __attribute__((format(printf, 3, 4)));
 
#if TRACE_OVER_UCI_EN
extern int uci_print_enable;
void trace_upto_host(uint16_t module, uint8_t level, const char *fmt, ...) __attribute__((format(printf, 3, 4)));
#endif
 
int trace_dump(uint16_t module, uint8_t level, const char *fmt, uint32_t size, const void *data, uint32_t count);
void __NO_RETURN trace_assert_dump(const char *file, const char *func, uint32_t line, const char *fmt, ...) __attribute__((format(printf, 4, 5)));
 
#define mk_printf(str, ...) TRACE(TRACE_NO_OPTION | TRACE_MODULE_APP, TRACE_LEVEL_VERBOSE, str, ##__VA_ARGS__)
#define LOG_VERBOSE(LOG_MODULE, str, ...) TRACE(LOG_MODULE, TRACE_LEVEL_VERBOSE, str, ##__VA_ARGS__)
#define LOG_INFO(LOG_MODULE, str, ...) TRACE(LOG_MODULE, TRACE_LEVEL_INFO, str, ##__VA_ARGS__)
#define LOG_WARNING(LOG_MODULE, str, ...) TRACE(LOG_MODULE, TRACE_LEVEL_WARNING, str, ##__VA_ARGS__)
#define LOG_ERROR(LOG_MODULE, str, ...) TRACE(LOG_MODULE, TRACE_LEVEL_ERROR, str, ##__VA_ARGS__)
 
#define LOG_DATA8(LOG_MODULE, str, buf, cnt) TRACE_DUMP8(LOG_MODULE, TRACE_LEVEL_INFO, str, (buf), (cnt))
#define LOG_DATA16(LOG_MODULE, str, buf, cnt) TRACE_DUMP16(LOG_MODULE, TRACE_LEVEL_INFO, str, (buf), (cnt))
#define LOG_DATA32(LOG_MODULE, str, buf, cnt) TRACE_DUMP32(LOG_MODULE, TRACE_LEVEL_INFO, str, (buf), (cnt))
 
#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
#pragma clang diagnostic pop
#endif
 
#ifdef __cplusplus
extern "C" {
#endif
 
/**
 * @brief Open trace.
 * @param[in] port          Trace port ID @ref TRACE_PORT_T
 * @param[in] baud_rate     Baud rate @ref UART_BAUD_T
 */
int trace_open(enum TRACE_PORT_T port, enum UART_BAUD_T baud_rate);
 
/**
 * @brief Close trace.
 */
int trace_close(void);
 
/**
 * @brief Format trace output.
 * @param[out] buf          Trace output buffer
 * @param[in] size          Trace buffer size
 * @param[in] fmt format
 * @param[in] argp parameters
 */
int trace_format(char *buf, unsigned int size, const char *fmt, va_list argp);
 
/**
 * @brief snprintf.
 * @param[out] buf          Trace output buffer
 * @param[in] size          Trace buffer size
 * @param[in] fmt format
 */
int mk_snprintf(char *buf, unsigned int size, const char *fmt, ...);
 
/**
 * @brief Check if trace buffer is empty.
 * @return 1 represent trace buffer is empty
 */
int trace_buf_is_empty(void);
 
/**
 * @brief Output the trace.
 * @param[in] buf           Trace buffer
 * @param[in] len           Trace buffer length
 * @return the length of output trace
 */
int trace_output(const char *buf, int len);
 
/**
 * @brief Register crash dump callback function.
 * @param[in] cb            Crash dump callback function
 * @return 0 represent register successfully
 */
int trace_crash_dump_cb_register(TRACE_CRASH_DUMP_CB_T cb);
 
/**
 * @brief Flush trace buffer.
 */
void trace_flush(void);
 
#ifdef __cplusplus
}
#endif
 
/**
 * @}
 */
 
#endif /* MK_TRACE_H_ */