VxWorks 提供了一种特殊的看门狗机制,以实现在 C 函数中的延时操作。其操作函数如下:
函数名称:wdCreate( )
函数说明:创建看门狗定时器。
WDOG_ID wdCreate (void)
函数名称:wdDelete ( )
函数说明:删除定时器。
STATUS wdDelete
(
WDOG_ID wdId /* 看门狗定时器标识 */
)
函数名称:wdStart ( )
函数说明:启动定时器。
STATUS wdStart
(
WDOG_ID wdId, /* 看门狗定时器标识 */
int delay, /* 延时值 */
FUNCPTR pRoutine, /* 期满后的处理函数 */
int parameter /* 期满后处理函数的参数 */
)
函数名称:wdCancel ( )
函数说明:取消定时器。
STATUS wdCancel
(
WDOG_ID wdId /* 看门狗定时器标识 */
)
看门狗定时器的使用很简单,首先使用 wdCreate()创建定时器;随后可以使用 wdStart()指
时值和服务函数,并启动该定时器。程序清单 8-9 列举了一个例子,在看门狗定时器期满后会
printf 函数。
程序清单8-9 看门狗定时器的使用(eg8_5.c)
/* includes */
#include "vxWorks.h"
#include <stdio.h>
#include "wdLib.h"
/* defines */
#define SECONDS (3)
WDOG_ID myWatchDogId;
init (void)
{ /* 创建看门狗定时器 */
if ((myWatchDogId = wdCreate( )) == NULL)
return (ERROR);
}
task (void)
{
/* 启动看门狗定时器,并在超时后打印一些字符串 */
if (wdStart (myWatchDogId, sysClkRateGet( ) * SECONDS, printf,
"Watchdog timer just expired\n") == ERROR)
return (ERROR);
/* ... */
}
来一个看门狗加信号量的demo程序
不过不晓得为什么,编译能通过,下载过不了。
/* countingSemDemo.c - Demonstrates task synchronization using counting
* semaphores.
*/
/* Copyright 1984-1997 Wind River Systems, Inc. */
/*
modification history
--------------------
01c,06nov97,mm added copyright.
01b,16sep97,ram included files stdio.h, taskLib.h, usrLib.h, sysLib.h
Used the semShow() function to display info about
the semaphore inplace of the show() function.
01a,27mar94,ms cleaned up for VxDemo
*/
/*
* DESCRIPTION
* Counting semaphore example. Using binary semaphores for task
* synchronization may, if the events can occur rapidly enough, cause
* a loss of da
* faster then a task can process them. Using counting semaphores may
* solve this problem. This program demonstrates task synchronization using
* counting semaphores. User can also select to use a binary semaphore instead
* of a counting semaphore in this demonstration, for comparision between the
* two semaphores.
*
* RETURNS: OK or ERROR
*
* EXAMPLE
*
* -> sp countingSemDemo, typeOfSemaphore
*
* where typeOfSemaphore is the value of the type of semaphore to be used
* for task synchronization in this demonstration. For using counting
* semaphores use a value 'c' or 'C' for typeOfSemaphore parameter and for
* using binary semaphores use a value 'b' or 'B' for typeOfSemaphore
* parameter.
*
* example:
* -> sp countingSemDemo, 'c'
* -> sp countingSemDemo, 'b'
*
*/
/* include files */
#include "vxWorks.h"
#include "wdLib.h"
#include "stdio.h"
#include "semLib.h"
#include "taskLib.h"
#include "usrLib.h"
#include "sysLib.h"
/* defines */
#define TASK_PRIORITY 101
#define TASK_STACK_SIZE 5000
#define TIME_BETWEEN_INTERRUPTS 1 /* 1 tick */
#define TASK_WORK_TIME 2 /* 2 ticks */
#define NUM_OF_GIVES 3
/* globals */
LOCAL SEM_ID semId = NULL; /* counting or binary semaphore ID */
LOCAL WDOG_ID wdId = NULL; /* watchdog ID */
LOCAL int syncTaskTid = 0; /* tid of syncTask */
LOCAL int numToGive = NUM_OF_GIVES; /* Number of times semGive is called */
/* forward declaratiuon */
void syncISR(int); /* ISR to unblock syncTask */
void cleanUp (); /* cleanup routine */
void syncTask (); /* task that needs to be synchronized
* with external events */
/*****************************************************************************
* countingSemDemo - demonstrates task synchronization using counting
* semaphores. User can also select to use binary semaphore instead of
* counting semaphore in this demonstration, for comparision between the two
* semaphores.
*
* RETURNS: OK or ERROR
*
*/
STATUS countingSemDemo
(
char semType /* counting semaphore type 'c' or binary semaphore
* type 'b'
*/
)
{
switch (semType)
{
case 'c':
case 'C':
if ((semId = semCCreate (SEM_Q_PRIORITY, 0)) == NULL)
{
perror ("semCCreate");
return (ERROR);
}
break;
case 'b':
case 'B':
if ((semId = semBCreate(SEM_Q_PRIORITY, SEM_EMPTY)) == NULL)
{
perror ("semBCreate");
return (ERROR);
}
break;
default:
printf ("Unknown semType -- must be 'c' or 'b'\n");
return (ERROR);
}
if ((wdId = wdCreate()) == NULL)
{
perror ("wdCreate");
cleanUp ();
return (ERROR);
}
if ((syncTaskTid = taskSpawn ("tsyncTask", TASK_PRIORITY, 0,TASK_STACK_SIZE,(FUNCPTR) syncTask, 0,0,0,0,0,0,0,0,0,0)) == ERROR)
{
perror ("taskSpawn");
cleanUp();
return (ERROR);
}
/* watchdog simulates hardware interrupts */
if (wdStart (wdId, TIME_BETWEEN_INTERRUPTS, (FUNCPTR) syncISR, numToGive)
== ERROR)
{
perror ("wdStart");
cleanUp ();
return (ERROR);
}
/* arbitrary delay to allow program to complete before clean up */
taskDelay (sysClkRateGet() + ((TASK_WORK_TIME + 2) * numToGive));
cleanUp();
return (OK);
}
/*****************************************************************************
* syncTask - synchronizes with interrupts using counting or binary
* semaphores.
*/
void syncTask (void)
{
int eventCount = 0;
FOREVER
{
if (semTake (semId, WAIT_FOREVER) == ERROR)
{
perror ("syncTask semTake");
return;
}
/* Do "work" */
taskDelay (TASK_WORK_TIME);
semShow (semId,1);
eventCount++;
printf ("semaphore taken %d times\n", eventCount);
}
}
/*****************************************************************************
* syncISR - simulates a hardware device which generates interrupts very
* quickly and synchronizes with syncTask using semaphores.
*/
void syncISR
(
int times
)
{
semGive (semId);
times--;
if (times > 0)
wdStart (wdId, TIME_BETWEEN_INTERRUPTS, (FUNCPTR) syncISR, times);
}
/*****************************************************************************
* cleanUP - deletes the syncTask, deletes the semaphore and the watchdog timer
* previously created by countingSemDemo.
*/
void cleanUp ()
{
if (syncTaskTid)
taskDelete (syncTaskTid);
if (semId)
semDelete (semId);
if (wdId)
wdDelete (wdId);
}
//
taskDelay (sysClkRateGet()*60);为何是延时60秒?怎么计算的?
------Solutions------
STATUS taskDelay (int ticks);
参数的单位是ticks.
1 在configAll.h中定义了
#define SYS_CLK_RATE60/* default system clock rate */
2 在usrRoot()函数中调用sysClkRateSet (SYS_CLK_RATE); /* set system clock rate */
这样就相当于60ticks/s, 也可也改变每秒种的ticks.
3 所以taskDelay (sysClkRateGet()*60 是延时60秒
------Solutions------
sysClkRateGet()得到的是1s的ticks数
------Solutions------
sysClkRateGet()得到的是1s的ticks数
是不是可以这样理解,cpu延迟1s?
------Solutions------
不是cpu延迟1s,是调用taskDelay()的任务
------Solutions------
是让任务延时1s
sysClkRateGet()得到的是1s的ticks数