just like previous example, this program create two task from different functions but this time we block each task with the function vTaskDelay to create a delay
Code Example:
#include "bsp.h"
/*Functions to act as tasks*/
static void vTask1( void *parameters );
static void vTask2( void *parameters );
int main( void )
{
HAL_Init( ); /*Inicializamos la librerÃa*/
/*enable RTT and system view*/
SEGGER_SYSVIEW_Conf( );
SEGGER_SYSVIEW_Start( );
/*register first task using the function vTask1 with (128*4) bytes de stack, no parameters
priority of 1 and no task handler registered*/
xTaskCreate( vTask1, "Task1", 128u, NULL, 1u, NULL );
/*register second task using the function vTask2 with (128*4) bytes de stack, no parameters
priority of 1 and no task handler registered*/
xTaskCreate( vTask2, "Task2", 128u, NULL, 1u, NULL );
/*run the RTOS scheduler*/
vTaskStartScheduler( );
return 0u;
}
/*Simple task that initialize pin C0 as output and toggle the pin state every 1000ms*/
static void vTask1( void *parameters )
{
GPIO_InitTypeDef GPIO_InitStruct; /* GPIO PIN Handler */
__HAL_RCC_GPIOC_CLK_ENABLE( ); /* Enable the clock for the port C */
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; /* Configure the Pin as output */
GPIO_InitStruct.Pull = GPIO_NOPULL; /* Configure pin as no pull up - no pull down */
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; /* Configure frequency of pin as High */
GPIO_InitStruct.Pin = GPIO_PIN_0; /* Configure PIN 0 */
HAL_GPIO_Init( GPIOC, &GPIO_InitStruct ); /* Initialize the GPIO */
for( ; ; )
{
HAL_GPIO_TogglePin( GPIOC, GPIO_PIN_0 ); /* change the state of pin 0, port c */
vTaskDelay( 1000u ); /*block task for 1000 ticks*/
}
}
/*Simple task that initialize pin C1 as output and toggle the pin state every 2000ms*/
static void vTask2( void *parameters )
{
GPIO_InitTypeDef GPIO_InitStruct; /* GPIO PIN Handler */
__HAL_RCC_GPIOC_CLK_ENABLE( ); /* Enable the clock for the port C */
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; /* Configure the Pin as output */
GPIO_InitStruct.Pull = GPIO_NOPULL; /* Configure pin as no pull up - no pull down */
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; /* Configure frequency of pin as High */
GPIO_InitStruct.Pin = GPIO_PIN_1; /* Configure PIN 1 */
HAL_GPIO_Init( GPIOC, &GPIO_InitStruct ); /* Initialize the GPIO */
for( ; ; )
{
HAL_GPIO_TogglePin( GPIOC, GPIO_PIN_1 ); /* change the state of pin 1, port c */
vTaskDelay( 2000u ); /*block task for 2000 ticks*/
}
}
SystemView Output
Now the tasks are not consuming unnecessary time processor, each task is blocked before execute for a specified time. The tasks is more visible and unsderstanble on time line.
|
|
When a second is elapsed, Just Task 1 is executed changing the LED 0 state and blocked again by 1 second.
| |
When another second elapsed, both task are executed changing the LED state like at the beginning.
|