#include <windows.h>
#include <types.h>
//#include <drv_glob.h>
#include <nkintr.h>
//#include <oalintr.h>
#include <MCP2510.h>
unsigned short ReadBuf[256] ;
//TE6410定义CAN的接口
/***************************************************************************
MCP2510_SO GPC4 input ( SPIMISO1 )
MCP2510_SCK GPC5 output ( SPICLK1 )
MCP2510_SI GPC6 output ( SPIMOSI1 )
MCP2510_CS GPC7 output ( nSS1 )
MCP2510_INT GPL10 input ( EINT18 )
****************************************************************************/
#define GPIOCON v_pIOPregs->GPCCON
#define GPIODAT v_pIOPregs->GPCDAT
TCHAR read_buf[256] ;
// Pointer to device control registers
volatile S3C6410_GPIO_REG *v_pIOPregs;
BOOL InitializeAddresses(VOID)
{
/* IO Register Allocation */
PHYSICAL_ADDRESS ioPhysicalBase = {0,0};
ioPhysicalBase.LowPart = S3C6410_BASE_REG_PA_GPIO;
v_pIOPregs = (S3C6410_GPIO_REG *)MmMapIoSpace(ioPhysicalBase, sizeof(S3C6410_GPIO_REG), FALSE);
if (v_pIOPregs == NULL)
{
RETAILMSG(1,(_T("[PMU: pGPIOReg MmMapIoSpace() Failed \n\r")));
return FALSE;
}
GPIOCON =(GPIOCON & ~(0xF<<16))|(0<<16);
GPIOCON =(GPIOCON & ~(0xF<<20))|(1<<20);
GPIOCON =(GPIOCON & ~(0xF<<24))|(1<<24);
GPIOCON =(GPIOCON & ~(0xF<<28))|(1<<28);
RETAILMSG(1,(TEXT("CAN Initalize address \r\n")));
return(TRUE);
}
//liudiping
static void Delay(USHORT count)
{
volatile int i, j = 0;
volatile static int loop = 400000000/100000;
for(;count > 0;count--)
for(i=0;i < loop; i++) { j++; }
}
/****************************************************************************
【功能说明】SPI接口IO片选初始化
****************************************************************************/
void MCP2510_IO_CS_Init( void )
{
InitializeAddresses();
GPIODAT &= (~(1<<6)) ; //SI put 0
GPIODAT &= (~(1<<5)) ; //SCK put 0
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延时至少300ns
GPIODAT |= (1<<7) ; // unselect the MCP2510
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延时至少300ns
}
/****************************************************************************
【功能说明】SPI接口读写开始,片选有效
****************************************************************************/
void MCP2510_RW_Start( void )
{
GPIODAT &= (~(1<<6)) ; //SI put 0
GPIODAT &= (~(1<<5)) ; //SCK put 0
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延时至少300ns
GPIODAT &= (~(1<<7)) ; // Select the MCP2510
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延时至少300ns
}
/****************************************************************************
【功能说明】SPI接口写入数据
****************************************************************************/
void Spi_Write( U8 Data )
{
U8 m ;
for( m = 0; m < 8; m++ )
{
if( (Data&0x80)==0x80 )
GPIODAT |= (1<<6); //SI put 1
else
GPIODAT &= (~(1<<6)); //SI put 0
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延时至少300ns
GPIODAT |= (1<<5) ; //SCK put 1
Data = Data<<1 ;
GPIODAT &= (~(1<<5)) ; //SCK put 0
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延时至少300ns
}
}
/****************************************************************************
【功能说明】SPI接口读出数据
****************************************************************************/
U8 Spi_Read( )
{
U8 m ;
U8 data = 0 ;
for( m = 0; m < 8; m++ )
{
GPIODAT |= (1<<5) ; //SCK put 1
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延时至少300ns
data = data<<1;
if( (GPIODAT & (1<<4)) != 0 )
data |= 0x01 ;
else
data &= 0xfe;
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延时至少300ns
GPIODAT &= (~(1<<5)) ; //SCK put 0
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延时至少300ns
}
return (data);
}
/****************************************************************************
【功能说明】 Send Command to MCP2510 via SPI
****************************************************************************/
void SendCMDMCP2510( U8 CMD )
{
MCP2510_RW_Start() ; //Initial IO port and CS is select
Spi_Write( CMD );
GPIODAT |= (1<<7) ; // Deselect the MCP2510
}
/****************************************************************************
【功能说明】软件复位MCP2510
****************************************************************************/
void MCP2510_Reset()
{
MCP2510_RW_Start() ;
Spi_Write( MCP2510INSTR_RESET );
GPIODAT |= (1<<7) ;
}
/****************************************************************************
【功能说明】向MCP2510指定地址写入一个字节
****************************************************************************/
void MCP2510_Write( U8 address, U8 value)
{
MCP2510_RW_Start() ;
Spi_Write(MCP2510INSTR_WRITE);
Spi_Write( address );
Spi_Write( value );
GPIODAT |= (1<<7) ;
}
/****************************************************************************
【功能说明】修改指定地址寄存器的某些位
****************************************************************************/
void MCP2510_WriteBits( U8 address, U8 data, U8 mask )
{
MCP2510_RW_Start() ;
Spi_Write( MCP2510INSTR_BITMDFY );
Spi_Write( address);
Spi_Write( mask);
Spi_Write( data);
GPIODAT |= (1<<7) ;
}
/****************************************************************************
【功能说明】 Read often used status
//Status 7 6 5 4 3 2 1 0
// | | | | | | | |
// | | | | | | | |___CANINTF.RX0IF
// | | | | | | |_______CANINTF.RX1IF
// | | | | | |___________TXB0CTRL.TXREQ
// | | | | |_______________CANINTF.TX0IF
// | | | |___________________TXB1CTRL.TXREQ
// | | |_______________________CANINTF.TX1IF
// | |___________________________TXB2CTRL.TXREQ
// |_______________________________CANINTF.TX2IF
****************************************************************************/
unsigned char MCP2510_ReadStatus()
{
unsigned char result;
MCP2510_RW_Start() ;
Spi_Write(MCP2510INSTR_RDSTAT);
result = Spi_Read() ;
Spi_Write( 0 ) ; //数据重复输出
GPIODAT |= (1<<7) ;
//if( MCP2510_DEBUG ) Uart_Printf( "StatusREG = 0x%x\n", result ) ;
return result;
}
/****************************************************************************
【功能说明】从MCP2510指定地址中读出一个字节
****************************************************************************/
unsigned char MCP2510_Read( U8 address )
{
unsigned char result;
MCP2510_RW_Start() ;
Spi_Write(MCP2510INSTR_READ) ; //0x03
Spi_Write( address ) ;
result = Spi_Read() ;
GPIODAT |= (1<<7) ;
return result ;
}
/****************************************************************************
【功能说明】序列读取MCP2510数据
****************************************************************************/
void MCP2510_SRead( U8 address, unsigned char* pdata, U8 nlength )
{
int i;
MCP2510_RW_Start() ;
Spi_Write(MCP2510INSTR_READ);
Spi_Write( address );
for (i=0; i<nlength; i++)
{
*pdata=Spi_Read();
//if( MCP2510_DEBUG ) Uart_Printf( " 0x%x\n", (unsigned char)*pdata ) ;
pdata++;
}
GPIODAT |= (1<<7) ;
}
/****************************************************************************
【功能说明】序列写入MCP2510数据
****************************************************************************/
void MCP2510_Swrite( U8 address, unsigned char* pdata, U8 nlength)
{
int i;
MCP2510_RW_Start() ;
Spi_Write(MCP2510INSTR_WRITE);
Spi_Write((unsigned char)address);
for (i=0; i < nlength; i++)
{
Spi_Write( (unsigned char)*pdata );
//if( MCP2510_DEBUG ) Uart_Printf( "0x%x\n", (unsigned char)*pdata ) ;
pdata++;
}
GPIODAT |= (1<<7) ;
}
/****************************************************************************
【功能说明】
****************************************************************************/
void MCP2510_SetBandRate(CanBan
xslig
- 粉丝: 0
- 资源: 2
- 1
- 2
前往页