Merge pull request #16071 from benpicco/cpu/native/timer-periodic

cpu/native: timer: implement timer_set_periodic()
This commit is contained in:
Francisco 2021-02-23 13:26:53 +01:00 committed by GitHub
commit f85628cdb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 13 deletions

View File

@ -19,6 +19,7 @@ config CPU_ARCH_NATIVE
select HAS_PERIPH_HWRNG select HAS_PERIPH_HWRNG
select HAS_PERIPH_PM select HAS_PERIPH_PM
select HAS_PERIPH_PWM select HAS_PERIPH_PWM
select HAS_PERIPH_TIMER_PERIODIC
select HAS_SSP select HAS_SSP
# needed modules # needed modules

View File

@ -17,6 +17,7 @@ FEATURES_PROVIDED += periph_flashpage_pagewise
FEATURES_PROVIDED += periph_hwrng FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_pm FEATURES_PROVIDED += periph_pm
FEATURES_PROVIDED += periph_pwm FEATURES_PROVIDED += periph_pwm
FEATURES_PROVIDED += periph_timer_periodic
FEATURES_PROVIDED += ssp FEATURES_PROVIDED += ssp
ifeq ($(OS),Linux) ifeq ($(OS),Linux)

View File

@ -105,7 +105,7 @@ int timer_init(tim_t dev, uint32_t freq, timer_cb_t cb, void *arg)
return 0; return 0;
} }
static void do_timer_set(unsigned int offset) static void do_timer_set(tim_t dev, unsigned int offset, bool periodic)
{ {
DEBUG("%s\n", __func__); DEBUG("%s\n", __func__);
@ -116,19 +116,17 @@ static void do_timer_set(unsigned int offset)
memset(&itv, 0, sizeof(itv)); memset(&itv, 0, sizeof(itv));
itv.it_value.tv_sec = (offset / 1000000); itv.it_value.tv_sec = (offset / 1000000);
itv.it_value.tv_usec = offset % 1000000; itv.it_value.tv_usec = offset % 1000000;
if (periodic) {
DEBUG("timer_set(): setting %u.%06u\n", (unsigned)itv.it_value.tv_sec, (unsigned)itv.it_value.tv_usec); itv.it_interval = itv.it_value;
_native_syscall_enter();
if (real_setitimer(ITIMER_REAL, &itv, NULL) == -1) {
err(EXIT_FAILURE, "timer_arm: setitimer");
} }
_native_syscall_leave();
DEBUG("timer_set(): setting %lu.%06lu\n", itv.it_value.tv_sec, itv.it_value.tv_usec);
timer_start(dev);
} }
int timer_set(tim_t dev, int channel, unsigned int offset) int timer_set(tim_t dev, int channel, unsigned int offset)
{ {
(void)dev;
DEBUG("%s\n", __func__); DEBUG("%s\n", __func__);
if (channel != 0) { if (channel != 0) {
@ -139,7 +137,7 @@ int timer_set(tim_t dev, int channel, unsigned int offset)
offset = NATIVE_TIMER_MIN_RES; offset = NATIVE_TIMER_MIN_RES;
} }
do_timer_set(offset); do_timer_set(dev, offset, false);
return 0; return 0;
} }
@ -150,12 +148,24 @@ int timer_set_absolute(tim_t dev, int channel, unsigned int value)
return timer_set(dev, channel, value - now); return timer_set(dev, channel, value - now);
} }
int timer_set_periodic(tim_t dev, int channel, unsigned int value, uint8_t flags)
{
(void)flags;
if (channel != 0) {
return -1;
}
do_timer_set(dev, value, true);
return 0;
}
int timer_clear(tim_t dev, int channel) int timer_clear(tim_t dev, int channel)
{ {
(void)dev;
(void)channel; (void)channel;
do_timer_set(0); do_timer_set(dev, 0, false);
return 0; return 0;
} }
@ -164,12 +174,27 @@ void timer_start(tim_t dev)
{ {
(void)dev; (void)dev;
DEBUG("%s\n", __func__); DEBUG("%s\n", __func__);
_native_syscall_enter();
if (real_setitimer(ITIMER_REAL, &itv, NULL) == -1) {
err(EXIT_FAILURE, "timer_arm: setitimer");
}
_native_syscall_leave();
} }
void timer_stop(tim_t dev) void timer_stop(tim_t dev)
{ {
(void)dev; (void)dev;
DEBUG("%s\n", __func__); DEBUG("%s\n", __func__);
_native_syscall_enter();
struct itimerval zero = {0};
if (real_setitimer(ITIMER_REAL, &zero, &itv) == -1) {
err(EXIT_FAILURE, "timer_arm: setitimer");
}
_native_syscall_leave();
DEBUG("time left: %lu.%06lu\n", itv.it_value.tv_sec, itv.it_value.tv_usec);
} }
unsigned int timer_read(tim_t dev) unsigned int timer_read(tim_t dev)