diff options
Diffstat (limited to 'example/timer/odp_timer_simple.c')
-rw-r--r-- | example/timer/odp_timer_simple.c | 86 |
1 files changed, 57 insertions, 29 deletions
diff --git a/example/timer/odp_timer_simple.c b/example/timer/odp_timer_simple.c index 70804bb7b..ceba66c62 100644 --- a/example/timer/odp_timer_simple.c +++ b/example/timer/odp_timer_simple.c @@ -1,23 +1,24 @@ -/* Copyright (c) 2016, Linaro Limited - * All rights reserved. - * - * SPDX-License-Identifier: BSD-3-Clause +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright (c) 2016-2018 Linaro Limited */ + /** - * @file + * @example odp_timer_simple.c + * + * Minimal example application demonstrating ODP timer API usage * - * @example odp_timer_simple.c ODP simple example to schedule timer - * action for 1 second. + * @cond _ODP_HIDE_FROM_DOXYGEN_ */ #include <string.h> #include <stdlib.h> #include <inttypes.h> -#include <example_debug.h> /* ODP main header */ #include <odp_api.h> +#include <odp/helper/odph_api.h> + int main(int argc ODP_UNUSED, char *argv[] ODP_UNUSED) { odp_instance_t instance; @@ -35,6 +36,7 @@ int main(int argc ODP_UNUSED, char *argv[] ODP_UNUSED) uint64_t tick; odp_timeout_t tmo; int ret = 0; + odp_timer_capability_t timer_capa; /* * Init ODP app @@ -61,24 +63,40 @@ int main(int argc ODP_UNUSED, char *argv[] ODP_UNUSED) /* * Create pool of timeouts */ - tparams.res_ns = 10 * ODP_TIME_MSEC_IN_NS; + if (odp_timer_capability(ODP_CLOCK_DEFAULT, &timer_capa)) { + ret += 1; + goto err_tp; + } + + odp_timer_pool_param_init(&tparams); + tparams.res_ns = ODPH_MAX(10 * ODP_TIME_MSEC_IN_NS, + timer_capa.highest_res_ns); tparams.min_tmo = 10 * ODP_TIME_MSEC_IN_NS; tparams.max_tmo = 1 * ODP_TIME_SEC_IN_NS; tparams.num_timers = 1; /* One timer per worker */ tparams.priv = 0; /* Shared */ - tparams.clk_src = ODP_CLOCK_CPU; + tparams.clk_src = ODP_CLOCK_DEFAULT; timer_pool = odp_timer_pool_create("timer_pool", &tparams); if (timer_pool == ODP_TIMER_POOL_INVALID) { ret += 1; goto err; } + if (odp_timer_pool_start_multi(&timer_pool, 1) != 1) { + ODPH_ERR("Timer pool start failed\n"); + ret += 1; + goto err; + } + + /* Configure scheduler */ + odp_schedule_config(NULL); + /* * Create a queue for timer test */ odp_queue_param_init(&qparam); qparam.type = ODP_QUEUE_TYPE_SCHED; - qparam.sched.prio = ODP_SCHED_PRIO_DEFAULT; + qparam.sched.prio = odp_schedule_default_prio(); qparam.sched.sync = ODP_SCHED_SYNC_PARALLEL; qparam.sched.group = ODP_SCHED_GROUP_ALL; @@ -90,22 +108,21 @@ int main(int argc ODP_UNUSED, char *argv[] ODP_UNUSED) tim = odp_timer_alloc(timer_pool, queue, NULL); if (tim == ODP_TIMER_INVALID) { - EXAMPLE_ERR("Failed to allocate timer\n"); + ODPH_ERR("Failed to allocate timer\n"); ret += 1; goto err; } tmo = odp_timeout_alloc(timeout_pool); if (tmo == ODP_TIMEOUT_INVALID) { - EXAMPLE_ERR("Failed to allocate timeout\n"); + ODPH_ERR("Failed to allocate timeout\n"); return -1; } ev = odp_timeout_to_event(tmo); - /* Calculate period for timer in uint64_t value, in current case - * we will schedule timer for 1 second */ - period = odp_timer_ns_to_tick(timer_pool, 1 * ODP_TIME_SEC_IN_NS); + /* Calculate timer period in ticks */ + period = odp_timer_ns_to_tick(timer_pool, 100 * ODP_TIME_MSEC_IN_NS); /* Wait time to return from odp_schedule() if there are no * events @@ -114,45 +131,56 @@ int main(int argc ODP_UNUSED, char *argv[] ODP_UNUSED) for (i = 0; i < 5; i++) { odp_time_t time; + odp_timer_start_t start_param; /* Program timeout action on current tick + period */ tick = odp_timer_current_tick(timer_pool); - rc = odp_timer_set_abs(tim, tick + period, &ev); + + start_param.tick_type = ODP_TIMER_TICK_ABS; + start_param.tick = tick + period; + start_param.tmo_ev = ev; + + rc = odp_timer_start(tim, &start_param); /* Too early or too late timeout requested */ if (odp_unlikely(rc != ODP_TIMER_SUCCESS)) - EXAMPLE_ABORT("odp_timer_set_abs() failed: %d\n", - rc); + ODPH_ABORT("odp_timer_start() failed: %d\n", rc); /* Wait for 2 seconds for timeout action to be generated */ ev = odp_schedule(&queue, sched_tmo); if (ev == ODP_EVENT_INVALID) - EXAMPLE_ABORT("Invalid event\n"); + ODPH_ABORT("Invalid event\n"); if (odp_event_type(ev) != ODP_EVENT_TIMEOUT) - EXAMPLE_ABORT("Unexpected event type (%u) received\n", - odp_event_type(ev)); + ODPH_ABORT("Unexpected event type (%u) received\n", + odp_event_type(ev)); time = odp_time_global(); printf("timer tick %d, time ns %" PRIu64 "\n", i, odp_time_to_ns(time)); /* Do not free current event, just go back to loop and program - * timeout to next second. + * the next timeout. */ } /* Destroy created resources */ - rc += odp_timer_cancel(tim, &ev); - rc += -(odp_timer_free(tim) == ODP_EVENT_INVALID); odp_event_free(ev); - ret += odp_queue_destroy(queue); + if (odp_timer_free(tim)) + ret++; + + if (odp_queue_destroy(queue)) + ret++; err: odp_timer_pool_destroy(timer_pool); err_tp: - ret += odp_pool_destroy(timeout_pool); - ret += odp_term_local(); + if (odp_pool_destroy(timeout_pool)) + ret++; + + if (odp_term_local()) + ret++; err_local: - ret += odp_term_global(instance); + if (odp_term_global(instance)) + ret++; err_global: return ret; } |