In this example, a Timer is created to blink an LED. The period of Timer can be modified by 3 buttons.

A task is created to check if a button was pressed, if one of them is pressed, then execute the function xTimerChangePeriod() that is in charge of changing the timer period. Each button has an established period to change in the configured timer. The timer just executes the Callback function vTimerCallback() which contain the function to shift LED Satate.

Code Example:

#include "bsp.h"

#define TIMER_ID       (void *const)1                   /* ID of Timer */
TimerHandle_t xTimer;                                   /* Handler of Timer */

static void TaskReadButton( void* parameter );          /* Task to Read buttons to Change Period Timer */

static void vTimerCallback( TimerHandle_t pxTimer );    /* Timer function callback to blink LED */

int main( void )
{
    HAL_Init();

    SEGGER_SYSVIEW_Conf();
    SEGGER_SYSVIEW_Start(); 

    xTimer = xTimerCreate( "Timer", 250, pdTRUE, TIMER_ID, vTimerCallback );    /* Timer creation with a period of 250ms */
    xTimerStart( xTimer, 0 );                                                   /* Start Timer */

    xTaskCreate( TaskReadButton, "Task_Read_Button", 128u, NULL, 1u, NULL );    /* Task Creation */

    vTaskStartScheduler();                                                      /* start Kernel execution */
}

/* Function to read button and change Timer period */
static void TaskReadButton( void* parameter )
{
    UNUSED(parameter);

    for(;;)
    {
        if( HAL_GPIO_ReadPin( GPIOB, GPIO_PIN_5 ) == GPIO_PIN_RESET )       /* Check if button 5 was preseed */
        {
            xTimerChangePeriod( xTimer, 100, 0 );                           /* Change the Timer period to 100ms */
            SEGGER_SYSVIEW_PrintfHost( "Change period to 100ms" );
        }
        else if ( HAL_GPIO_ReadPin( GPIOB, GPIO_PIN_7 ) == GPIO_PIN_RESET ) /* Check if button 7 was preseed */
        {
            xTimerChangePeriod( xTimer, 500, 0 );                           /* Change the Timer period to 500ms */
            SEGGER_SYSVIEW_PrintfHost( "Change period to 500ms" );
        }
        else if ( HAL_GPIO_ReadPin( GPIOB, GPIO_PIN_15 ) == GPIO_PIN_RESET )/* Check if button 15 was preseed */
        {
            xTimerChangePeriod( xTimer, 1500, 0 );                          /* Change the Timer period to 1500ms */
            SEGGER_SYSVIEW_PrintfHost( "Change period to 1500ms" );
        }

        vTaskDelay( 40 / portTICK_PERIOD_MS );                              /* wait 40ms to read if button is pressed */
    }
}

/* Callback timer function to blink a LED */
void vTimerCallback( TimerHandle_t pxTimer )
{
    HAL_GPIO_TogglePin( GPIOC, GPIO_PIN_0 );
    SEGGER_SYSVIEW_PrintfHost( "LED %d", HAL_GPIO_ReadPin( GPIOC, GPIO_PIN_0 ));
}

SystemView output:

Terminal shows the behavior of the Timer callback and task execution.

Document

When the application starts, the default period of the timer when is created is 250 ticks. Observe that each message on the Terminal has a time space of 250 ticks.

When the first button is pressed, the task prints a message on the terminal to indicate the new period set on the timer.
Again, observe the time space between each message of the callback timer, now the messages are sent every 100 ticks.

The second button is pressed, the task detects the button and prints a message to indicate that now the terminal period has changed to 500 ticks.
The next messages on the terminal of the timer callback now are sent every 500 ticks.

Finally, the third button is pressed. Similar to the other 2 buttons this prints the message of the new timer period to change.
Notice that the separation between messages is now the new ticks configured ( 1500 ticks ).

TimeLine:

Document

On the timeline, we can observe that "TaskReadButton()" executes each 40ms and one of them detects the pressed button.
Task "TaskReadButton()" changes the period of the Timer, observe in the next image that the task prints the message of the new period set. Now timer period is configured at 100ms.



Button 1 was pressed, the period of the timer changed thus the callback must be called each 100ms. Compare the timeline of each image, the callback is executing the configured period.



Similar behavior when the second button is pressed, the new period is configured in the timer.
compare again, now timer callback is executed every 500ms.



Finally, the third button is pressed, now the Timer period was changed to 1500ms.
Just like the other 2 buttons, notice that the callback is executed now in the period set.