📄 kslinux.c
字号:
//--------------------------------------------------------------------------
//
// KSLINUX.C (c) Copyright 2002 Micrel-Kendin Operations
//
//--------------------------------------------------------------------------
#include "ks95mspi.h"
#ifdef LINUX
#include <asm/MC68EZ328.h>
//--------------------------------------------------------------------------
//
//
//
// This module contains routines for the SPIM of Motorola Dragonball (MC68EX328)
// processor
//
//
//
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//
// void EnableLINUXkbhit
//
// Description:
// Change keyboard attributes to emulate DOS kbhit() function
//
// Parameters:
// None
//
// Return Vlaue:
// None
//
//--------------------------------------------------------------------------
void EnableLINUXkbhit()
{
tcgetattr(0, &orig);
new = orig;
new.c_lflag &= ~ICANON;
new.c_lflag &= ~ECHO;
new.c_lflag &= ~ISIG;
new.c_cc[VMIN] = 1;
new.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &new);
return;
}
//--------------------------------------------------------------------------
//
// void EnableLINUXkbhit
//
// Description:
// Restore keyboard attributes
//
// Parameters:
// None
//
// Return Vlaue:
// None
//
//--------------------------------------------------------------------------
//
void DisableLINUXkbhit ()
{
tcsetattr(0,TCSANOW, &orig);
return;
}
//--------------------------------------------------------------------------
//
// vint _kbhit
//
// Description:
// Emulate DOS _kbhit() function.
//
// Parameters:
// None
//
// Return Vlaue:
// 1 if a key troke was detected, 0 otherwise
//
//--------------------------------------------------------------------------
int _kbhit()
{
char ch;
int nread;
if(peek != -1) return 1;
new.c_cc[VMIN]=0;
tcsetattr(0, TCSANOW, &new);
nread = read(0,&ch,1);
new.c_cc[VMIN]=1;
tcsetattr(0, TCSANOW, &new);
if(nread == 1) {
peek = ch;
return 1;
}
return 0;
}
//--------------------------------------------------------------------------
//
// int _getch
//
// Description:
// Emulate DOS _getch() function.
//
// Parameters:
// None
//
// Return Vlaue:
// key value
//
//--------------------------------------------------------------------------
int _getch()
{
char ch;
if(peek != -1) {
ch = peek;
peek = -1;
return ch;
}
read(0,&ch,1);
return ch;
}
//--------------------------------------------------------------------------
//
// void InitSPIM
// Initialize Dragonball SPIM.
//
// Description:
//
// Parameters:
//
// Return Vlaue:
// SUCCESS or SPIMINITERROR
//
//--------------------------------------------------------------------------
int InitSPIM ()
{
PESEL &= 0xf8; // Port E is muxed with and SPIM
// Select Port E pins (0-2) to SPIM function
PBSEL |= 0x80; // Set port B bit 7 to I/O port function so we
// use it as GPIO to serve as SPI Slave Select
PBDIR |= 0x80; // Set to output
PBDATA |= 0x80; // Pull the signal high
#if 0
// Phase 1 operation
SPIMCONT |= SPIMCONT_PHA;
// Polarity is active high
SPIMCONT &= ~SPIMCONT_POL;
#endif
#if 1
SPIMCONT |= SPIMCONT_PHA;
SPIMCONT |= SPIMCONT_POL;
#endif
// Data rate is 16M Hz divide by 4
SPIMCONT &= ~SPIMCONT_DATA_RATE_MASK;
SPIMCONT |= SPIMCONT_DIVIDE_BY_4 << SPIMCONT_DATA_RATE_SHIFT;
// Bit count is 8
SPIMCONT &= ~SPIMCONT_BIT_COUNT_MASK;
SPIMCONT |= SPIMCONT_8_BIT;
if (( GetChipID() ) != KS8995MID )
{
return SPIMINITERROR;
}
return SUCCESS;
}
//--------------------------------------------------------------------------
//
// BYTE SPIMExchangeData
//
// Description:
// This is the core routine of SPI operation. It loads the output data and
// then trigger the SPI to exchange data.
//
// Parameters:
// BYTE cData Data written to the port.
//
// Return Vlaue:
// Data read from the port.
//
//--------------------------------------------------------------------------
BYTE SPIMExchangeData ( BYTE cData )
{
BYTE bTemp;
SPIMCONT &= ~SPIMCONT_IRQ; // Clear the interrupt bit
SPIMDATA = cData; // Load data to be transmitted
SPIMCONT |= SPIMCONT_XCH; // Trigger the exchange
// poll the IRQ bit
while (1)
{
if ( SPIMCONT & SPIMCONT_IRQ )
break;
}
SPIMCONT &= ~SPIMCONT_IRQ; // Clear the interrupt bit
bTemp = SPIMDATA; // Read the data from the external device
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 )
{
SPIMCONT |= SPIMCONT_ENABLE; // Enable SPIM module
PBDATA &= 0x7f; // Pull the Slave Select signal low
SPIMExchangeData ( SPI_WRITE );
SPIMExchangeData ( cReg );
SPIMExchangeData ( cData );
PBDATA |= 0x80; // Pull the Slave Select signal high
SPIMCONT &= ~SPIMCONT_ENABLE; // Disable SPIM module
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 )
{
SPIMCONT |= SPIMCONT_ENABLE; // Enable SPIM module
PBDATA &= 0x7f; // Pull the Slave Select signal low
SPIMExchangeData ( SPI_WRITE );
SPIMExchangeData ( cReg );
SPIMExchangeData ( 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 )
{
SPIMExchangeData ( 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 )
{
SPIMExchangeData ( cData );
PBDATA |= 0x80; // Pull the Slave Select signal high
SPIMCONT &= ~SPIMCONT_ENABLE; // Disable SPIM module
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 )
{
#if 0
BYTE bData;
#endif
int bData;
SPIMCONT |= SPIMCONT_ENABLE; // Enable SPIM module
PBDATA &= 0x7f; // Pull the Slave Select signal low
SPIMExchangeData ( SPI_READ );
SPIMExchangeData ( cReg );
bData = SPIMExchangeData ( 0 );
PBDATA |= 0x80; // Pull the Slave Select signal high
SPIMCONT &= ~SPIMCONT_ENABLE; // Disable SPIM module
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;
SPIMCONT |= SPIMCONT_ENABLE; // Enable SPIM module
PBDATA &= 0x7f; // Pull the Slave Select signal low
SPIMExchangeData ( SPI_READ );
SPIMExchangeData ( cReg );
bData = SPIMExchangeData ( 0 );
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 ( SPIMExchangeData ( 0 ));
}
//--------------------------------------------------------------------------
//
// 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 = SPIMExchangeData ( 0 );
PBDATA |= 0x80; // Pull the Slave Select signal high
SPIMCONT &= ~SPIMCONT_ENABLE; // Disable SPIM module
return ( bData );
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -