f ckx
f ckx

Reputation: 47

Problem with creation of freeRTOS timer in ESP-IDF

I use ESP-IDF 4.2 and want to create a freeRTOS timer. My code is similar to what I have used successfully before but now I get a compilation error that I can't resolve. My code is based on the example in the ESP-IDF documentation on the xTimerCreate function. In the documentation an array of timers is created, whereas I want to create only one.

Here is my code:

void myTimerCallback(void){
    std::cout << "test timer callback" << std::endl; 
};

TimerHandle_t create_freeRTOSTimer(){
    
  TimerHandle_t timerHandle;
  int32_t Id = 0;
  int durationTicks = 100; 
  
  timerHandle  = xTimerCreate(  "Timer",          // Just a text name, not used by the kernel.
                                durationTicks,    // The timer period in ticks.
                                pdTRUE,           // The timer will auto-reload itself when it expires.
                                ( void * ) Id,    // unique id.
                                myTimerCallback   // Callback when timer expires.
                             );  
    return timerHandle;  
};

This is the compiler response:

error: invalid conversion from 'void ()()' to 'TimerCallbackFunction_t' {aka 'void ()(void*)'} [-fpermissive] myTimerCallback // Each timer calls the same callback when it expires. ^~~~~~~~~~~~~~~ In file included from ../components/NiCMidi/src/../include/timer.h:46, from ../components/NiCMidi/src/timer.cpp:26: C:/Users/Fred/esp-idf/components/freertos/include/freertos/timers.h:267:33: note: initializing argument 5 of 'void* xTimerCreate(const char*, TickType_t, UBaseType_t, void*, TimerCallbackFunction_t)' TimerCallbackFunction_t pxCallbackFunction ) PRIVILEGED_FUNCTION; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */

For testing I replaced the myTimerCallback in the call of xTimerCreate by NULL. Then the compiler does not complain.

Upvotes: 0

Views: 1057

Answers (1)

f ckx
f ckx

Reputation: 47

Oops, I forgot to use a parameter in the definition of the callback function!

When I use void myTimerCallback(TimerHandle_t pxTimer) for the definition, everything is OK!

Upvotes: 0

Related Questions