tinyusb
assertion.h
Go to the documentation of this file.
1 /**************************************************************************/
37 /**************************************************************************/
38 
44 #ifndef _TUSB_ASSERTION_H_
45 #define _TUSB_ASSERTION_H_
46 
47 #ifdef __cplusplus
48 extern "C"
49 {
50 #endif
51 
52 #include "tusb_option.h"
53 #include "hal/hal.h" // TODO find a way to break hal dependency
54 
55 #define VOID_RETURN
56 
57 //--------------------------------------------------------------------+
58 // Compile-time Assert
59 //--------------------------------------------------------------------+
60 #ifdef __ICCARM__
61  #define STATIC_ASSERT static_assert
62 #else
63  #if defined __COUNTER__ && __COUNTER__ != __COUNTER__
64  #define _ASSERT_COUNTER __COUNTER__
65  #else
66  #define _ASSERT_COUNTER __LINE__
67  #endif
68 
69  #define STATIC_ASSERT(const_expr, message) enum { XSTRING_CONCAT_(static_assert_, _ASSERT_COUNTER) = 1/(!!(const_expr)) }
70 #endif
71 
72  //#if ( defined CFG_PRINTF_UART || defined CFG_PRINTF_USBCDC || defined CFG_PRINTF_DEBUG )
73 #if TUSB_CFG_DEBUG == 3
74  #define _PRINTF(...) printf(__VA_ARGS__) // PRINTF
75 #else
76  #define _PRINTF(...)
77 #endif
78 
79 //--------------------------------------------------------------------+
80 // Assert Helper
81 //--------------------------------------------------------------------+
82 #ifndef _TEST_
83  #define ASSERT_MESSAGE(format, ...)\
84  _PRINTF("Assert at %s: %s: %d: " format "\n", __BASE_FILE__, __func__ , __LINE__, __VA_ARGS__)
85 #else
86  #define ASSERT_MESSAGE(format, ...)\
87  _PRINTF("%d:note: Assert " format "\n", __LINE__, __VA_ARGS__)
88 #endif
89 
90 #ifndef _TEST_ASSERT_
91  #define ASSERT_ERROR_HANDLER(x, para) return x
92 #else
93  #define ASSERT_ERROR_HANDLER(x, para) Throw(x)
94 #endif
95 
96 #define ASSERT_DEFINE_WITH_HANDLER(error_handler, handler_para, setup_statement, condition, error, format, ...) \
97  do{\
98  setup_statement;\
99  if (!(condition)) {\
100  if (hal_debugger_is_attached()){\
101  hal_debugger_breakpoint();\
102  }else{\
103  ASSERT_MESSAGE(format, __VA_ARGS__);\
104  }\
105  error_handler(error, handler_para);\
106  }\
107  }while(0)
108 
109 #define ASSERT_DEFINE(...) ASSERT_DEFINE_WITH_HANDLER(ASSERT_ERROR_HANDLER, NULL, __VA_ARGS__)
110 
111 //--------------------------------------------------------------------+
112 // tusb_error_t Status Assert TODO use ASSERT_DEFINE
113 //--------------------------------------------------------------------+
114 #define ASSERT_STATUS_MESSAGE(sts, message) \
115  ASSERT_DEFINE(tusb_error_t status = (tusb_error_t)(sts),\
116  TUSB_ERROR_NONE == status, status, "%s: %s", TUSB_ErrorStr[status], message)
117 
118 #define ASSERT_STATUS(sts) \
119  ASSERT_DEFINE(tusb_error_t status = (tusb_error_t)(sts),\
120  TUSB_ERROR_NONE == status, status, "%s", TUSB_ErrorStr[status])
121 
122 //--------------------------------------------------------------------+
123 // Logical Assert
124 //--------------------------------------------------------------------+
125 #define ASSERT(...) ASSERT_TRUE(__VA_ARGS__)
126 #define ASSERT_TRUE(condition , error) ASSERT_DEFINE( , (condition), error, "%s", "evaluated to false")
127 #define ASSERT_FALSE(condition , error) ASSERT_DEFINE( ,!(condition), error, "%s", "evaluated to true")
128 
129 //--------------------------------------------------------------------+
130 // Pointer Assert
131 //--------------------------------------------------------------------+
132 #define ASSERT_PTR(...) ASSERT_PTR_NOT_NULL(__VA_ARGS__)
133 #define ASSERT_PTR_NOT_NULL(pointer, error) ASSERT_DEFINE( , NULL != (pointer), error, "%s", "pointer is NULL")
134 #define ASSERT_PTR_NULL(pointer, error) ASSERT_DEFINE( , NULL == (pointer), error, "%s", "pointer is not NULL")
135 
136 //--------------------------------------------------------------------+
137 // Integral Assert
138 //--------------------------------------------------------------------+
139 #define ASSERT_XXX_EQUAL(type_format, expected, actual, error) \
140  ASSERT_DEFINE(\
141  uint32_t exp = (expected); uint32_t act = (actual),\
142  exp==act,\
143  error,\
144  "expected " type_format ", actual " type_format, exp, act)
145 
146 #define ASSERT_XXX_WITHIN(type_format, lower, upper, actual, error) \
147  ASSERT_DEFINE(\
148  uint32_t low = (lower); uint32_t up = (upper); uint32_t act = (actual),\
149  (low <= act) && (act <= up),\
150  error,\
151  "expected within " type_format " - " type_format ", actual " type_format, low, up, act)
152 
153 //--------------------------------------------------------------------+
154 // Integer Assert
155 //--------------------------------------------------------------------+
156 #define ASSERT_INT(...) ASSERT_INT_EQUAL(__VA_ARGS__)
157 #define ASSERT_INT_EQUAL(...) ASSERT_XXX_EQUAL("%d", __VA_ARGS__)
158 #define ASSERT_INT_WITHIN(...) ASSERT_XXX_WITHIN("%d", __VA_ARGS__)
159 
160 //--------------------------------------------------------------------+
161 // Hex Assert
162 //--------------------------------------------------------------------+
163 #define ASSERT_HEX(...) ASSERT_HEX_EQUAL(__VA_ARGS__)
164 #define ASSERT_HEX_EQUAL(...) ASSERT_XXX_EQUAL("0x%x", __VA_ARGS__)
165 #define ASSERT_HEX_WITHIN(...) ASSERT_XXX_WITHIN("0x%x", __VA_ARGS__)
166 
167 //--------------------------------------------------------------------+
168 // TODO Bin Assert
169 //--------------------------------------------------------------------+
170 #define BIN8_PRINTF_PATTERN "%d%d%d%d%d%d%d%d"
171 #define BIN8_PRINTF_CONVERT(byte) \
172  ((byte) & 0x80 ? 1 : 0), \
173  ((byte) & 0x40 ? 1 : 0), \
174  ((byte) & 0x20 ? 1 : 0), \
175  ((byte) & 0x10 ? 1 : 0), \
176  ((byte) & 0x08 ? 1 : 0), \
177  ((byte) & 0x04 ? 1 : 0), \
178  ((byte) & 0x02 ? 1 : 0), \
179  ((byte) & 0x01 ? 1 : 0)
180 
181 #define ASSERT_BIN8(...) ASSERT_BIN8_EQUAL(__VA_ARGS__)
182 #define ASSERT_BIN8_EQUAL(expected, actual, error)\
183  ASSERT_DEFINE(\
184  uint8_t exp = (expected); uint8_t act = (actual),\
185  exp==act,\
186  error,\
187  "expected " BIN8_PRINTF_PATTERN ", actual " BIN8_PRINTF_PATTERN, BIN8_PRINTF_CONVERT(exp), BIN8_PRINTF_CONVERT(act) )
188 
189 //--------------------------------------------------------------------+
190 // TODO Bit Assert
191 //--------------------------------------------------------------------+
192 
193 
194 #ifdef __cplusplus
195 }
196 #endif
197 
198 #endif /* _TUSB_ASSERTION_H_ */
199