• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2022 Beken Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <components/log.h>
18 #include <components/system.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #define BK_ASSERT_TAG "ASSERT"
25 
26 #if CONFIG_ARCH_RISCV
27 extern void trap_entry(void);
28 #define BK_ASSERT_DUMP()    trap_entry()
29 #else
30 #define BK_ASSERT_DUMP()
31 #endif
32 
33 #if CONFIG_STDIO_PRINTF
34 #define _OS_PRINTF printf
35 #else
36 #define _OS_PRINTF os_printf
37 #endif
38 
39 #if CONFIG_SHELL_ASYNCLOG
40 
41 #define BK_DUMP_OUT(format, ... )           shell_assert_out(true, format, ##__VA_ARGS__)
42 #define BK_DUMP_RAW_OUT(buf, len)           shell_assert_raw(true, buf, len)
43 
44 #else // #if CONFIG_SHELL_ASYNCLOG
45 
46 #define BK_DUMP_OUT                         _OS_PRINTF
47 #define BK_DUMP_RAW_OUT(buf, len)           uart_write_string(bk_get_printf_port(), buf)
48 
49 #endif // #if CONFIG_SHELL_ASYNCLOG
50 
51 #if (CONFIG_DEBUG_FIRMWARE)
52 
53 #define BK_ASSERT(exp)                                       \
54 do {                                                         \
55     if ( !(exp) ) {                                          \
56         rtos_disable_int();                                  \
57         BK_LOG_FLUSH();                                      \
58         BK_DUMP_OUT("(%d)Assert at: %s:%d\r\n", rtos_get_time(), __FUNCTION__, __LINE__);    \
59         BK_ASSERT_DUMP();                                    \
60         while(1);                                            \
61     }                                                        \
62 } while (0)
63 
64 #define BK_ASSERT_EX(exp, format, ... )                      \
65 do {                                                         \
66     if ( !(exp) ) {                                          \
67         rtos_disable_int();                                  \
68         BK_LOG_FLUSH();                                      \
69         BK_DUMP_OUT(format, ##__VA_ARGS__);                  \
70         BK_DUMP_OUT("(%d)Assert at: %s:%d\r\n", rtos_get_time(), __FUNCTION__, __LINE__);    \
71         BK_ASSERT_DUMP();                                    \
72         while(1);                                            \
73     }                                                        \
74 } while (0)
75 
76 #else // #if (CONFIG_DEBUG_FIRMWARE)
77 
78 #define BK_ASSERT(exp)
79 
80 #define BK_ASSERT_EX(exp, format, ... )
81 
82 #define BK_DUMP_OUT(format, ... )
83 
84 #define BK_DUMP_RAW_OUT(buf, len)
85 
86 #endif // #if (CONFIG_DEBUG_FIRMWARE)
87 
88 #define BK_ASSERT_HALT			BK_DUMP_OUT
89 
90 
91 #ifdef __cplusplus
92 
93 }
94 
95 #endif
96