Open FFBoard
Open source force feedback firmware
tusb_verify.h
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2019 Ha Thach (tinyusb.org)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 * This file is part of the TinyUSB stack.
25 */
26#ifndef TUSB_VERIFY_H_
27#define TUSB_VERIFY_H_
28
29#include <stdbool.h>
30#include <stdint.h>
31#include "tusb_option.h"
32#include "tusb_compiler.h"
33
34/*------------------------------------------------------------------*/
35/* This file use an advanced macro technique to mimic the default parameter
36 * as C++ for the sake of code simplicity. Beware of a headache macro
37 * manipulation that you are told to stay away.
38 *
39 * This contains macros for both VERIFY and ASSERT:
40 *
41 * VERIFY: Used when there is an error condition which is not the
42 * fault of the MCU. For example, bounds checking on data
43 * sent to the micro over USB should use this function.
44 * Another example is checking for buffer overflows, where
45 * returning from the active function causes a NAK.
46 *
47 * ASSERT: Used for error conditions that are caused by MCU firmware
48 * bugs. This is used to discover bugs in the code more
49 * quickly. One example would be adding assertions in library
50 * function calls to confirm a function's (untainted)
51 * parameters are valid.
52 *
53 * The difference in behavior is that ASSERT triggers a breakpoint while
54 * verify does not.
55 *
56 * #define TU_VERIFY(cond) if(cond) return false;
57 * #define TU_VERIFY(cond,ret) if(cond) return ret;
58 *
59 * #define TU_ASSERT(cond) if(cond) {TU_MESS_FAILED(); TU_BREAKPOINT(), return false;}
60 * #define TU_ASSERT(cond,ret) if(cond) {TU_MESS_FAILED(); TU_BREAKPOINT(), return ret;}
61 *------------------------------------------------------------------*/
62
63#ifdef __cplusplus
64 extern "C" {
65#endif
66
67//--------------------------------------------------------------------+
68// TU_VERIFY Helper
69//--------------------------------------------------------------------+
70
71#if CFG_TUSB_DEBUG
72 #include <stdio.h>
73 #define TU_MESS_FAILED() tu_printf("%s %d: ASSERT FAILED\r\n", __func__, __LINE__)
74#else
75 #define TU_MESS_FAILED() do {} while (0)
76#endif
77
78// Halt CPU (breakpoint) when hitting error, only apply for Cortex M3, M4, M7, M33. M55
79#if defined(__ARM_ARCH_7M__) || defined (__ARM_ARCH_7EM__) || defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8_1M_MAIN__) || \
80 defined(__ARM7M__) || defined (__ARM7EM__) || defined(__ARM8M_MAINLINE__) || defined(__ARM8EM_MAINLINE__)
81 #define TU_BREAKPOINT() do { \
82 volatile uint32_t* ARM_CM_DHCSR = ((volatile uint32_t*) 0xE000EDF0UL); /* Cortex M CoreDebug->DHCSR */ \
83 if ( (*ARM_CM_DHCSR) & 1UL ) __asm("BKPT #0\n"); /* Only halt mcu if debugger is attached */ \
84 } while(0)
85
86#elif defined(__riscv) && !TUSB_MCU_VENDOR_ESPRESSIF
87 #define TU_BREAKPOINT() do { __asm("ebreak\n"); } while(0)
88
89#elif defined(_mips)
90 #define TU_BREAKPOINT() do { __asm("sdbbp 0"); } while (0)
91
92#else
93 #define TU_BREAKPOINT() do {} while (0)
94#endif
95
96/*------------------------------------------------------------------*/
97/* TU_VERIFY
98 * - TU_VERIFY_1ARGS : return false if failed
99 * - TU_VERIFY_2ARGS : return provided value if failed
100 *------------------------------------------------------------------*/
101#define TU_VERIFY_DEFINE(_cond, _ret) \
102 do { \
103 if ( !(_cond) ) { return _ret; } \
104 } while(0)
105
106#define TU_VERIFY_1ARGS(_cond) TU_VERIFY_DEFINE(_cond, false)
107#define TU_VERIFY_2ARGS(_cond, _ret) TU_VERIFY_DEFINE(_cond, _ret)
108
109#define TU_VERIFY(...) TU_GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, _dummy)(__VA_ARGS__)
110
111/*------------------------------------------------------------------*/
112/* ASSERT
113 * basically TU_VERIFY with TU_BREAKPOINT() as handler
114 * - 1 arg : return false if failed
115 * - 2 arg : return error if failed
116 *------------------------------------------------------------------*/
117#define TU_ASSERT_DEFINE(_cond, _ret) \
118 do { \
119 if ( !(_cond) ) { TU_MESS_FAILED(); TU_BREAKPOINT(); return _ret; } \
120 } while(0)
121
122#define TU_ASSERT_1ARGS(_cond) TU_ASSERT_DEFINE(_cond, false)
123#define TU_ASSERT_2ARGS(_cond, _ret) TU_ASSERT_DEFINE(_cond, _ret)
124
125#ifndef TU_ASSERT
126#define TU_ASSERT(...) TU_GET_3RD_ARG(__VA_ARGS__, TU_ASSERT_2ARGS, TU_ASSERT_1ARGS, _dummy)(__VA_ARGS__)
127#endif
128
129#ifdef __cplusplus
130 }
131#endif
132
133#endif