From a82bd1756498606ea5235d77a14a95b3a3c23808 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Fri, 29 Nov 2024 21:20:42 -0700 Subject: tusb_config.h: Have TU_ASSERT use libmisc logging --- cmd/sbc_harness/CMakeLists.txt | 1 + cmd/sbc_harness/config/tusb_config.h | 31 +++++++++++++++++++++++++++++++ cmd/sbc_harness/tusb_log.c | 15 +++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 cmd/sbc_harness/tusb_log.c (limited to 'cmd/sbc_harness') diff --git a/cmd/sbc_harness/CMakeLists.txt b/cmd/sbc_harness/CMakeLists.txt index 6199e0c..bb4e423 100644 --- a/cmd/sbc_harness/CMakeLists.txt +++ b/cmd/sbc_harness/CMakeLists.txt @@ -10,6 +10,7 @@ if (PICO_PLATFORM STREQUAL "rp2040") add_library(sbc_harness_objs OBJECT main.c usb_keyboard.c + tusb_log.c ) target_include_directories(sbc_harness_objs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/config) target_include_directories(sbc_harness_objs PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/cmd/sbc_harness/config/tusb_config.h b/cmd/sbc_harness/config/tusb_config.h index a27c841..0a6d3e4 100644 --- a/cmd/sbc_harness/config/tusb_config.h +++ b/cmd/sbc_harness/config/tusb_config.h @@ -35,6 +35,37 @@ extern "C" { #endif +//-------------------------------------------------------------------- +// Override the default definition of TU_ASSERT() to use our logging +//-------------------------------------------------------------------- + +// "magically" select between the 1-arg and 2-args variants, inject a +// stringified version of `_cond`. +// +// Note: Use GNU-C `, ##__VA_ARGS__`, not standard C `__VA_OPT__(,) +// __VA_ARGS__`; because __VA_OPT__ doesn't handle present-but-empty +// arguments the way that we need. +#define TU_ASSERT(_cond, ...) \ + _GET_3RD_ARG(_cond, ##__VA_ARGS__, \ + _LIBMISC_TU_ASSERT_2ARGS, _LIBMISC_TU_ASSERT_1ARGS, _dummy) \ + (_cond, #_cond, ##__VA_ARGS__) + +#define _LIBMISC_TU_ASSERT_1ARGS(_cond, _cond_str) \ + _LIBMISC_TU_ASSERT_2ARGS(_cond, _cond_str, false) + +#define _LIBMISC_TU_ASSERT_2ARGS(_cond, _cond_str, _ret) \ + do { \ + if ( !(_cond) ) { \ + _libmisc_tu_mess_failed(_cond_str, \ + __FILE__, __LINE__, __func__); \ + TU_BREAKPOINT(); \ + return _ret; \ + } \ + } while(0) + +void _libmisc_tu_mess_failed(const char *expr, + const char *file, unsigned int line, const char *func); + //-------------------------------------------------------------------- // Configuration: common //-------------------------------------------------------------------- diff --git a/cmd/sbc_harness/tusb_log.c b/cmd/sbc_harness/tusb_log.c new file mode 100644 index 0000000..4c6b7df --- /dev/null +++ b/cmd/sbc_harness/tusb_log.c @@ -0,0 +1,15 @@ +/* sbc_harness/tusb_log.c - Logger for tusb_config.h + * + * Copyright (C) 2024 Luke T. Shumaker + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#define LOG_NAME TINY_USB +#include + +void _libmisc_tu_mess_failed(const char *expr, + const char *file, unsigned int line, const char *func) { + errorf("%s:%u:%s(): assertion \"%s\" failed", + file, line, func, + expr); +} -- cgit v1.2.3-2-g168b