📄 kspport.c
字号:
//--------------------------------------------------------------------------
//
// KSPPORT.C (c) Copyright 2002 Micrel-Kendin Operations
//
//--------------------------------------------------------------------------
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "ks95mspi.h"
#ifdef DOSBOX
#include <conio.h>
void SPIMWrite ( BYTE bData );
BYTE SPIMRead ();
//--------------------------------------------------------------------------
//
//
//
// This module contains routines for SPI emulation with PC parallel port.
//
//
//
//--------------------------------------------------------------------------
//
// KS8995M Parallel Port
// +----------+
// | SPIS_N +---------+ (SS) pin [1] (Output)
// | |
// | SPID (Rx)+---------+ (Tx) pin [9] (Output)
// | |
// | SPIQ (Tx)+---------+ (Rx) pin [10] (Input)
// | |
// | SPIC +---------+ (CLK) pin [16] (Output)
// +----------+
//
//
// Parallel Port I/O
//
// Output to address 278h/378h/3BCh
//
// Bit 7 6 5 4 3 2 1 0
//
// Pin [9] 8 7 6 5 4 3 2
//
//
// Output to address 27Ah/37Ah/3BEh
//
// Bit 7 6 5 4 ~3 2 ~1 ~0
//
// Pin - - - IRQ 17 [16] 14 [1]
//
//
// Input from address 279h/379h/3BDh
//
// Bit 7 6 5 4 3 2 1 0
//
// Pin 11 [10] 12 13 15 - - -
//--------------------------------------------------------------------------
//
// BYTE SPIMWrite
//
// Description:
// MS Visual C++ does not support printf with type "%b".
//
// This routine converts the value to a string with binary digits.
//
// Parameters:
// int iIn Input value
// char * sOut Output binary string
// int iDigits Number of digits to convert
//
// Return Vlaue:
// None
//
//--------------------------------------------------------------------------
void ConvertToBinaryDigits ( int iIn, char * sOut, int iDigits )
{
int i;
for ( i = 0; i < iDigits; i++ )
{
sOut [ iDigits - i - 1 ] = '0' + ( ( iIn >> i ) & 0x01 );
}
sOut [ iDigits ] = '\0';
return;
}
//--------------------------------------------------------------------------
//
// BYTE SPIMWrite
//
// Description:
// This is the core routine of SPI operation emulation.
//
// Parameters:
// BYTE cData Data written to the port.
//
// Return Vlaue:
// None
//
//--------------------------------------------------------------------------
void SPIMWrite ( BYTE bData )
{
int i = 0;
for ( i = 7; i >= 0; i-- )
{
( bData & ( 0x01 << i ) ) ? _outp ( uSPIDPort, SPIDMASK ) : _outp ( uSPIDPort, ~SPIDMASK );
// Toggle the clock without effecting SS signal
_outp( uSPICPort, SPICMASK | SPISMASK );
_outp( uSPICPort, SPISMASK );
}
}
//--------------------------------------------------------------------------
//
// BYTE SPIMRead
//
// Description:
// This is the core routine of SPI operation emulation.
//
// Parameters:
// None
//
// Return Vlaue:
// None
//
//--------------------------------------------------------------------------
BYTE SPIMRead ()
{
int i;
BYTE bTemp = 0;
for ( i = 7; i >= 0; i-- )
{
bTemp |= ( _inp ( uSPIQPort ) & SPIQMASK ) ? 1 << i : 0;
// Toggle the clock without effecting SS signal
_outp( uSPICPort, SPICMASK | SPISMASK );
_outp( uSPICPort, SPISMASK );
}
return bTemp;
}
//--------------------------------------------------------------------------
//
// void WriteData
//
// Description:
// This is the high level SPIM routine. It calls SPIMExchangeData to write
// the data to SPI port.
//
// Parameters:
// BYTE cReg Register number
// BYTE cData Data to write
//
// Return Vlaue:
// None
//
//--------------------------------------------------------------------------
void WriteData ( BYTE cReg, BYTE cData )
{
_outp ( uSPISPort, SPISMASK );
SPIMWrite ( SPI_WRITE );
SPIMWrite ( cReg );
SPIMWrite ( cData );
_outp ( uSPISPort, _inp ( uSPICPort ) & ~SPISMASK );
return;
}
//--------------------------------------------------------------------------
//
// void WriteDataBegin
//
// Description:
// This is the high level SPIM routine. It calls SPIMExchangeData to write
// the data to SPI port.
//
// It leaves the SS signal unchanged after the data write so the follwoing
// calls to WriteDataContinue() or WriteDataEnd() can write data to cReg + 1
// without issuing SPI_WRITE command first.
//
// There is no disable SPIM module at the end of the rouine.
//
//
// Parameters:
// BYTE cReg Register number
// BYTE cData Data to write
//
// Return Vlaue:
// None
//
//--------------------------------------------------------------------------
void WriteDataBegin ( BYTE cReg, BYTE cData )
{
_outp ( uSPISPort, SPISMASK );
SPIMWrite ( SPI_WRITE );
SPIMWrite ( cReg );
SPIMWrite ( cData );
return;
}
//--------------------------------------------------------------------------
//
// void WriteDataContinue
//
// Description:
// This is the high level SPIM routine. It calls SPIMExchangeData to write
// the data to SPI port.
//
// This is a companion routine of WriteDataBegin(), it writes data to the register
// next to cReg of WriteDataCotinue().
//
// Parameters:
// BYTE cData Data to write
//
// Return Vlaue:
// None
//
//--------------------------------------------------------------------------
void WriteDataContinue ( BYTE cData )
{
SPIMWrite ( cData );
return;
}
//--------------------------------------------------------------------------
//
// void WriteDataEnd
//
// Description:
// This is the high level SPIM routine. It calls SPIMExchangeData to write
// the data to SPI port.
//
// This is a companion routine of WriteDataBegin() and WriteDataContinue(), it writes data
// to the register next to cReg of WriteDataBegin() or WriteDataContinue().
//
// It disables the SPIM after the write operation.
//
// Parameters:
// BYTE cData Data to write
//
// Return Vlaue:
// None
//
//--------------------------------------------------------------------------
void WriteDataEnd ( BYTE cData )
{
SPIMWrite ( cData );
_outp ( uSPISPort, _inp ( uSPICPort ) & ~SPISMASK );
return;
}
//--------------------------------------------------------------------------
//
// void ReadData
//
// Description:
// This is the high level SPIM routine. It calls SPIMExchangeData to read
// data from SPI port.
//
// Parameters:
// BYTE cReg Register number
//
// Return Vlaue:
// Byte read from the SPI port
//
//--------------------------------------------------------------------------
BYTE ReadData ( BYTE cReg )
{
BYTE bData;
_outp ( uSPISPort, SPISMASK );
SPIMWrite ( SPI_READ );
SPIMWrite ( cReg );
bData = SPIMRead ();
_outp ( uSPISPort, _inp ( uSPICPort ) & ~SPISMASK );
return ( bData );
}
//--------------------------------------------------------------------------
//
// void ReadDataBegin
//
// Description:
// This is the high level SPIM routine. It calls SPIMExchangeData to read
// data from SPI port.
//
// It leaves the SS signal unchanged after the data read so the follwoing
// calls to ReadDataContinue() or ReadDataEnd() can read data from cReg + 1 without
// issuing SPI_READ command first.
//
// There is no disable SPIM module at the end of the rouine.
//
//
// Parameters:
// BYTE cReg Register number
//
// Return Vlaue:
// Byte read from SPI port.
//
//--------------------------------------------------------------------------
BYTE ReadDataBegin ( BYTE cReg )
{
BYTE bData;
_outp ( uSPISPort, SPISMASK );
SPIMWrite ( SPI_READ );
SPIMWrite ( cReg );
bData = SPIMRead ();
return ( bData );
}
//--------------------------------------------------------------------------
//
// void ReadDataContinue
//
// Description:
// This is the high level SPIM routine. It calls SPIMExchangeData to read
// data from SPI port.
//
// This is a companion routine of ReadDataBegin(), it reads data from the register
// next to cReg of ReadDataCotinue().
//
// Parameters:
// BYTE cReg Register number
//
// Return Vlaue:
// Byte read from the port
//
//--------------------------------------------------------------------------
BYTE ReadDataContinue ()
{
return ( SPIMRead () );
}
//--------------------------------------------------------------------------
//
// void ReadDataEnd
//
// Description:
// This is the high level SPIM routine. It calls SPIMExchangeData to read
// the data from SPI port.
//
// This is a companion routine of ReadDataBegin() and ReadDataContinue(), it writes data
// to the register next to cReg of ReadDataBegin() or ReadDataContinue().
//
// It disables the SPIM after the write operation.
//
// Parameters:
// BYTE cData Data to write
//
// Return Vlaue:
// None
//
//--------------------------------------------------------------------------
BYTE ReadDataEnd ()
{
BYTE bData;
bData = SPIMRead ();
_outp ( uSPISPort, _inp ( uSPICPort ) & ~SPISMASK );
return ( bData );
}
int ProcessConfig ()
{
#define CFGFILE "pspi.cfg"
#define PORTCFG "port"
FILE * pCfgFile = NULL;
char acBuffer[ MAXLINE ];
char * pStr;
unsigned int uValue;
pCfgFile = fopen( "pspi.cfg", "r" ) ;
if ( !pCfgFile )
return SUCCESS;
while ( fgets( acBuffer, MAXLINE, pCfgFile ) )
{
if ( acBuffer[0] != '#' && acBuffer[0] != '\0' ) // Skip all comment and NULL lines
{
// Shaping the command string. converting the string to all lowercase and removing the extra blanks.
ConvertToLowerRemoveExtraBlanks ( acBuffer );
if ( strlen ( acBuffer ) )
{
pStr = strchr ( acBuffer, DELIMITER );
if ( pStr )
{
if ( GetHex ( pStr + 1, &uValue ) != SUCCESS )
return FAIL;
*pStr = '\0';
if ( !strcmp ( acBuffer, PORTCFG ) )
{
uParallelBase = uValue;
}
else
printf ("\n=%s", acBuffer);
*pStr = DELIMITER;
}
}
}
}
return SUCCESS;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -