Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
20009: cpu/native: fix bug in periph_timer r=MrKevinWeiss a=maribu

### Contribution description

While debugging RIOT-OS#18977 (comment) it became obvious that the `periph_timer` in `native` is broken and issues early IRQs. This replaces the use of `setitimer` that cannot use a monotonic clock source with `timer_settime()`.

### Testing procedure

I have some non-publishable code that tests if the time an ISR fires in terms of `timer_read()` is no earlier than the time expected. This occasionally triggered with `master`, but I didn't see any of these issues anymore with this PR. I guess I should revive my PR to spice up the periph timer tests and add a polished version of this and let this run for an hour or two.

The tests ins `tests/periph/timer*` should still succeed on `native`. (They do for me in a container running `riot/riotbuild`).

### Issues/PRs references

Found while debugging RIOT-OS#18977 (comment)

20042: dist/tools/uf2: add target to also copy families.json file r=MrKevinWeiss a=MichelRottleuthner

### Contribution description

The updated UF2 pkg (RIOT-OS#20035) stores the family ID in an external .json file. I overlooked that and flashing fails if this file is not present. This PR fixes it by also copying the json into the tool folder.

### Testing procedure
Check if the `feather-nrf52840-sense` can be flashed when the new UF2 pkg is cloned freshly.


### Issues/PRs references
 Fixes a regression introduced with RIOT-OS#20035 


Co-authored-by: Marian Buschsieweke <[email protected]>
Co-authored-by: Michel Rottleuthner <[email protected]>
  • Loading branch information
3 people authored Nov 3, 2023
3 parents c0ae75b + 50b841e + 1e6ac1f commit 4250c15
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
42 changes: 26 additions & 16 deletions cpu/native/periph/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@
* @}
*/

#include <time.h>
#include <sys/time.h>
#include <err.h>
#include <signal.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <sys/time.h>
#include <time.h>

#include "cpu.h"
#include "cpu_conf.h"
#include "native_internal.h"
#include "periph/timer.h"
#include "time_units.h"

#define ENABLE_DEBUG 0
#include "debug.h"
Expand All @@ -49,7 +50,9 @@ static unsigned long time_null;
static timer_cb_t _callback;
static void *_cb_arg;

static struct itimerval itv;
static struct itimerspec its;

static timer_t itimer_monotonic;

/**
* returns ticks for give timespec
Expand Down Expand Up @@ -89,8 +92,15 @@ int timer_init(tim_t dev, uint32_t freq, timer_cb_t cb, void *arg)

_callback = cb;
_cb_arg = arg;

if (timer_create(CLOCK_MONOTONIC, NULL, &itimer_monotonic) != 0) {
DEBUG_PUTS("Failed to create a monotonic itimer");
return -1;
}

if (register_interrupt(SIGALRM, native_isr_timer) != 0) {
DEBUG("darn!\n\n");
DEBUG_PUTS("Failed to register SIGALRM handler");
return -1;
}

return 0;
Expand All @@ -104,14 +114,14 @@ static void do_timer_set(unsigned int offset, bool periodic)
offset = NATIVE_TIMER_MIN_RES;
}

memset(&itv, 0, sizeof(itv));
itv.it_value.tv_sec = (offset / 1000000);
itv.it_value.tv_usec = offset % 1000000;
memset(&its, 0, sizeof(its));
its.it_value.tv_sec = offset / NATIVE_TIMER_SPEED;
its.it_value.tv_nsec = (offset % NATIVE_TIMER_SPEED) * (NS_PER_SEC / NATIVE_TIMER_SPEED);
if (periodic) {
itv.it_interval = itv.it_value;
its.it_interval = its.it_value;
}

DEBUG("timer_set(): setting %lu.%06lu\n", itv.it_value.tv_sec, itv.it_value.tv_usec);
DEBUG("timer_set(): setting %lu.%09lu\n", (unsigned long)its.it_value.tv_sec, its.it_value.tv_nsec);
}

int timer_set(tim_t dev, int channel, unsigned int offset)
Expand Down Expand Up @@ -171,8 +181,8 @@ void timer_start(tim_t dev)
DEBUG("%s\n", __func__);

_native_syscall_enter();
if (real_setitimer(ITIMER_REAL, &itv, NULL) == -1) {
err(EXIT_FAILURE, "timer_arm: setitimer");
if (timer_settime(itimer_monotonic, 0, &its, NULL) == -1) {
err(EXIT_FAILURE, "timer_start: timer_settime");
}
_native_syscall_leave();
}
Expand All @@ -183,13 +193,13 @@ void timer_stop(tim_t dev)
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");
struct itimerspec zero = {0};
if (timer_settime(itimer_monotonic, 0, &zero, &its) == -1) {
err(EXIT_FAILURE, "timer_stop: timer_settime");
}
_native_syscall_leave();

DEBUG("time left: %lu.%06lu\n", itv.it_value.tv_sec, itv.it_value.tv_usec);
DEBUG("time left: %lu.%09lu\n", (unsigned long)its.it_value.tv_sec, its.it_value.tv_nsec);
}

unsigned int timer_read(tim_t dev)
Expand Down
3 changes: 0 additions & 3 deletions cpu/native/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ int (*real_pause)(void);
int (*real_pipe)(int[2]);
int (*real_select)(int nfds, ...);
int (*real_poll)(struct pollfd *fds, ...);
int (*real_setitimer)(int which, const struct itimerval
*restrict value, struct itimerval *restrict ovalue);
int (*real_setsid)(void);
int (*real_setsockopt)(int socket, ...);
int (*real_socket)(int domain, int type, int protocol);
Expand Down Expand Up @@ -535,7 +533,6 @@ void _native_init_syscalls(void)
*(void **)(&real_dup2) = dlsym(RTLD_NEXT, "dup2");
*(void **)(&real_select) = dlsym(RTLD_NEXT, "select");
*(void **)(&real_poll) = dlsym(RTLD_NEXT, "poll");
*(void **)(&real_setitimer) = dlsym(RTLD_NEXT, "setitimer");
*(void **)(&real_setsid) = dlsym(RTLD_NEXT, "setsid");
*(void **)(&real_setsockopt) = dlsym(RTLD_NEXT, "setsockopt");
*(void **)(&real_socket) = dlsym(RTLD_NEXT, "socket");
Expand Down
5 changes: 4 additions & 1 deletion dist/tools/uf2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ PKG_LICENSE=MIT

include $(RIOTBASE)/pkg/pkg.mk

all: $(CURDIR)/uf2conv.py
all: $(CURDIR)/uf2conv.py $(CURDIR)/uf2families.json

$(CURDIR)/uf2conv.py:
cp $(PKG_SOURCE_DIR)/utils/uf2conv.py .
chmod a+x uf2conv.py

$(CURDIR)/uf2families.json:
cp $(PKG_SOURCE_DIR)/utils/uf2families.json .

0 comments on commit 4250c15

Please sign in to comment.