我用Driver Wizard生成的一个工程里用的这个函数指定中断函数的,但是结果总是失败ucb.istHandle = RtAttachInterruptVector (
NULL, // security attributes (default)
0, // stack size (default)
DeviceISR, // pointer to Interrupt Service Routine (ISR).
(void *) &pContext, // context argument
IST_PRIORITY, // thread priority for the handler routine
Isa, // interface type
0, // bus number
ucb.irq, // interrupt level
ucb.intVector // interrupt vector
);
其中的DeviceISR定义如下
INTERRUPT_DISPOSITION RTFCNDCL DeviceISR (PVOID pContext);
ucb.irq 为0x4,也就是COM1的irq
ucb.intVector为0x0c,
执行完以后ucb.istHandle总是NULL,有没有大侠指点一下为什么会这样子
NULL, // security attributes (default)
0, // stack size (default)
DeviceISR, // pointer to Interrupt Service Routine (ISR).
(void *) &pContext, // context argument
IST_PRIORITY, // thread priority for the handler routine
Isa, // interface type
0, // bus number
ucb.irq, // interrupt level
ucb.intVector // interrupt vector
);
其中的DeviceISR定义如下
INTERRUPT_DISPOSITION RTFCNDCL DeviceISR (PVOID pContext);
ucb.irq 为0x4,也就是COM1的irq
ucb.intVector为0x0c,
执行完以后ucb.istHandle总是NULL,有没有大侠指点一下为什么会这样子
然后编译运行,在RtOpenSerialPort ( )函数下断点,发现ucb.irq 为0x4,ucb.intVector为0x0c, 并且ucb.istHandle是0x00100000WORD
RtOpenSerialPort ( )
{
//
// Setup access to the I/O space, this is not required in an
// RTSS process
//
if ( !RtEnablePortIo ( ucb.baseAddress, COM_IO_RANGE ) )
{
return ERROR_PORT_IO_FAILURE;
}
//
// Attach to the interrupt vector and setup IST
//
ucb.istHandle = RtAttachInterruptVectorEx (
NULL, // security attributes (default)
0, // stack size (default)
DeviceIST, // pointer to Interrupt Service Thread (IST).
NULL, // context argument
IST_PRIORITY, // thread priority for the handler routine
Isa, // interface type
0, // bus number
ucb.irq, // interrupt level
ucb.intVector, // interrupt vector
TRUE, //prepared to share the interrupt vector
LevelSensitive, // interrupt Mode
DeviceISR // pointer to Interrupt Service Routine (ISR)
);
if ( ucb.istHandle == NULL)
{
return( ERROR_CONNECT_INTERRUPT_FAILED );
}
//
// Set up Port based on information UART control block
//
InitializePort ( ); return ERROR_NORMAL;
} 我将 RtAttachInterruptVectorEx 的参数换成 DeviceISR 后,ucb.istHandle也照样不为空,你用的串口调试工具试试吧,看看你的串口有没有问题。反正我这儿是一切正常。
读写寄存器正常,但始终进不了中断啊
代码如下:WORD InitializePort( UCB *ucb )
//----------------------------------------------------------------------------
// function: InitializePort
//
// description: Initializes a COM port using the UART Control Block (UCB)
// as follows:
// - Disable all interrupts for the port
// - Set the baud rate
// - Set the Line Control Register (i.e. stop bits, parity & word size )
// - Set the Fifo Control Register
// - Set the Modem Control Register
// - Enable Interrupts
//
//-----------------------------------------------------------------------------
{
BYTE tempH;
BYTE tempL;
BYTE tempRegister;
RtWritePortUchar(ucb->baseAddress + UART_IER, 0 );; //禁止中断(0x3f9)
//-------------------------------------------------
// Set up the baud rate. First turn on DLAB.
//------------------------------------------------- //计算机串口时钟频率 Fpclk=1.8432MHz ------------------------------- (Fpclk>>4)/baud
RtWritePortUchar(ucb->baseAddress + UART_LCR, LCR_DIVISOR_LATCH ); //准备设置波特率(0x3fb) tempL=RtReadPortUchar( ucb->baseAddress + UART_DIV_LATCH_LOW);
tempH=RtReadPortUchar( ucb->baseAddress + UART_DIV_LATCH_HIGH);
RtPrintf("baud H %x L %x \n",tempH,tempL);
RtWritePortUchar( ucb->baseAddress + UART_DIV_LATCH_LOW, // (0x3f8)
baudRateTable[ucb->baudRate].DLLow );
RtWritePortUchar( ucb->baseAddress + UART_DIV_LATCH_HIGH,
baudRateTable[ucb->baudRate].DLHigh ); tempL=RtReadPortUchar( ucb->baseAddress + UART_DIV_LATCH_LOW);
tempH=RtReadPortUchar( ucb->baseAddress + UART_DIV_LATCH_HIGH);
RtPrintf("baud H %x L %x \n",tempH,tempL); RtWritePortUchar(ucb->baseAddress + UART_LCR, 0 ); //取消设置波特率(0x3fb)
//----------------------------------------------------
// now set up the line control register
//----------------------------------------------------
tempRegister = ucb->stopBits | ucb->parity | ucb->wordSize; //设置数据帧格式(位数、奇偶校验、停止位数)(0x3fb)
RtWritePortUchar(ucb->baseAddress + UART_LCR, tempRegister );
//------------------------------------------------------
// set up the FIFO
//------------------------------------------------------
tempRegister = RtReadPortUchar( ucb->baseAddress + UART_FCR ); //FIFO设置 (0x3fa)
tempRegister |= ucb->fifoMask;
RtWritePortUchar(ucb->baseAddress + UART_FCR, tempRegister );
//-------------------------------------------------------
//complete by setting up the Modem Control Register and
//enabling interrupts
//--------------------------------------------------------
tempL=RtReadPortUchar(ucb->baseAddress + UART_IER );
RtPrintf("IER %x \n",tempL);
RtWritePortUchar(ucb->baseAddress + UART_IER, IER_RECEIVE_DATA | IER_TRANSMIT_DATA ); //开中断(0x3f9)
tempL=RtReadPortUchar(ucb->baseAddress + UART_IER );
RtPrintf("IER %x \n",tempL);// if ( ucb->flowControl ) //流控 (0x3fc)
// {
// RtWritePortUchar(ucb->baseAddress + UART_MCR, MCR_OUT2 | MCR_RTS | MCR_DTR );
// }
// else
{
RtWritePortUchar(ucb->baseAddress + UART_MCR, MCR_OUT2 );
}
// ReadPic(); //debug
return( COM_NORMAL );
}void
_cdecl
main(
int argc,
char **argv,
char **envp
)
{
//test data
UCHAR Buffer0[64]="abcdefghijklmnopqrstuvwxyz";
WORD uLen,uWrted; RtCloseComPort(COM1);
RtPrintf("Start COM test------------\n"); //Search for the device using vendor and device IDs
RtOpenComPort(COM1,COM_BAUD_RATE_9600,COM_WORDSIZE_8,COM_STOPBITS_1,COM_PARITY_NONE);
//
//1st, we test the polled mode
//
Sleep(10); for(;;)
{
RtPrintf("Sending-----------\n");
uWrted = RtWriteComPort(COM1,Buffer0,10,&uLen);
RtPrintf("Has Sended %d -\n",uWrted);
Sleep(1000);
}
Sleep(5000);
RtPrintf("Test End!\n");
RtSuspendThread(RtGetCurrentThread()); ExitProcess(0);
}