Updated to latest online libraries (See also mbed Apps: HelloBlue, FOTA4)
Dependencies: BLE_API mbed nRF51822
Fork of BLE_Default_APP by
nRF51822/common/assertion.h@1:a607cd9655d7, 2014-10-10 (annotated)
- Committer:
- yihui
- Date:
- Fri Oct 10 03:36:28 2014 +0000
- Revision:
- 1:a607cd9655d7
use NVIC_SystemReset() to run bootloader
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 1:a607cd9655d7 | 1 | /**************************************************************************/ |
yihui | 1:a607cd9655d7 | 2 | /*! |
yihui | 1:a607cd9655d7 | 3 | @file assertion.h |
yihui | 1:a607cd9655d7 | 4 | @author hathach (tinyusb.org) |
yihui | 1:a607cd9655d7 | 5 | |
yihui | 1:a607cd9655d7 | 6 | @section LICENSE |
yihui | 1:a607cd9655d7 | 7 | |
yihui | 1:a607cd9655d7 | 8 | Software License Agreement (BSD License) |
yihui | 1:a607cd9655d7 | 9 | |
yihui | 1:a607cd9655d7 | 10 | Copyright (c) 2013, K. Townsend (microBuilder.eu) |
yihui | 1:a607cd9655d7 | 11 | All rights reserved. |
yihui | 1:a607cd9655d7 | 12 | |
yihui | 1:a607cd9655d7 | 13 | Redistribution and use in source and binary forms, with or without |
yihui | 1:a607cd9655d7 | 14 | modification, are permitted provided that the following conditions are met: |
yihui | 1:a607cd9655d7 | 15 | 1. Redistributions of source code must retain the above copyright |
yihui | 1:a607cd9655d7 | 16 | notice, this list of conditions and the following disclaimer. |
yihui | 1:a607cd9655d7 | 17 | 2. Redistributions in binary form must reproduce the above copyright |
yihui | 1:a607cd9655d7 | 18 | notice, this list of conditions and the following disclaimer in the |
yihui | 1:a607cd9655d7 | 19 | documentation and/or other materials provided with the distribution. |
yihui | 1:a607cd9655d7 | 20 | 3. Neither the name of the copyright holders nor the |
yihui | 1:a607cd9655d7 | 21 | names of its contributors may be used to endorse or promote products |
yihui | 1:a607cd9655d7 | 22 | derived from this software without specific prior written permission. |
yihui | 1:a607cd9655d7 | 23 | |
yihui | 1:a607cd9655d7 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
yihui | 1:a607cd9655d7 | 25 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
yihui | 1:a607cd9655d7 | 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
yihui | 1:a607cd9655d7 | 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY |
yihui | 1:a607cd9655d7 | 28 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
yihui | 1:a607cd9655d7 | 29 | INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
yihui | 1:a607cd9655d7 | 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND |
yihui | 1:a607cd9655d7 | 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
yihui | 1:a607cd9655d7 | 32 | INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS |
yihui | 1:a607cd9655d7 | 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
yihui | 1:a607cd9655d7 | 34 | */ |
yihui | 1:a607cd9655d7 | 35 | /**************************************************************************/ |
yihui | 1:a607cd9655d7 | 36 | |
yihui | 1:a607cd9655d7 | 37 | /** \file |
yihui | 1:a607cd9655d7 | 38 | * \brief TBD |
yihui | 1:a607cd9655d7 | 39 | * |
yihui | 1:a607cd9655d7 | 40 | * \note TBD |
yihui | 1:a607cd9655d7 | 41 | */ |
yihui | 1:a607cd9655d7 | 42 | |
yihui | 1:a607cd9655d7 | 43 | /** \ingroup TBD |
yihui | 1:a607cd9655d7 | 44 | * \defgroup TBD |
yihui | 1:a607cd9655d7 | 45 | * \brief TBD |
yihui | 1:a607cd9655d7 | 46 | * |
yihui | 1:a607cd9655d7 | 47 | * @{ |
yihui | 1:a607cd9655d7 | 48 | */ |
yihui | 1:a607cd9655d7 | 49 | |
yihui | 1:a607cd9655d7 | 50 | #ifndef _ASSERTION_H_ |
yihui | 1:a607cd9655d7 | 51 | #define _ASSERTION_H_ |
yihui | 1:a607cd9655d7 | 52 | |
yihui | 1:a607cd9655d7 | 53 | #include "projectconfig.h" |
yihui | 1:a607cd9655d7 | 54 | |
yihui | 1:a607cd9655d7 | 55 | #ifdef __cplusplus |
yihui | 1:a607cd9655d7 | 56 | extern "C" |
yihui | 1:a607cd9655d7 | 57 | { |
yihui | 1:a607cd9655d7 | 58 | #endif |
yihui | 1:a607cd9655d7 | 59 | |
yihui | 1:a607cd9655d7 | 60 | static inline void debugger_breakpoint(void) ATTR_ALWAYS_INLINE; |
yihui | 1:a607cd9655d7 | 61 | static inline void debugger_breakpoint(void) |
yihui | 1:a607cd9655d7 | 62 | { |
yihui | 1:a607cd9655d7 | 63 | #ifndef _TEST_ |
yihui | 1:a607cd9655d7 | 64 | __asm("BKPT #0\n"); |
yihui | 1:a607cd9655d7 | 65 | #endif |
yihui | 1:a607cd9655d7 | 66 | } |
yihui | 1:a607cd9655d7 | 67 | |
yihui | 1:a607cd9655d7 | 68 | //--------------------------------------------------------------------+ |
yihui | 1:a607cd9655d7 | 69 | // Compile-time Assert |
yihui | 1:a607cd9655d7 | 70 | //--------------------------------------------------------------------+ |
yihui | 1:a607cd9655d7 | 71 | #if defined __COUNTER__ && __COUNTER__ != __COUNTER__ |
yihui | 1:a607cd9655d7 | 72 | #define _ASSERT_COUNTER __COUNTER__ |
yihui | 1:a607cd9655d7 | 73 | #else |
yihui | 1:a607cd9655d7 | 74 | #define _ASSERT_COUNTER __LINE__ |
yihui | 1:a607cd9655d7 | 75 | #endif |
yihui | 1:a607cd9655d7 | 76 | |
yihui | 1:a607cd9655d7 | 77 | #define ASSERT_STATIC(const_expr, message) enum { XSTRING_CONCAT_(static_assert_, _ASSERT_COUNTER) = 1/(!!(const_expr)) } |
yihui | 1:a607cd9655d7 | 78 | |
yihui | 1:a607cd9655d7 | 79 | //--------------------------------------------------------------------+ |
yihui | 1:a607cd9655d7 | 80 | // Assert Helper |
yihui | 1:a607cd9655d7 | 81 | //--------------------------------------------------------------------+ |
yihui | 1:a607cd9655d7 | 82 | //#ifndef _TEST_ |
yihui | 1:a607cd9655d7 | 83 | // #define ASSERT_MESSAGE(format, ...)\ |
yihui | 1:a607cd9655d7 | 84 | // _PRINTF("Assert at %s: %s: %d: " format "\n", __BASE_FILE__, __PRETTY_FUNCTION__, __LINE__, __VA_ARGS__) |
yihui | 1:a607cd9655d7 | 85 | //#else |
yihui | 1:a607cd9655d7 | 86 | // #define ASSERT_MESSAGE(format, ...)\ |
yihui | 1:a607cd9655d7 | 87 | // _PRINTF("%d:note: Assert " format "\n", __LINE__, __VA_ARGS__) |
yihui | 1:a607cd9655d7 | 88 | //#endif |
yihui | 1:a607cd9655d7 | 89 | |
yihui | 1:a607cd9655d7 | 90 | #if CFG_DEBUG == 3 |
yihui | 1:a607cd9655d7 | 91 | #define ASSERT_MESSAGE(format, ...) debugger_breakpoint() |
yihui | 1:a607cd9655d7 | 92 | #elif CFG_DEBUG == 2 |
yihui | 1:a607cd9655d7 | 93 | #define ASSERT_MESSAGE(format, ...) printf("Assert at %s: %s: %d: " format "\n", __BASE_FILE__, __PRETTY_FUNCTION__, __LINE__, __VA_ARGS__) |
yihui | 1:a607cd9655d7 | 94 | #else |
yihui | 1:a607cd9655d7 | 95 | #define ASSERT_MESSAGE(format, ...) |
yihui | 1:a607cd9655d7 | 96 | #endif |
yihui | 1:a607cd9655d7 | 97 | |
yihui | 1:a607cd9655d7 | 98 | #define ASSERT_ERROR_HANDLER(x, para) \ |
yihui | 1:a607cd9655d7 | 99 | return (x) |
yihui | 1:a607cd9655d7 | 100 | |
yihui | 1:a607cd9655d7 | 101 | #define ASSERT_DEFINE_WITH_HANDLER(error_handler, handler_para, setup_statement, condition, error, format, ...) \ |
yihui | 1:a607cd9655d7 | 102 | do{\ |
yihui | 1:a607cd9655d7 | 103 | setup_statement;\ |
yihui | 1:a607cd9655d7 | 104 | if (!(condition)) {\ |
yihui | 1:a607cd9655d7 | 105 | ASSERT_MESSAGE(format, __VA_ARGS__);\ |
yihui | 1:a607cd9655d7 | 106 | error_handler(error, handler_para);\ |
yihui | 1:a607cd9655d7 | 107 | }\ |
yihui | 1:a607cd9655d7 | 108 | }while(0) |
yihui | 1:a607cd9655d7 | 109 | |
yihui | 1:a607cd9655d7 | 110 | #define ASSERT_DEFINE(...) ASSERT_DEFINE_WITH_HANDLER(ASSERT_ERROR_HANDLER, NULL, __VA_ARGS__) |
yihui | 1:a607cd9655d7 | 111 | |
yihui | 1:a607cd9655d7 | 112 | //--------------------------------------------------------------------+ |
yihui | 1:a607cd9655d7 | 113 | // error_t Status Assert TODO use ASSERT_DEFINE |
yihui | 1:a607cd9655d7 | 114 | //--------------------------------------------------------------------+ |
yihui | 1:a607cd9655d7 | 115 | #define ASSERT_STATUS_MESSAGE(sts, message) \ |
yihui | 1:a607cd9655d7 | 116 | ASSERT_DEFINE(error_t status = (error_t)(sts),\ |
yihui | 1:a607cd9655d7 | 117 | ERROR_NONE == status, status, "%s: %s", ErrorStr[status], message) |
yihui | 1:a607cd9655d7 | 118 | |
yihui | 1:a607cd9655d7 | 119 | #define ASSERT_STATUS(sts) \ |
yihui | 1:a607cd9655d7 | 120 | ASSERT_DEFINE(error_t status = (error_t)(sts),\ |
yihui | 1:a607cd9655d7 | 121 | ERROR_NONE == status, status, "error = %d", status) |
yihui | 1:a607cd9655d7 | 122 | |
yihui | 1:a607cd9655d7 | 123 | #define ASSERT_STATUS_RET_VOID(sts) \ |
yihui | 1:a607cd9655d7 | 124 | ASSERT_DEFINE(error_t status = (error_t)(sts),\ |
yihui | 1:a607cd9655d7 | 125 | ERROR_NONE == status, (void) 0, "error = %d", status) |
yihui | 1:a607cd9655d7 | 126 | |
yihui | 1:a607cd9655d7 | 127 | //--------------------------------------------------------------------+ |
yihui | 1:a607cd9655d7 | 128 | // Logical Assert |
yihui | 1:a607cd9655d7 | 129 | //--------------------------------------------------------------------+ |
yihui | 1:a607cd9655d7 | 130 | #define ASSERT(...) ASSERT_TRUE(__VA_ARGS__) |
yihui | 1:a607cd9655d7 | 131 | #define ASSERT_TRUE(condition , error) ASSERT_DEFINE( , (condition), error, "%s", "evaluated to false") |
yihui | 1:a607cd9655d7 | 132 | #define ASSERT_FALSE(condition , error) ASSERT_DEFINE( ,!(condition), error, "%s", "evaluated to true") |
yihui | 1:a607cd9655d7 | 133 | |
yihui | 1:a607cd9655d7 | 134 | //--------------------------------------------------------------------+ |
yihui | 1:a607cd9655d7 | 135 | // Pointer Assert |
yihui | 1:a607cd9655d7 | 136 | //--------------------------------------------------------------------+ |
yihui | 1:a607cd9655d7 | 137 | #define ASSERT_PTR(...) ASSERT_PTR_NOT_NULL(__VA_ARGS__) |
yihui | 1:a607cd9655d7 | 138 | #define ASSERT_PTR_NOT_NULL(pointer, error) ASSERT_DEFINE( , NULL != (pointer), error, "%s", "pointer is NULL") |
yihui | 1:a607cd9655d7 | 139 | #define ASSERT_PTR_NULL(pointer, error) ASSERT_DEFINE( , NULL == (pointer), error, "%s", "pointer is not NULL") |
yihui | 1:a607cd9655d7 | 140 | |
yihui | 1:a607cd9655d7 | 141 | //--------------------------------------------------------------------+ |
yihui | 1:a607cd9655d7 | 142 | // Integral Assert |
yihui | 1:a607cd9655d7 | 143 | //--------------------------------------------------------------------+ |
yihui | 1:a607cd9655d7 | 144 | #define ASSERT_XXX_EQUAL(type_format, expected, actual, error) \ |
yihui | 1:a607cd9655d7 | 145 | ASSERT_DEFINE(\ |
yihui | 1:a607cd9655d7 | 146 | uint32_t exp = (expected); uint32_t act = (actual),\ |
yihui | 1:a607cd9655d7 | 147 | exp==act,\ |
yihui | 1:a607cd9655d7 | 148 | error,\ |
yihui | 1:a607cd9655d7 | 149 | "expected " type_format ", actual " type_format, exp, act) |
yihui | 1:a607cd9655d7 | 150 | |
yihui | 1:a607cd9655d7 | 151 | #define ASSERT_XXX_WITHIN(type_format, lower, upper, actual, error) \ |
yihui | 1:a607cd9655d7 | 152 | ASSERT_DEFINE(\ |
yihui | 1:a607cd9655d7 | 153 | uint32_t low = (lower); uint32_t up = (upper); uint32_t act = (actual),\ |
yihui | 1:a607cd9655d7 | 154 | (low <= act) && (act <= up),\ |
yihui | 1:a607cd9655d7 | 155 | error,\ |
yihui | 1:a607cd9655d7 | 156 | "expected within " type_format " - " type_format ", actual " type_format, low, up, act) |
yihui | 1:a607cd9655d7 | 157 | |
yihui | 1:a607cd9655d7 | 158 | //--------------------------------------------------------------------+ |
yihui | 1:a607cd9655d7 | 159 | // Integer Assert |
yihui | 1:a607cd9655d7 | 160 | //--------------------------------------------------------------------+ |
yihui | 1:a607cd9655d7 | 161 | #define ASSERT_INT(...) ASSERT_INT_EQUAL(__VA_ARGS__) |
yihui | 1:a607cd9655d7 | 162 | #define ASSERT_INT_EQUAL(...) ASSERT_XXX_EQUAL("%d", __VA_ARGS__) |
yihui | 1:a607cd9655d7 | 163 | #define ASSERT_INT_WITHIN(...) ASSERT_XXX_WITHIN("%d", __VA_ARGS__) |
yihui | 1:a607cd9655d7 | 164 | |
yihui | 1:a607cd9655d7 | 165 | //--------------------------------------------------------------------+ |
yihui | 1:a607cd9655d7 | 166 | // Hex Assert |
yihui | 1:a607cd9655d7 | 167 | //--------------------------------------------------------------------+ |
yihui | 1:a607cd9655d7 | 168 | #define ASSERT_HEX(...) ASSERT_HEX_EQUAL(__VA_ARGS__) |
yihui | 1:a607cd9655d7 | 169 | #define ASSERT_HEX_EQUAL(...) ASSERT_XXX_EQUAL("0x%x", __VA_ARGS__) |
yihui | 1:a607cd9655d7 | 170 | #define ASSERT_HEX_WITHIN(...) ASSERT_XXX_WITHIN("0x%x", __VA_ARGS__) |
yihui | 1:a607cd9655d7 | 171 | |
yihui | 1:a607cd9655d7 | 172 | //--------------------------------------------------------------------+ |
yihui | 1:a607cd9655d7 | 173 | // Bin Assert |
yihui | 1:a607cd9655d7 | 174 | //--------------------------------------------------------------------+ |
yihui | 1:a607cd9655d7 | 175 | #define BIN8_PRINTF_PATTERN "%d%d%d%d%d%d%d%d" |
yihui | 1:a607cd9655d7 | 176 | #define BIN8_PRINTF_CONVERT(byte) \ |
yihui | 1:a607cd9655d7 | 177 | ((byte) & 0x80 ? 1 : 0), \ |
yihui | 1:a607cd9655d7 | 178 | ((byte) & 0x40 ? 1 : 0), \ |
yihui | 1:a607cd9655d7 | 179 | ((byte) & 0x20 ? 1 : 0), \ |
yihui | 1:a607cd9655d7 | 180 | ((byte) & 0x10 ? 1 : 0), \ |
yihui | 1:a607cd9655d7 | 181 | ((byte) & 0x08 ? 1 : 0), \ |
yihui | 1:a607cd9655d7 | 182 | ((byte) & 0x04 ? 1 : 0), \ |
yihui | 1:a607cd9655d7 | 183 | ((byte) & 0x02 ? 1 : 0), \ |
yihui | 1:a607cd9655d7 | 184 | ((byte) & 0x01 ? 1 : 0) |
yihui | 1:a607cd9655d7 | 185 | |
yihui | 1:a607cd9655d7 | 186 | #define ASSERT_BIN8(...) ASSERT_BIN8_EQUAL(__VA_ARGS__) |
yihui | 1:a607cd9655d7 | 187 | #define ASSERT_BIN8_EQUAL(expected, actual, error)\ |
yihui | 1:a607cd9655d7 | 188 | ASSERT_DEFINE(\ |
yihui | 1:a607cd9655d7 | 189 | uint8_t exp = (expected); uint8_t act = (actual),\ |
yihui | 1:a607cd9655d7 | 190 | exp==act,\ |
yihui | 1:a607cd9655d7 | 191 | error,\ |
yihui | 1:a607cd9655d7 | 192 | "expected " BIN8_PRINTF_PATTERN ", actual " BIN8_PRINTF_PATTERN, BIN8_PRINTF_CONVERT(exp), BIN8_PRINTF_CONVERT(act) ) |
yihui | 1:a607cd9655d7 | 193 | |
yihui | 1:a607cd9655d7 | 194 | #ifdef __cplusplus |
yihui | 1:a607cd9655d7 | 195 | } |
yihui | 1:a607cd9655d7 | 196 | #endif |
yihui | 1:a607cd9655d7 | 197 | |
yihui | 1:a607cd9655d7 | 198 | #endif /* _ASSERTION_H_ */ |
yihui | 1:a607cd9655d7 | 199 | |
yihui | 1:a607cd9655d7 | 200 | /** @} */ |