Let’s create another example, this time using 2 different callbacks in charge of toggle 2 leds. Remember each timer should have its own control structure and in this case its own callback.

#include "bsp.h"

static OS_TIMER Timer0, Timer1;   //* Timer control structures

static void Callback0(void);     //* Timer 0 callback
static void Callback1(void);     //* Timer 1 callback

int main( void ){

    OS_Init();      // Initialize embOS (must be first)
    HAL_Init();
    OS_InitHW();   // Initialize Hardware for embOS

    GPIO_InitTypeDef GPIO_InitStruct;

    __HAL_RCC_GPIOC_CLK_ENABLE( );

    GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull  = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Pin   = GPIO_PIN_0 | GPIO_PIN_2 | GPIO_PIN_3;
    HAL_GPIO_Init( GPIOC, &GPIO_InitStruct );

    OS_TIMER_CREATE(&Timer0, Callback0, 200u); //* Using macro to create timer 0
    OS_TIMER_CREATE(&Timer1, Callback1, 500u); //* Using macro to create timer 1

    OS_Start();     // Start multitasking

    return 0u;
}

//* Callback 0 implementation
static void Callback0(void){
  SEGGER_SYSVIEW_PrintfHost( "Callback 0\n" );
  HAL_GPIO_TogglePin( GPIOC, GPIO_PIN_0 );
  OS_TIMER_Restart(&Timer0);                //* Function to restart the timer
}

//* Callback 1 implementation
static void Callback1(void){
  SEGGER_SYSVIEW_PrintfHost( "Callback 1\n" );
  HAL_GPIO_TogglePin( GPIOC, GPIO_PIN_1 );
  OS_TIMER_Restart(&Timer1);                //* Function to restart the timer
}

If we want use a timer in periodic mode it is as simple as using “OS_TIMER_Restart()” function within the callback. In this way the timer will be recharged with its original timeout .

SystemView

Build and flash the program and run systemview. First, let's look at the entire recording on the timeline. As you can see, the timers are running according the established time.

Looking closer you can notice how the timers are running.

Now in the event list you can see Now in the event list you can see the behavior in more detail.