|
| 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 | + |
| 22 | +#if UTEST_SHIM_SCHEDULER_USE_MINAR |
| 23 | +#include "minar/minar.h" |
| 24 | + |
| 25 | +static int32_t utest_minar_init() |
| 26 | +{ |
| 27 | + return 0; |
| 28 | +} |
| 29 | +static void *utest_minar_post(const utest_v1_harness_callback_t callback, const uint32_t delay_ms) |
| 30 | +{ |
| 31 | + void *handle = minar::Scheduler::postCallback(callback).delay(minar::milliseconds(delay_ms)).getHandle(); |
| 32 | + return handle; |
| 33 | +} |
| 34 | +static int32_t utest_minar_cancel(void *handle) |
| 35 | +{ |
| 36 | + int32_t ret = minar::Scheduler::cancelCallback(handle); |
| 37 | + return ret; |
| 38 | +} |
| 39 | +static int32_t utest_minar_run() |
| 40 | +{ |
| 41 | + return 0; |
| 42 | +} |
| 43 | +extern "C" 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 | + |
| 51 | +#elif UTEST_SHIM_SCHEDULER_USE_US_TICKER |
| 52 | +// only one callback is active at any given time |
| 53 | +volatile utest_v1_harness_callback_t minimal_callback; |
| 54 | +volatile utest_v1_harness_callback_t ticker_callback; |
| 55 | +const ticker_data_t *ticker_data; |
| 56 | +ticker_event_t ticker_event; |
| 57 | + |
| 58 | +static void ticker_handler(uint32_t) |
| 59 | +{ |
| 60 | + // printf("\t\t>>> Ticker callback fired for %p.\n", ticker_callback); |
| 61 | + minimal_callback = ticker_callback; |
| 62 | +} |
| 63 | + |
| 64 | +static int32_t utest_us_ticker_init() |
| 65 | +{ |
| 66 | + ticker_data = get_us_ticker_data(); |
| 67 | + return 0; |
| 68 | +} |
| 69 | +static void *utest_us_ticker_post(const utest_v1_harness_callback_t callback, const uint32_t delay_ms) |
| 70 | +{ |
| 71 | + // printf("\t\t>>> Schedule %p with %ums delay => %p.\n", callback, (unsigned int)delay_ms, (void*)1); |
| 72 | + if (delay_ms) { |
| 73 | + ticker_callback = callback; |
| 74 | + // setup ticker to call the handler manually |
| 75 | + ticker_set_handler(ticker_data, ticker_handler); |
| 76 | + // fire the interrupt in 1000us * delay_ms |
| 77 | + ticker_insert_event(ticker_data, &ticker_event, ticker_read(ticker_data) + delay_ms * 1000, 0); |
| 78 | + } else { |
| 79 | + minimal_callback = callback; |
| 80 | + } |
| 81 | + |
| 82 | + // return a bogus handle |
| 83 | + return (void*)1; |
| 84 | +} |
| 85 | +static int32_t utest_us_ticker_cancel(void *handle) |
| 86 | +{ |
| 87 | + // printf("\t\t>>> Cancel %p => %u\n", handle, (unsigned int)0); |
| 88 | + (void) handle; |
| 89 | + ticker_remove_event(ticker_data, &ticker_event); |
| 90 | + return 0; |
| 91 | +} |
| 92 | +static int32_t utest_us_ticker_run() |
| 93 | +{ |
| 94 | + while(1) |
| 95 | + { |
| 96 | + // check if a new callback has been set |
| 97 | + if (minimal_callback) |
| 98 | + { |
| 99 | + // printf("\t\t>>> Firing callback %p\n", minimal_callback); |
| 100 | + // copy the callback |
| 101 | + utest_v1_harness_callback_t callback = minimal_callback; |
| 102 | + // reset the shared callback |
| 103 | + minimal_callback = NULL; |
| 104 | + // execute the copied callback |
| 105 | + callback(); |
| 106 | + } |
| 107 | + } |
| 108 | + return 0; |
| 109 | +} |
| 110 | +const utest_v1_scheduler_t utest_v1_scheduler = |
| 111 | +{ |
| 112 | + utest_us_ticker_init, |
| 113 | + utest_us_ticker_post, |
| 114 | + utest_us_ticker_cancel, |
| 115 | + utest_us_ticker_run |
| 116 | +}; |
| 117 | +#else |
| 118 | +const utest_v1_scheduler_t utest_v1_scheduler = |
| 119 | +{ |
| 120 | + NULL, |
| 121 | + NULL, |
| 122 | + NULL, |
| 123 | + NULL |
| 124 | +}; |
| 125 | +#endif |
0 commit comments