This is an automated email from the ASF dual-hosted git repository. janc pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
commit 94aca117f33f6d394ad9cc031b2818b624f3f178 Author: Wiktor Kwiatkowski <[email protected]> AuthorDate: Wed Jan 14 12:32:55 2026 +0100 test/mtest: Add cputime_timer vs callout timing accuracy test This test measures the timing drift between the hardware cputime timer and the OS callout mechanism. --- .../include/mtest_cputimer/mtest_cputimer.h | 56 +++++++++++++ test/mtest/mtest_cputimer/pkg.yml | 30 +++++++ .../mtest/mtest_cputimer/src/cputimer_test_cases.c | 98 ++++++++++++++++++++++ test/mtest/mtest_cputimer/src/main.c | 47 +++++++++++ test/mtest/mtest_cputimer/syscfg.yml | 22 +++++ 5 files changed, 253 insertions(+) diff --git a/test/mtest/mtest_cputimer/include/mtest_cputimer/mtest_cputimer.h b/test/mtest/mtest_cputimer/include/mtest_cputimer/mtest_cputimer.h new file mode 100644 index 000000000..71ca04a00 --- /dev/null +++ b/test/mtest/mtest_cputimer/include/mtest_cputimer/mtest_cputimer.h @@ -0,0 +1,56 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 MTEST_CPUTIMER_H +#define MTEST_CPUTIMER_H +#include "mtest/mtest.h" + +#ifdef __cplusplus +extern "C" { +#endif + +MTEST_CASE_DECL(cputimer_test_case_1); +MTEST_CASE_DECL(cputimer_test_case_2); +MTEST_CASE_DECL(cputimer_test_case_3); +MTEST_CASE_DECL(cputimer_test_case_4); +MTEST_CASE_DECL(cputimer_test_case_5); + +void ms_tick_cb(void *arg); +void check_cb(struct os_event *ev); + +struct cputimer_test_ctx { + struct os_callout check_callout; + struct hal_timer ms_timer; + + int ms_counter; + int act_ms; + int exp_ms; + int dur_s; + + int err_scaled; + int err_int; + int err_frac; +}; + +extern struct cputimer_test_ctx test_ctx; + +#ifdef __cplusplus +} +#endif +#endif diff --git a/test/mtest/mtest_cputimer/pkg.yml b/test/mtest/mtest_cputimer/pkg.yml new file mode 100644 index 000000000..dbd013e73 --- /dev/null +++ b/test/mtest/mtest_cputimer/pkg.yml @@ -0,0 +1,30 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# + +pkg.name: test/mtest/mtest_cputimer +pkg.type: app +pkg.description: Accuracy test app measuring drift between hardware cputime and OS callouts. +pkg.keywords: + +pkg.deps: + - "@apache-mynewt-core/kernel/os" + - "@apache-mynewt-core/hw/hal" + - "@apache-mynewt-core/sys/console" + - "@apache-mynewt-core/sys/log" + - "@apache-mynewt-core/test/mtest/mtest" diff --git a/test/mtest/mtest_cputimer/src/cputimer_test_cases.c b/test/mtest/mtest_cputimer/src/cputimer_test_cases.c new file mode 100644 index 000000000..e0e93cad7 --- /dev/null +++ b/test/mtest/mtest_cputimer/src/cputimer_test_cases.c @@ -0,0 +1,98 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 "bsp.h" +#include "stdlib.h" +#include "console/console.h" +#include "mtest_cputimer/mtest_cputimer.h" + +void +ms_tick_cb(void *arg) +{ + test_ctx.ms_counter++; + os_cputime_timer_relative(&test_ctx.ms_timer, 1000); +} + +void +check_cb(struct os_event *ev) +{ + test_ctx.act_ms = test_ctx.ms_counter; + test_ctx.exp_ms = test_ctx.dur_s * 1000; + + int delta = abs(test_ctx.act_ms - test_ctx.exp_ms); + test_ctx.err_scaled = delta * 10000 / test_ctx.exp_ms; + + test_ctx.err_int = test_ctx.err_scaled / 100; + test_ctx.err_frac = test_ctx.err_scaled % 100; +} + +void +test_cputimer_accuracy(int duration_s) +{ + struct cputimer_test_ctx *tc = &test_ctx; + + tc->dur_s = duration_s; + tc->ms_counter = 0; + + os_cputime_timer_init(&tc->ms_timer, ms_tick_cb, NULL); + os_callout_init(&tc->check_callout, os_eventq_dflt_get(), check_cb, NULL); + + os_cputime_timer_relative(&tc->ms_timer, 1000); + os_callout_reset(&tc->check_callout, OS_TICKS_PER_SEC * duration_s); + + os_eventq_run(os_eventq_dflt_get()); + + int thrld = MYNEWT_VAL(TOLERANCE) * 100; + + printf("expected=%d, actual=%d, error=%d.%02d%%\n", tc->exp_ms, tc->act_ms, + tc->err_int, tc->err_frac); + + MTEST_CASE_ASSERT( + tc->err_scaled < thrld, + "timing out of tolerance: exp=%d, act=%d, err=%d.%02d%%, (tol=%d%%)", + tc->exp_ms, tc->act_ms, tc->err_int, tc->err_frac, MYNEWT_VAL(TOLERANCE)); + + os_cputime_timer_stop(&tc->ms_timer); + os_callout_stop(&tc->check_callout); +} + +MTEST_CASE(cputimer_test_case_1) +{ + test_cputimer_accuracy(1); +} + +MTEST_CASE(cputimer_test_case_2) +{ + test_cputimer_accuracy(2); +} + +MTEST_CASE(cputimer_test_case_3) +{ + test_cputimer_accuracy(4); +} + +MTEST_CASE(cputimer_test_case_4) +{ + test_cputimer_accuracy(8); +} + +MTEST_CASE(cputimer_test_case_5) +{ + test_cputimer_accuracy(16); +} diff --git a/test/mtest/mtest_cputimer/src/main.c b/test/mtest/mtest_cputimer/src/main.c new file mode 100644 index 000000000..876dbeaa7 --- /dev/null +++ b/test/mtest/mtest_cputimer/src/main.c @@ -0,0 +1,47 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 "console/console.h" +#include "sysinit/sysinit.h" +#include "os/os.h" +#include "syscfg/syscfg.h" +#include "mtest_cputimer/mtest_cputimer.h" + +struct cputimer_test_ctx test_ctx; + +MTEST_SUITE(cputimer_test) +{ + cputimer_test_case_1(); + cputimer_test_case_2(); + cputimer_test_case_3(); + cputimer_test_case_4(); + cputimer_test_case_5(); +} + +int +mynewt_main(int argc, char **argv) +{ + sysinit(); + + cputimer_test(); + + while (1) { + os_time_delay(OS_TICKS_PER_SEC); + } +} diff --git a/test/mtest/mtest_cputimer/syscfg.yml b/test/mtest/mtest_cputimer/syscfg.yml new file mode 100644 index 000000000..61ee5273f --- /dev/null +++ b/test/mtest/mtest_cputimer/syscfg.yml @@ -0,0 +1,22 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# + +syscfg.defs: + TOLERANCE: + description: "Percent drift allowed before fail" + value: 10
