串口驅(qū)動(dòng)(Serial Drivers)編寫(xiě)實(shí)例解讀
[代碼步驟:Code Steps]
。初始化
。定義支持的串口通道數(shù)
。初始化驅(qū)動(dòng)的設(shè)備描述
。寫(xiě)你的設(shè)備初始化代碼
。寫(xiě)入口程序(entry point routines)
。寫(xiě)ISRs(中斷服務(wù)程序)來(lái)管理設(shè)備
。使用模板 wind/target/src/drv/ssio/templateSio.c
注意:串口驅(qū)動(dòng)在VxWorks系統(tǒng)開(kāi)始的代碼里初始化
[設(shè)備描述:The Device Descriptor]
。XX_DRV結(jié)構(gòu)每個(gè)通道有一個(gè)XX_CHAN
。每個(gè)XX_CHAN指向SIO_DRV_FUNCS
。SIO_DRV_FUNCS引誘驅(qū)動(dòng)的入口(entry point)
。XX_DRV是xxDrv使用的中心數(shù)據(jù)結(jié)構(gòu)
。xx_CHAN包括:
。xxDrv需要的通道特定信息
。指向驅(qū)動(dòng)SIO_DRV_FUNCS結(jié)構(gòu)的指針
。例:
/* device and channel structures */
typedef struct
{
/* must be first */
SIO_CHAN sio; /* standard SIO_CHAN element */
/* callbacks */
STATUS (*getTxChar) ();
STATUS (*putRcvChar) ();
void * getTxArg;
void * putRcvArg;
/* register addresses */
volatile char * cr; /* channel control register */
volatile char * dr; /* channel data register */
volatile char * sr; /* channel status register */
volatile char * ms; /* channel modem status register */
volatile char * mc; /* channel modem control register */
volatile short * br; /* channel baud constant register */
/* misc */
int mode; /* current mode (interrupt or poll) */
int baudFreq; /* input clock frequency */
int options; /* Hardware options */
} TEMPLATE_CHAN;
typedef struct
{
TEMPLATE_CHAN portA; /* DUSRAT has two channels */
TEMPLATE_CHAN portB;
volatile char * masterCr; /* master control register */
} TEMPLATE_DUSART;
[SIO_CHAN結(jié)構(gòu)]
。對(duì)一般字符驅(qū)動(dòng),一個(gè)指向DEV_FDR的指針被用來(lái)在驅(qū)動(dòng)和I/O system 之間通訊
。對(duì)一個(gè)串口驅(qū)動(dòng),一個(gè)指向SIO_CHAN的指針用來(lái)在驅(qū)動(dòng)和高層協(xié)議之間通訊
在 wind/target/h/sioLib.h;里,SIO_CHAN如下定義:
typedef struct sio_chan/* a serial channel */
{
SIO_DRV_FUNCS * pDrvFuncs;
/* device data */
} SIO_CHAN;
.由于高層協(xié)議不知道驅(qū)動(dòng)的XX_CHAN結(jié)構(gòu),SIO_CHAN被用來(lái)允許一個(gè)精心定義的數(shù)據(jù)類(lèi)型在協(xié)議間交換數(shù)據(jù)
.ttyDrv通過(guò)SIO_DRV_FUNCS里的入口向xxDrv發(fā)送信息,而xxDrv通過(guò)回調(diào)向ttyDrv發(fā)送信息
[入口:Entry Points]
xxCallBackInstall() 安裝到高層協(xié)議的入口(I/O system,target agent(目標(biāo)代理),等等)
xxPollOutPut() 輪巡模式輸出
xxPollInput() 輪巡模式輸入
xxIoctl() 支持設(shè)備特定的ioctl命令
xxTxStaartup() 初始化一個(gè)傳輸循環(huán)(transmit cycle)
[驅(qū)動(dòng)回調(diào)安裝程序]
int xxCallbackInstall(pSsioChan,callbackType,callback,callbbackArg)
pSioChan 指向SIO_CHAN的指針
callbackType SIO_CALLBACK_GET_TX_CHAR 或 SIO_CALLBACK_PUT_RCV_CHAR
callback 指向回調(diào)程序的指針
callbackArg 回調(diào)的參數(shù)
。初始化SIO_CHAN結(jié)構(gòu)里的特定成員
。返回OK 或 ENOSYS(當(dāng)callbackType不是上兩種中的一種)
/********************************************************************************
templateCallbackInstall - install ISR callbacks to get/put chars
This driver allows interrupt callbacks for transmitting characters
and receiving characters. In general, drivers may support other
types of callbacks too.
*
* RETURNS: OK on success, or ENOSYS for an unsupported callback type.*/
LOCAL int templateCallbackInstall
(
SIO_CHAN * pSioChan, /* channel */
int callbackType, /* type of callback */
STATUS (*callback)(), /* callback */
void * callbackArg /* parameter to callback */
)
{
TEMPLATE_CHAN * pChan = (TEMPLATE_CHAN *)pSioChan;
switch (callbackType)
{
case SIO_CALLBACK_GET_TX_CHAR:
pChan->getTxChar = callback;
pChan->getTxArg = callbackArg;
return (OK);
case SIO_CALLBACK_PUT_RCV_CHAR:
pChan->putRcvChar = callback;
pChan->putRcvArg = callbackArg;
return (OK);
default:
return (ENOSYS);
}
}
[驅(qū)動(dòng)初始化]
。參數(shù)是沒(méi)一個(gè)指向XX_DRV的指針
。初始化XX_CHAN
。帶你的程序的SIO_DRV_FUNCS
。傀儡回調(diào)
。所有設(shè)備特定
。重啟芯片
/* local variables */
LOCAL SIO_DRV_FUNCS templateSioDrvFuncs =
{
templateIoctl,
templateTxStartup,
templateCallbackInstall,
templatePollInput,
templatePollOutput
};
void templateDevInit
(
TEMPLATE_DUSART * pDusart
)
{
/* initialize each channel"s driver function pointers */
pDusart->portA.sio.pDrvFuncs = templateSioDrvFuncs;
pDusart->portB.sio.pDrvFuncs = templateSioDrvFuncs;
/* install dummy driver callbacks */
pDusart->portA.getTxChar = dummyCallback;
pDusart->portA.putRcvChar = dummyCallback;
pDusart->portB.getTxChar = dummyCallback;
pDusart->portB.putRcvChar = dummyCallback;
/* reset the chip */
TEMPLATE_REG_WRITE(pDusart, masterCr,
TEMPLATE_RESET_CHIP);
/* setting polled mode is one way to make the device quiet */
templateIoctl ((SIO_CHAN *)pDusart->portA, SIO_MODE_SET,
(void *)SIO_MODE_POLL);
templateIoctl ((SIO_CHAN *)pDusart->portB, SIO_MODE_SET,
(void *)SIO_MODE_POLL);
}
/*******************************************************************************
*
* dummyCallback - dummy callback routine
*
* RETURNS: ERROR.
*/
LOCAL STATUS dummyCallback (void)
{
return (ERROR);
}
linux操作系統(tǒng)文章專(zhuān)題:linux操作系統(tǒng)詳解(linux不再難懂)
評(píng)論