⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tasks.c

📁 modbus开发程序包
💻 C
📖 第 1 页 / 共 4 页
字号:
 * NORMAL APPLICATION CODE.

 */

#if ( configUSE_TRACE_FACILITY == 1 )



	static void prvListTaskWithinSingleList( signed portCHAR *pcWriteBuffer, xList *pxList, signed portCHAR cStatus );



#endif



/*

 * When a task is created, the stack of the task is filled with a known value.

 * This function determines the 'high water mark' of the task stack by

 * determining how much of the stack remains at the original preset value.

 */

#if ( configUSE_TRACE_FACILITY == 1 )



	unsigned portSHORT usTaskCheckFreeStackSpace( const unsigned portCHAR *pucStackByte );



#endif



/*lint +e956 */











/*-----------------------------------------------------------

 * TASK CREATION API documented in task.h

 *----------------------------------------------------------*/



signed portBASE_TYPE xTaskCreate( pdTASK_CODE pvTaskCode, const signed portCHAR * const pcName, unsigned portSHORT usStackDepth, void *pvParameters, unsigned portBASE_TYPE uxPriority, xTaskHandle *pxCreatedTask )

{

signed portBASE_TYPE xReturn;

tskTCB * pxNewTCB;

static unsigned portBASE_TYPE uxTaskNumber = 0; /*lint !e956 Static is deliberate - this is guarded before use. */



	/* Allocate the memory required by the TCB and stack for the new task.

	checking that the allocation was successful. */

	pxNewTCB = prvAllocateTCBAndStack( usStackDepth );



	if( pxNewTCB != NULL )

	{		

		portSTACK_TYPE *pxTopOfStack;



		/* Setup the newly allocated TCB with the initial state of the task. */

		prvInitialiseTCBVariables( pxNewTCB, usStackDepth, pcName, uxPriority );



		/* Calculate the top of stack address.  This depends on whether the

		stack grows from high memory to low (as per the 80x86) or visa versa.

		portSTACK_GROWTH is used to make the result positive or negative as

		required by the port. */

		#if portSTACK_GROWTH < 0

		{

			pxTopOfStack = pxNewTCB->pxStack + ( pxNewTCB->usStackDepth - 1 );

		}

		#else

		{

			pxTopOfStack = pxNewTCB->pxStack;	

		}

		#endif



		/* Initialize the TCB stack to look as if the task was already running,

		but had been interrupted by the scheduler.  The return address is set

		to the start of the task function. Once the stack has been initialised

		the	top of stack variable is updated. */

		pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pvTaskCode, pvParameters );



		/* We are going to manipulate the task queues to add this task to a

		ready list, so must make sure no interrupts occur. */

		portENTER_CRITICAL();

		{

			uxCurrentNumberOfTasks++;

			if( uxCurrentNumberOfTasks == ( unsigned portBASE_TYPE ) 1 )

			{

				/* As this is the first task it must also be the current task. */

				pxCurrentTCB =  pxNewTCB;



				/* This is the first task to be created so do the preliminary

				initialisation required.  We will not recover if this call

				fails, but we will report the failure. */

				prvInitialiseTaskLists();

			}

			else

			{	

				/* If the scheduler is not already running, make this task the

				current task if it is the highest priority task to be created

				so far. */

				if( xSchedulerRunning == pdFALSE )

				{

					if( pxCurrentTCB->uxPriority <= uxPriority )

					{

						pxCurrentTCB = pxNewTCB;	

					}

				}

			}				



			/* Remember the top priority to make context switching faster.  Use

			the priority in pxNewTCB as this has been capped to a valid value. */

			if( pxNewTCB->uxPriority > uxTopUsedPriority )

			{

				uxTopUsedPriority = pxNewTCB->uxPriority;

			}



			/* Add a counter into the TCB for tracing only. */

			pxNewTCB->uxTCBNumber = uxTaskNumber;

			uxTaskNumber++;



			prvAddTaskToReadyQueue( pxNewTCB );



			xReturn = pdPASS;

		}

		portEXIT_CRITICAL();

	}

	else

	{

		xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;

	}



	if( xReturn == pdPASS )

	{

		if( ( void * ) pxCreatedTask != NULL )

		{

			/* Pass the TCB out - in an anonymous way.  The calling function/

			task can use this as a handle to delete the task later if

			required.*/

			*pxCreatedTask = ( xTaskHandle ) pxNewTCB;

		}



		if( xSchedulerRunning != pdFALSE )

		{

			/* If the created task is of a higher priority than the current task

			then it should run now. */

			if( pxCurrentTCB->uxPriority < uxPriority )

			{

				taskYIELD();

			}

		}

	}



	return xReturn;

}

