mediCAL's first BLE project

Dependents:   BLE_mediCAL

Fork of nRF51822 by Nordic Semiconductor

Committer:
bogdanm
Date:
Wed Mar 26 14:38:17 2014 +0000
Revision:
0:eff01767de02
Initial import of the nRF51822 code

Who changed what in which revision?

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