diff --git a/tinyusb/class/cdc/cdc_host.c b/tinyusb/class/cdc/cdc_host.c index 25abda27..56f36aed 100644 --- a/tinyusb/class/cdc/cdc_host.c +++ b/tinyusb/class/cdc/cdc_host.c @@ -199,8 +199,8 @@ tusb_error_t cdch_open_subtask(uint8_t dev_addr, tusb_desc_interface_t const *p_ for(uint32_t i=0; i<2; i++) { tusb_desc_endpoint_t const *p_endpoint = (tusb_desc_endpoint_t const *) p_desc; - ASSERT_INT(TUSB_DESC_ENDPOINT, p_endpoint->bDescriptorType, TUSB_ERROR_USBH_DESCRIPTOR_CORRUPTED); - ASSERT_INT(TUSB_XFER_BULK, p_endpoint->bmAttributes.xfer, TUSB_ERROR_USBH_DESCRIPTOR_CORRUPTED); + TU_ASSERT(TUSB_DESC_ENDPOINT == p_endpoint->bDescriptorType, TUSB_ERROR_USBH_DESCRIPTOR_CORRUPTED); + TU_ASSERT(TUSB_XFER_BULK == p_endpoint->bmAttributes.xfer, TUSB_ERROR_USBH_DESCRIPTOR_CORRUPTED); pipe_handle_t * p_pipe_hdl = ( p_endpoint->bEndpointAddress & TUSB_DIR_IN_MASK ) ? &p_cdc->pipe_in : &p_cdc->pipe_out; diff --git a/tinyusb/class/custom_class_host.c b/tinyusb/class/custom_class_host.c index 318813f4..0d24c01b 100644 --- a/tinyusb/class/custom_class_host.c +++ b/tinyusb/class/custom_class_host.c @@ -117,7 +117,7 @@ tusb_error_t cush_open_subtask(uint8_t dev_addr, tusb_desc_interface_t const *p_ for(uint32_t i=0; i<2; i++) { tusb_desc_endpoint_t const *p_endpoint = (tusb_desc_endpoint_t const *) p_desc; - ASSERT_INT(TUSB_DESC_ENDPOINT, p_endpoint->bDescriptorType, TUSB_ERROR_INVALID_PARA); + TU_ASSERT(TUSB_DESC_ENDPOINT == p_endpoint->bDescriptorType, TUSB_ERROR_INVALID_PARA); pipe_handle_t * p_pipe_hdl = ( p_endpoint->bEndpointAddress & TUSB_DIR_IN_MASK ) ? &custom_interface[dev_addr-1].pipe_in : &custom_interface[dev_addr-1].pipe_out; diff --git a/tinyusb/class/hid/hid_device.c b/tinyusb/class/hid/hid_device.c index 8bad4cbb..47aa55ab 100644 --- a/tinyusb/class/hid/hid_device.c +++ b/tinyusb/class/hid/hid_device.c @@ -264,12 +264,12 @@ tusb_error_t hidd_open(uint8_t rhport, tusb_desc_interface_t const * p_interface //------------- HID descriptor -------------// p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH]; tusb_hid_descriptor_hid_t const *p_desc_hid = (tusb_hid_descriptor_hid_t const *) p_desc; - ASSERT_INT(HID_DESC_TYPE_HID, p_desc_hid->bDescriptorType, TUSB_ERROR_HIDD_DESCRIPTOR_INTERFACE); + TU_ASSERT(HID_DESC_TYPE_HID == p_desc_hid->bDescriptorType, TUSB_ERROR_HIDD_DESCRIPTOR_INTERFACE); //------------- Endpoint Descriptor -------------// p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH]; tusb_desc_endpoint_t const *p_desc_endpoint = (tusb_desc_endpoint_t const *) p_desc; - ASSERT_INT(TUSB_DESC_ENDPOINT, p_desc_endpoint->bDescriptorType, TUSB_ERROR_HIDD_DESCRIPTOR_INTERFACE); + TU_ASSERT(TUSB_DESC_ENDPOINT == p_desc_endpoint->bDescriptorType, TUSB_ERROR_HIDD_DESCRIPTOR_INTERFACE); if (p_interface_desc->bInterfaceSubClass == HID_SUBCLASS_BOOT) { diff --git a/tinyusb/class/hid/hid_host.c b/tinyusb/class/hid/hid_host.c index 43acddcf..b0537875 100644 --- a/tinyusb/class/hid/hid_host.c +++ b/tinyusb/class/hid/hid_host.c @@ -76,7 +76,7 @@ tusb_error_t hidh_interface_get_report(uint8_t dev_addr, void * report, hidh_int { //------------- parameters validation -------------// // TODO change to use is configured function - ASSERT_INT (TUSB_DEVICE_STATE_CONFIGURED, tuh_device_get_state(dev_addr), TUSB_ERROR_DEVICE_NOT_READY); + TU_ASSERT (TUSB_DEVICE_STATE_CONFIGURED == tuh_device_get_state(dev_addr), TUSB_ERROR_DEVICE_NOT_READY); VERIFY (report, TUSB_ERROR_INVALID_PARA); TU_ASSSERT (!hcd_pipe_is_busy(p_hid->pipe_hdl), TUSB_ERROR_INTERFACE_IS_BUSY); @@ -188,12 +188,12 @@ tusb_error_t hidh_open_subtask(uint8_t dev_addr, tusb_desc_interface_t const *p_ //------------- HID descriptor -------------// p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH]; tusb_hid_descriptor_hid_t const *p_desc_hid = (tusb_hid_descriptor_hid_t const *) p_desc; - ASSERT_INT(HID_DESC_TYPE_HID, p_desc_hid->bDescriptorType, TUSB_ERROR_INVALID_PARA); + TU_ASSERT(HID_DESC_TYPE_HID == p_desc_hid->bDescriptorType, TUSB_ERROR_INVALID_PARA); //------------- Endpoint Descriptor -------------// p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH]; tusb_desc_endpoint_t const * p_endpoint_desc = (tusb_desc_endpoint_t const *) p_desc; - ASSERT_INT(TUSB_DESC_ENDPOINT, p_endpoint_desc->bDescriptorType, TUSB_ERROR_INVALID_PARA); + TU_ASSERT(TUSB_DESC_ENDPOINT == p_endpoint_desc->bDescriptorType, TUSB_ERROR_INVALID_PARA); OSAL_SUBTASK_BEGIN diff --git a/tinyusb/common/assertion.h b/tinyusb/common/assertion.h index c16e7d1a..8a132d5b 100644 --- a/tinyusb/common/assertion.h +++ b/tinyusb/common/assertion.h @@ -89,20 +89,9 @@ extern "C" ASSERT_DEFINE(tusb_error_t status = (tusb_error_t)(sts),\ TUSB_ERROR_NONE == status, status, "%s", tusb_strerr[status]) -//--------------------------------------------------------------------+ -// Logical Assert -//--------------------------------------------------------------------+ -#define ASSERT_FAILED(error) ASSERT_DEFINE( , false, error, "%s", "FAILED") - //--------------------------------------------------------------------+ // Integral Assert //--------------------------------------------------------------------+ -#define ASSERT_XXX_EQUAL(type_format, expected, actual, error) \ - ASSERT_DEFINE( uint32_t exp = (expected); uint32_t act = (actual),\ - exp==act,\ - error,\ - "expected " type_format ", actual " type_format, exp, act) - #define ASSERT_XXX_WITHIN(type_format, lower, upper, actual, error) \ ASSERT_DEFINE( uint32_t low = (lower); uint32_t up = (upper); uint32_t act = (actual),\ (low <= act) && (act <= up),\ @@ -112,15 +101,11 @@ extern "C" //--------------------------------------------------------------------+ // Integer Assert //--------------------------------------------------------------------+ -#define ASSERT_INT(...) ASSERT_INT_EQUAL(__VA_ARGS__) -#define ASSERT_INT_EQUAL(...) ASSERT_XXX_EQUAL("%d", __VA_ARGS__) #define ASSERT_INT_WITHIN(...) ASSERT_XXX_WITHIN("%d", __VA_ARGS__) //--------------------------------------------------------------------+ // Hex Assert //--------------------------------------------------------------------+ -#define ASSERT_HEX(...) ASSERT_HEX_EQUAL(__VA_ARGS__) -#define ASSERT_HEX_EQUAL(...) ASSERT_XXX_EQUAL("0x%x", __VA_ARGS__) #define ASSERT_HEX_WITHIN(...) ASSERT_XXX_WITHIN("0x%x", __VA_ARGS__) diff --git a/tinyusb/common/verify.h b/tinyusb/common/verify.h index 07ff7179..4704dede 100644 --- a/tinyusb/common/verify.h +++ b/tinyusb/common/verify.h @@ -41,15 +41,21 @@ #include "tusb_option.h" #include "tusb_compiler.h" +/*------------------------------------------------------------------*/ +/* 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 + * + * - VERIFY( cond ) will return false if cond is false + * - VERIFY( cond, err) will return err instead if cond is false + *------------------------------------------------------------------*/ + #ifdef __cplusplus extern "C" { #endif -/** - * Helper to implement optional parameter for VERIFY Macro family - */ -#define GET_3RD_ARG(arg1, arg2, arg3, ...) arg3 -#define GET_4TH_ARG(arg1, arg2, arg3, arg4, ...) arg4 //--------------------------------------------------------------------+ // VERIFY Helper @@ -76,93 +82,99 @@ static inline void verify_breakpoint(void) } #else - #define verify_breakpoint() - #endif /*------------------------------------------------------------------*/ -/* VERIFY STATUS - * - VERIFY_STS_1ARGS : return status of condition if failed - * - VERIFY_STS_2ARGS : return provided status code if failed +/* Macro Generator *------------------------------------------------------------------*/ -#define VERIFY_STS_1ARGS(sts) \ - do { \ - uint32_t _status = (uint32_t)(sts); \ - if ( 0 != _status ) { _VERIFY_MESS(_status) return _status; } \ - } while(0) -#define VERIFY_STS_2ARGS(sts, _error) \ - do { \ - uint32_t _status = (uint32_t)(sts); \ - if ( 0 != _status ) { _VERIFY_MESS(_status) return _error; }\ - } while(0) +// Helper to implement optional parameter for VERIFY Macro family +#define GET_3RD_ARG(arg1, arg2, arg3, ...) arg3 +#define GET_4TH_ARG(arg1, arg2, arg3, arg4, ...) arg4 -/** - * Check if status is success (zero), otherwise return - * - status value if called with 1 parameter e.g VERIFY_STATUS(status) - * - 2 parameter if called with 2 parameters e.g VERIFY_STATUS(status, errorcode) - */ -#define VERIFY_STATUS(...) GET_3RD_ARG(__VA_ARGS__, VERIFY_STS_2ARGS, VERIFY_STS_1ARGS)(__VA_ARGS__) +/*------------- Generator for VERIFY and VERIFY_HDLR -------------*/ +#define VERIFY_DEFINE(_cond, _handler, _error) do { if ( !(_cond) ) { _handler return _error; } } while(0) -/*------------------------------------------------------------------*/ -/* VERIFY STATUS WITH HANDLER - * - VERIFY_STS_HDLR_2ARGS : execute handler, return status if failed - * - VERIFY_STS_HDLR_3ARGS : execute handler, return provided error if failed - *------------------------------------------------------------------*/ -#define VERIFY_STS_HDLR_2ARGS(sts, _handler) \ +/*------------- Generator for VERIFY_STATUS and VERIFY_STATUS_HDLR -------------*/ +#define VERIFY_STS_DEF2(sts, _handler) \ do { \ uint32_t _status = (uint32_t)(sts); \ - if ( 0 != _status ) { verify_breakpoint(); _VERIFY_MESS(_status) _handler; return _status; }\ + if ( 0 != _status ) { _VERIFY_MESS(_status) _handler return _status; }\ } while(0) -#define VERIFY_STS_HDLR_3ARGS(sts, _handler, _error) \ +#define VERIFY_STS_DEF3(sts, _handler, _error) \ do { \ uint32_t _status = (uint32_t)(sts); \ - if ( 0 != _status ) { verify_breakpoint(); _VERIFY_MESS(_status) _handler; return _error; }\ + if ( 0 != _status ) { _VERIFY_MESS(_status) _handler return _error; }\ } while(0) -#define VERIFY_STATUS_HDLR(...) GET_4TH_ARG(__VA_ARGS__, VERIFY_STS_HDLR_3ARGS, VERIFY_STS_HDLR_2ARGS)(__VA_ARGS__) + + /*------------------------------------------------------------------*/ /* VERIFY * - VERIFY_1ARGS : return false if failed * - VERIFY_2ARGS : return provided value if failed *------------------------------------------------------------------*/ -#define VERIFY_1ARGS(cond) do { if (!(cond)) return false; } while(0) -#define VERIFY_2ARGS(cond, _error) do { if (!(cond)) return _error; } while(0) +#define VERIFY_1ARGS(_cond) VERIFY_DEFINE(_cond, , false) +#define VERIFY_2ARGS(_cond, _error) VERIFY_DEFINE(_cond, , _error) + +#define VERIFY(...) GET_3RD_ARG(__VA_ARGS__, VERIFY_2ARGS, VERIFY_1ARGS)(__VA_ARGS__) -/** - * Check if condition is success (true), otherwise return - * - false value if called with 1 parameter e.g VERIFY(condition) - * - 2 parameter if called with 2 parameters e.g VERIFY(condition, errorcode) - */ -#define VERIFY(...) GET_3RD_ARG(__VA_ARGS__, VERIFY_2ARGS, VERIFY_1ARGS)(__VA_ARGS__) /*------------------------------------------------------------------*/ /* VERIFY WITH HANDLER * - VERIFY_HDLR_2ARGS : execute handler, return false if failed * - VERIFY_HDLR_3ARGS : execute handler, return provided error if failed *------------------------------------------------------------------*/ -#define VERIFY_HDLR_2ARGS(cond, _handler) \ - do { if ( !(cond) ) { _handler; return false; } } while(0) +#define VERIFY_HDLR_2ARGS(cond, _handler) VERIFY_DEFINE(cond, _handler; , false) +#define VERIFY_HDLR_3ARGS(cond, _handler, _error) VERIFY_DEFINE(cond, _handler; , _error) -#define VERIFY_HDLR_3ARGS(cond, _handler, _error) \ - do { if ( !(cond) ) { _handler; return _error; } } while(0) +#define VERIFY_HDLR(...) GET_4TH_ARG(__VA_ARGS__, VERIFY_HDLR_3ARGS, VERIFY_HDLR_2ARGS)(__VA_ARGS__) + + +/*------------------------------------------------------------------*/ +/* VERIFY STATUS + * - VERIFY_STS_1ARGS : return status of condition if failed + * - VERIFY_STS_2ARGS : return provided status code if failed + *------------------------------------------------------------------*/ +#define VERIFY_STS_1ARGS(sts) VERIFY_STS_DEF2(sts, ) +#define VERIFY_STS_2ARGS(sts, _error) VERIFY_STS_DEF3(sts, ,_error) + +#define VERIFY_STATUS(...) GET_3RD_ARG(__VA_ARGS__, VERIFY_STS_2ARGS, VERIFY_STS_1ARGS)(__VA_ARGS__) + +/*------------------------------------------------------------------*/ +/* VERIFY STATUS WITH HANDLER + * - VERIFY_STS_HDLR_2ARGS : execute handler, return status if failed + * - VERIFY_STS_HDLR_3ARGS : execute handler, return provided error if failed + *------------------------------------------------------------------*/ +#define VERIFY_STS_HDLR_2ARGS(sts, _handler) VERIFY_STS_DEF2(sts, _handler;) +#define VERIFY_STS_HDLR_3ARGS(sts, _handler, _error) VERIFY_STS_DEF3(sts, _handler; , _error) + +#define VERIFY_STATUS_HDLR(...) GET_4TH_ARG(__VA_ARGS__, VERIFY_STS_HDLR_3ARGS, VERIFY_STS_HDLR_2ARGS)(__VA_ARGS__) -#define VERIFY_HDLR(...) GET_4TH_ARG(__VA_ARGS__, VERIFY_HDLR_3ARGS, VERIFY_HDLR_2ARGS)(__VA_ARGS__) /*------------------------------------------------------------------*/ /* ASSERT - * basically VERIFY with verify_breakpoint as handler + * basically VERIFY with verify_breakpoint() as handler * - 1 arg : return false if failed * - 2 arg : return error if failed *------------------------------------------------------------------*/ -#define ASSERT_1ARGS(cond) do { if (!(cond)) { verify_breakpoint(); _ASSERT_MESS() return false; } } while(0) -#define ASSERT_2ARGS(cond, _error) do { if (!(cond)) { verify_breakpoint(); _ASSERT_MESS() return _error;} } while(0) +#define ASSERT_1ARGS(cond) VERIFY_DEFINE(cond, verify_breakpoint(); , false) +#define ASSERT_2ARGS(cond, _error) VERIFY_DEFINE(cond, verify_breakpoint(); , _error) -#define TU_ASSERT(...) GET_3RD_ARG(__VA_ARGS__, ASSERT_2ARGS, ASSERT_1ARGS)(__VA_ARGS__) +#define TU_ASSERT(...) GET_3RD_ARG(__VA_ARGS__, ASSERT_2ARGS, ASSERT_1ARGS)(__VA_ARGS__) + +/*------------------------------------------------------------------*/ +/* ASSERT Error + * basically VERIFY Error with verify_breakpoint() as handler + *------------------------------------------------------------------*/ +#define ASERT_ERR_1ARGS(_err) VERIFY_STS_DEF2(_err, verify_breakpoint();) +#define ASERT_ERR_2ARGS(_err, _ret) VERIFY_STS_DEF3(_err, verify_breakpoint();, _ret) + +#define ASSERT_ERR(...) GET_3RD_ARG(__VA_ARGS__, ASERT_ERR_2ARGS, ASERT_ERR_1ARGS)(__VA_ARGS__) #ifdef __cplusplus } diff --git a/tinyusb/host/hub.c b/tinyusb/host/hub.c index 6432ff62..7bcd009e 100644 --- a/tinyusb/host/hub.c +++ b/tinyusb/host/hub.c @@ -230,7 +230,7 @@ void hub_isr(pipe_handle_t pipe_hdl, tusb_event_t event, uint32_t xferred_bytes) else { // TODO [HUB] check if hub is still plugged before polling status endpoint since failed usually mean hub unplugged -// ASSERT_INT ( TUSB_ERROR_NONE, hcd_pipe_xfer(pipe_hdl, &p_hub->status_change, 1, true) ); +// TU_ASSERT ( TUSB_ERROR_NONE == hcd_pipe_xfer(pipe_hdl, &p_hub->status_change, 1, true) ); } } diff --git a/tinyusb/host/usbh.c b/tinyusb/host/usbh.c index 38f4bb34..a491988c 100644 --- a/tinyusb/host/usbh.c +++ b/tinyusb/host/usbh.c @@ -259,7 +259,7 @@ void usbh_xfer_isr(pipe_handle_t pipe_hdl, uint8_t class_code, tusb_event_t even usbh_class_drivers[class_index].isr(pipe_hdl, event, xferred_bytes); }else { - ASSERT_FAILED(VOID_RETURN); // something wrong, no one claims the isr's source + TU_ASSERT(false); // something wrong, no one claims the isr's source } }