This program will start a software timer once a button is press.
#include "bsp.h"
static OS_TIMER Timer;
static OS_TASK Task;
static int TaskStack[128];
static void Callback(void);
static void TaskDemo(void);
int main( void )
{
OS_Init(); // Initialize embOS (must be first)
HAL_Init();
OS_InitHW(); // Initialize Hardware for embOS
OS_TASK_CREATE( &Task, "Task Demo", 100, TaskDemo, TaskStack ); //* creating a task just to init the GPIOs
OS_TIMER_Create(&Timer, Callback, 400u); //* Creating the software timer
OS_Start(); // Start multitasking
return 0u;
}
/* This task initializes the corresponding GPIOs where the button and LED are and then in charge to check if the button is press*/
static void TaskDemo(void)
{
__HAL_RCC_GPIOC_CLK_ENABLE( ); //* Enabling the GPIO C clock
__HAL_RCC_GPIOB_CLK_ENABLE( ); //* Enabling the GPIO B clock
GPIO_InitTypeDef GPIO_InitStruct; //* GPIO Initialization struct
/* Configuring the GPIO where the button is*/
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; //* interrup in falling edge mode
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pin = GPIO_PIN_5;
HAL_GPIO_Init( GPIOB, &GPIO_InitStruct ); //* Initializing GPIO B - Pin 5 to use the button as interrupt
/* Configuring the GPIO where the LED is */
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; //* interrup in falling edge mode
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_2;
HAL_GPIO_Init( GPIOC, &GPIO_InitStruct ); //* Initializing GPIO C - Pin 0 to use it in our callback
for( ; ; )
{
if( HAL_GPIO_ReadPin( GPIOB, GPIO_PIN_5 ) == RESET )
{
SEGGER_SYSVIEW_PrintfHost( "Button Pressed\n" );
OS_TIMER_Restart( &Timer ); //* If the button is press the timer is restarted
}
OS_TASK_Delay( 80u );
}
}
static void Callback(void)
{
SEGGER_SYSVIEW_PrintfHost( "Callback\n" );
HAL_GPIO_TogglePin( GPIOC, GPIO_PIN_0 ); //* Callback to toggle the pin
}
SystemView
Build and flash the program and run systemview. First, let's look at the entire recording on the timeline. Every time I press the button the timer is restarted.

Looking at the event list and terminal window you can see the timer behavior in more detail. You can see how every time I press the button the timer is restarted and after 400 milliseconds the callback function is executed.
