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

📄 canbus.cpp

📁 s3c2440在wince5.0下的CAN总线驱动
💻 CPP
📖 第 1 页 / 共 2 页
字号:

/***********************************************************\
*	写入MCP2510 发送的数据							*
*	参数: nbuffer为第几个缓冲区可以为0、1、2	*
*			ext表示是否是扩展总线					*
*			can_id为返回的ID值							*
*			rxRTR表示是否是RXRTR						*
*			data表示读取的数据						*
*			dlc表示data length code							*
*		FALSE,表示非扩展总线						*
\***********************************************************/
void MCP2510_Write_Can( U8 nbuffer, int ext, U32 can_id, int rxRTR, U8* data,U8 dlc )
{
	U8 mcp_addr = (nbuffer<<4) + 0x31;
	MCP2510_Swrite(mcp_addr+5, data, dlc );  // write data bytes
	MCP2510_Write_Can_ID( mcp_addr, can_id,ext);  // write CAN id
	if (rxRTR)
		dlc |= RTR_MASK;  // if RTR set bit in byte
	MCP2510_Write((mcp_addr+4), dlc);            // write the RTR and DLC
}

/*******************************************\
*	设置MCP2510 CAN总线ID				*
*	参数: address为MCP2510寄存器地址*
*			can_id为设置的ID值			*
*			IsExt表示是否为扩展ID	*
\*******************************************/
void MCP2510_Write_Can_ID(U8 address, U32 can_id, int IsExt)
{
	U32 tbufdata;

	if (IsExt) {
		can_id&=0x1fffffff;	//29位
		tbufdata=can_id &0xffff;
		tbufdata<<=16;
		tbufdata|=(can_id>>(18-5)&(~0x1f));
		tbufdata |= TXB_EXIDE_M;
	}
	else{
		can_id&=0x7ff;	//11位
		tbufdata= (can_id>>3)|((can_id&0x7)<<13);
	}
	MCP2510_Swrite(address, (unsigned char*)&tbufdata, 4);
}

/***********************************************************************************\
								发送数据
	参数:
		data,发送数据

	Note: 使用三个缓冲区循环发送,没有做缓冲区有效检测
\***********************************************************************************/
void Can_Write(U32 id, U8 *pdata, unsigned char dlc, int IsExt, int rxRTR)
{
	unsigned char err ;
	static int ntxbuffer=0;
	MCP2510_Write_Can(ntxbuffer, IsExt, id, rxRTR, pdata, dlc);

	switch(ntxbuffer){
	case 0:
		MCP2510_WriteBits(TXB0CTRL, (TXB_TXREQ_M|TXB_TXP10_M), 0xff) ;
		do { err = MCP2510_Read(TXB0CTRL) ; }
		while( (err &0x08)==0x08 )  ;
		if( (err &0x70) != 0 )  RETAILMSG(1,(TEXT( " Can Send Err = 0x%x\n"), err ) );
		ntxbuffer=1;
		break;
	case 1:
		MCP2510_WriteBits(TXB1CTRL, (TXB_TXREQ_M|TXB_TXP10_M), 0xff) ;
		do { err = MCP2510_Read(TXB1CTRL) ; }
		while( (err &0x08)==0x08 )  ;
		if( (err &0x70) != 0 )  RETAILMSG(1,(TEXT( "  Can Send Err = 0x%x\n"), err ) );
		ntxbuffer=2;
		break;
	case 2:
		MCP2510_WriteBits(TXB2CTRL, (TXB_TXREQ_M|TXB_TXP10_M), 0xff) ;
		do { err = MCP2510_Read(TXB2CTRL) ; }
		while( (err &0x08)==0x08 )  ;
		if( (err &0x70) != 0 )  RETAILMSG(1,(TEXT( "  Can Send Err = 0x%x\n"), err ) );
		ntxbuffer=0;
		break;
	}

}


/***********************************************************************************\
								查询是否收到数据
	返回值:如果没有数据,则返回-1,
			否则,返回收到数据的缓冲区号
	Note: 如果两个缓冲区都收到数据,则返回第一个缓冲区
\***********************************************************************************/
int Can_Poll()
{
	if( MCP2510_ReadStatus()&RX0INT )
		return 0;
	
	if( MCP2510_ReadStatus()&RX1INT )
		return 1;

	return -1;
}

/****************************************************************************
【功能说明】
****************************************************************************/
int Can_Read(int n, U32* id, U8 *pdata,  U8*dlc, int* rxRTR, int *isExt)
{
	U8 byte;
	byte = MCP2510_Read(CANINTF);

	if(n==0)
	{
		if(byte & RX0INT)
		{
			*isExt=MCP2510_Read_Can(n+3, rxRTR, id, pdata, dlc);
			MCP2510_WriteBits(CANINTF, (U8)(~(RX0INT)), RX0INT); // Clear interrupt
			return TRUE ;
		}
		RETAILMSG(1,(TEXT( "Error! 0 bytes is Read!!! CANINTF=0x%x\n"), byte) ) ;
		return FALSE;
	}
	else if(n ==1 )
	{
		if(byte & RX1INT)
		{
			*isExt=MCP2510_Read_Can(n+4, rxRTR, id, pdata, dlc);
			MCP2510_WriteBits(CANINTF, (U8)(~(RX1INT)), RX1INT); // Clear interrupt
			return TRUE ;
		}
		RETAILMSG(1,(TEXT( "0 bytes is Read!!! CANINTF=0x%x\n"), byte) ) ;
		return FALSE;
	}

	RETAILMSG(1,(TEXT( "Error! Receive channel=0x%x\n"), n )) ;
	return FALSE;
}

/****************************************************************************
【功能说明】
****************************************************************************/
// 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.
	MCP2510_WriteBits(RXB0CTRL, (RXB_BUKT+RXB_RX_ANY), 0xFF);		//关闭屏蔽滤波功能,接收所有报文,允许滚存 
	MCP2510_WriteBits(RXB1CTRL, RXB_RX_ANY, 0xFF);		//关闭屏蔽滤波功能,接收所有报文
}

/****************************************************************************
【功能说明】
****************************************************************************/
void Init_MCP2510(CanBandRate bandrate)
{
	unsigned char i,j,a;

	MCP2510_IO_CS_Init() ;
	MCP2510_Reset();

	MCP2510_SetBandRate(bandrate,FALSE);		//设置波特率

	// Disable interrups.
	MCP2510_Write(CANINTE, NO_IE);  		//禁止所有中断

	// Mark all filter bits as don't care:
	MCP2510_Write_Can_ID(RXM0SIDH, 0,0);
	MCP2510_Write_Can_ID(RXM1SIDH, 0,0);
	// Anyway, set all filters to 0:
	MCP2510_Write_Can_ID(RXF0SIDH, 0, 0);
	MCP2510_Write_Can_ID(RXF1SIDH, 0, 0);
	MCP2510_Write_Can_ID(RXF2SIDH, 0, 0);
	MCP2510_Write_Can_ID(RXF3SIDH, 0, 0);
	MCP2510_Write_Can_ID(RXF4SIDH, 0, 0);
	MCP2510_Write_Can_ID(RXF5SIDH, 0, 0);

	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++) {
			MCP2510_Write(a, 0);
			a++;
	        }
       	a += 2; // We did not clear CANSTAT or CANCTRL
	}
	// and the two receive buffers.
	MCP2510_Write(RXB0CTRL, 0);
	MCP2510_Write(RXB1CTRL, 0);

	// The two pins RX0BF and RX1BF are used to control two LEDs; set them as outputs and set them as 00.
	MCP2510_Write(BFPCTRL, 0x3C);
	
	//Open Interrupt
	MCP2510_Write(CANINTE, RX0IE|RX1IE);
}

/****************************************************************************
【功能说明】MCP2510实验程序
****************************************************************************/
void Test_MCP2510(void)
{
	int i;
	U32 id;
	unsigned char dlc;
	int rxRTR, isExt;
	int temp;
	
	U8 data_write[8]={1,2,3,4,5,6,7,8};
	U8 data_read[8] ;
	
   	RETAILMSG(1,(TEXT( "\r\nCAN BUS Test[ MCP2510 ], press ESC key to exit !\r\n") ) ) ;

	Init_MCP2510(BandRate_10kbps);
	Can_Setup();

    //while( Uart_GetKey() != ESC_KEY )
    for ( int j=0; j<5;j++)
    {
		Can_Write( 0x5a5, data_write, 8, FALSE, FALSE);
		
		while( (i=Can_Poll())==-1 ) ;
	
		for( temp=0; temp<8; temp++)  data_read[temp] = 0 ;
		temp = Can_Read(i, &id, data_read, &dlc, &rxRTR, &isExt);
		
		RETAILMSG(1,(TEXT( "  ID=0x%x\n"),id) );
		RETAILMSG( 1,(TEXT("Data=%x,%x,%x,%x,%x,%x,%x,%x\n"),data_read[0],data_read[1],data_read[2],data_read[3],data_read[4],data_read[5],data_read[6],data_read[7] ));
/*
		for( temp=0; temp<8; temp++)  data_read[temp] = 0 ;
		temp = Can_Read(1, &id, data_read, &dlc, &rxRTR, &isExt);
		Uart_Printf( "  ID=0x%x\n",id );
		Uart_Printf( "Data=%x,%x,%x,%x,%x,%x,%x,%x\n",data_read[0],data_read[1],data_read[2],data_read[3],data_read[4],data_read[5],data_read[6],data_read[7] );
*/		
		Delay(500);
	}

}



