?? mcp2510.c
字號:
/****************************************************************************
【文 件 名 稱】MCP2510.C
【功 能 描 述】MCP2510的demo程序代碼
【程 序 版 本】4.0
【創建人及創建日期】龔俊( gongjun98@sohu.com )//2002年11月19日19:26
【修改人及修改日期】龔俊( gongjun98@sohu.com )//2004-12-8 17:25
****************************************************************************/
//***************************************************************************
#include "AT91RM9200.h"
#include "lib_AT91RM9200.h"
#include "def.h"
#include "console.h"
#include "MCP2510.h"
#include "SPI.h"
//***************************************************************************
#define MCP2510_DEBUG 1
#define MCP2510_CS_H SPCS1_DS()
#define MCP2510_CS_L SPCS1_EN()
#define MCP2510_SI_H DOUT_H()
#define MCP2510_SI_L DOUT_L()
#define MCP2510_SCK_H SPCK_H()
#define MCP2510_SCK_L SPCK_L()
#define MCP2510_SO_GET DIN_GET()
#define MCP2510_INT_GET INT2510_GET()
/********************** MCP2510 Instruction *********************************/
#define DELAY_TIME 80
#define MCP2510INSTR_RESET 0xc0 //復位為缺省狀態,并設定為配置模式
#define MCP2510INSTR_READ 0x03 //從寄存器中讀出數據
#define MCP2510INSTR_WRITE 0x02 //向寄存器寫入數據
#define MCP2510INSTR_RTS 0x80 //啟動一個或多個發送緩沖器的報文發送
#define MCP2510INSTR_RDSTAT 0xa0 //讀取狀態
#define MCP2510INSTR_BITMDFY 0x05 //位修改
//***************************************************************************
/****************************************************************************
【功能說明】SPI接口IO片選初始化
****************************************************************************/
void MCP2510_IO_CS_Init( void )
{
SPI_MCP2510_Init() ;
MCP2510_SI_L ; //SI put 0
MCP2510_SCK_L ; //SCK put 0
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延時至少300ns
MCP2510_CS_H ; // unselect the MCP2510
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延時至少300ns
}
/****************************************************************************
【功能說明】SPI接口讀寫開始,片選有效
****************************************************************************/
void MCP2510_RW_Start( void )
{
MCP2510_SI_L ; //SI put 0
MCP2510_SCK_L ; //SCK put 0
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延時至少300ns
MCP2510_CS_L ; // 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 )
MCP2510_SI_H; //SI put 1
else
MCP2510_SI_L; //SI put 0
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延時至少300ns
MCP2510_SCK_H ; //SCK put 1
Data = Data<<1 ;
MCP2510_SCK_L ; //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++ )
{
MCP2510_SCK_H ; //SCK put 1
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延時至少300ns
data = data<<1;
if( MCP2510_SO_GET != 0 )
data |= 0x01 ;
else
data &= 0xfe;
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延時至少300ns
MCP2510_SCK_L ; //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 );
MCP2510_CS_H ; // Deselect the MCP2510
}
/****************************************************************************
【功能說明】軟件復位MCP2510
****************************************************************************/
void MCP2510_Reset()
{
MCP2510_RW_Start() ;
Spi_Write( MCP2510INSTR_RESET );
MCP2510_CS_H ;
}
/****************************************************************************
【功能說明】向MCP2510指定地址寫入一個字節
****************************************************************************/
void MCP2510_Write( U8 address, U8 value)
{
MCP2510_RW_Start() ;
Spi_Write(MCP2510INSTR_WRITE);
Spi_Write( address );
Spi_Write( value );
MCP2510_CS_H ;
}
/****************************************************************************
【功能說明】修改指定地址寄存器的某些位
****************************************************************************/
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);
MCP2510_CS_H ;
}
/****************************************************************************
【功能說明】 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 ) ; //數據重復輸出
MCP2510_CS_H ;
//if( MCP2510_DEBUG ) 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() ;
MCP2510_CS_H ;
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 ) printf( " 0x%x\n", (unsigned char)*pdata ) ;
pdata++;
}
MCP2510_CS_H ;
}
/****************************************************************************
【功能說明】序列寫入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 ) printf( "0x%x\n", (unsigned char)*pdata ) ;
pdata++;
}
MCP2510_CS_H ;
}
/****************************************************************************
【功能說明】
****************************************************************************/
void MCP2510_SetBandRate(CanBandRate bandrate, int IsBackNormal)
{
U8 value=0;
U8 ReadBackCNT = 0;
// Bit rate calculations.
//
//Input clock fre=16MHz
// In this case, we'll use a speed of 125 kbit/s, 250 kbit/s, 500 kbit/s.
// If we set the length of the propagation segment to 7 bit time quanta,
// and we set both the phase segments to 4 quanta each,
// one bit will be 1+7+4+4 = 16 quanta in length.
//
// setting the prescaler (BRP) to 0 => 500 kbit/s.
// setting the prescaler (BRP) to 1 => 250 kbit/s.
// setting the prescaler (BRP) to 3 => 125 kbit/s.
//
// If we set the length of the propagation segment to 3 bit time quanta,
// and we set both the phase segments to 1 quanta each,
// one bit will be 1+3+2+2 = 8 quanta in length.
// setting the prescaler (BRP) to 0 => 1 Mbit/s.
// Go into configuration mode
MCP2510_Write(MCP2510REG_CANCTRL, MODE_CONFIG);
//if( MCP2510_DEBUG ) printf( "MCP2510REG_CANCTRL = 0x%x\n", MCP2510_Read(MCP2510REG_CANCTRL) );
while( ReadBackCNT<8 )
{
value = ( MCP2510_Read( MCP2510REG_CANSTAT ) & 0xe0 );
if(value == MODE_CONFIG ){
//printf( "ReadBackCNT = 0x%x\n", ReadBackCNT );
break;
}
ReadBackCNT++ ;
}
if( ReadBackCNT == 8 ) //Set mcp2510's mode failed,redo it again
{
printf( "Set config mode is failed! CANCTRL = 0x%x\n", value );
MCP2510_Reset();
MCP2510_Write(MCP2510REG_CANCTRL, MODE_CONFIG); //redo to set mcp2510 mode
delay( 150 );
value = ( MCP2510_Read(MCP2510REG_CANCTRL) & 0xe0 ); //read back mode from CANSTAT Register
printf( "Set is 0x%x , Read is 0x%x\n", MODE_CONFIG, value ) ;
}
switch(bandrate){
case BandRate_10kbps:
MCP2510_Write(CNF1, 0x31); //10k 16TQ
MCP2510_Write(CNF2, 0xb0); //PS1=7 TQ PSeg=1 TQ
MCP2510_Write(CNF3, 0x06); //PS2=7 TQ SYNC=1 TQ
//if( MCP2510_DEBUG ) printf( "CNF1 = 0x%x\n", MCP2510_Read(CNF1) );
//if( MCP2510_DEBUG ) printf( "CNF2 = 0x%x\n", MCP2510_Read(CNF2) );
//if( MCP2510_DEBUG ) printf( "CNF3 = 0x%x\n", MCP2510_Read(CNF3) );
break;
case BandRate_125kbps:
MCP2510_Write(CNF1, SJW1|BRP4); //Synchronization Jump Width Length =1 TQ
MCP2510_Write(CNF2, BTLMODE_CNF3|(SEG4<<3)|SEG7); // Phase Seg 1 = 4, Prop Seg = 7
MCP2510_Write(CNF3, SEG4);// Phase Seg 2 = 4
break;
case BandRate_250kbps:
MCP2510_Write(CNF1, SJW1|BRP2); //Synchronization Jump Width Length =1 TQ
MCP2510_Write(CNF2, BTLMODE_CNF3|(SEG4<<3)|SEG7); // Phase Seg 1 = 4, Prop Seg = 7
MCP2510_Write(CNF3, SEG4);// Phase Seg 2 = 4
break;
case BandRate_500kbps:
MCP2510_Write(CNF1, SJW1|BRP1); //Synchronization Jump Width Length =1 TQ
MCP2510_Write(CNF2, BTLMODE_CNF3|(SEG4<<3)|SEG7); // Phase Seg 1 = 4, Prop Seg = 7
MCP2510_Write(CNF3, SEG4);// Phase Seg 2 = 4
break;
case BandRate_1Mbps:
MCP2510_Write(CNF1, SJW1|BRP1); //Synchronization Jump Width Length =1 TQ
MCP2510_Write(CNF2, BTLMODE_CNF3|(SEG3<<3)|SEG2); // Phase Seg 1 = 2, Prop Seg = 3
MCP2510_Write(CNF3, SEG2);// Phase Seg 2 = 1
break;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -