分类: LINUX
2008-08-22 22:10:28
file fooDrv.h
#ifndef __INCfooDrv
#define __INCfooDrv
#include "vxWorks.h"
#include "sioLib.h"
STATUS fooDrv();
STATUS fooDevCreate(char *devName);
STATUS fooDelete(char *devName);
int fooOpen(DEV_HDR *pfooDevHdr, char *devName, int option, int flags);
int fooClose(int fooDevId);
int fooRead(int fooDevId, char *pBuf, int nBytes);
int fooWrite(int fooDevId, char *pBuf, int nBytes);
int fooIoctl(int fooDevId, int cmd, int arg);
#endif
/*****************************************************************************/
file fooDrv.c
#include "vxWorks.h"
#include "ioLib.h"
#include "iosLib.h"
#include "stat.h"
#include "dirent.h"
#include "memLib.h"
#include "errnoLib.h"
#include "string.h"
#include "stdlib.h"
#include "stdio.h"
#include
#include "foo.h"
#include "fooDrv.h"
typedef struct
{
DEV_HDR devHdr;
BOOL isOpen;
}fooDev;
LOCAL int fooDrvNum = 0;
STATUS fooDrv()
{
/* 驱动是否已经安装过 */
if (fooDrvNum > 0)
return (OK);
if((fooDrvNum = iosDrvInstall(fooOpen, NULL, fooOpen, fooClose,
fooRead, fooWrite, fooIoctl)) == ERROR)
return (ERROR);
return (OK);
}
STATUS fooDevCreate(char *devName)
{
fooDev *pfooDev;
/* 检查驱动是否已经安装过 */
if (fooDrvNum < 1)
return (ERROR);
if ((pfooDev = (fooDev *)malloc(sizeof(fooDev))) == NULL)
return (ERROR);
memset(pfooDev, 0x00, sizeof(fooDev));
pfooDev->isOpen = FALSE;
/* 添加设备 */
if (iosDevAdd(&pfooDev->devHdr, devName, fooDrvNum) == ERROR)
{
free((char *)pfooDev);
return (ERROR);
}
return (OK);
}
STATUS fooDevDelete(char *devName)
{
DEV_HDR *pDevHdr;
char *pNameTail;
pDevHdr = iosDevFind(devName, &pNameTail);
if (pDevHdr == NULL || *pNameTail != '\0')
return (ERROR);
iosDevDelete(pDevHdr);
return (OK);
}
int fooOpen(DEV_HDR *pdevHdr, char *devName, int option, int flags)
{
fooDev *pdevFd = (fooDev *)pdevHdr;
if (pdevFd == NULL)
return ERROR;
if (pdevFd->isOpen == TRUE)
return ERROR;
pdevFd->isOpen = TRUE;
/* 硬件初始化 */
return (int) pdevFd;
}
int fooClose(int devId)
{
fooDev *pdevFd = (fooDev *)devId;
if (pdevFd == NULL)
return (ERROR);
pdevFd->isOpen = FALSE;
return (OK);
}
int fooRead(int devId, char *pBuf, int nBytes)
{
int readNum = 0;
fooDev *pdevFd = (fooDev *)devId;
if (pdevFd == NULL)
return (ERROR);
/* 读操作 */
return readNum;
}
int fooWrite(int devId, char *pBuf, int nBytes)
{
fooDev *pdevFd = (fooDev *)devId;
if (pdevFd == NULL)
return (ERROR);
//写操作
return nBytes;
}
int fooIoctl(int devId, int cmd, int arg)
{
fooDev *pdevFd = (fooDev *)devId;
if (pdevFd == NULL)
return (ERROR);
/* 控制操作 */
return 0;
}
/*****************************************************************************/
file fooDrvTest.c
#include "selectlib.h"
#include "vxWorks.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "ioLib.h"
#include "iosLib.h"
#include "memLib.h"
#include "errnoLib.h"
#include "string.h"
#include "fooDrv.h"
void fooDrvTest()
{
int devFd = 0;
char readBuf[15];
char writeBuf[15];
int ix = 0;
int readNum = 0;
memset(readBuf, 0, sizeof(readBuf));
memset(writeBuf, 0, sizeof(writeBuf));
//填充buf中的数据
devFd = open("/xxx", O_RDWR, 0);
if (devFd == ERROR )
{
printf("open file error\n");
return;
}
write(devFd, writeBuf, sizeof(writeBuf));
for (ix = 0; ix < 15; ix ++)
printf("writeBuf[%d] is %x\n", ix, writeBuf[ix]);
readNum = read(devFd, readBuf, sizeof(readBuf));
printf("read %d char\n", readNum);
for (ix = 0; ix < 15; ix ++)
printf("readBuf[%d] is %x\n", ix, readBuf[ix]);
close(devFd);
}