Create 2 tasks with 1 task function, but only 1 task is running

Create 2 tasks with 1 task function and 2 parameters. But It seems only the 1st task is running.
The source-code like Below:

The Board Task is running OK,
The SIDE_A Task is running OK , but the SIDE_B task seems not running, And cannot reached.

HELP! HELP! HELP!

THANKS!

 #define	START_TASK_PRIORITY		1
#define START_STACK_SIZE			256
TaskHandle_t StartTaskHandler;
void Start_Task( void *pvPara );

// Board Task
#define	BOARD_TASK_PRIORITY		3
#define   BOARD_STACK_SIZE			1024
TaskHandle_t BOARD_TaskHandler;

// FP Task
#define	FP_TASK_PRIORITY			2
#define   FP_STACK_SIZE			1024
TaskHandle_t FP_TaskHandler[4];


void FP_Task( void *pvPara )
{
	FP_SIDE_enum side;
	
	side = *(FP_SIDE_enum *)pvPara;
	
	if( side == SIDE_A )
	{
		while( 1 )
		{
                        // 
			vTaskDelay( pdMS_TO_TICKS( 100 ));
		}
	}
	else if( side == SIDE_B )
	{
		while( 1 )
		{
                        //
			vTaskDelay( pdMS_TO_TICKS( 100 ));
		}
	}
	else	// Task_C
	{
		while( 1 )
		{
                        // Not Reached.
			vTaskDelay( pdMS_TO_TICKS( 100 ));
		}
	}
}


void Board_Task( void *pvPara )
{
									  
	while( 1 )
	{
		// 
		vTaskDelay( pdMS_TO_TICKS( 100 ));
	}
}


void Start_Task( void *pvPara )
{
	BaseType_t status;
	static FP_SIDE_enum fp[4] = {SIDE_A, SIDE_B, SIDE_C};
	
	taskENTER_CRITICAL();
	
	// Creat Board Task
	status = xTaskCreate(( TaskFunction_t 	)Board_Task,
						 ( const char *		)"BoardProc",
						 ( uint16_t		)BOARD_STACK_SIZE,
						 ( void *			)NULL,
						 ( UBaseType_t    
 )BOARD_TASK_PRIORITY,
						 ( TaskHandle_t *	)&BOARD_TaskHandler );
				
	// Creat Side A {SIDE_A} Task
	status = xTaskCreate(( TaskFunction_t 	)FP_Task,
						 ( const char *		)"FP0 Proc",
						 ( uint16_t			)FP_STACK_SIZE,
						 ( void *			)( FP_SIDE_enum *)&fp[0],
						 ( UBaseType_t		)FP_TASK_PRIORITY,
						 ( TaskHandle_t *	)&FP_TaskHandler[0] );
				
	// Creat Side B{SIDE_B} Task
	status = xTaskCreate(( TaskFunction_t 	)FP_Task,
						 ( const char *		)"FP1 Proc",
						 ( uint16_t			)FP_STACK_SIZE,
						 ( void *			)( FP_SIDE_enum *)&fp[1],
						 ( UBaseType_t		)FP_TASK_PRIORITY,
						 ( TaskHandle_t *	)&FP_TaskHandler[1] );
				
				
	vTaskDelete( StartTaskHandler );

	taskEXIT_CRITICAL();
				
}



int main(void)
{
    	systick_config();
	
	initialize_board();
	
	// Creat Startup Task
	xTaskCreate(( TaskFunction_t 	)Start_Task,
				( const char *		)"StartProcess",
				( uint16_t			)START_STACK_SIZE,
				( void *			)NULL,
				( UBaseType_t		)START_TASK_PRIORITY,
				( TaskHandle_t *	)&StartTaskHandler );
				
	vTaskStartScheduler();
}

Try making the fp array global. Even though it is declared static, your control flow is questionable. At the beginning of your task fn, dump the value of your task parameter to ensure that it truly has the SIDE value you expect.

1 Like

There’s no need to enclose task creation in taskENTER/EXIT_CRITICAL(); and I think the problem is that you taskEXIT_CRITICAL(); after vTaskDelete( StartTaskHandler );.
(BTW task self deletion can be done by vTaskDelete( NULL );)
Also when correctly fetching the return value of FreeRTOS calls like xTaskCreate you really should evaluate this value and I’d do so also in main creating the Start_Task.

1 Like

Thanks a lot!
The “configTOTAL_HEAP_SIZE” is too small. I re-set the value, and It works!

Thanks RAc and hs2

Another minor hint: Even though the xTaskCreate task parameter forwarded to the task functions is a pointer the actual parameter used doesn’t need to be a real pointer. A void* is often used as kind of universal type which can be used for and casted to anything. If you know what you’re doing, of course :wink:
In your case you could use the SIDE_A or B enum directly (casted to void*). It’s just a opaque value forwarded to the task function as parameter and the task function has to know what it really was/is to properly cast it back to the correctly typed original parameter. That’s the contract between the code calling xTaskCreate and the task functions.

2 Likes