Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 1 | /* ---------------------------------------------------------------------------- |
| 2 | * ATMEL Microcontroller Software Support |
| 3 | * ---------------------------------------------------------------------------- |
| 4 | * Copyright (c) 2008, Atmel Corporation |
| 5 | * |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
| 11 | * - Redistributions of source code must retain the above copyright notice, |
| 12 | * this list of conditions and the disclaimer below. |
| 13 | * |
| 14 | * Atmel's name may not be used to endorse or promote products derived from |
| 15 | * this software without specific prior written permission. |
| 16 | * |
| 17 | * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR |
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE |
| 20 | * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
| 23 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 24 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 25 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 26 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | * ---------------------------------------------------------------------------- |
| 28 | */ |
| 29 | |
| 30 | //------------------------------------------------------------------------------ |
| 31 | /// \unit |
| 32 | /// |
| 33 | /// !Purpose |
| 34 | /// |
| 35 | /// Definition of the ASSERT() and SANITY_CHECK() macros, which are used for |
| 36 | /// runtime condition & parameter verifying. |
| 37 | /// |
| 38 | /// !Usage |
| 39 | /// |
| 40 | /// -# Use ASSERT() in your code to check the value of function parameters, |
| 41 | /// return values, etc. *Warning:* the ASSERT() condition must not have |
| 42 | /// any side-effect; otherwise, the program may not work properly |
| 43 | /// anymore when assertions are disabled. |
| 44 | /// -# Use SANITY_CHECK() to perform checks with a default error message |
| 45 | /// (outputs the file and line number where the error occured). This |
| 46 | /// reduces memory overhead caused by assertion error strings. |
| 47 | /// -# Initialize the dbgu to see failed assertions at run-time. |
| 48 | /// -# Assertions can be entirely disabled by defining the NOASSERT symbol |
| 49 | /// at compilation time. |
| 50 | //------------------------------------------------------------------------------ |
| 51 | |
| 52 | #ifndef ASSERT_H |
| 53 | #define ASSERT_H |
| 54 | |
| 55 | //------------------------------------------------------------------------------ |
| 56 | // Headers |
| 57 | //------------------------------------------------------------------------------ |
| 58 | |
| 59 | #include <stdio.h> |
| 60 | #include "trace.h" |
| 61 | |
| 62 | #define assert ASSERT |
| 63 | |
| 64 | //------------------------------------------------------------------------------ |
| 65 | // Definitions |
| 66 | //------------------------------------------------------------------------------ |
| 67 | #if defined(NOASSERT) |
| 68 | #define ASSERT(...) |
| 69 | #define SANITY_CHECK(...) |
| 70 | #else |
| 71 | |
| 72 | #if (TRACE_LEVEL == 0) |
| 73 | /// Checks that the given condition is true, |
| 74 | /// otherwise stops the program execution. |
| 75 | /// \param condition Condition to verify. |
| 76 | #define ASSERT(condition) { \ |
| 77 | if (!(condition)) { \ |
| 78 | while (1); \ |
| 79 | } \ |
| 80 | } |
| 81 | |
| 82 | /// Performs the same duty as the ASSERT() macro |
| 83 | /// \param condition Condition to verify. |
| 84 | #define SANITY_CHECK(condition) ASSERT(condition, ...) |
| 85 | |
| 86 | #else |
| 87 | /// Checks that the given condition is true, otherwise displays an error |
| 88 | /// message and stops the program execution. |
| 89 | /// \param condition Condition to verify. |
| 90 | #define ASSERT(condition) { \ |
| 91 | if (!(condition)) { \ |
| 92 | printf("-F- ASSERT: %s %s:%d\r\n", #condition, __BASE_FILE__, __LINE__); \ |
| 93 | while (1); \ |
| 94 | } \ |
| 95 | } |
| 96 | #define SANITY_ERROR "Sanity check failed at %s:%d\n\r" |
| 97 | |
| 98 | /// Performs the same duty as the ASSERT() macro, except a default error |
| 99 | /// message is output if the condition is false. |
| 100 | /// \param condition Condition to verify. |
| 101 | #define SANITY_CHECK(condition) ASSERT(condition, SANITY_ERROR, __FILE__, __LINE__) |
| 102 | #endif |
| 103 | #endif |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | #endif //#ifndef ASSERT_H |
| 115 | |