Idle hook from freeRtos

Hi all,

I need to have an idle hook to do some background jobs.
So I implemented the function ‘void vApplicationIdleHook( void )’ and now I want to set the configuration param ‘configUSE_IDLE_HOOK’ to 1.
I tryed the following two possibilities in the mos.yaml file without success:

build_vars:
configUSE_IDLE_HOOK: 1

cdefs:
configUSE_IDLE_HOOK: 1

What is the correct way?

Thanks for your help
Rolf

Per the documentation,

cdefs:
  configUSE_IDLE_HOOK: 1

Should translate to:

cflags:
  - "-DconfigUSE_IDLE_HOOK=1"
cxxflags:
  - "-DconfigUSE_IDLE_HOOK=1"

Perhaps the indentation is required? What failure or error do you see?

UPDATE:

I ran across something similar to this while looking for something else. Make sure the indent is there. Also, you might try:

build_vars:
   APP_CFLAGS: "-Dno-error=configUSE_IDLE_HOOK=1"

or

build_vars:
   APP_CXXFLAGS: "-Dno-error=configUSE_IDLE_HOOK=1"

Depending of course if its C or C++.

Hi Jim,
Thanks for your inputs! Unfortunately, I was not successul by applying them.
I tried the following four options:

build_vars:
configUSE_IDLE_HOOK: 1

cdefs:
configUSE_IDLE_HOOK: 1

cflags:

  • “-DconfigUSE_IDLE_HOOK=1”

build_vars:
APP_CFLAGS: “-Dno-error=configUSE_IDLE_HOOK=1”

The first tree were compilable but unsuccessful, the fourth was not compilable.

Then, I tried all of the four with just ‘USE_IDLE_HOOK’ (without ‘config’), with the same result.

Could the problem be in the definition of the hook itself (void vApplicationIdleHook(void)) ?
I found it in the freertos documentation .

Thanks a lot for more information!

Regards
Rolf

Oh, I was under the impression that you wrote code checking the macro. When I get a chance later I’ll see how the FreeRTOS idle hook is supposed to work. Remember that Mongoose uses a modified version of FreeRTOS, but I can’t say I know exactly what was changed.

I suggest that you test if the macro is getting defined by writing a little code in the init function, at least that way you’ll know if you’re defining it correctly.

Hi Jim,
I checked the macro itself and its ok. But the hook is still never called … :-/
Thanks for any help!
Rolf

I’m updating my last post because it won’t work. The info here is tested on an ESP32.

The Espressif API modified the FreeRTOS hooks to be compatible with multicore CPUs, allowing you to specify a separate idle task for each core. Under this system, you have to register your idle task function. The original idle hook has been modified to call the hooks you register, and you can not write:

void vApplicationIdleHook(void)

If you do, when you compile you’ll get an error indication that this function has been redefined by your code. This example is written for the idle hook, but the tick hook has similar changes.

All this is (mostly) explained here:

https://demo-dijiudu.readthedocs.io/en/latest/api-reference/system/hooks.html

I tested this, and here is what you need to do to make this work:

In your mos.yml file:

build_vars:
  ESP_IDF_SDKCONFIG_OPTS: >
    "${build_vars.ESP_IDF_SDKCONFIG_OPTS}"
     CONFIG_FREERTOS_LEGACY_HOOKS=1

In your code, you’ll need these header files:

#include "FreeRTOS.h"
#include "esp_freertos_hooks.h"

Then you’ll need to write your idle hook callback:

static long int ctr;

bool myTestIdleHook(void)
{
  ctr++;
  return true;
}

The callback should return true if it should be called by the idle hook once per interrupt (or FreeRTOS tick), and return false if it should be called repeatedly as fast as possible by the idle hook.

Finally, register the idle task callback. There are a bunch of functions available for this, see the link above for complete details. This function registers the callback for the core on which it is called:

  if(esp_register_freertos_idle_hook(myTestIdleHook) == ESP_OK)
    LOG(LL_INFO, ("Idle hook successfully registered."));
  else
    LOG(LL_INFO, ("Idle hook registration failed."));

Check the link above for return information. It took a bit of research to figure this out, so I hope its useful to someone.

Cheers!

Hi Jim,
Thanks for this detailed explanation. I will check it out next week!
Kind regards
Rolf

Hello Jim,

I checked this matter. It worked correctly for the ESP32. Thanks for your inputs.
For the ESP8266 I did not succeed, unfortunately. Is there a solution, too?

Kind regards
Rolf

Hi Rolf… Sorry, I have no idea, I don’t have an ESP8266.