A real-time application can be used as a standalone task when using an RTOS. Each task has its own operating environment, independent of other tasks in the system or the RTOS scheduler. There can be only one task running at any point in time, which is determined by the RTOS scheduler, so the RTOS scheduler will repeatedly turn on and off each task
Task Status
Operating State
When a task is running, it is said that the task is in a running state, the task is in the running state is currently
Tasks that use the processor
Ready state
Tasks that can be run. A task with a priority or higher priority is running
Blocking states
A task is currently waiting for an external event
Hang-up state
It cannot be called by the scheduler after it enters the suspend state. The task enters and exits the suspend state by calling the function Vtasksuspend () and Xtaskresume ()
Task State Transitions
Task Creation
basetype_t xtaskcreate (taskfunction_t pxtaskcode,//function pointer
const char * const PCNAME,
const uint16_t usstackdepth ,//stack size
void * Const pvparameters,//Give task pass parameter
ubasetype_t uxpriority,//Task Priority
taskhandle_t * const Pxcreatedtask/* Task handle */) privileged_function;
Pxtaskcode: Task function
Pcname: Task Name
Usstackdepth: Task Stack size
Pvparameters: Arguments passed to a task function
Uxpriority: Task Priority
Pxcreatedtask: Task handle
taskhandle_t xtaskcreatestatic (taskfunction_t pxtaskcode, const
char * const PCNAME,
const uint32_t Ulstackdepth,
void * Const pvparameters,
ubasetype_t uxpriority,
stacktype_t * Const Puxstackbuffer,
statictask_t * const pxtaskbuffer) privileged_function;
Puxstackbuffer: Task stack. General Array
Pxtaskbuffer: Task control block
Task Delete
void Vtaskdelete (taskhandle_t xtasktodelete) privileged_function;
Xtasktodelete: Task handle
Suspend Task
void Vtasksuspend (taskhandle_t xtasktosuspend)
Restore Tasks
void Vtaskresume (taskhandle_t xtasktoresume)
Resume task run in interrupt service function
basetype_t Xtaskresumefromisr (taskhandle_t xtasktoresume)
Example
taskhandle_t Pxcreatedtask;
void Tim4_irqhandler ()
{
if (Tim_getitstatus (TIM4, tim_it_update) = = 1)
{
Xtaskresumefromisr ( Pxcreatedtask); Resume task run in interrupt service function
}
tim_clearitpendingbit (TIM4, tim_it_update);
}
void key ()
{
static U8 flag = 1;
if (flag = = = 1 && key_up = = 1)
{
vtaskdelay (ten);
if (key_up = = 1)
{
flag = 0;
Vtasksuspend (Pxcreatedtask); Suspend task
}
}
else if (key_up = = 0)
{
vtaskdelay (ten);
if (key_up = = 0)
{
flag = 1
;
}}} void Vtask_key (void *t)
{
while (1)
{
key ()}
}
int main (void)
{
tim4_init (10000, 36000-1);//Initialize timer (see timer section)
xtaskcreate (Vtask, "Vtask", 1, NULL &pxcreatedtask);
Xtaskcreate (Vtask_key, "Vtask_key", N, NULL, 2, NULL);
Vtaskstartscheduler ();
}
Experimental phenomenon: When the button is pressed, the task is suspended. When the timer is interrupted, the recovery task runs