📄 driver.c
字号:
outportb(usBase+11, usCtr1Byte);
usLow2Byte = inportb(usBase+counter+8); /* read low byte */
usHigh2Byte = inportb(usBase+counter+8); /* read high byte */
}
else // configure 8254 chip 4
{
outportb(usBase+15, usCtr1Byte);
usLow2Byte = inportb(usBase+(counter%3)+12); /* read low byte */
usHigh2Byte = inportb(usBase+(counter%3)+12); /* read high byte */
}
// forth : ? frequency
usHigh1Byte <<= 8;
usCurrentCount1 = usHigh1Byte | usLow1Byte;
usHigh2Byte <<= 8;
usCurrentCount2 = usHigh2Byte | usLow2Byte;
if (usCurrentCount1 == usCurrentCount2)
{
*freq = 0;
return 0L;
}
if (usCurrentCount2 > usCurrentCount1)
return((ULONG) FreqMeasurementFailed);
*freq = (FLOAT)(((FLOAT)65536 - (FLOAT)usCurrentCount2)
* fSampleRate);
return 0L;
}
//------------------------------------------------------------------
// Function : CounterReset
// PURPOSE : Turns off the spefified counter operation. This function
// supports the boards with the timer/counter chip.
// Parameters : DeviceNumber(IN), counter(IN)
// Return : NULL (success)
// ErrorCode (failure)
// Call/Called procedure cross reference :
// Call Called Explanation
//------------------------------------------------------------------
ULONG CounterReset(USHORT DeviceNumber,USHORT counter)
{
USHORT usBase, usCtrByte;
// Is DeviceNumber out of range ?
if (DeviceNumber > MaxBoardNumber)
return ((ULONG) InvalidDeviceNumber);
// Is BaseAddr valid ?
if (usBaseAddr[DeviceNumber] == NULL)
return ((ULONG) LostBaseAddr);
// check input param correct ?
if (counter > MaxCounterNumber)
return((ULONG) InvalidCounterChannel);
// get base address
usBase = usBaseAddr[DeviceNumber];
usCtrByte = (counter%3) << 6;
usCtrByte = usCtrByte | 0x39;
// stop counting by setting counter chan for mode 5
if (counter < 3) // configure 8254 chip 3
{
outportb(usBase+11, usCtrByte);
outportb(usBase+counter+8, 0x0); /* write low byte */
outportb(usBase+counter+8, 0x0); /* write high byte */
}
else // configure 8254 chip 4
{
outportb(usBase+15, usCtrByte);
outportb(usBase+(counter%3)+12, 0x0); /* write low byte */
outportb(usBase+(counter%3)+12, 0x0); /* write high byte */
}
return 0L;
}
//------------------------------------------------------------------
// Function : DioReadPortByte()
// PURPOSE : Returns digital input data from the specified digital
// I/O port.
// Parameters : DeviceNumber(IN)
// port(IN)
// value(OUT)
// Return : TRUE (success), FALSE (failure)
//
// Call/Called procedure cross reference :
// Call Called Explanation
// DiSection
//------------------------------------------------------------------
ULONG DioReadPortByte(USHORT DeviceNumber, USHORT port,USHORT * value)
{
return(DiSection(DeviceNumber, port, (USHORT *)value));
}
//------------------------------------------------------------------
// Function : DioWritePortByte()
// PURPOSE : Writes digital output data to the specified the port.
// I/O port.
// Parameters : DeviceNumber(IN)
// port(IN)
// mask(IN)
// state(IN)
// Return : TRUE (success), FALSE (failure)
//
// Call/Called procedure cross reference :
// Call Called Explanation
// DoSection
//------------------------------------------------------------------
ULONG DioWritePortByte(USHORT DeviceNumber,
USHORT port, USHORT mask, USHORT state)
{
return(DoSection(DeviceNumber, port, mask, state));
}
//------------------------------------------------------------------
// Function : DioReadBit()
// PURPOSE : Returns the bit state of digital input from the specified
// digital I/O port.
// Parameters : DeviceNumber(IN)
// port(IN)
// bit(IN)
// state(OUT)
// Return : TRUE (success), FALSE (failure)
//
// Call/Called procedure cross reference :
// Call Called Explanation
// DiSection
//------------------------------------------------------------------
ULONG DioReadBit(USHORT DeviceNumber,
USHORT port, USHORT bit, USHORT * state)
{
USHORT usMask;
ULONG retCode;
if (bit > 7)
return((ULONG) InvalidInputParam);
retCode = DiSection(DeviceNumber, port, (USHORT *)state);
if (retCode == 0L)
{
usMask = 0x01;
usMask = usMask << bit;
*state = *state & usMask;
if (*state != 0)
*state = 1;
}
return((ULONG) retCode);
}
//------------------------------------------------------------------
// Function : DioWriteBit()
// PURPOSE : Writes digital output data to the specified bit.
// I/O port.
// Parameters : DeviceNumber(IN)
// port(IN)
// state(IN)
// Return : TRUE (success), FALSE (failure)
//
// Call/Called procedure cross reference :
// Call Called Explanation
// DoSection
//------------------------------------------------------------------
ULONG DioWriteBit(USHORT DeviceNumber,
USHORT port, USHORT bit, USHORT state)
{
USHORT usMask;
ULONG retCode;
if (bit > 7)
return((ULONG) InvalidInputParam);
usMask = 0x01;
usMask = usMask << bit;
state = state << bit;
return((ULONG) DoSection(DeviceNumber, port, usMask, state));
}
//------------------------------------------------------------------
// Function : DioGetCurrentDOByte()
// PURPOSE : Returns digital input data from the specified digital
// I/O port.
// Parameters : DeviceNumber(IN)
// port(IN)
// value(OUT)
// Return : TRUE (success), FALSE (failure)
//
// Call/Called procedure cross reference :
// Call Called Explanation
//------------------------------------------------------------------
ULONG DioGetCurrentDOByte(USHORT DeviceNumber, USHORT port, USHORT * value)
{
return((ULONG) GetDoStatus(DeviceNumber, port, (USHORT *)value));
}
//------------------------------------------------------------------
// Function : DioGetCurrentDOBit()
// PURPOSE : Returns the bit data of digital input from the specified
// digital I/O port.
// Parameters : DeviceNumber(IN)
// port(IN)
// bit(IN)
// state(OUT)
// Return : TRUE (success), FALSE (failure)
//
// Call/Called procedure cross reference :
// Call Called Explanation
//------------------------------------------------------------------
ULONG DioGetCurrentDOBit(USHORT DeviceNumber,
USHORT port, USHORT bit, USHORT * state)
{
USHORT usMask;
ULONG retCode;
if (bit > 7)
return((ULONG) InvalidInputParam);
retCode = GetDoStatus(DeviceNumber, port, (USHORT *)state);
if (retCode == 0L)
{
usMask = 0x01;
usMask = usMask << bit;
*state = *state & usMask;
if (*state != 0)
*state = 1;
}
return((ULONG) retCode);
}
//------------------------------------------------------------------
// Function : DoSection()
// PURPOSE : Writes digital output data to the specified the port.
// I/O port.
// Parameters : DeviceNumber(IN)
// usChlNum(IN)
// usmask(IN)
// usData(IN)
// Return : None
// Call/Called procedure cross reference :
// Call Called Explanation
// DioWritePortByte
// DioWriteBit
//------------------------------------------------------------------
ULONG DoSection(USHORT DeviceNumber, USHORT usChlNum,
USHORT usMask, USHORT usData)
{
USHORT usBase;
// Is DeviceNumber out of range ?
if (DeviceNumber > MaxBoardNumber)
return ((ULONG) InvalidDeviceNumber);
// Is BaseAddr valid ?
if (usBaseAddr[DeviceNumber] == NULL)
return ((ULONG) LostBaseAddr);
// check input param correct ?
if (usChlNum > MaxDioNumber)
return((ULONG) InvalidPortChannel);
// get base address
usBase = usBaseAddr[DeviceNumber];
usData = usData & 0x00FF;
usMask = usMask & 0x00FF;
/* set the bits enable by the mask word */
/* to values specified by the data word */
/* leaving all other bits in the port unchange */
usData = (usData & usMask) |
(usPreState[DeviceNumber][usChlNum] & ~(usMask));
/* write the desired output state to the hardware */
outportb(usBase+usChlNum+16, usData);
/* save current channel output state */
usPreState[DeviceNumber][usChlNum] = usData;
return((ULONG) 0L);
}
//------------------------------------------------------------------
// Function : DiSection()
// PURPOSE : Returns digital input data from the specified digital
// I/O port.
// Parameters : DeviceNumber(IN)
// usChlNum(IN)
// usData(OUT)
// Return : None
// Call/Called procedure cross reference :
// Call Called Explanation
// DioReadPortByte
// DioReadBit
//------------------------------------------------------------------
ULONG DiSection(USHORT DeviceNumber, USHORT usChlNum, USHORT * usData)
{
USHORT usBase;
// Is DeviceNumber out of range ?
if (DeviceNumber > MaxBoardNumber)
return ((ULONG) InvalidDeviceNumber);
// Is BaseAddr valid ?
if (usBaseAddr[DeviceNumber] == NULL)
return ((ULONG) LostBaseAddr);
// check input param correct ?
if (usChlNum > MaxDioNumber)
return((ULONG) InvalidPortChannel);
// get base address
usBase = usBaseAddr[DeviceNumber];
*usData = inportb(usBase+usChlNum+16);
return((ULONG) 0L);
}
//------------------------------------------------------------------
// Function : GetDoStatus()
// PURPOSE : Returns digital output data from the specified digital
// I/O port or lpDevInfo.
// Parameters : DeviceNumber(IN)
// usChlNum(IN)
// usData(OUT)
// Return : None
// Call/Called procedure cross reference :
// Call Called Explanation
// DioReadPortByte
// DioReadBit
//------------------------------------------------------------------
ULONG GetDoStatus(USHORT DeviceNumber, USHORT usChlNum, USHORT * usData)
{
// check input param correct ?
if (usChlNum > MaxDioNumber)
return((ULONG) InvalidPortChannel);
*usData = usPreState[DeviceNumber][usChlNum];
return((ULONG) 0L);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -