⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mcp2515.h

📁 2410 spi 外接2515 can转换 驱动代码
💻 H
📖 第 1 页 / 共 3 页
字号:
	}*/

	switch(bandrate){
	case BandRate_10kbps:
		MCP2515_Write(CNF1, 0x31);	//SJWP=1TQ  BRO=50 	16TQ=100us
		MCP2515_Write(CNF2, 0xb0);  //PS1=7TQ PSeg=1TQ
		MCP2515_Write(CNF3, 0x06);  //PS2=7TQ SYNC=1TQ	
		break;
	case BandRate_125kbps:
		MCP2515_Write(CNF1, SJW1|BRP4);						//SJWP=1 TQ
		MCP2515_Write(CNF2, BTLMODE_SET|(SEG4<<3)|SEG7);	//PS1=4TQ, PRSEG=7TQ
		MCP2515_Write(CNF3, SEG4);							//PS2=4TQ
		break;
	case BandRate_250kbps:
		MCP2515_Write(CNF1, SJW1|BRP2);						//SJWP=1 TQ
		MCP2515_Write(CNF2, BTLMODE_SET|(SEG4<<3)|SEG7);	//PS1=4TQ,PRSEG=7TQ
		MCP2515_Write(CNF3, SEG4);							//PS2=4TQ
		break;
	case BandRate_500kbps:
		MCP2515_Write(CNF1, SJW1|BRP1);						//SJWP=1 TQ
		MCP2515_Write(CNF2, BTLMODE_SET|(SEG4<<3)|SEG7);	//PS1=4TQ,PRSEG=7TQ
		MCP2515_Write(CNF3, SEG4);							//PS2=4TQ
		break;
	case BandRate_1Mbps:
		MCP2515_Write(CNF1, SJW1|BRP1);						//SJWP=1 TQ
		MCP2515_Write(CNF2, BTLMODE_SET|(SEG3<<3)|SEG2);	//PS1=3TQ,PRSEG=2TQ
		MCP2515_Write(CNF3, SEG2);							//PS2=2TQ
		break;
	}

	if( IsBackNormal == TRUE  )
	{
		//?Enable clock output f/8 
		MCP2515_Write(MCP2515REG_CANCTRL, MODE_NORMAL | CLKEN | CLK8);
	}

}


/*****************************************************
                   MCU write can id				
******************************************************/
 
void MCP2515_Write_Can_ID(U8 address, U32 can_id, int IsExt)
{
	U32 tbufdata;

	if (IsExt)	//expend ok
	{
		can_id&=0x1fffffff;					//29bit:h--l 11sid--18eid		tbufdata=can_id &0xff;				//obtain low8 of 18eid 
		tbufdata<<=24;						//low8 of 18eid at high address
		tbufdata|=((can_id>>8)&0xff)<<16;	//high8 of 18eid
		tbufdata|=((can_id>>16)&0x3)<<8;	//eid<17:16>
		tbufdata|=((can_id>>18)&0x7)<<13;	//sid<2:0>
		tbufdata|=can_id>>21;				//sid<10:3>
		//tbufdata|=(can_id>>(18-5)&(~0x1f));  //?
		tbufdata|= TXB_EXIDE_SET;			//?
	} 
	else		//standard ok
	{
		can_id&=0x7ff;						//11sid			tbufdata= (can_id>>3)|((can_id&0x7)<<13);
	}

	MCP2515_Swrite(address, (unsigned char*)&tbufdata, 4);
	
}


/****************************************************************
                 mcu write mcp2515 TXn_REG						        
nbuffer	: n=0,1,2 TX0 TX1 TX2
ext		: IsExt	expend?					        
*****************************************************************/
void MCP2515_Write_Can( U8 nbuffer, int ext, U32 can_id, int rxRTR, U8* data,U8 dlc )
{
	U8 mcp_addr = (nbuffer<<4) + 0x31;			//select TXn
	MCP2515_Swrite(mcp_addr+5, data, dlc );		//write data bytes
	MCP2515_Write_Can_ID( mcp_addr, can_id,ext);//write CAN id
	if (rxRTR)	//RTR?
	dlc |= TXB_RTR_SET;							//write TXBnDLC bit6 RTR
	MCP2515_Write((mcp_addr+4), dlc);           //
}


/***********************************************************************************
								发送数据
	参数:
		data发送数据
		id芁发送的id
		*pdata 芁发送的数据
		dlc 芁发送的数据长床
		isext 是吊䞺扩展垧
		rxrtr 是吊䞺远皋垧

	Note: 䜿甚䞉䞪猓冲区埪环发送没有做猓冲区有效检测
***********************************************************************************/
void CanWrite(U32 id, U8 *pdata, unsigned char dlc, int IsExt, int rxRTR)
{
	unsigned char err = 0;
	static int ntxbuffer = 0;    	int i = 0;
		MCP2515_Write_Can(ntxbuffer, IsExt, id, rxRTR, pdata, dlc);

	switch(ntxbuffer){
	case 0:
		MCP2515_WriteBits(TXB0CTRL, 0xff, (TXB_TXREQ_SET|TXB_TXP10_H_SET));	//setting TXB0CTRL
		do 		{ 			err = MCP2515_Read(TXB0CTRL) ;			uiErr++; 			printk("CAN_No. = %d " , rwflag);			printk("err = %d (%d)\n", err , uiErr);			if( uiErr >= 3)			{				MCP2515_Write(TXB0CTRL, 0x03);				uiErr = 0;                brk = brk +1;			}            if ( brk >= 3)            {                brk = 0;                break;            }		}		while( (err & 0x08) == 0x08 );
		uiErr = 0;        brk  = 0 ;                /*for (i=0; i<200; i++)                {                   err = MCP2510_Read(TXB0CTRL) ;                   if ( (err &0x08)==0x00 )           //waiting for finish of sending.                     break;                }*/
		//if( (err &0x70) != 0 )  printf( "  Can Send Err = 0x%x\n", err  );
		
		ntxbuffer=1;
		break;
	case 1:
		MCP2515_WriteBits(TXB1CTRL, 0xff, (TXB_TXREQ_SET|TXB_TXP10_H_SET));
		do { err = MCP2515_Read(TXB1CTRL) ; }
		while( (err &0x08)==0x08 )  ;
		//if( (err &0x70) != 0 )  printf( "  Can Send Err = 0x%x\n", err  );
		ntxbuffer=2;
		break;
	case 2:
		MCP2515_WriteBits(TXB2CTRL, 0xff, (TXB_TXREQ_SET|TXB_TXP10_H_SET)) ;
		do { err = MCP2515_Read(TXB2CTRL) ; }
		while( (err &0x08)==0x08 )  ;
		//if( (err &0x70) != 0 )  printf( "  Can Send Err = 0x%x\n", err  );
		ntxbuffer=0;
		break;	default: break;
	}

}


/****************************************************************************
【功胜诎明】
****************************************************************************/
// Setup the CAN buffers used by the application.
// We currently use only one for reception and one for transmission.
// It is possible to use several to get a simple form of queue.
//
// We setup the unit to receive all CAN messages.
// As we only have at most 4 different messages to receive, we could use the
// filters to select them for us.
//
// Init_MCP2510() should already have been called.
void Can_Setup(void)
{
    // As no filters are active, all messages will be stored in RXB0 only if
    // no roll-over is active. We want to recieve all CAN messages (standard and extended)
    // (RXM<1:0> = 11).
    //SPI_mcp_write_bits(RXB0CTRL, RXB_RX_ANY, 0xFF);
    //SPI_mcp_write_bits(RXB1CTRL, RXB_RX_ANY, 0xFF);

    // But there is a bug in the chip, so we have to activate roll-over.
	MCP2515_WriteBits(RXB0CTRL, 0xFF, (RXB_BUKT_SET+RXB_RX_ANY));  //¹Ø±ÕÆÁ±ÎÂ˲š¹ŠÄÜ,œÓÊÕËùÓбšÎÄ£¬ÔÊÐí¹öŽæ       
	MCP2515_WriteBits(RXB1CTRL, 0xFF, RXB_RX_ANY);		//¹Ø±ÕÆÁ±ÎÂ˲š¹ŠÄÜ,œÓÊÕËùÓбšÎÄ}


/****************************************************************************
【功胜诎明】
****************************************************************************/
void Init1_MCP2515(CanBandRate bandrate)		//ÅäÖÃ
{
	unsigned char i,j,a ;    //rwflag = 1;

	//MCP2510_IO_CS_Init() ;
	
	//MCP2510_Reset();  //已经通过倖囎䞊电倍䜍了

	MCP2515_SetBandRate(bandrate,FALSE);		//disable clkout 
	MCP2515_Write(CANINTE, NO_IE);  			// disable all interrupts
		MCP2515_Write(TEC, 0x00);        MCP2515_Write(REC, 0x00);        MCP2515_Write(EFLG, 0x00);
        MCP2515_Write(CANINTF, 0x00);

	// Mark all filter bits as don't care:  屏蔜寄存噚写0时党接收,写1时芁䞎滀波噚笊合埗报文才接收
	MCP2515_Write_Can_ID(RXM0SIDH, 0xffffffff, 1);
	MCP2515_Write_Can_ID(RXM1SIDH, 0x00ffff00, 1);
	
	// Anyway, set all filters to 0:
	MCP2515_Write_Can_ID(RXF0SIDH, 0x55aa55aa, 1);
	MCP2515_Write_Can_ID(RXF1SIDH, 0x55aa55aa, 1);
	MCP2515_Write_Can_ID(RXF2SIDH, 0x00ff0100, 1);
	MCP2515_Write_Can_ID(RXF3SIDH, 0x00fffe00, 1);
	MCP2515_Write_Can_ID(RXF4SIDH, 0x00ff0900, 1);
	MCP2515_Write_Can_ID(RXF5SIDH, 0x00ff0c00, 1);	//expend=1

	//MCP2510_Write(CLKCTRL, MODE_LOOPBACK| CLKEN | CLK8);//回环暡匏
    //劂果䞍胜甚䞀台讟倇联机实验的话可以选择回环暡匏
	//MCP2510_Write(CLKCTRL, MODE_NORMAL| CLKEN | CLK8);//标准暡匏
  
	// Clear, deactivate the three transmit buffers
	a = TXB0CTRL;
	for (i = 0; i < 3; i++) 
	{
		for (j = 0; j < 14; j++) 
		{
			MCP2515_Write(a, 0);
			a++;
	    }
       	a += 2; // We did not clear CANSTAT or CANCTRL except TX_REG
	}
	// and the two receive buffers.
	MCP2515_Write(RXB0CTRL, 0x00);  //bulk disable	MCP2515_Write(RXB1CTRL, 0x00);
	MCP2515_Write(BFPCTRL, 0x00);
	

	//MCP2510_Write(CANINTE, RX0IE|RX1IE);    MCP2515_Write(CANINTE, 0x03);	//RX interrupt enable    MCP2515_Write(MCP2515REG_CANCTRL, MODE_NORMAL| CLKEN | CLK8);	//clk out????
	//rwflag = 0;
	
}void Init2_MCP2515(CanBandRate bandrate)
{
	unsigned char i,j,a;    //rwflag = 2;

	//MCP2510_IO_CS_Init() ;
	
	//MCP2510_Reset();  //已经通过倖囎䞊电倍䜍了

	MCP2515_SetBandRate(bandrate,FALSE);		//讟眮波特率

	// Disable interrups.
	MCP2515_Write(CANINTE, NO_IE);  		//犁止所有䞭断        MCP2515_Write(TEC, 0x00);        MCP2515_Write(REC, 0x00);        MCP2515_Write(EFLG, 0x00);
        MCP2515_Write(CANINTF, 0x00);

	// Mark all filter bits as don't care:  屏蔜寄存噚写0时党接收,写1时芁䞎滀波噚笊合埗报文才接收
	MCP2515_Write_Can_ID(RXM0SIDH, 0x00ff0000, 1);
	MCP2515_Write_Can_ID(RXM1SIDH, 0xffffffff, 1);
	
	// Anyway, set all filters to 0:
	MCP2515_Write_Can_ID(RXF0SIDH, 0x00f10000, 1);
	MCP2515_Write_Can_ID(RXF1SIDH, 0x00f00000, 1);
	MCP2515_Write_Can_ID(RXF2SIDH, 0x55aa55aa, 1);
	MCP2515_Write_Can_ID(RXF3SIDH, 0x55aa55aa, 1);
	MCP2515_Write_Can_ID(RXF4SIDH, 0x55aa55aa, 1);
	MCP2515_Write_Can_ID(RXF5SIDH, 0x55aa55aa, 1);

	//MCP2510_Write(CLKCTRL, MODE_LOOPBACK| CLKEN | CLK8);//回环暡匏
        //劂果䞍胜甚䞀台讟倇联机实验的话可以选择回环暡匏
	//MCP2510_Write(CLKCTRL, MODE_NORMAL| CLKEN | CLK8);//标准暡匏
  
	// Clear, deactivate the three transmit buffers
	a = TXB0CTRL;
	for (i = 0; i < 3; i++) 
	{
		for (j = 0; j < 14; j++) 
		{
			MCP2515_Write(a, 0);
			a++;
	    }
       	a += 2; // We did not clear CANSTAT or CANCTRL
	}
	// and the two receive buffers.
	MCP2515_Write(RXB0CTRL, 0x00);
	MCP2515_Write(RXB1CTRL, 0x00);
	MCP2515_Write(BFPCTRL, 0x00);
	
	//Open Interrupt
	//MCP2510_Write(CANINTE, RX0IE|RX1IE);    MCP2515_Write(CANINTE, 0x03);    MCP2515_Write(MCP2515REG_CANCTRL, MODE_NORMAL| CLKEN | CLK8);//标准暡匏
	//rwflag = 0;
	
}void Init3_MCP2515(CanBandRate bandrate)//
{
	unsigned char i,j,a;    //rwflag = 3;

	//MCP2510_IO_CS_Init() ;
	
	//MCP2510_Reset();  //已经通过倖囎䞊电倍䜍了

	MCP2515_SetBandRate(bandrate,FALSE);		//讟眮波特率

	// Disable interrups.
	MCP2515_Write(CANINTE, NO_IE);  		//犁止所有䞭断        MCP2515_Write(TEC, 0x00);        MCP2515_Write(REC, 0x00);        MCP2515_Write(EFLG, 0x00);
        MCP2515_Write(CANINTF, 0x00);

	// Mark all filter bits as don't care:  屏蔜寄存噚写0时党接收,写1时芁䞎滀波//噚笊合埗报文才接收
	MCP2515_Write_Can_ID(RXM0SIDH, 0x00ff0000, 1);
	MCP2515_Write_Can_ID(RXM1SIDH, 0x00ff0000, 1);
	
	// Anyway, set all filters to 0:
	MCP2515_Write_Can_ID(RXF0SIDH, 0x00ff0000, 1);
	MCP2515_Write_Can_ID(RXF1SIDH, 0, 1);
	MCP2515_Write_Can_ID(RXF2SIDH, 0x00f10000, 1);
	MCP2515_Write_Can_ID(RXF3SIDH, 0x00f00000, 1);
	MCP2515_Write_Can_ID(RXF4SIDH, 0, 1);
	MCP2515_Write_Can_ID(RXF5SIDH, 0, 1);

	//MCP2510_Write(CLKCTRL, MODE_LOOPBACK| CLKEN | CLK8);//回环暡匏
        //劂果䞍胜甚䞀台讀‡è”机实验的话可以选择回环暡匏
	//MCP2510_Write(CLKCTRL, MODE_NORMAL| CLKEN | CLK8);//标准暡匏
  
	// Clear, deactivate the three transmit buffers
	a = TXB0CTRL;
	for (i = 0; i < 3; i++) 
	{
		for (j = 0; j < 14; j++) 
		{
			MCP2515_Write(a, 0);
			a++;
	    }
       	a += 2; // We did not clear CANSTAT or CANCTRL
	}
	// and the two receive buffers.
	MCP2515_Write(RXB0CTRL, 0x04);
	MCP2515_Write(RXB1CTRL, 0x00);
	//MCP2510_Write(BFPCTRL, 0x00);
	
	//Open Interrupt
	//MCP2510_Write(CANINTE, RX0IE|RX1IE);    MCP2515_Write(CANINTE, 0x03);    MCP2515_Write(MCP2515REG_CANCTRL, MODE_NORMAL| CLKEN | CLK8);//标准暡匏
	//rwflag = 0;
	
}



/*****************************************************************************************/
/*#define B2RTS           0x20
#define B1RTS           0x10
#define B0RTS           0x08
#define B2RTSM          0x04
#define B1RTSM          0x02
#define B0RTSM          0x01




#define RXB_RXMx_M      0x60
// #define RXB_RXIE_M      0x10*/
/*****************************************************************************************/

#endif /* _LINUX_MCP2515_H */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -