⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vxworksnormal.c

📁 vxWorks下的DEMO程序。简单示范了任务、信号量、ISR、PIPE等的使用
💻 C
📖 第 1 页 / 共 2 页
字号:
/* vxWorksNormal.c - a standard example */
 
/* Copyright 2001-2005 Gao Surf. */
 
/*
modification history 
-------------------- 
01b,11apr01,surf  add pipe device for ISR to write to pipes 
                 in the same way as task-level code. 
01a,11apr01,surf  use myDelay() need POSIX support.
*/
 
/* 
DESCRIPTION 
This module is an example of the standard template. 
 
INCLUDE FILES: None 
*/
 
/* includes */  
 
#include "vxWorks.h"
#include "intLib.h"
#include "semLib.h"
#include "taskLib.h"
#include "iv.h"
#include "vmLib.h"
#include "stdlib.h"
#include "ioLib.h"
#include "iosLib.h"
#include "tyLib.h"
#include "errnoLib.h"
#include "sysLib.h"
#include "stdio.h"
#include "string.h"
#include "sys/ioctl.h" /* for sysInWord or sysInByte or sysOutWord ... */
#include "sysLib.h"
#include "msgQLib.h"
#include "logLib.h" 
#include "wdLib.h"
#include "time.h"
#include "pipeDrv.h"        /* for pipeDevCreate() */
#include "usrlib.h"         /* for sp() */

/* my includes */

/* defines */

#define MAX_FDS 2 
#define MAX_DATA 1024 
#define PIPEHI  "/pipe/highPriority" 
#define PIPENORM "/pipe/normalPriority"

#define SECONDS  (2)  /* delay 2 seconds: the specified number of ticks to delay for the Watchdog Timers */
#define MYINTNUM  4   /* the Interrupt Level */

#ifndef INT_NUM_IRQ0
#define INT_NUM_IRQ0 0x20
#endif /* INT_NUM_IRQ0 */
#ifndef SEM_DELAY
#define SEM_DELAY 1
#endif /* SEM_DELAY */

/* typedefs */

/* globals */

struct   fd_set readFds;                   /* bit mask of fds to read from */ 
int      fds[MAX_FDS];                     /* array of fds on which to pend */ 
WDOG_ID  myWatchDogId=0;                   /* WatchDog Timers ID */
MSG_Q_ID myMsgQId=0,myMsgQIntId=0;         /* 消息队列 */
SEM_ID   mySemFlag=0;                      /* 作互斥用信号量 */
SEM_ID   mySemIntFlag=0;                   /* 接收中断信号量 */
int      myIntId=0,myTaskId0=0,myTaskId1=0;/* 任务ID号 */

/* locals */

/* function declarations */ 

/* forward declarations */ 

void myIntHandle(void) ;       /* 中断处理程序 */
void myIntTask(void) ;         /* 中断任务 */
void myPerform0(void);         /* task */
void myPerform1(void);         /* task */
STATUS myInitial(void);
STATUS myIntInitial(void);
void ghbRun(void);     
STATUS myWDTask(int i);
void myFunc(int i);
void myDelay(void);

/* external references */

/*************************************************************************** 
* 
* CRun - close run
* 
* This routine close the running tasks and defined semaphores. 
* 
* RETURNS: N/A 
* 
* ERRNO: 
*  NONE 
*/ 
 
void CRun(void)
{
/*  Delete WatchDog */
    if (myWatchDogId != NULL) 
    {
        printf("\nDelete WatchDog Timers ID");
        if (wdDelete(myWatchDogId) == OK) printf(" OK!");
        else printf(" Error!");
    }
/*  Delete MSG_Q */
    if (myMsgQId != NULL) 
    {
        printf("\nDelete MSG_Q ID");
        if (msgQDelete(myMsgQId) == OK) printf(" OK!");
        else printf(" Error!");
    }
    if (myMsgQIntId != NULL) 
    {
        printf("\nDelete MSG_Q_INT ID");
        if (msgQDelete(myMsgQIntId) == OK) printf(" OK!");
        else printf(" Error!");
    }
/*  Delete Sem Flag */
    if (mySemFlag != NULL) 
    {
        printf("\nDelete SEM FLAG");
        if (semDelete(mySemFlag) == OK) printf(" OK!");
        else printf(" Error!");
    }
    if (mySemIntFlag != NULL) 
    {
        printf("\nDelete SEM INT FLAG");
        if (semDelete(mySemIntFlag) == OK) printf(" OK!");
        else printf(" Error!");
    }
/*  Delete Int task */
    if ( myIntId != 0 ) 
    {
        printf("\nReady to delete Int ID %x ... ",myIntId);
        if( taskDelete(myIntId) == OK ) printf(" OK!");
        else printf(" Error!");
    }
    myIntId = 0;
/*  Delete user task */
    if ( myTaskId0 != 0 ) 
    {
        printf("\nReady to delete task ID0 %x ... ",myTaskId0);
        if( taskDelete(myTaskId0) == OK ) printf(" OK!");
        else printf(" Error!");
    }
    myTaskId0 = 0;
    if ( myTaskId1 != 0 ) 
    {
        printf("\nReady to delete task ID1 %x ... ",myTaskId1);
        if( taskDelete(myTaskId1) == OK ) printf(" OK!");
        else printf(" Error!");
    }
    myTaskId1 = 0;

    printf("\n");
}

 
/*************************************************************************** 
* 
* myIntHandle - Interrupt Service Routine
* 
* This routine is the ISR. 
* When an interrupt occurs, this routine is called. At the end, send a 
* semaphore to myIntTask(). 
* In this routine, you could send message to the message receiver, 
* and also could display information by calling function logMsg(). 
* 
* RETURNS: N/A 
* 
* ERRNO: 
*  NONE 
*/ 
 
void myIntHandle(void)
{
#ifdef NEED_SEND_MSG_TO_OTHER     /* Send message to task */
    unsigned char intbuf[10];
    int i;

    for (i=0;i<10;i++) intbuf[i]=i;
    msgQSend(myMsgQIntId,intbuf,10,NO_WAIT,MSG_PRI_URGENT);
#endif /* NEED_SEND_MSG_TO_OTHER */

#ifdef NEED_DISPLAY_INFORMATION   /* print information in the ISR */
    char *name;
    int num;

    name = "GRONK";
    num = 123;
    logMsg ("ERROR - name = %s, num = %d.\n", (int)name, num, 0, 0, 0, 0);
/*
If the following code were executed by task 20, then the following error message 
would appear on the system log: 
    0x180400 (t20): ERROR - name = GRONK, num = 123.
*/
#endif /* NEED_DISPLAY_INFORMATION */

	semGive(mySemIntFlag);
	sysOutByte(0x20, 0x20);  /* sysOutByte (PIC_IACK (PIC1_BASE_ADR), 0x20); */
	sysOutByte(0xa0, 0x20);  /* sysOutByte (PIC_IACK (PIC2_BASE_ADR), 0x20); */
}

/*************************************************************************** 
* 
* myIntTask - interrupt task 
* 
* This task is created by myIntInitial(). 
* It keep waiting for a semaphore that sended by myIntHandle(). 
* At the end, give the semaphore. 
* 
* RETURNS: N/A 
* 
* ERRNO: 
*  NONE 
*/ 
 
void myIntTask(void)
{
    short oldint;

    FOREVER {
        semTake(mySemIntFlag,WAIT_FOREVER);   

        while ( semTake(mySemFlag,SEM_DELAY) == ERROR ) ;  /* take mutex sem. */
        oldint=intLock();
/*
        .
        .
        .
*/
        intUnlock(oldint);
        semGive(mySemFlag);                                /* release mutex sem. */
    }
}

/*************************************************************************** 
* 
* myInitial - Initialization 
* 
* This routine initialize some semaphores. 
* 
* RETURNS: OK or ERROR if the semaphore creation is failed.
* 
* ERRNO: 
*  ERROR 
*/ 
 
STATUS myInitial(void)
{
    /* Create watchdog */
    if ((myWatchDogId = wdCreate()) == NULL) {
        printf("\nCreation WatchDog Error!");
        return (ERROR);
    }

    if ((myMsgQId = msgQCreate(10,0xff,MSG_Q_FIFO)) == NULL) {        /* create msg queue */
        printf("\nCreation MSG_Q Error!");
        return (ERROR);
    }
    if ((myMsgQIntId = msgQCreate(10,0xff,MSG_Q_PRIORITY)) == NULL) { /* create msg queue for Int. */
        printf("\nCreation MSG_Q_INT Error!");
        return (ERROR);
    }

/*  If the semaphore is full, the semaphore is made empty, and the calling task
    continues executing.*/
    if (mySemFlag == 0) {                                             /* create mutex Sem. */
	    if ((mySemFlag = semBCreate(SEM_Q_FIFO,SEM_FULL)) == NULL) {       
            printf("\nCreation SEM_Q Error!");
            return (ERROR);
        }
    }
	while ( semTake(mySemFlag,SEM_DELAY) == ERROR );  /* first take Sem. */
    /* ...
    ... */
	semGive(mySemFlag);                               /* then release Sem. */

/* If the semaphore is empty, the task is blocked, pending the
   availability of the semaphore.  If a timeout is specified and the timeout
   expires, the pended task is removed from the queue of pended tasks
   and enters the ready state with an ERROR status.  A pended task
   is ineligible for CPU allocation.  Any number of tasks may be pended
   simultaneously on the same binary semaphore.*/
    if (mySemIntFlag == 0) {                                          /* create interrupt Sem. */
        if ((mySemIntFlag = semBCreate(SEM_Q_FIFO,SEM_EMPTY)) == NULL) {
            printf("\nCreation SEM_Q_INT Error!");
            return (ERROR);
        }
    }

	semGive(mySemIntFlag);                            /* first release Sem. */
    semTake(mySemIntFlag,WAIT_FOREVER);               /* then take Sem. */
    /* ...
    ... */

    printf("\n");
    return (OK);
}

/*************************************************************************** 
* 
* myIntInitial - interrupt initialization task 
* 
* This routine connect interrupt to ISR and spawn interrupt task. 
* 
* RETURNS: Always OK 
* 
* ERRNO: 
*  NONE 
*/ 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -