chen
2024-11-08 cc432b761c884a0bd8e9d83db0a4e26109fc08b1
keil/include/drivers/mk_trace.h
对比新文件
@@ -0,0 +1,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_ */