/*-----------------------------------------------------------*/



#if ( INCLUDE_vTaskDelete == 1 )



	void vTaskDelete( xTaskHandle pxTaskToDelete )

	{

	tskTCB *pxTCB;



		taskENTER_CRITICAL();

		{

			/* Ensure a yield is performed if the current task is being 

			deleted. */

			if( pxTaskToDelete == pxCurrentTCB )

			{

				pxTaskToDelete = NULL;

			}



			/* If null is passed in here then we are deleting ourselves. */

			pxTCB = prvGetTCBFromHandle( pxTaskToDelete );



			/* Remove task from the ready list and place in the	termination list.

			This will stop the task from be scheduled.  The idle task will check

			the termination list and free up any memory allocated by the

			scheduler for the TCB and stack. */

			vListRemove( &( pxTCB->xGenericListItem ) );



			/* Is the task waiting on an event also? */												

			if( pxTCB->xEventListItem.pvContainer )

			{

				vListRemove( &( pxTCB->xEventListItem ) );

			}



			vListInsertEnd( ( xList * ) &xTasksWaitingTermination, &( pxTCB->xGenericListItem ) );



			/* Increment the ucTasksDeleted variable so the idle task knows

			there is a task that has been deleted and that it should therefore

			check the xTasksWaitingTermination list. */

			++uxTasksDeleted;

		}

		taskEXIT_CRITICAL();



		/* Force a reschedule if we have just deleted the current task. */

		if( xSchedulerRunning != pdFALSE ) 

		{

			if( ( void * ) pxTaskToDelete == NULL )

			{

				taskYIELD();

			}

		}

	}



#endif













/*-----------------------------------------------------------

 * TASK CONTROL API documented in task.h

 *----------------------------------------------------------*/



#if ( INCLUDE_vTaskDelayUntil == 1 )



	void vTaskDelayUntil( portTickType *pxPreviousWakeTime, portTickType xTimeIncrement )

	{

	portTickType xTimeToWake;

	portBASE_TYPE xAlreadyYielded, xShouldDelay = pdFALSE;



		vTaskSuspendAll();

		{

			/* Generate the tick time at which the task wants to wake. */

			xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;



			if( xTickCount < *pxPreviousWakeTime )

			{

				/* The tick count has overflowed since this function was

				lasted called.  In this case the only time we should ever

				actually delay is if the wake time has also	overflowed,

				and the wake time is greater than the tick time.  When this

				is the case it is as if neither time had overflowed. */

				if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xTickCount ) )

				{

					xShouldDelay = pdTRUE;

				}

			}

			else

			{

				/* The tick time has not overflowed.  In this case we will

				delay if either the wake time has overflowed, and/or the

				tick time is less than the wake time. */

				if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xTickCount ) )

				{

					xShouldDelay = pdTRUE;

				}

			}



			/* Update the wake time ready for the next call. */

			*pxPreviousWakeTime = xTimeToWake;



			if( xShouldDelay )

			{

				/* We must remove ourselves from the ready list before adding

				ourselves to the blocked list as the same list item is used for

				both lists. */

				vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );



				/* The list item will be inserted in wake time order. */

				listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );



				if( xTimeToWake < xTickCount )

				{

					/* Wake time has overflowed.  Place this item in the

					overflow list. */

					vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );

				}

				else

				{

					/* The wake time has not overflowed, so we can use the

					current block list. */

					vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );

				}

			}

		}

		xAlreadyYielded = xTaskResumeAll();



		/* Force a reschedule if xTaskResumeAll has not already done so, we may

		have put ourselves to sleep. */

		if( !xAlreadyYielded )

		{

			taskYIELD();

		}

	}	

	

#endif

/*-----------------------------------------------------------*/



