How to skip a function if timeout elapsed in background

yonip94
Posts: 15
Joined: Mon Jul 11, 2022 12:42 pm

How to skip a function if timeout elapsed in background

Postby yonip94 » Tue Jul 26, 2022 9:29 am

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
Last edited by yonip94 on Wed Aug 03, 2022 6:55 pm, edited 1 time in total.

Zeni241
Posts: 86
Joined: Tue Nov 20, 2018 4:28 am

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

Postby Zeni241 » Wed Jul 27, 2022 10:20 am

You can do it very easily if you use FreeRTOS.

ESP_Sprite
Posts: 9308
Joined: Thu Nov 26, 2015 4:08 am

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

Postby ESP_Sprite » Thu Jul 28, 2022 1:10 am

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();

yonip94
Posts: 15
Joined: Mon Jul 11, 2022 12:42 pm

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

Postby yonip94 » Tue Aug 02, 2022 4:08 pm

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

yonip94
Posts: 15
Joined: Mon Jul 11, 2022 12:42 pm

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

Postby yonip94 » Tue Aug 02, 2022 4:20 pm

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

ESP_Sprite
Posts: 9308
Joined: Thu Nov 26, 2015 4:08 am

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

Postby ESP_Sprite » Wed Aug 03, 2022 1:19 am

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.

yonip94
Posts: 15
Joined: Mon Jul 11, 2022 12:42 pm

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

Postby yonip94 » Wed Aug 03, 2022 7:22 am

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

ESP_Sprite
Posts: 9308
Joined: Thu Nov 26, 2015 4:08 am

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

Postby ESP_Sprite » Wed Aug 03, 2022 7:31 am

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.

yonip94
Posts: 15
Joined: Mon Jul 11, 2022 12:42 pm

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

Postby yonip94 » Wed Aug 03, 2022 8:07 am

ESP_Sprite, very thanks! ill give it a try :)

Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot] and 130 guests