📄 foodrv.c
字号:
/*
DESCRIPTION
This is a driver template for a virtual foo device in vxworks system
modification history
--------------------
00a,27Feb04 initialization.
*/
/************************************************/
/* */
/* Source files: */
/* */
/* FooDrv.c */
/* FooDrv.h */
/* FooTest.c */
/* */
/* */
/* Associat files: */
/* none */
/* */
/* Install: */
/* */
/* 1> Add FooHwInit() to sysLib.c */
/* 2> Add FooDrvInit() to usrAppInit.c */
/* */
/* */
/* Useage: */
/* 1> open device "/foo" */
/* 2> set parameters by ioctl() */
/* 3> do read or write operations */
/* on the device */
/* 4> close the device */
/* */
/* Examples: */
/* */
/* 1>foodrvtest */
/* Foo driver test */
/* */
/* Modify this template to become your own */
/* device driver: */
/* */
/* 1> Replace all "Foo" with your driver */
/* name in all the three file. */
/* Remember that replacing with case */
/* sensitively */
/* */
/* 2> Connecting the virtual functions */
/* with your relative api functions */
/* */
/* 3> Compile and test, enjoy your work! */
/* */
/************************************************/
/* includes */
#include "vxWorks.h"
#include "stdio.h"
#include "stdlib.h"
#include "ioslib.h"
#include "intLib.h"
#include "sysLib.h"
#include "logLib.h"
#include "time.h"
#include "FooDrv.h"
/* globals */
int FooDrvNum;
STATUS FooDrv(void);
STATUS FooDevCreate(void);
int FooOpen(DEV_HDR * pDevHdr, char * name, int flags);
int FooClose(int fd);
int FooRead (int deviceId, unsigned char *pBuf, int nBytes);
int FooWrite (int deviceId, unsigned char *pBuf, int nBytes);
int FooIoctl (int deviceId, int cmd, int arg);
typedef struct
{
DEV_HDR devHdr;
int temp;
} Foo_DEV;
typedef struct
{
Foo_DEV * pFooDev;
int offset;
} Foo_DEVpf;
/* hardware access methods */
#ifndef GPIO_REG_READ
#define GPIO_REG_READ(x,result) ((result) = *(volatile UINT32 *)(x))
#endif /*GPIO_REG_READ*/
#ifndef GPIO_REG_WRITE
#define GPIO_REG_WRITE(x,data) (*((volatile UINT32 *)(x))) = (data)
#endif /*GPIO_REG_WRITE*/
void FooDrvInit(void)
{
FooDrv();
FooDevCreate();
return;
}
STATUS FooHwInit(void)
{
/* you can write some hardware configuration scripts in here */
/* but if there has something wrong with the vxworks kernel, */
/* such as kernel crash or rebooting repeatedly.Please do */
/* your hardware configuration IN romInit.s !! */
/* */
/* BTW. I suggest you locate this function in your api source */
/* file ,not here! */
return (OK);
}
STATUS FooDrv(void){
/*
l Installs driver in driver table -- iosDrvInstall( )
l Performs any driver initialization
l Called once and once only
*/
int Result;
/* if a Foo driver has already being installed, just return OK. */
if(FooDrvNum > 0)
return (OK);
/* driver initialization */
/* config gpio port*/
/* add Foo driver to driver table */
if((FooDrvNum = iosDrvInstall
(
FooOpen, /* pointer to driver create function */
NULL, /* pointer to driver delete function */ /*not used*/
FooOpen, /* pointer to driver open function */
FooClose, /* pointer to driver close function */
FooRead, /* pointer to driver read function */
FooWrite, /* pointer to driver write function */
FooIoctl /* pointer to driver ioctl function */
)) == ERROR) /* if there has any errors, just return ERROR */
return (ERROR);
#ifdef FooDrv_debug
logMsg("Installing Foo driver\n",0,0,0,0,0,0);
#endif
return (OK);
}
STATUS FooDevCreate(void){
/*
l Adds device to the system device list -- iosDevAdd( )
l Performs any device initialization
l Called once for each device
*/
Foo_DEV * pFooDev;
/* Check if driver is installed */
if (FooDrvNum < 1)
{
errno = S_ioLib_NO_DRIVER;
return (ERROR);
}
/* Allocate and zero device structure */
if ((pFooDev = (Foo_DEV *) malloc (sizeof (Foo_DEV))) == NULL)
return (ERROR);
bzero (pFooDev, sizeof (Foo_DEV));
/* Init device specific portion of FOO_DEV here*/
/* Perform any device-specific initialization here*/
/* Add device to the device list */
if (iosDevAdd (&pFooDev->devHdr, FooDrvName, FooDrvNum) == ERROR)
{
free ((char *) pFooDev);
return (ERROR);
}
#ifdef FooDrv_debug
logMsg("Adding Foo device\n",0,0,0,0,0,0);
#endif
return (OK);
}
int FooOpen(DEV_HDR * pDevHdr, char * name, int flags)
{
Foo_DEVpf * pFooDevpf;
pFooDevpf = (Foo_DEVpf *) malloc( sizeof(Foo_DEVpf) );
if(pFooDevpf == NULL)
return (ERROR);
pFooDevpf->pFooDev = (Foo_DEV *) pDevHdr;
pFooDevpf->offset = 0;
#ifdef FooDrv_debug
logMsg("Foo Opening\n",0,0,0,0,0,0);
#endif
return ((int) pFooDevpf);
}
int FooClose(int fd)
{
#ifdef FooDrv_debug
logMsg("Foo Closing\n",0,0,0,0,0,0);
#endif
return (OK);
}
int FooRead (int deviceId, unsigned char *pBuf, int nBytes)
{
#ifdef FooDrv_debug
logMsg("Foo Reading\n",0,0,0,0,0,0);
#endif
return (OK);
}
int FooWrite (int deviceId, unsigned char *pBuf, int nBytes)
{
#ifdef FooDrv_debug
logMsg("Foo Writing\n",0,0,0,0,0,0);
#endif
return (OK);
}
int FooIoctl (int deviceId, int cmd, int arg){
#ifdef FooDrv_debug
logMsg("Foo Ioctling (%d,%d)\n",cmd,arg,0,0,0,0);
#endif
return (OK);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -