1. Task Creation
Task creation function: OSTaskCreate()
void OSTaskCreate (OS_TCB *p_tcb, //point to the task control block of the task
CPU_CHAR *p_name, //task name
OS_TASK_PTR p_task, //task function name
void *p_arg, //Parameters passed to the task
OS_PRIO prio, //Task priority
CPU_STK *p_stk_base, //task stack base address
CPU_STK_SIZE stk_limit, //Task stack depth
CPU_STK_SIZE stk_size, //task stack size
OS_MSG_QTY q_size, //Optional message queue
OS_TICK time_quanta, // Enable time slice round-robin scheduling, the default clock beat is divided by 10
void *p_ext, //points to the storage area supplemented by the user
OS_OPT opt, //Task specific options
OS_ERR *p_err) //Save error code
① First, define the priority, stack size, task control block and stack of each task
//Task priority
#define START_TASK_PRIO 3
//Task stack size
#define START_STK_SIZE 512
//Task control block
OS_TCB StartTaskTCB;
//Task stack
CPU_STK START_TASK_STK[START_STK_SIZE];
//Task function declaration
void start_task(void *p_arg);
② Write the main function. First, initialize the external function, then initialize UCOSIII, and then create the start task (before creating the start task, you must call the critical section function, and after creation, you must exit the critical section and then start UCOIII). The parameters passed to create the start task are some parameters defined by your own macro.
int main(void)
{
OS_ERR err; //Error value: all are macro definitions. Find the meaning of the returned error value according to the corresponding value.
CPU_SR_ALLOC();
delay_init(168); //Clock initialization
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //Interrupt group configuration
uart_init(115200); //Serial port initialization
LED_Init(); //LED initialization
OSInit(&err); //Initialize UCOSIII
OS_CRITICAL_ENTER(); //Enter the critical section
//Create the start task
OSTaskCreate((OS_TCB * )&StartTaskTCB, //Task control block
(CPU_CHAR * )"start task", //task name
(OS_TASK_PTR )start_task, //task function
(void *)0, //Parameters passed to the task function
(OS_PRIO )START_TASK_PRIO, //Task priority
(CPU_STK * )&START_TASK_STK[0], //task stack base address
(CPU_STK_SIZE)START_STK_SIZE/10, //Task stack depth limit
(CPU_STK_SIZE)START_STK_SIZE, //Task stack size
(OS_MSG_QTY) 0, //The maximum number of messages that the task internal message queue can receive. When it is 0, it is forbidden to receive messages.
(OS_TICK) 0, //When the time slice rotation is enabled, the time slice length is 0, which is the default length.
(void *) 0, // user-supplemented storage area
(OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR, //Task options
(OS_ERR * )&err); //Store the return value of the function when an error occurs
OS_CRITICAL_EXIT(); //Exit the critical section
OSStart(&err); //Start UCOSIII
while(1);
}
③After the main function calls the creation of the start task function, the functional function of the start task function should be written. The functional function is also called in the start task function. The principle is the same as above. Then write the functions to be implemented by the functional function.
//Start task function
void start_task(void *p_arg)
{
OS_ERR err;
CPU_SR_ALLOC();
p_arg = p_arg;
CPU_Init();
#if OS_CFG_STAT_TASK_EN > 0u
OSStatTaskCPUUsageInit(&err); //Statistical task
#endif
#ifdef CPU_CFG_INT_DIS_MEAS_EN //If the measurement interrupt shutdown time is enabled
CPU_IntDisMeasMaxCurReset();
#endif
#if OS_CFG_SCHED_ROUND_ROBIN_EN //When using time slice rotation
// Enable the time slice round-robin scheduling function. The time slice length is 1 system clock beat, that is, 1*5=5ms
OSSchedRoundRobinCfg(DEF_ENABLED,1,&err);
#endif
OS_CRITICAL_ENTER(); //Enter the critical section
//Create LED0 task
OSTaskCreate((OS_TCB * )&Led0TaskTCB,
(CPU_CHAR * )"led0 task",
(OS_TASK_PTR )led0_task,
(void * )0,
(OS_PRIO )LED0_TASK_PRIO,
(CPU_STK * )&LED0_TASK_STK[0],
(CPU_STK_SIZE)LED0_STK_SIZE/10,
(CPU_STK_SIZE)LED0_STK_SIZE,
(OS_MSG_QTY )0,
(OS_TICK )0,
(void * )0,
(OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
(OS_ERR * )&err);
//Create LED1 task
OSTaskCreate((OS_TCB * )&Led1TaskTCB,
(CPU_CHAR * )"led1 task",
(OS_TASK_PTR )led1_task,
(void * )0,
(OS_PRIO )LED1_TASK_PRIO,
(CPU_STK * )&LED1_TASK_STK[0],
(CPU_STK_SIZE)LED1_STK_SIZE/10,
(CPU_STK_SIZE)LED1_STK_SIZE,
(OS_MSG_QTY )0,
(OS_TICK )0,
(void * )0,
(OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
(OS_ERR * )&err);
//Create a floating point test task
OSTaskCreate((OS_TCB * )&FloatTaskTCB,
(CPU_CHAR * )"float test task",
(OS_TASK_PTR )float_task,
(void * )0,
(OS_PRIO )FLOAT_TASK_PRIO,
(CPU_STK * )&FLOAT_TASK_STK[0],
(CPU_STK_SIZE)FLOAT_STK_SIZE/10,
(CPU_STK_SIZE)FLOAT_STK_SIZE,
(OS_MSG_QTY )0,
(OS_TICK )0,
(void * )0,
(OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
(OS_ERR * )&err);
OS_TaskSuspend((OS_TCB*)&StartTaskTCB,&err); //Suspend the start task
OS_CRITICAL_EXIT(); //Enter the critical section
}
//led0 task function
void led0_task(void *p_arg)
{
OS_ERR err;
p_arg = p_arg;
while(1)
{
LED0=0;
OSTimeDlyHMSM(0,0,0,200,OS_OPT_TIME_HMSM_STRICT,&err); //Delay 200ms
LED0=1;
OSTimeDlyHMSM(0,0,0,500,OS_OPT_TIME_HMSM_STRICT,&err); //Delay 500ms
}
}
//led1 task function
void led1_task(void *p_arg)
{
OS_ERR err;
p_arg = p_arg;
while(1)
{
LED1=~LED1;
OSTimeDlyHMSM(0,0,0,500,OS_OPT_TIME_HMSM_STRICT,&err); //Delay 500ms
}
}
//Floating point test task
void float_task(void *p_arg)
{
CPU_SR_ALLOC();
static float float_num=0.01;
while(1)
{
float_num+=0.01f;
OS_CRITICAL_ENTER(); //Enter the critical section
printf("The value of float_num is: %.4frn",float_num);
OS_CRITICAL_EXIT(); //Exit the critical section
delay_ms(500); //delay 500ms
}
}
2. Deleting a task
If we don't want to use a task anymore, we can delete it. To delete a task, we use the OSTaskDel() function. The function prototype is as follows:
void OSTaskDel (OS_TCB *p_tcb,
OS_ERR *p_err)
1. After deleting a task, the OS_TCB and stack it occupies can be reused to create other tasks.
2. Although UCOSIII allows tasks to be deleted while the system is running, this operation should be avoided as much as possible. If the task may occupy resources shared with other tasks, strange results may occur if the occupied resources are not released before deleting the task.
//led0 task function
void led0_task(void *p_arg)
{
OS_ERR err;
p_arg = p_arg;
while(1)
{
task1_num++; //Increase the number of task executions by 1. Note that task1_num1 will be cleared when it reaches 255!
LED0=~LED0;
OSTimeDlyHMSM(0,0,0,200,OS_OPT_TIME_HMSM_STRICT,&err); //Delay 200ms
printf("Task 1 has been executed: %d timesrn",task1_num);
if(task1_num==5)
{
OSTaskDel((OS_TCB*)&Task2_TaskTCB,&err); //Task 1 executes 5 and then deletes Task 2
printf("Task 1 deleted Task 2!rn");
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------
3. Suspend and resume tasks
When we want to suspend a task but don't want to delete it, we can use the OSTaskSuspend() function to suspend the task. The function prototype is as follows:
void OSTaskSuspend ( OS_TCB *p_tcb,OS_ERR *p_err)
When we want to resume a suspended task, we can call the function OSTaskResume(). The function prototype is as follows
void OSTaskResume (OS_TCB *p_tcb, OS_ERR *p_err)
//led0 task function
void led0_task(void *p_arg)
{
OS_ERR err;
p_arg = p_arg;
while(1)
{
task1_num++; //Task 1 execution times plus 1 Note that task1_num1 will be cleared when it is added to 255!!
Previous article:UCOSIII time slice round-robin scheduler
Next article:Scheduling and switching of tasks in UCOIII
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- DIY-Handmade Constant Temperature Heating Table
- How to write driver for HT16K33_16*8 dot matrix
- I'm learning SOC FPGA recently. I don't know if any great god has a bare metal tutorial. I'm a newbie, the more detailed the better.
- Understanding of Bluetooth protocol based on TI cc2540 module
- STM32 sets different input and output modes for different pins of the same IO port
- [SC8905 EVM Evaluation] Power Supply Ripple Test
- Mbed OS changes the way it is released
- STM32MP157A-DK1 Evaluation + SPI Device
- How to define a const structure variable
- [Silicon Labs BG22-EK4108A Bluetooth Development Review] Received the fastest, unboxing