2019-03-20 16:11:42 +07:00
|
|
|
/*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2019-05-14 11:48:05 +07:00
|
|
|
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
2019-03-20 16:11:42 +07:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* This file is part of the TinyUSB stack.
|
|
|
|
*/
|
2018-03-29 13:44:29 +07:00
|
|
|
#ifndef TUSB_VERIFY_H_
|
|
|
|
#define TUSB_VERIFY_H_
|
2018-03-01 12:35:06 +07:00
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
2018-03-28 13:38:17 +07:00
|
|
|
#include "tusb_option.h"
|
|
|
|
#include "tusb_compiler.h"
|
2018-03-01 12:35:06 +07:00
|
|
|
|
2018-03-29 13:07:27 +07:00
|
|
|
/*------------------------------------------------------------------*/
|
|
|
|
/* This file use an advanced macro technique to mimic the default parameter
|
|
|
|
* as C++ for the sake of code simplicity. Beware of a headache macro
|
|
|
|
* manipulation that you are told to stay away.
|
|
|
|
*
|
|
|
|
* e.g
|
|
|
|
*
|
2018-08-13 18:10:23 +07:00
|
|
|
* - TU_VERIFY( cond ) will return false if cond is false
|
|
|
|
* - TU_VERIFY( cond, err) will return err instead if cond is false
|
2018-03-29 13:07:27 +07:00
|
|
|
*------------------------------------------------------------------*/
|
|
|
|
|
2018-03-01 12:35:06 +07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------+
|
2018-08-13 18:10:23 +07:00
|
|
|
// TU_VERIFY Helper
|
2018-03-01 12:35:06 +07:00
|
|
|
//--------------------------------------------------------------------+
|
2019-05-14 11:46:22 +07:00
|
|
|
#if CFG_TUSB_DEBUG
|
2018-07-14 23:43:19 +07:00
|
|
|
#include <stdio.h>
|
2018-04-17 22:16:26 +07:00
|
|
|
#define _MESS_ERR(_err) printf("%s: %d: failed, error = %s\n", __func__, __LINE__, tusb_strerr[_err])
|
|
|
|
#define _MESS_FAILED() printf("%s: %d: failed\n", __func__, __LINE__)
|
2018-03-01 12:35:06 +07:00
|
|
|
#else
|
2018-03-29 13:38:00 +07:00
|
|
|
#define _MESS_ERR(_err)
|
|
|
|
#define _MESS_FAILED()
|
2018-03-01 12:35:06 +07:00
|
|
|
#endif
|
|
|
|
|
2018-03-28 15:23:33 +07:00
|
|
|
// Halt CPU (breakpoint) when hitting error, only apply for Cortex M3, M4, M7
|
|
|
|
#if defined(__ARM_ARCH_7M__) || defined (__ARM_ARCH_7EM__)
|
|
|
|
|
2018-10-24 12:37:43 +07:00
|
|
|
#define TU_BREAKPOINT() \
|
2018-04-16 13:46:28 +07:00
|
|
|
do {\
|
|
|
|
volatile uint32_t* ARM_CM_DHCSR = ((volatile uint32_t*) 0xE000EDF0UL); /* Cortex M CoreDebug->DHCSR */ \
|
|
|
|
if ( (*ARM_CM_DHCSR) & 1UL ) __asm("BKPT #0\n"); /* Only halt mcu if debugger is attached */\
|
|
|
|
} while(0)
|
2018-03-28 15:23:33 +07:00
|
|
|
|
|
|
|
#else
|
2018-10-24 12:37:43 +07:00
|
|
|
#define TU_BREAKPOINT()
|
2018-03-28 15:23:33 +07:00
|
|
|
#endif
|
|
|
|
|
2018-03-01 12:35:06 +07:00
|
|
|
/*------------------------------------------------------------------*/
|
2018-03-29 13:07:27 +07:00
|
|
|
/* Macro Generator
|
2018-03-01 12:35:06 +07:00
|
|
|
*------------------------------------------------------------------*/
|
|
|
|
|
2018-08-13 18:10:23 +07:00
|
|
|
// Helper to implement optional parameter for TU_VERIFY Macro family
|
2018-03-29 13:07:27 +07:00
|
|
|
#define GET_3RD_ARG(arg1, arg2, arg3, ...) arg3
|
|
|
|
#define GET_4TH_ARG(arg1, arg2, arg3, arg4, ...) arg4
|
2018-03-01 12:35:06 +07:00
|
|
|
|
2018-08-13 18:10:23 +07:00
|
|
|
/*------------- Generator for TU_VERIFY and TU_VERIFY_HDLR -------------*/
|
|
|
|
#define TU_VERIFY_DEFINE(_cond, _handler, _ret) do { if ( !(_cond) ) { _handler; return _ret; } } while(0)
|
2018-03-01 12:35:06 +07:00
|
|
|
|
2018-08-13 18:10:23 +07:00
|
|
|
/*------------- Generator for TU_VERIFY_ERR and TU_VERIFY_ERR_HDLR -------------*/
|
|
|
|
#define TU_VERIFY_ERR_DEF2(_error, _handler) \
|
2018-03-03 12:30:38 +07:00
|
|
|
do { \
|
2018-03-29 13:38:00 +07:00
|
|
|
uint32_t _err = (uint32_t)(_error); \
|
|
|
|
if ( 0 != _err ) { _MESS_ERR(_err); _handler; return _err; }\
|
2018-03-03 12:30:38 +07:00
|
|
|
} while(0)
|
|
|
|
|
2018-08-13 18:10:23 +07:00
|
|
|
#define TU_VERIFY_ERR_DEF3(_error, _handler, _ret) \
|
2018-03-03 12:30:38 +07:00
|
|
|
do { \
|
2018-03-29 13:38:00 +07:00
|
|
|
uint32_t _err = (uint32_t)(_error); \
|
|
|
|
if ( 0 != _err ) { _MESS_ERR(_err); _handler; return _ret; }\
|
2018-03-03 12:30:38 +07:00
|
|
|
} while(0)
|
|
|
|
|
2018-03-29 13:07:27 +07:00
|
|
|
|
|
|
|
|
2018-03-01 12:35:06 +07:00
|
|
|
|
|
|
|
/*------------------------------------------------------------------*/
|
2018-08-13 18:10:23 +07:00
|
|
|
/* TU_VERIFY
|
|
|
|
* - TU_VERIFY_1ARGS : return false if failed
|
|
|
|
* - TU_VERIFY_2ARGS : return provided value if failed
|
2018-03-01 12:35:06 +07:00
|
|
|
*------------------------------------------------------------------*/
|
2018-08-13 18:10:23 +07:00
|
|
|
#define TU_VERIFY_1ARGS(_cond) TU_VERIFY_DEFINE(_cond, , false)
|
|
|
|
#define TU_VERIFY_2ARGS(_cond, _ret) TU_VERIFY_DEFINE(_cond, , _ret)
|
2018-03-29 13:07:27 +07:00
|
|
|
|
2018-08-13 18:10:23 +07:00
|
|
|
#define TU_VERIFY(...) GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS)(__VA_ARGS__)
|
2018-03-01 12:35:06 +07:00
|
|
|
|
2018-03-03 12:30:38 +07:00
|
|
|
|
|
|
|
/*------------------------------------------------------------------*/
|
2018-08-13 18:10:23 +07:00
|
|
|
/* TU_VERIFY WITH HANDLER
|
|
|
|
* - TU_VERIFY_HDLR_2ARGS : execute handler, return false if failed
|
|
|
|
* - TU_VERIFY_HDLR_3ARGS : execute handler, return provided error if failed
|
2018-03-03 12:30:38 +07:00
|
|
|
*------------------------------------------------------------------*/
|
2018-08-13 18:10:23 +07:00
|
|
|
#define TU_VERIFY_HDLR_2ARGS(_cond, _handler) TU_VERIFY_DEFINE(_cond, _handler, false)
|
|
|
|
#define TU_VERIFY_HDLR_3ARGS(_cond, _handler, _ret) TU_VERIFY_DEFINE(_cond, _handler, _ret)
|
2018-03-29 13:07:27 +07:00
|
|
|
|
2018-08-13 18:10:23 +07:00
|
|
|
#define TU_VERIFY_HDLR(...) GET_4TH_ARG(__VA_ARGS__, TU_VERIFY_HDLR_3ARGS, TU_VERIFY_HDLR_2ARGS)(__VA_ARGS__)
|
2018-03-29 13:07:27 +07:00
|
|
|
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------*/
|
2018-08-13 18:10:23 +07:00
|
|
|
/* TU_VERIFY STATUS
|
|
|
|
* - TU_VERIFY_ERR_1ARGS : return status of condition if failed
|
|
|
|
* - TU_VERIFY_ERR_2ARGS : return provided status code if failed
|
2018-03-29 13:07:27 +07:00
|
|
|
*------------------------------------------------------------------*/
|
2018-08-13 18:10:23 +07:00
|
|
|
#define TU_VERIFY_ERR_1ARGS(_error) TU_VERIFY_ERR_DEF2(_error, )
|
|
|
|
#define TU_VERIFY_ERR_2ARGS(_error, _ret) TU_VERIFY_ERR_DEF3(_error, ,_ret)
|
2018-03-29 13:07:27 +07:00
|
|
|
|
2018-08-13 18:10:23 +07:00
|
|
|
#define TU_VERIFY_ERR(...) GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_ERR_2ARGS, TU_VERIFY_ERR_1ARGS)(__VA_ARGS__)
|
2018-03-29 13:07:27 +07:00
|
|
|
|
|
|
|
/*------------------------------------------------------------------*/
|
2018-08-13 18:10:23 +07:00
|
|
|
/* TU_VERIFY STATUS WITH HANDLER
|
|
|
|
* - TU_VERIFY_ERR_HDLR_2ARGS : execute handler, return status if failed
|
|
|
|
* - TU_VERIFY_ERR_HDLR_3ARGS : execute handler, return provided error if failed
|
2018-03-29 13:07:27 +07:00
|
|
|
*------------------------------------------------------------------*/
|
2018-08-13 18:10:23 +07:00
|
|
|
#define TU_VERIFY_ERR_HDLR_2ARGS(_error, _handler) TU_VERIFY_ERR_DEF2(_error, _handler)
|
|
|
|
#define TU_VERIFY_ERR_HDLR_3ARGS(_error, _handler, _ret) TU_VERIFY_ERR_DEF3(_error, _handler, _ret)
|
2018-03-01 12:35:06 +07:00
|
|
|
|
2018-08-13 18:10:23 +07:00
|
|
|
#define TU_VERIFY_ERR_HDLR(...) GET_4TH_ARG(__VA_ARGS__, TU_VERIFY_ERR_HDLR_3ARGS, TU_VERIFY_ERR_HDLR_2ARGS)(__VA_ARGS__)
|
2018-03-01 12:35:06 +07:00
|
|
|
|
|
|
|
|
2018-03-08 11:42:28 +07:00
|
|
|
|
|
|
|
/*------------------------------------------------------------------*/
|
|
|
|
/* ASSERT
|
2018-10-24 12:37:43 +07:00
|
|
|
* basically TU_VERIFY with TU_BREAKPOINT() as handler
|
2018-03-08 11:42:28 +07:00
|
|
|
* - 1 arg : return false if failed
|
|
|
|
* - 2 arg : return error if failed
|
|
|
|
*------------------------------------------------------------------*/
|
2018-10-24 12:37:43 +07:00
|
|
|
#define ASSERT_1ARGS(_cond) TU_VERIFY_DEFINE(_cond, _MESS_FAILED(); TU_BREAKPOINT(), false)
|
|
|
|
#define ASSERT_2ARGS(_cond, _ret) TU_VERIFY_DEFINE(_cond, _MESS_FAILED(); TU_BREAKPOINT(), _ret)
|
2018-03-29 13:07:27 +07:00
|
|
|
|
2018-04-12 17:55:15 +07:00
|
|
|
#define TU_ASSERT(...) GET_3RD_ARG(__VA_ARGS__, ASSERT_2ARGS, ASSERT_1ARGS)(__VA_ARGS__)
|
2018-03-29 13:07:27 +07:00
|
|
|
|
|
|
|
/*------------------------------------------------------------------*/
|
|
|
|
/* ASSERT Error
|
2018-10-24 12:37:43 +07:00
|
|
|
* basically TU_VERIFY Error with TU_BREAKPOINT() as handler
|
2018-03-29 13:07:27 +07:00
|
|
|
*------------------------------------------------------------------*/
|
2018-10-24 12:37:43 +07:00
|
|
|
#define ASERT_ERR_1ARGS(_error) TU_VERIFY_ERR_DEF2(_error, TU_BREAKPOINT())
|
|
|
|
#define ASERT_ERR_2ARGS(_error, _ret) TU_VERIFY_ERR_DEF3(_error, TU_BREAKPOINT(), _ret)
|
2018-03-08 11:42:28 +07:00
|
|
|
|
2018-04-12 18:10:35 +07:00
|
|
|
#define TU_ASSERT_ERR(...) GET_3RD_ARG(__VA_ARGS__, ASERT_ERR_2ARGS, ASERT_ERR_1ARGS)(__VA_ARGS__)
|
2018-04-12 17:55:15 +07:00
|
|
|
|
|
|
|
/*------------------------------------------------------------------*/
|
|
|
|
/* ASSERT HDLR
|
|
|
|
*------------------------------------------------------------------*/
|
2018-03-08 11:42:28 +07:00
|
|
|
|
2018-03-01 12:35:06 +07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-03-29 13:44:29 +07:00
|
|
|
#endif /* TUSB_VERIFY_H_ */
|