#if ( INCLUDE_vTaskDelay == 1 )



	void vTaskDelay( portTickType xTicksToDelay )

	{

	portTickType xTimeToWake;

	signed portBASE_TYPE xAlreadyYielded = pdFALSE;



		/* A delay time of zero just forces a reschedule. */

		if( xTicksToDelay > ( portTickType ) 0 )

		{

			vTaskSuspendAll();

			{

				/* A task that is removed from the event list while the

				scheduler is suspended will not get placed in the ready

				list or removed from the blocked list until the scheduler

				is resumed.

				

				This task cannot be in an event list as it is the currently

				executing task. */



				/* Calculate the time to wake - this may overflow but this is

				not a problem. */

				xTimeToWake = xTickCount + xTicksToDelay;



				/* We must remove ourselves from the ready list before adding

				ourselves to the blocked list as the same list item is used for

				both lists. */

				vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );



				/* The list item will be inserted in wake time order. */

				listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );



				if( xTimeToWake < xTickCount )

				{

					/* Wake time has overflowed.  Place this item in the

					overflow list. */

					vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );

				}

				else

				{

					/* The wake time has not overflowed, so we can use the

					current block list. */

					vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );

				}

			}

			xAlreadyYielded = xTaskResumeAll();

		}

		

		/* Force a reschedule if xTaskResumeAll has not already done so, we may

		have put ourselves to sleep. */

		if( !xAlreadyYielded )

		{

			taskYIELD();

		}

	}

	

#endif

/*-----------------------------------------------------------*/



#if ( INCLUDE_uxTaskPriorityGet == 1 )



	unsigned portBASE_TYPE uxTaskPriorityGet( xTaskHandle pxTask )

	{

	tskTCB *pxTCB;

	unsigned portBASE_TYPE uxReturn;



		taskENTER_CRITICAL();

		{

			/* If null is passed in here then we are changing the

			priority of the calling function. */

			pxTCB = prvGetTCBFromHandle( pxTask );

			uxReturn = pxTCB->uxPriority;

		}

		taskEXIT_CRITICAL();



		return uxReturn;

	}



#endif

/*-----------------------------------------------------------*/



#if ( INCLUDE_vTaskPrioritySet == 1 )



	void vTaskPrioritySet( xTaskHandle pxTask, unsigned portBASE_TYPE uxNewPriority )

	{

	tskTCB *pxTCB;

	unsigned portBASE_TYPE uxCurrentPriority, xYieldRequired = pdFALSE;



		/* Ensure the new priority is valid. */

		if( uxNewPriority >= configMAX_PRIORITIES )

		{

			uxNewPriority = configMAX_PRIORITIES - 1;

		}



		taskENTER_CRITICAL();

		{

			/* If null is passed in here then we are changing the

			priority of the calling function. */

			pxTCB = prvGetTCBFromHandle( pxTask );

			uxCurrentPriority = pxTCB->uxPriority;



			if( uxCurrentPriority != uxNewPriority )

			{

				/* The priority change may have readied a task of higher

				priority than the calling task. */

				if( uxNewPriority > pxCurrentTCB->uxPriority ) 

				{

					if( pxTask != NULL )

					{

						/* The priority of another task is being raised.  If we

						were raising the priority of the currently running task

						there would be no need to switch as it must have already

						been the highest priority task. */

						xYieldRequired = pdTRUE;

					}

				}

				else if( pxTask == NULL )

				{

					/* Setting our own priority down means there may now be another

					task of higher priority that is ready to execute. */

					xYieldRequired = pdTRUE;

				}

			

				pxTCB->uxPriority = uxNewPriority;

				listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxNewPriority );



				/* If the task is in the blocked or suspended list we need do

				nothing more than change it's priority variable. However, if

				the task is in a ready list it needs to be removed and placed

				in the queue appropriate to its new priority. */

				if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxCurrentPriority ] ), &( pxTCB->xGenericListItem ) ) )

				{

					/* The task is currently in its ready list - remove before adding

					it to it's new ready list.  As we are in a critical section we

					can do this even if the scheduler is suspended. */

					vListRemove( &( pxTCB->xGenericListItem ) );

					prvAddTaskToReadyQueue( pxTCB );

				}			

				

				if( xYieldRequired == pdTRUE )

				{

					taskYIELD();

				}				

			}

		}

		taskEXIT_CRITICAL();

	}



#endif

/*-----------------------------------------------------------*/



#if ( INCLUDE_vTaskSuspend == 1 )



	void vTaskSuspend( xTaskHandle pxTaskToSuspend )

	{

	tskTCB *pxTCB;



		taskENTER_CRITICAL();

		{

			/* Ensure a yield is performed if the current task is being 

			suspended. */

			if( pxTaskToSuspend == pxCurrentTCB )

			{

				pxTaskToSuspend = NULL;

			}



			/* If null is passed in here then we are suspending ourselves. */

			pxTCB = prvGetTCBFromHandle( pxTaskToSuspend );



			/* Remove task from the ready/delayed list and place in the	suspended list. */

			vListRemove( &( pxTCB->xGenericListItem ) );



			/* Is the task waiting on an event also? */												

			if( pxTCB->xEventListItem.pvContainer )

			{

				vListRemove( &( pxTCB->xEventListItem ) );

			}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -