vxWorks字符设备驱动模板
2008-10-14 17:40
288 查看
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 <logLib.h>
#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);
}
#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 <logLib.h>
#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);
}
相关文章推荐
- linux字符设备驱动一般模板
- Linux驱动开发--Linux字符设备驱动模板
- linux2.6字符设备驱动开发模板
- linux字符设备驱动开发模板及Makefile
- 基于VxWorks的VxBus字符设备驱动
- linux字符设备驱动开发模板及Makefile
- 写Linux字符设备驱动模板--测试通过
- Linux驱动开发之字符设备模板
- 字符设备驱动模板
- 【Linux内核驱动】字符设备驱动框架模板
- linux字符设备驱动模板(新标准)
- 标准的字符设备驱动的模板
- vxworks字符设备驱动傻瓜教程
- linux驱动学习之字符设备驱动模板
- 字符设备驱动第八课----互斥锁
- 驱动学习第2天 之 最简单的字符设备驱动testchr
- 字符设备驱动(一)
- 字符设备驱动3:驱动代码与文件系统中的设备信息的关系
- scull0字符设备驱动示例(参考《Linux设备驱动程序》)
- 基于ARM9开发板的按键字符设备驱动实现