📄 deadlinewd.c
字号:
#include "vxWorks.h"
#include "taskLib.h"
#include "msgQLib.h"
#include "wdLib.h"
#include "usrLib.h"
#include "sysLib.h"
#include "stdio.h"
#include "logLib.h"
#define FIVE_SEC 5
#define TWENTY_SEC 20
#define DEADLINE_TIME 20
#define NUM_MSGS 10
#define TASK_STACK_SIZE 20000
#define PRIORITY 101
/* globals */
LOCAL int countNum;
LOCAL int valueGot;
LOCAL BOOL working;
LOCAL BOOL notDone;
LOCAL WDOG_ID wdId;
LOCAL MSG_Q_ID msgQId;
/* function prototypes*/
LOCAL STATUS deadlineHandler(); /* deadline handler - watchdog handler routine */
LOCAL STATUS coordinatorTask(); /* sends data to organizer */
LOCAL STATUS organizerTask(); /* receives data from the coordinator and
* resets the coordinator while deadline time elapses */
LOCAL STATUS getDataFromDevice(); /* simulation data collection */
STATUS deadlineWd()
{
/* initialize the globals */
notDone = TRUE;
working = TRUE;
countNum = 0;
valueGot = 0;
/* Create msgQ */
if ((msgQId = msgQCreate(NUM_MSGS,sizeof(int),MSG_Q_FIFO)) == NULL)
{
perror ("cannot create msgQ");
return (ERROR);
}
/* Create watchDog timer */
if ((wdId = wdCreate()) == NULL)
{
perror ("cannot create watchdog");
return (ERROR);
}
/* Spawn the organizerTask */
if ((taskSpawn("tOrgTask",PRIORITY,0,TASK_STACK_SIZE,
(FUNCPTR)organizerTask,0,0,0,0,0,0,0,0,0,0)) == ERROR)
{
perror ("deadlineWd: Error in spawning organizerTask");
return (ERROR);
}
/* Spawn the coordinatorTask */
if ((taskSpawn("tCordTask",PRIORITY,0,TASK_STACK_SIZE,
(FUNCPTR)coordinatorTask,0,0,0,0,0,0,0,0,0,0)) == ERROR)
{
perror ("deadlineWd: Error in spawning coordinatorTask");
return (ERROR);
}
/* stop the demo after 20 seconds */
taskDelay (sysClkRateGet() * TWENTY_SEC);
printf ("\n\nStopping deadlineWd...\n");
notDone = FALSE;
if (msgQDelete (msgQId) == ERROR )
{
perror ("Error in deleting msgQ");
return (ERROR);
}
if (wdCancel (wdId) == ERROR )
{
perror ("Error in canceling watchdog timer");
return (ERROR);
}
if (wdDelete (wdId) == ERROR )
{
perror ("Error in deleting watchdog timer");
return (ERROR);
}
return (OK);
}
STATUS coordinatorTask()
{
FOREVER
{
countNum++;
if ((msgQSend(msgQId,(char *)&countNum,sizeof(int),NO_WAIT,MSG_PRI_NORMAL)) == ERROR)
{
perror ("Error in sending the msg");
return (ERROR);
}
printf ("\n\ncoordinatorTask: Send item = %d\n", countNum);
printf ("coordinatorTask: idle for %d seconds\n", countNum);
getDataFromDevice();
if (notDone == FALSE)
break;
}
return (OK);
}
STATUS organizerTask()
{
while (notDone)
{
if ((wdStart(wdId, sysClkRateGet()*DEADLINE_TIME, (FUNCPTR)deadlineHandler, 0)) == ERROR)
{
perror ("Error in starting watchdog timer");
return (ERROR);
}
if ((msgQReceive(msgQId, (char *)&valueGot, sizeof(int), WAIT_FOREVER)) == ERROR)
{
perror ("Error in receiving the message");
return (OK);
}
else
printf ("organizerTask: Received item = %d\n", valueGot);
}
return (OK);
}
STATUS deadlineHandler ()
{
logMsg ("\n\nReseting the coordinator on elapse of the deadline time\n",0,0,0,0,0,0);
countNum = 0;
return (OK);
}
STATUS getDataFromDevice ()
{
taskDelay (sysClkRateGet()*countNum);
return (OK);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -