nordic

Fork of nRF51822 by Nordic Semiconductor

Committer:
vcoubard
Date:
Tue Jan 12 19:58:03 2016 +0000
Revision:
616:1751e2e2637a
Parent:
373:758e9a3a346a
Synchronized with git rev 78b4ea55
Author: Vincent Coubard
Merge pull request #106 from ARMmbed/develop

merge version

Who changed what in which revision?

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