Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

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