Write the method device driver under VxWorks.

xiaoxiao2021-03-06  112

1. Standard I / O Functions are Open, Create, Read, Write, IOCTL, Close, Remove.2, core structure A, Device list iOSDevShow or devs can view device B installed in the system B, driver description table iosDrvshow can view The number of drivers in the system and the address C, file descriptor table, I / O system of each IO function, and the means 3, fd = open ("/ xxdev", o_read The process of 0) is the first / xxdev file name to find the device (in the device list), find the driver index number according to the device description structure, find the driver, return to file descriptor 4, read (fd, & buf, nbytes) The process is to find the driver index number directly through the file descriptor table. Use the driver. 5, src6, .h // Data struct # ifndef __xyxtydrv_h __ # define __xyxtydrv_h__

#ifdef __cplusplusextern "c" {# ENDIF

TypedEf struct {dev_hed devhdr; bool iscreate; bool isopen; uint32 regmembase; uint32 oaddr; sleel_wakeup_list selwakeuplist; bool readytoread; bool readytowrite;} ttyxyx_dev;} TTYXYX_DEV;

#ifdef __cplusplus} #ENDIF

# ENDIF7, .C # include "xyxtydrv.h"

Local int TTYXYXDRVNUM = 0;

// install STATUS ttyXyxDrv () {if (ttyXyxDrvNum> 0) return (OK); // drv init code here // put the drv into drv table if (ttyXyxDrvNum = iosDrvInstall (ttyXyxOpen, ttyXyxDelete, ttyXyxOpen, ttyXyxClose, / ttyXyxRead, TTYXYXWRITE, TTYXYXIOCTL) == Error) {Return (Error);}

Return (OK);

// DRV CREATE

STATUS ttyXyxDevCreate (char * devName) {ttyXyx_DEV * pttyxyxDev; if (ttyXyxDrvNum <1) {errno = S_ioLib_NO_DRIVER; return (ERROR);} if ((pttyxyxDev = (ttyXyx_DEV *) malloc (sizeof (ttyXyx_DEV))) == NULL) {return (ERROR);} bzero (pttyxyxDev, sizeof (ttyXyx_DEV)); selWakeupListInit (& pttyxyxDev-> selWakeupList); // init pttyxyxDev if (iosDevAdd (& pttyxyxDev-> devHdr, devName, ttyXyxDrvNum) == ERROR) {free (( Char *) PTTYXYXDEV); Return (Error);} return (ok);

// open functionint ttyXyxOpen (DEV_HDR * pttyDevHdr, int option, int flags) {ttyXyx_DEV * pttyDev = (ttyXyxDEV *) pttyDevHdr; if (pttyDev == NULL) {errnoSet (S_xyx_NODEV); return (ERROR);} if (pttyDev- > isopen) {errnoset; return (error);} pttydev-> isopen = true; //....init return (int) pttydeDevhdr);} // read functionint ttyxyxread (int TTYDEVID, CHAR * PBUF, int nBytes) {ttyXyx_DEV * pttyXyxDev = (ttyXyx_DEV *) ttyDevId; int ReadLength = ERROR; BOOL FoundError; if (pttyXyxDev = (ttyXyx_DEV *) NULL) {errnoSet (S_xyx_NODEV); return ERROR;} if (pttyXyxDev-> ReadyToRead) { ReadLength = 0; WHILE (ReadLength

// Write Functionint TTYXYXWRITE (INT TTYDEVID, CHAR * PBUF, INT NBYTES) {TTYXYX_DEV * PTTYXYXDEV = (TTYXYX_DEV *) TTYDEVID; int WriteLength = 0; Bool Founderror;

if (pttyXyxDev == (ttyXyx_DEV *) NULL) {errnoSet (S_xyx_NODEV); return (ERROR);} if (pttyXyxDev-> ReadyToWrite) {pttyXyxDev-> ReadToWrite = FALSE; // write data to data // judge the state} PTTYXYXDEV-> ReadyToWrite = true; if (Founderror) Return (Error);

Return (WriteLength);

// ioctl functionint ttyXyxIoctl (int ttyDevId, int cmd, int arg) {int status; ttyXyx_DEV * pttyXyxDev = (ttyXyx_DEV *) ttyDevId; switch (cmd) {case FIOSELECT: selNodeAdd (& pttyXyxDev-> selWakeupList, (SEL_WAKUP_NODE *) arg) ; if ((selWakeupType ((SEL_WAKUP_NODE *) arg) == SELREAD) && (& pttyXyxDev-> ReadyToRead) selWakeup ((SEL_WAKUP_NODE *) arg); if ((selWakeupType ((SEL_WAKUP_NODE *) arg) == SELWRITE) && (& pttyXyxDev -> ReadyToWrite) selWakeup ((SEL_WAKUP_NODE *) arg); break; case FIOUNSELECt: selNodeDelete (& pttyXyxDev-> selWakeupList, (SEL_WAKUP_NODE *) arg); break; case xx_STATUS_GET: status = xxStatusGet (& arg); case xx_CONTROL_SET: status = xxCMDSet (arg); break; // other cmd default: errno = S_ioLib_UNKNOWN_REQUEST; status = ERROR;} return (status);} // close functionint ttyXyxClose (int ttyDevId) {ttyXyx_DEV * pttyXyxDev = (ttyXyx_DEV *) ttyDevId; if (pttyXyxDev = (TTYXYX_DEV *) NULL) {Errnoset (S_XYX_NOMEM); Return (Error);} // deal with equipment // Release Resourse FRE e (pttyxyxdev);

// unsintall functionSTATUS ttyXyxDelete (char * devName) {DEV_HDR * pDevHdr; char * pNameTail; // search dev pDevHdr = iosDevFind (devName, pNameTail); if (! PDevHdr == NULL || * pNameTail = '/ 0') return (Error);

// Release Resourse Sample Sam Wakeup Signal

// Uninstall iOSDevdelete (PDEVHDR); Return (OK);

LOCAL ULONG ttyXyxIntHandler (int ttyDevId) {ttyXyx_DEV * pttyXyxDev = (ttyXyx_DEV *) ttyDevId; if (pttyXyxDev = (ttyXyx_DEV *) NULL) {errnoSet (S_xyx_NoMEM); return (ERROR);} // read the interrupt status

// if can resceive pttyxyxdev-> readytoread = true;

转载请注明原文地址:https://www.9cbs.com/read-104073.html

New Post(0)