📄 ex8.c
字号:
//
// example 8: Use mail to create complex task:
// 1.The main program creates first task and runs OS kenel.
// 2.First task creates second task,
// send the third task's structure including parameter and address and so on
// and turn on lamp every other 17 tick.
// 3. Second task receives mailbox create third task and turn on lamp every other 11 tick.
// 4. After third task delay 4 second, it delete first task and second task and display lamp.
// author: Taiyun Wang
// date:2003/2/22
///////////////////////////////////////////////////////////////////////////
#include "sposvar.h"
#include "spos.h"
typedef struct {
void (*start_add)(void *para); //function pointer
void* para; //task parameter
int* stacktop; //task stack size
int priority; //task priority
} stru_task; //structure name
int err; //Error No
int t1stack[30]; //Task 1 stack
int t2stack[30]; //Task 2 stack
int t3stack[30]; //Task 3 stack
HEvent mailbox; //Event handle
volatile unsigned int *P_IOB_BUFFER =(unsigned int*)(0x7006); //Port B data register
volatile unsigned int *P_IOB_DIR =(unsigned int*)(0x7007); //Port B direction register
volatile unsigned int *P_IOB_ATTRIB = (unsigned int*)(0x7008); //Port B attribute register
main()
{
void Task1();
SpSInit();
*P_IOB_DIR = 0XFFFF; //Set port B direction
*P_IOB_ATTRIB = 0XFFFF; //Set prot B attribute
err = SpSTaskCreate(Task1,0,t1stack+29,1); //Create first task
mailbox = SpSMboxCreate((void*)0); //create mailbox
SpSStart(); //Start OS kernel
}
void Task1()
{
void Task2();
void Task3();
stru_task msg;
msg.start_add = Task3;
msg.para = (void*) 0;
msg.stacktop = t3stack+29;
msg.priority = 3;
err = SpSTaskCreate(Task2,0,t2stack+29,2); //Create second task
SpSTimeDly(20); //Delay 20 tick,running task2
SpSMboxPost(mailbox,&msg); //Send mail to task 2
while(1) {
*P_IOB_BUFFER = 0x55;
SpSTimeDly(17);
}
}
void Task2()
{
stru_task msg;
stru_task* pmsg;
int err;
pmsg = SpSMboxPend(mailbox,0,&err); //Waiting mail
msg = *pmsg; //copy mail to local variable
//create task 3
err = SpSTaskCreate(msg.start_add,msg.para,msg.stacktop,msg.priority);
while(1) {
*P_IOB_BUFFER = 0xAA;
SpSTimeDly(11);
}
}
void Task3()
{
int i = 1;
SpSTimeDly(512); //Delay 512 tick
SpSTaskDel(1); //Delete task 1
SpSTaskDel(2); //Delete task 2
while(1) {
*P_IOB_BUFFER = i;
i <<=1;
if (i == 0x100)
i = 1;
SpSTimeDly(20); //Delay 20 tick
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -