1. 引言
通用串行總線(USB,Universal Serial Bus)是現(xiàn)代PC數(shù)據(jù)傳輸?shù)陌l(fā)展趨勢,
PC的所有外設(shè),包括鍵盤、鼠標(biāo)、顯示器、打印機(jī)、錄音機(jī)、數(shù)字音響、電視機(jī)頂盒、數(shù)碼相機(jī)、掃描儀、MODEM及各種多媒體音頻、視頻設(shè)備均可通過USB接口接入PC。USB總線同步數(shù)據(jù)采集系統(tǒng)即為這種總線接入系統(tǒng)。
2. 硬件方案
本系統(tǒng)采用MAXIAM公司的MAX125四路12位同步采集芯片,只需一個啟動信號即可實現(xiàn)同步采集、數(shù)據(jù)轉(zhuǎn)換,完成后給出一個轉(zhuǎn)換完成信號,可從端口依次讀取A/D轉(zhuǎn)換數(shù)據(jù),送入單片機(jī)處理;USB接口芯片采用PHILIPS公司的PDIUSBD12,此芯片單片集成SIE、FIFO存儲器、收發(fā)器及電壓變換器, 并嚴(yán)格遵從USB1.1協(xié)議,PHILIPS SIE完成USB協(xié)議層,并且完全高速硬連接,無須任何軟件干預(yù)。此模塊功能包括:同步模式識別,并/串轉(zhuǎn)換,位填充/解填充,CRC檢驗/產(chǎn)生,PID確認(rèn)/產(chǎn)生,地址識別,握手響應(yīng)/產(chǎn)生;類似于控制其它接口芯片(如并口芯片)一樣控制此接口芯片,單片機(jī)將A/D轉(zhuǎn)換結(jié)果送至PIDUSBD12,PIDUSBD12將自動完成通過USB接口傳輸數(shù)據(jù)至PC的功能(按USB1.1協(xié)議), 具體實現(xiàn)電路如圖1
3. 軟件接口程序
3. 軟件接口程序
雖然Win95 OSR2.1 支持USB,但微軟推薦在win98或win2000上使用USB。軟件部分由WDM驅(qū)動程序和應(yīng)用程序組成。對于USB客戶驅(qū)動程序,主要是與USBDI打交道,下面的代碼完成了對于IRP_MJ_READ和IRP_MJ_WRITE的響應(yīng)。
/////////////////////////////////////////////////////////////////////////////
// UsbgatherRead:
//
// Description:
// Handle IRP_MJ_READ requests
//
// Arguments:
// Pointer to our FDO
// Pointer to the IRP
// IrpStack-》ParAMEters.Read.xxx has read parameters
// User buffer at: AssociatedIrp.SystemBuffer (buffered I/O)
// MdlAddrESS (direct I/O)
//
// Return Value:
// This function returns STATUS_XXX
NTSTATUS UsbgatherRead( IN PDEVICE_OBJECT fdo,
IN PIRP Irp)
{
PUSBgather_DEVICE_EXTENSION dx = (PUSBgather_DEVICE_EXTENSION)fdo-》DeviceExtension;
if( dx-》IODisabled)
return CompleteIrp( Irp, STATUS_DEVICE_NOT_CONNECTED, 0);
if (!LockDevice(dx))
return CompleteIrp( Irp, STATUS_DELETE_PENDING, 0);
PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
NTSTATUS status = STATUS_SUCCESS;
ULONG BytesTxd = 0;
// 得到參數(shù)
LONGLONG FilePointer = IrpStack-》Parameters.Read.ByteOffset.QuadPart;
ULONG ReadLen = IrpStack-》Parameters.Read.Length;
// 檢查文件指針
if( FilePointer《0)
status = STATUS_INVALID_PARAMETER;
else
{
status = UsbDoInterruptTransfer( dx, Irp-》AssociatedIrp.SystemBuffer, ReadLen);
BytesTxd = ReadLen;
}
// 完成 IRP
CompleteIrp(Irp,status,BytesTxd);
UnlockDevice(dx);
return status;
}
/////////////////////////////////////////////////////////////////////////////
// UsbgatherWrite:
//
// Description:
// Handle IRP_MJ_WRITE requests
//
// Arguments:
// Pointer to our FDO
// Pointer to the IRP
// IrpStack-》Parameters.Write.xxx has write parameters
// User buffer at: AssociatedIrp.SystemBuffer (buffered I/O)
// MdlAddress (direct I/O)
//
// Return Value:
// This function returns STATUS_XXX
NTSTATUS UsbgatherWrite( IN PDEVICE_OBJECT fdo,
IN PIRP Irp)
{
PUSBgather_DEVICE_EXTENSION dx = (PUSBgather_DEVICE_EXTENSION)fdo-》DeviceExtension;
if( dx-》IODisabled)
return CompleteIrp( Irp, STATUS_DEVICE_NOT_CONNECTED, 0);
if (!LockDevice(dx))
return CompleteIrp( Irp, STATUS_DELETE_PENDING, 0);
PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
NTSTATUS status = STATUS_SUCCESS;
ULONG BytesTxd = 0;
// 得到參數(shù)
LONGLONG FilePointer = IrpStack-》Parameters.Write.ByteOffset.QuadPart;
ULONG WriteLen = IrpStack-》Parameters.Write.Length;
if( FilePointer《0 || WriteLen《1)
status = STATUS_INVALID_PARAMETER;
else
{
// 僅寫一個字節(jié)
BytesTxd = 1;
PUCHAR pData = (PUCHAR)Irp-》AssociatedIrp.SystemBuffer;
UsbSendOutputReport( dx, *pData);
}
// 完成 IRP
CompleteIrp(Irp,status,BytesTxd);
UnlockDevice(dx);
return status;
}
應(yīng)用程序采用標(biāo)準(zhǔn)的文件操作方法。使用CreateFile API打開文件。使用WriteFile API發(fā)出開始命令,啟動ADC,使用ReadFile讀回采樣值。
4. 結(jié)束語
USB數(shù)據(jù)采集系統(tǒng)設(shè)計上嚴(yán)格遵循USB1.1協(xié)議,實現(xiàn)了USB的即插即用特性,可熱插拔,使用便捷;擴(kuò)展能力強(qiáng),可擴(kuò)展多達(dá)127個外設(shè),總帶寬達(dá)12Mbs;實現(xiàn)容易,為特殊設(shè)計PC提供了一種簡便易行的外設(shè)擴(kuò)展方案。
責(zé)任編輯:gt
評論