Skip to content

Commit 336bffa

Browse files
niklarm0xc0170
authored andcommitted
Add shim definitions for mbed OS definitions.
This allows utest to be used without mbed OS dependencies.
1 parent 53377a1 commit 336bffa

File tree

2 files changed

+202
-0
lines changed

2 files changed

+202
-0
lines changed

frameworks\utest/source/shim.cpp

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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

frameworks\utest/utest/shim.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/****************************************************************************
2+
* Copyright (c) 2016, 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+
#ifndef UTEST_SHIM_H
20+
#define UTEST_SHIM_H
21+
22+
#include <stdint.h>
23+
#include <stdbool.h>
24+
#include <stdio.h>
25+
#include "scheduler.h"
26+
27+
#ifdef YOTTA_CFG
28+
# include "compiler-polyfill/attributes.h"
29+
#else
30+
# ifndef __deprecated_message
31+
# if defined(__CC_ARM)
32+
# define __deprecated_message(msg) __attribute__((deprecated))
33+
# else
34+
# define __deprecated_message(msg) __attribute__((deprecated(msg)))
35+
# endif
36+
# endif
37+
#endif
38+
39+
#ifdef YOTTA_CORE_UTIL_VERSION_STRING
40+
# include "core-util/CriticalSectionLock.h"
41+
# define UTEST_ENTER_CRITICAL_SECTION mbed::util::CriticalSectionLock lock
42+
# define UTEST_LEAVE_CRITICAL_SECTION
43+
#else
44+
# ifndef UTEST_ENTER_CRITICAL_SECTION
45+
# error "You must provide a UTEST_ENTER_CRITICAL_SECTION implementation!"
46+
# endif
47+
# ifndef UTEST_LEAVE_CRITICAL_SECTION
48+
# error "You must provide a UTEST_LEAVE_CRITICAL_SECTION implementation!"
49+
# endif
50+
#endif
51+
52+
#ifndef YOTTA_CFG_UTEST_USE_CUSTOM_SCHEDULER
53+
# ifdef YOTTA_MINAR_VERSION_STRING
54+
# define UTEST_MINAR_AVAILABLE 1
55+
# else
56+
# define UTEST_MINAR_AVAILABLE 0
57+
# endif
58+
# ifndef UTEST_SHIM_SCHEDULER_USE_MINAR
59+
# define UTEST_SHIM_SCHEDULER_USE_MINAR UTEST_MINAR_AVAILABLE
60+
# endif
61+
# ifndef UTEST_SHIM_SCHEDULER_USE_US_TICKER
62+
# define UTEST_SHIM_SCHEDULER_USE_US_TICKER (!UTEST_MINAR_AVAILABLE)
63+
# endif
64+
#endif // YOTTA_CFG_UTEST_USE_CUSTOM_SCHEDULER
65+
66+
#ifdef __cplusplus
67+
extern "C" {
68+
#endif
69+
70+
/// This is the default scheduler implementation used by the harness.
71+
extern const utest_v1_scheduler_t utest_v1_scheduler;
72+
73+
#ifdef __cplusplus
74+
}
75+
#endif
76+
77+
#endif // UTEST_SHIM_H

0 commit comments

Comments
 (0)