|
| 1 | +/**************************************************************************** |
| 2 | + * Copyright (c) 2015, ARM Limited, All Rights Reserved |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | + * not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + **************************************************************************** |
| 17 | + */ |
| 18 | + |
| 19 | +#include "utest/shim.h" |
| 20 | + |
| 21 | +#if UTEST_SHIM_SCHEDULER_USE_MINAR |
| 22 | +#include "minar/minar.h" |
| 23 | + |
| 24 | +static int32_t utest_minar_init() |
| 25 | +{ |
| 26 | + return 0; |
| 27 | +} |
| 28 | +static void *utest_minar_post(const utest_v1_harness_callback_t callback, const uint32_t delay_ms) |
| 29 | +{ |
| 30 | + void *handle = minar::Scheduler::postCallback(callback).delay(minar::milliseconds(delay_ms)).getHandle(); |
| 31 | + return handle; |
| 32 | +} |
| 33 | +static int32_t utest_minar_cancel(void *handle) |
| 34 | +{ |
| 35 | + int32_t ret = minar::Scheduler::cancelCallback(handle); |
| 36 | + return ret; |
| 37 | +} |
| 38 | +static int32_t utest_minar_run() |
| 39 | +{ |
| 40 | + return 0; |
| 41 | +} |
| 42 | +extern "C" { |
| 43 | +static const utest_v1_scheduler_t utest_v1_scheduler = |
| 44 | +{ |
| 45 | + utest_minar_init, |
| 46 | + utest_minar_post, |
| 47 | + utest_minar_cancel, |
| 48 | + utest_minar_run |
| 49 | +}; |
| 50 | +utest_v1_scheduler_t utest_v1_get_scheduler() |
| 51 | +{ |
| 52 | + return utest_v1_scheduler; |
| 53 | +} |
| 54 | +} |
| 55 | + |
| 56 | +#elif UTEST_SHIM_SCHEDULER_USE_US_TICKER |
| 57 | +#ifdef YOTTA_MBED_HAL_VERSION_STRING |
| 58 | +# include "mbed-hal/us_ticker_api.h" |
| 59 | +#else |
| 60 | +# include "us_ticker_api.h" |
| 61 | +#endif |
| 62 | +// only one callback is active at any given time |
| 63 | +static volatile utest_v1_harness_callback_t minimal_callback; |
| 64 | +static volatile utest_v1_harness_callback_t ticker_callback; |
| 65 | +static const ticker_data_t *ticker_data; |
| 66 | +static ticker_event_t ticker_event; |
| 67 | + |
| 68 | +static void ticker_handler(uint32_t) |
| 69 | +{ |
| 70 | + // printf("\t\t>>> Ticker callback fired for %p.\n", ticker_callback); |
| 71 | + minimal_callback = ticker_callback; |
| 72 | +} |
| 73 | + |
| 74 | +static int32_t utest_us_ticker_init() |
| 75 | +{ |
| 76 | + ticker_data = get_us_ticker_data(); |
| 77 | + ticker_set_handler(ticker_data, ticker_handler); |
| 78 | + return 0; |
| 79 | +} |
| 80 | +static void *utest_us_ticker_post(const utest_v1_harness_callback_t callback, const uint32_t delay_ms) |
| 81 | +{ |
| 82 | + // printf("\t\t>>> Schedule %p with %ums delay => %p.\n", callback, (unsigned int)delay_ms, (void*)1); |
| 83 | + if (delay_ms) { |
| 84 | + ticker_callback = callback; |
| 85 | + // fire the interrupt in 1000us * delay_ms |
| 86 | + ticker_insert_event(ticker_data, &ticker_event, ticker_read(ticker_data) + delay_ms * 1000, 0); |
| 87 | + } else { |
| 88 | + minimal_callback = callback; |
| 89 | + } |
| 90 | + |
| 91 | + // return a bogus handle |
| 92 | + return (void*)1; |
| 93 | +} |
| 94 | +static int32_t utest_us_ticker_cancel(void *handle) |
| 95 | +{ |
| 96 | + // printf("\t\t>>> Cancel %p => %u\n", handle, (unsigned int)0); |
| 97 | + (void) handle; |
| 98 | + ticker_remove_event(ticker_data, &ticker_event); |
| 99 | + return 0; |
| 100 | +} |
| 101 | +static int32_t utest_us_ticker_run() |
| 102 | +{ |
| 103 | + while(1) |
| 104 | + { |
| 105 | + // check if a new callback has been set |
| 106 | + if (minimal_callback) |
| 107 | + { |
| 108 | + // printf("\t\t>>> Firing callback %p\n", minimal_callback); |
| 109 | + // copy the callback |
| 110 | + utest_v1_harness_callback_t callback = minimal_callback; |
| 111 | + // reset the shared callback |
| 112 | + minimal_callback = NULL; |
| 113 | + // execute the copied callback |
| 114 | + callback(); |
| 115 | + } |
| 116 | + } |
| 117 | + return 0; |
| 118 | +} |
| 119 | +extern "C" { |
| 120 | +static const utest_v1_scheduler_t utest_v1_scheduler = |
| 121 | +{ |
| 122 | + utest_us_ticker_init, |
| 123 | + utest_us_ticker_post, |
| 124 | + utest_us_ticker_cancel, |
| 125 | + utest_us_ticker_run |
| 126 | +}; |
| 127 | +utest_v1_scheduler_t utest_v1_get_scheduler() |
| 128 | +{ |
| 129 | + return utest_v1_scheduler; |
| 130 | +} |
| 131 | +} |
| 132 | +#endif |
| 133 | + |
| 134 | +#ifdef YOTTA_CORE_UTIL_VERSION_STRING |
| 135 | +// their functionality is implemented using the CriticalSectionLock class |
| 136 | +void utest_v1_enter_critical_section(void) {} |
| 137 | +void utest_v1_leave_critical_section(void) {} |
| 138 | +#endif |
0 commit comments