gettimeofday from isr crashes

this.wiederkehr
Posts: 15
Joined: Thu Jul 13, 2017 8:04 am

gettimeofday from isr crashes

Postby this.wiederkehr » Fri May 29, 2020 3:26 pm

How to get the timeofday from an isr?

If I do use gettimeofday from isr it crashes if the application is also executing gettimeofday because the lock is acquired already by the application.

Here is the relevant traceback:

Code: Select all

0x40097ba0: invoke_abort at panic.c:?
0x40097f11: abort at ??:?
0x4008685b: lock_acquire_generic at locks.c:?
0x40086955: _lock_acquire at ??:?
0x40086766: _gettimeofday_r$part$0 at time.c:?
0x400867b7: _gettimeofday_r at ??:?
0x4017b34d: gettimeofday at /home/jeroen/esp8266/esp32/newlib_xtensa-2.2.0-bin/newlib_xtensa-2.2.0/xtensa-esp32-elf/newlib/libc/
syscalls/../../../.././newlib/libc/syscalls/sysgettod.c:12
0x40081855: unlock_spi_from_isr at modqueue.c:?
0x400828c5: gpio_isr_loop at gpio.c:?
0x4008292d: gpio_intr_service at gpio.c:?
0x400863f9: _xt_lowint1 at xtensa_vectors.o:?
0x4011bc1d: set_boot_time at time.c:?
0x4008676c: _gettimeofday_r$part$0 at time.c:?
0x400867b7: _gettimeofday_r at ??:?
0x4017b34d: gettimeofday at /home/jeroen/esp8266/esp32/newlib_xtensa-2.2.0-bin/newlib_xtensa-2.2.0/xtensa-esp32-elf/newlib/libc/
syscalls/../../../.././newlib/libc/syscalls/sysgettod.c:12
0x400f7c70: time_time at modutime.c:?
so what is the way to get the time in a isr.

I already tried to just use esp_timer_get_time() inside the isr and assembly the time later on myself. but this seems especially nontrivial as all the required functions in https://github.com/espressif/esp-idf/bl ... lib/time.c are not public accessible...

Do I really have to go down that route or do you see an easier solution?

this.wiederkehr
Posts: 15
Joined: Thu Jul 13, 2017 8:04 am

Re: gettimeofday from isr crashes

Postby this.wiederkehr » Tue Oct 27, 2020 6:30 pm

Well this is quite an old question. But I still could not figure out how to do this in a nontrivial way.

I do use the timer to get a timestamp in the isr and assemble the time later by using the timer and gettimeofday function. By subtracting the difference in the timer value from the gettimeofday I get to the time the isr was called.

The problem here is that time and gettimeofday should be called as close in time as they can. BUT, gettimeofday requires the scheduler to be active so vTaskSuspendAll does not work in order to prevent a task switch inbetween the two function calls.
I now do raise task priority before the get_timer and gettimeofday function call and reset it thereafter but honestly this should be easier.

Anything I could do about this?

mikemoy
Posts: 627
Joined: Fri Jan 12, 2018 9:10 pm

Re: gettimeofday from isr crashes

Postby mikemoy » Wed Oct 28, 2020 3:08 am

I am not trying to be rude. But it sounds like you need a bit more understanding of how an RTOS works.

Your ISR should only be setting a BIT value there. You want to get in and out of an ISR as fast as you can.

Meanwhile, You should have a thread running just waiting for the bit you set in the ISR to change condition.
Take a look at the GPIO example to get more information on how to put all that together.
https://github.com/espressif/esp-idf/bl ... ple_main.c

this.wiederkehr
Posts: 15
Joined: Thu Jul 13, 2017 8:04 am

Re: gettimeofday from isr crashes

Postby this.wiederkehr » Mon Nov 02, 2020 3:51 pm

hey mikemoy

well, I don't care if the answers I am getting are rude or not as long as it helps me to get forward...

So, let me rephrase the question as I don't see how the example you linked would help me:

The original question I have:
How can I get the exact timeofday of a pin (gpio interrupt) change?


As simple as it sounds, here is what i discovered:

- if i unlock a task from the isr, there is already the time difference from the isr to the task being scheduled to run. Even with high priority tasks the time difference sometimes exceeds a few ms (isr and task are running on app cpu, pro cpu runs wlan in station mode, and lwip) Not sure why this difference is present. Yes, the task runs at high priority (21), and is released directly from the isr (portYIELD_FROM_ISR()). There is only the ipc task on app core which has higher prio. I do have a low prio task (prio 1)which does quite a lot, i2c, spi, emmc access ... tough.

- using gettimeofday inside an isr does not work. as the offset from timeofday to the timer is protected by a lock which has to be taken. taking the lock only works if the rtos scheduler is running. Do you really consider the few cpu cycles to execute gettimeofday to much to be run inside an isr?

- What I am doing now is: I use esp_timer_get_time() inside the isr and set that value with xTaskNotifyFromISR which unlocks a task and supplies the timer value the isr was unlocked. Inside the task I call esp_timer_get_time() and gettimeofday and remove the difference of the timer values inside the isr and in the taks from gettimeofday. However there is still an issue: If the task is suspended between the call to esp_timer_get_time and gettimeofday as that time difference can not be accounted for... Well, the obvious solution to this is to wrap these two function calls into vTaskSuspendAll and xTaskResumeAll. However that does not work either as, the scheduler has to be running to call gettimeofday.

What do you think. Am I that wrong in the way to achieve the original question. If yes, I'll be thankful about any information about how to achieve the original question.

BR

This

Who is online

Users browsing this forum: No registered users and 111 guests