FlexiTimer2 for Arduino STM32 board

Hi everyone,

I want to port my existing project from Arduino Mega to a Arduino STM32 board (Blue Pill clone).

My project use the FlexiTimer2 library to provide an accurate timing for my need, but it does not compatible with my Blue Pill clone.

After some examinations to the library included in the STM32 core, it seems that there is a timer.c & timer.h file that can be useful, and these 2 function call can do what I need,

/**

  • @brief This function will set the timer to generate pulse in interrupt mode with a particular duty cycle
  • @param timer_id : timer_id_e
  • @param period : timer period in microseconds
  • @param pulseWidth : pulse width in microseconds
  • @param irqHandle : interrupt routine to call
  • @retval None
    */
    void TimerPulseInit(stimer_t *obj, uint16_t period, uint16_t pulseWidth, void (irqHandle)(stimer_t, uint32_t))
    {
    TIM_OC_InitTypeDef sConfig = {};
    TIM_HandleTypeDef *handle = &(obj->handle);

obj->timer = TIMER_SERVO;

//min pulse = 1us - max pulse = 65535us
handle->Instance = obj->timer;
handle->Init.Period = period;
handle->Init.Prescaler = (uint32_t)(getTimerClkFreq(obj->timer) / (1000000)) - 1;
handle->Init.ClockDivision = 0;
handle->Init.CounterMode = TIM_COUNTERMODE_UP;
#if !defined(STM32L0xx) && !defined(STM32L1xx)
handle->Init.RepetitionCounter = 0;
#endif
obj->irqHandleOC = irqHandle;

sConfig.OCMode = TIM_OCMODE_TIMING;
sConfig.Pulse = pulseWidth;
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfig.OCFastMode = TIM_OCFAST_DISABLE;
#if !defined(STM32L0xx) && !defined(STM32L1xx)
sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
#endif
HAL_NVIC_SetPriority(getTimerIrq(obj->timer), 14, 0);
HAL_NVIC_EnableIRQ(getTimerIrq(obj->timer));

if(HAL_TIM_OC_Init(handle) != HAL_OK) return;

if(HAL_TIM_OC_ConfigChannel(handle, &sConfig, TIM_CHANNEL_1) != HAL_OK) return;
if(HAL_TIM_OC_Start_IT(handle, TIM_CHANNEL_1) != HAL_OK) return;
}

/**

  • @brief This function will set the timer to the required value
  • @param timer_id : timer_id_e
  • @param period : Timer period in milliseconds
  • @param prescaler : clock divider
  • @retval None
    */
    void TimerHandleInit(stimer_t *obj, uint16_t period, uint16_t prescaler)
    {
    if(obj == NULL)
    return;

TIM_HandleTypeDef *handle = &(obj->handle);

handle->Instance = obj->timer;
handle->Init.Prescaler = prescaler;
handle->Init.CounterMode = TIM_COUNTERMODE_UP;
handle->Init.Period = period;
handle->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
#if !defined(STM32L0xx) && !defined(STM32L1xx)
handle->Init.RepetitionCounter = 0x0000;
#endif
if(HAL_TIM_Base_Init(handle) != HAL_OK){
return;
}

if(HAL_TIM_Base_Start_IT(handle) != HAL_OK){
return;
}
}

I’m not quite understand how to use it, but I tried these codes :

#include <timer.h>
long counting ;

void setup()
{
Serial.begin(19200);

stimer_t _timer;
TimerPulseInit(&_timer, 500, 40000, timer_run);
TimerHandleInit(&_timer, 100, 10);

}

void loop() {
if(counting%100==0){
Serial.println(counting/100);
}
}

void timer_run(void) {
counting=counting+1 ;
}

It give the following errors :

J:\Arduino_Project\STM32\Timer_Interurpt\Timer_Interurpt.ino: In function ‘void setup()’:

Timer_Interurpt:14: error: invalid conversion from ‘void ()()’ to 'void ()(stimer_t*, uint32_t) {aka void ()(timer_s, long unsigned int)}’ [-fpermissive]

TimerPulseInit(&_timer, 500, 40000, timer_run);

^

In file included from C:\Users\Home User\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.4.0\cores\arduino/board.h:20:0,

from C:\Users\Home User\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.4.0\cores\arduino/wiring.h:41,

from C:\Users\Home User\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.4.0\cores\arduino/Arduino.h:32,

from C:\Users\HOMEUS~1\AppData\Local\Temp\arduino_build_912893\sketch\Timer_Interurpt.ino.cpp:1:

C:\Users\Home User\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.4.0\cores\arduino/stm32/timer.h:190:6: note: initializing argument 4 of ‘void TimerPulseInit(stimer_t*, uint16_t, uint16_t, void ()(stimer_t, uint32_t))’

void TimerPulseInit(stimer_t *obj, uint16_t period, uint16_t pulseWidth, void (*irqHandle)(stimer_t *, uint32_t));

^~~~~~~~~~~~~~

exit status 1
invalid conversion from ‘void ()()’ to 'void ()(stimer_t*, uint32_t) {aka void ()(timer_s, long unsigned int)}’ [-fpermissive]

Can anyone help ?