BOOL WINAPI CAN_DllEntry(HINSTANCE DllInstance, INT Reason, LPVOID Reserved)
{	
    switch(Reason) 
    {
    case DLL_PROCESS_ATTACH:
    RETAILMSG(1,(TEXT("CAN_DllEntry attach--------------\r\n")) ) ;
        break;
		
    case DLL_PROCESS_DETACH:
    RETAILMSG(1,(TEXT("uCAN_DllEntry detach-------\r\n")) ) ;
        break;
    }
    return TRUE;
}


DWORD CAN_Read(DWORD hOpenContext,LPVOID pBuffer ,DWORD count)
{
      //RETAILMSG(1,(TEXT("ucDragon read----\r\n")));
      /*
      int i;
      U32 id;
	unsigned char dlc;
	int rxRTR, isExt;
	int temp;
	U8 data_read[32];

	while( (i=Can_Poll())==-1 ) ;

	 memset(data_read,0,sizeof(data_read) ) ;

	 temp = Can_Read(i, &id, data_read, &dlc, &rxRTR, &isExt);
	 memcpy(pBuffer,data_read,8);
	 */
	memcpy(pBuffer,read_buf,sizeof(read_buf) ); 
      
	return 1 ;
}

DWORD CAN_Write(DWORD hOpenContext,LPCVOID pSourceBytes,DWORD NumberOfBytes)
{
    RETAILMSG(1,(TEXT("CAN_Write----\r\n")));

	int i;
	U32 id;
	unsigned char dlc;
	int rxRTR, isExt;
	int temp;

	int length ;
	unsigned char * pTmp ;
	U8 data_read[32];
	
	//pSourceBytes = new char[14] ;
	//memset((void*)pSourceBytes,0,strlen((const char*)pSourceBytes)) ;

	//pSourceBytes = "CAN bus test" ;
	//length = 14 ;

      memset(read_buf,0,sizeof(read_buf)) ;
	length = wcslen((unsigned short*)pSourceBytes)*2 ;
	RETAILMSG(1,(TEXT("length=%d\r\n"),length));
	RETAILMSG(1,(TEXT("Content=%s\r\n"),pSourceBytes));
	
	pTmp = (unsigned char *)pSourceBytes ;

	while ( length > 0 )
	{
	if ( length > 8 )
		{
               Can_Write( 0x5a5, (unsigned char *)pTmp, 8, FALSE, FALSE);
			   pTmp = pTmp + 8 ;
			   length = length - 8 ;
		}
	else
		{
		    Can_Write( 0x5a5, (unsigned char *)pTmp, 8, FALSE, FALSE);
			   //pTmp = pTmp + 8 ;
			   length = 0 ;
		}
	      

	 while( (i=Can_Poll())==-1 ) ;

	 memset(data_read,0,sizeof(data_read) ) ;

	 temp = Can_Read(i, &id, data_read, &dlc, &rxRTR, &isExt);

	 //printf("Data=%s\r\n",(const char*)data_read) ;
	 wcscat(read_buf,(unsigned short*)data_read) ;

	 RETAILMSG(1,(TEXT("%s\r\n"),data_read));
		}
	 
	 RETAILMSG(1,(TEXT("%s\r\n"),read_buf));
	return 1;
}

DWORD CAN_Seek(DWORD hOpenContext, long Amount,WORD Type)
{
      RETAILMSG(1,(TEXT("ucDragon seek----\r\n")));
	return 1;
}



DWORD CAN_Init(DWORD Index)
{
    RETAILMSG(1,(TEXT("CAN_Init----\r\n")));
	//Init_MCP2510(BandRate_10kbps);
	//Can_Setup();
	//Test_MCP2510();
    return 1;
}

BOOL CAN_Deinit(DWORD dwData)
{

    return TRUE;
}

DWORD CAN_Open( DWORD dwData,
                DWORD dwAccess,
                DWORD dwShareMode)
{
RETAILMSG(1,(TEXT("CAN_Open---------\r\n")));
  Init_MCP2510(BandRate_10kbps);
  Can_Setup();
    return 4;
}

BOOL CAN_Close(DWORD dwData)
{
   
    return TRUE;
}



BOOL CAN_IOControl(DWORD  dwOpenData,
                   DWORD  dwCode,
                   PBYTE  pBufIn,
                   DWORD  dwLenIn,
                   PBYTE  pBufOut,
                   DWORD  dwLenOut,
                   PDWORD pdwActualOut)
{

    //RETAILMSG(1,(TEXT("ucDragon IOControl----\r\n")));
    return TRUE;
}

⌨️ 快捷键说明

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