diff --git "a/frameworks\\utest/source/shim.cpp" "b/frameworks\\utest/source/shim.cpp" new file mode 100644 index 0000000000..bfb603cee8 --- /dev/null +++ "b/frameworks\\utest/source/shim.cpp" @@ -0,0 +1,125 @@ +/**************************************************************************** + * Copyright (c) 2015, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **************************************************************************** + */ + +#include "utest/shim.h" + + +#if UTEST_SHIM_SCHEDULER_USE_MINAR +#include "minar/minar.h" + +static int32_t utest_minar_init() +{ + return 0; +} +static void *utest_minar_post(const utest_v1_harness_callback_t callback, const uint32_t delay_ms) +{ + void *handle = minar::Scheduler::postCallback(callback).delay(minar::milliseconds(delay_ms)).getHandle(); + return handle; +} +static int32_t utest_minar_cancel(void *handle) +{ + int32_t ret = minar::Scheduler::cancelCallback(handle); + return ret; +} +static int32_t utest_minar_run() +{ + return 0; +} +extern "C" const utest_v1_scheduler_t utest_v1_scheduler = +{ + utest_minar_init, + utest_minar_post, + utest_minar_cancel, + utest_minar_run +}; + +#elif UTEST_SHIM_SCHEDULER_USE_US_TICKER +// only one callback is active at any given time +volatile utest_v1_harness_callback_t minimal_callback; +volatile utest_v1_harness_callback_t ticker_callback; +const ticker_data_t *ticker_data; +ticker_event_t ticker_event; + +static void ticker_handler(uint32_t) +{ + // printf("\t\t>>> Ticker callback fired for %p.\n", ticker_callback); + minimal_callback = ticker_callback; +} + +static int32_t utest_us_ticker_init() +{ + ticker_data = get_us_ticker_data(); + return 0; +} +static void *utest_us_ticker_post(const utest_v1_harness_callback_t callback, const uint32_t delay_ms) +{ + // printf("\t\t>>> Schedule %p with %ums delay => %p.\n", callback, (unsigned int)delay_ms, (void*)1); + if (delay_ms) { + ticker_callback = callback; + // setup ticker to call the handler manually + ticker_set_handler(ticker_data, ticker_handler); + // fire the interrupt in 1000us * delay_ms + ticker_insert_event(ticker_data, &ticker_event, ticker_read(ticker_data) + delay_ms * 1000, 0); + } else { + minimal_callback = callback; + } + + // return a bogus handle + return (void*)1; +} +static int32_t utest_us_ticker_cancel(void *handle) +{ + // printf("\t\t>>> Cancel %p => %u\n", handle, (unsigned int)0); + (void) handle; + ticker_remove_event(ticker_data, &ticker_event); + return 0; +} +static int32_t utest_us_ticker_run() +{ + while(1) + { + // check if a new callback has been set + if (minimal_callback) + { + // printf("\t\t>>> Firing callback %p\n", minimal_callback); + // copy the callback + utest_v1_harness_callback_t callback = minimal_callback; + // reset the shared callback + minimal_callback = NULL; + // execute the copied callback + callback(); + } + } + return 0; +} +const utest_v1_scheduler_t utest_v1_scheduler = +{ + utest_us_ticker_init, + utest_us_ticker_post, + utest_us_ticker_cancel, + utest_us_ticker_run +}; +#else +const utest_v1_scheduler_t utest_v1_scheduler = +{ + NULL, + NULL, + NULL, + NULL +}; +#endif diff --git "a/frameworks\\utest/utest/shim.h" "b/frameworks\\utest/utest/shim.h" new file mode 100644 index 0000000000..dd8f8437ca --- /dev/null +++ "b/frameworks\\utest/utest/shim.h" @@ -0,0 +1,77 @@ +/**************************************************************************** + * Copyright (c) 2016, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **************************************************************************** + */ + +#ifndef UTEST_SHIM_H +#define UTEST_SHIM_H + +#include +#include +#include +#include "scheduler.h" + +#ifdef YOTTA_CFG +# include "compiler-polyfill/attributes.h" +#else +# ifndef __deprecated_message +# if defined(__CC_ARM) +# define __deprecated_message(msg) __attribute__((deprecated)) +# else +# define __deprecated_message(msg) __attribute__((deprecated(msg))) +# endif +# endif +#endif + +#ifdef YOTTA_CORE_UTIL_VERSION_STRING +# include "core-util/CriticalSectionLock.h" +# define UTEST_ENTER_CRITICAL_SECTION mbed::util::CriticalSectionLock lock +# define UTEST_LEAVE_CRITICAL_SECTION +#else +# ifndef UTEST_ENTER_CRITICAL_SECTION +# error "You must provide a UTEST_ENTER_CRITICAL_SECTION implementation!" +# endif +# ifndef UTEST_LEAVE_CRITICAL_SECTION +# error "You must provide a UTEST_LEAVE_CRITICAL_SECTION implementation!" +# endif +#endif + +#ifndef YOTTA_CFG_UTEST_USE_CUSTOM_SCHEDULER +# ifdef YOTTA_MINAR_VERSION_STRING +# define UTEST_MINAR_AVAILABLE 1 +# else +# define UTEST_MINAR_AVAILABLE 0 +# endif +# ifndef UTEST_SHIM_SCHEDULER_USE_MINAR +# define UTEST_SHIM_SCHEDULER_USE_MINAR UTEST_MINAR_AVAILABLE +# endif +# ifndef UTEST_SHIM_SCHEDULER_USE_US_TICKER +# define UTEST_SHIM_SCHEDULER_USE_US_TICKER (!UTEST_MINAR_AVAILABLE) +# endif +#endif // YOTTA_CFG_UTEST_USE_CUSTOM_SCHEDULER + +#ifdef __cplusplus +extern "C" { +#endif + +/// This is the default scheduler implementation used by the harness. +extern const utest_v1_scheduler_t utest_v1_scheduler; + +#ifdef __cplusplus +} +#endif + +#endif // UTEST_SHIM_H