FreeRTOS Problem

sadegh916 wrote on Thursday, July 19, 2018:

Dear friends, Hi
I have a simple code that turn on two LED in two seprated task. It doesn’t work. I debuge an i found that only one task execute. In fact execute the tast which was made first.

please help me. I’m new in FreeRTOS

/* Includes ------------------------------------------------------------------*/
#include "stm32f4x7_eth.h"
#include "netconf.h"
#include "main.h"
#include "tcpip.h"
#include "Serial.h"
#include "LED.h"
#include "task.h"

             

/*--------------- Tasks Priority -------------*/
#define MAIN_TASK_PRIO   ( tskIDLE_PRIORITY + 1 )
#define LED1_TASK_PRIO    ( tskIDLE_PRIORITY + 3)
#define LED2_TASK_PRIO    ( tskIDLE_PRIORITY + 4)

 
/* Private function prototypes -----------------------------------------------*/
void Main_task(void * pvParameters);
void ToggleLed1(void * pvParameters);
void ToggleLed2(void * pvParameters);

xTaskHandle ToggleLedHandle1;
xTaskHandle ToggleLedHandle2;

int main(void)
{
  

 /* Configures the priority grouping: 4 bits pre-emption priority */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);

  /* Init task */
  xTaskCreate(Main_task, (int8_t *)"Main", configMINIMAL_STACK_SIZE * 2, NULL,MAIN_TASK_PRIO, NULL);
  
  /* Start scheduler */
  vTaskStartScheduler();

  /* We should never get here as control is now taken by the scheduler */
  for( ;; );
}

void Main_task(void * pvParameters)
{


  SER_Init();
  LED_Init();
  
  

  /* Start toogleLed4 task : Toggle LED4  every 250ms */
  xTaskCreateStatic(ToggleLed1, (int8_t *)"LED1", configMINIMAL_STACK_SIZE, NULL, LED1_TASK_PRIO, &ToggleLedHandle1);
  xTaskCreateStatic(ToggleLed2, (int8_t *)"LED2", configMINIMAL_STACK_SIZE, NULL, LED2_TASK_PRIO, &ToggleLedHandle2);


  for( ;; )
  {
    vTaskDelete(NULL);
  }
}

/**
  * @brief  Toggle Led4 task
  * @param  pvParameters not used
  * @retval None
  */
void ToggleLed1(void * pvParameters)
{
  
      while(1){
            LED_On(1);
              }
}

void ToggleLed2(void * pvParameters)
{
  
      while(1){
            LED_On(0);
              }
}

rtel wrote on Thursday, July 19, 2018:

Neither task enters the Blocked state, so are always able to run, that means the scheduler will always select the task that has the highest priority, starving the task that has the lowest priority of any CPU time. I would recommend reading through the first chapters of the free book you can download here: https://www.freertos.org/Documentation/RTOS_book.html They take you through the scheduling algorithm, with some diagrams etc. that will show the behaviour you are observing. Hope this helps.

sadegh916 wrote on Thursday, July 19, 2018:

Thanks for your quick answer
I’m using FreeRTOS V7.3…
In my program task2(ToggleLed2) has higher priority but task1(ToggleLed1) is always running… it confuse me…

xTaskCreate(ToggleLed2, (int8_t *)“LED2”, configMINIMAL_STACK_SIZE, NULL, LED2_TASK_PRIO, &ToggleLedHandle2);
xTaskCreate(ToggleLed1, (int8_t *)“LED1”, configMINIMAL_STACK_SIZE, NULL, LED1_TASK_PRIO, &ToggleLedHandle1);

in this case ToggleLed2 task is always running and

xTaskCreate(ToggleLed1, (int8_t *)“LED1”, configMINIMAL_STACK_SIZE, NULL, LED1_TASK_PRIO, &ToggleLedHandle1);
xTaskCreate(ToggleLed2, (int8_t *)“LED2”, configMINIMAL_STACK_SIZE, NULL, LED2_TASK_PRIO, &ToggleLedHandle2);

in this case ToggleLed1 task is always running .

xcubbi wrote on Thursday, July 26, 2018:

As Richard said, you should read the freeRTOS documentation and something general about the usage of tasks. But I’ll give you a little hint. One solution could be working with delays (e.g. vTaskDelay).