Page 1 of 1

How to skip a function if timeout elapsed in background

Posted: Tue Jul 26, 2022 9:29 am
by yonip94
Hello guys!
My name is Yoni and i am using ESP32 DevKitc MCU, esp.idf, Linux environment.

I wonder if you have such a way to skip a function if timeout elapsed in background.
Maybe the "CALL_FUNC_WITH_DELAY" macro idea that i wrote in the example below is already implemented by your team in another name/way, or something similar that can handle this?

The example:

#define CPU_TICKS_TO_SKIP_FUNC ( (uint32_t)1000 )
void foo1 (void);

void main(void)
{
----CALL_FUNC_WITH_DELAY(foo1(),CPU_TICKS_TO_SKIP_FUNC);
----printf("pass - timeout elapsed so foo1 was skipped");
}

void foo1 (void)
{
----while(1);
}

* foo1 can contain a lot of different functions calls + delays inside each
Thanks a lot,
Yoni

Re: How to skip a function if timeout elapsed in background

Posted: Wed Jul 27, 2022 10:20 am
by Zeni241
You can do it very easily if you use FreeRTOS.

Re: How to skip a function if timeout elapsed in background

Posted: Thu Jul 28, 2022 1:10 am
by ESP_Sprite
Or even without:

Code: Select all

#include "esp_timer.h"

int64_t timeout_us=1000*1000;
int64_t timeout_ts=esp_timer_get_time()+timeout_us;
if (esp_timer_get_time()<timeout_ts) do_function_1();
if (esp_timer_get_time()<timeout_ts) do_function_2();
if (esp_timer_get_time()<timeout_ts) do_function_3();
if (esp_timer_get_time()<timeout_ts) do_function_4();

Re: How to skip a function if timeout elapsed in background

Posted: Tue Aug 02, 2022 4:08 pm
by yonip94
Zeni241 - thanks a lot for your answer, i have searched on it but i didnt find any function to help

i found very "close" function that is doing the not of the function i need:
xTimerPendFunctionCall()
but this function will be performed after the time i define - so its not this.
i wonder if you can just point me any of the freertos functions that can help to solve the problem.
Thanks a lot Zeni241!
Yoni

Re: How to skip a function if timeout elapsed in background

Posted: Tue Aug 02, 2022 4:20 pm
by yonip94
ESP_Sprite, very thanks for your answer!
My question was that if i'm already INSIDE a function, skip from performing it if the defined timeout ticks elapsed.

ill give you another example:
imagine you call a function that should sample a sensor every 1ms, but the sensor is not giving you answer even after 1second
so you "wait" in while loop too much and not exit by timeout (of curse exit by timeout is needed, but assume i do not use break the loop with timeout because i want something general to solve it for some different function stuffs).
i wonder if there is a way to skip the function so the pc (program counter) will perform the next line of code after the function call.

Thanks again

Re: How to skip a function if timeout elapsed in background

Posted: Wed Aug 03, 2022 1:19 am
by ESP_Sprite
Well, technically you could start whatever you need to do in a task, then delete that task on a timeout. However, I think it may not be a good idea, as you're effectively killing code that may still have memory allocated and resources claimed and in an unknown state. (For instance, a read from a device may lock a mutex in the driver, and killing the task will never unlock that.) Cleaning that up, if at all possible, is not an easy thing to do.

Re: How to skip a function if timeout elapsed in background

Posted: Wed Aug 03, 2022 7:22 am
by yonip94
ESP_Sprite - thanks for your answer!
thanks for this advice, i understand why it can be risky to use it.
maybe something else that is not using freertos? even some of the main esp32 api's?
as you know, is there anything that can support it?
thanks again for your interest in my questions!
Yoni

Re: How to skip a function if timeout elapsed in background

Posted: Wed Aug 03, 2022 7:31 am
by ESP_Sprite
As I said - a solution would be to put the stuff that may timeout in its own task, then have some code outside that task wait until the timeout has passed and then do an xTaskDelete() to stop whatever is taking so long. As long as whatever you put in the task never ever allocates resources or gets ownership of anything (including any code that task calls, like drivers, the TCP/IP stack etc), that should work. That limits it to a very small subset of 'things that may time out' though.

Re: How to skip a function if timeout elapsed in background

Posted: Wed Aug 03, 2022 8:07 am
by yonip94
ESP_Sprite, very thanks! ill give it a try :)