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

📄 drv_can.h

📁 picos18的can驱动样例程序
💻 H
字号:
/**********************************************************************/
/*                                                                    */
/* File name: drv_can.h                                               */
/*                                                                    */
/* Since:     2005-Jan-19                                             */
/*                                                                    */
/* Version:   PICos18 v2.05                                           */
/*            Copyright (C) 2003, 2004, 2005 Pragmatec.               */
/*            CAN driver v1.05                                        */
/*                                                                    */
/* Author:    Designed by Pragmatec S.A.R.L.        www.pragmatec.net */
/*            MONTAGNE Xavier [XM]      xavier.montagne@pragmatec.net */
/*            ROZIER Bertrand [RZR]     bertrand.rozier@pragmatec.net */
/*                                                                    */
/* Purpose:   PIC18 CAN peripheral management.                        */
/*            It allows task tosend data at the send time through a   */
/*            dedicated FIFO, and to wait for a certain CAN frame.    */ 
/*                                                                    */
/* Distribution: This file is part of PICos18.                        */
/*            PICos18 is free software; you can redistribute it       */
/*            and/or modify it under the terms of the GNU General     */
/*            Public License as published by the Free Software        */
/*            Foundation; either version 2, or (at your option)       */
/*            any later version.                                      */
/*                                                                    */
/*            PICos18 is distributed in the hope that it will be      */
/*            useful, but WITHOUT ANY WARRANTY; without even the      */
/*            implied warranty of MERCHANTABILITY or FITNESS FOR A    */
/*            PARTICULAR PURPOSE.  See the GNU General Public         */
/*            License for more details.                               */
/*                                                                    */
/*            You should have received a copy of the GNU General      */
/*            Public License along with gpsim; see the file           */
/*            COPYING.txt. If not, write to the Free Software         */
/*            Foundation, 59 Temple Place - Suite 330,                */
/*            Boston, MA 02111-1307, USA.                             */
/*                                                                    */
/*          > A special exception to the GPL can be applied should    */
/*            you wish to distribute a combined work that includes    */
/*            PICos18, without being obliged to provide the source    */
/*            code for any proprietary components.                    */
/*                                                                    */
/* History:                                                           */
/* 2005/01/19 [XM]  Create this file.                                 */
/* 2006/03/03 [RZR]  Add Pre-defined BaudRate selection               */
/*                   Correct TX buffer allocation bug				  */
/*                                                                    */
/**********************************************************************/


#ifndef _CAN_DRV_H_
#define _CAN_DRV_H_

#include "pro_man.h"
#include "even_man.h"
#include "alarm.h"

#ifndef NULL
#define NULL 0
#endif

/*****************************************************
 * Definition dedicated to the local functions.
 ****************************************************/
#define CAN_MSG_SENT        0x02
#define CAN_FREE            0x00
#define CAN_FULL            0x01
#define CAN_EMPTY           0x00
#define TRUE                0x01
#define FALSE               0x00
#define TX_MESSAGE          0x01
#define RX_MESSAGE          0x00 
	
struct _CAN_frame {
   unsigned long CANID;
   unsigned char length;
   unsigned char data[8];
   unsigned char CallerID;
   unsigned char state;
   struct _CAN_frame *next;
};
typedef struct _CAN_frame CAN_message_t, *CAN_message_tRef;

#define          CAN_freeMsg(x)        x.state = CAN_FREE;



////////////////////////////////////////////////////////////////////////////////////
//
//	HARDWARE OVERLAY TYPES......
//	
//	HARDWARE........
//
//	Declare a type that matches the structure of the TX and RX
//	buffers so that we can overlay the structure variables into
//	the correct locations within the buffer.

//
//	TX Buffer looks like this.....
typedef volatile near struct _TXBUF{
	char	TXCON;
	char	SIDH;
	char	SIDL;
	char	EIDH;
	char	EIDL;
	char	DLC;
	char	DATA[8];
	char	CANSTAT;
	char	blank;
} TXBUF_t,	* pTXBUF_t;


//
//	TX HARDWARE BUFFERS
#define TXBUF_START		((TXBUF_t * )&TXB0CON)		//	See CAN Controller Register Map....
#define TXBUF_END		((TXBUF_t * )&TXB2CON)		//	Table 19-1 perhaps?
//
//	RX Buffer looks like this.....
typedef volatile near struct _RXBUF {
	char	RXCON;
	char	SIDH;
	char	SIDL;
	char	EIDH;
	char	EDIL;
	char	DLC;
	char	data[8];
	char	CANSTAT;
}RXBUF_t,	*pRXBUF_t;

#define	RXBUF_START		((pRXBUF_t)&RXB0CON)		//	See CAN Controller Register Map
#define	RXBUF_END		((pRXBUF_t)&RXB1CON)		//	Table 19-1
#define	RXBUF_STEP		0x01
#define	RX_BUFFER_FULL	0b10000000
//								^-- Mask applied to RXB?CON to test for buffer full

////////////////////////////////////////////////////////////////////////////////////////
//	Generic RX Filter and Mask type
//
//	It looks like all of the filters follow this type of repeating pattern
//	within the register area.  Use that to our advantage to define
//	an overlay for this area too...
typedef volatile near struct _RXFM {
	char	SIDH;
	char	SIDL;
	char	EIDH;
	char	EIDL;
}RXFilterMask_t,	*pRXFilterMask_t;

#define	RXFILTER_START	0xF00			//	See CAN Controller Register Map
#define	RXFILTER_END		0xF1C			//	Table 19-1
#define	RXFILTER_STEP		0x04

//////////////////////////////////////////////////////////////////////////////////
//
//	Function macros for Transmit Buffer Control
//
#define		TX_ABORTED(psTXBuf)				((psTXBuf->TXCON)&0x40)
#define		TX_LOSTARB(psTXBuf)				((psTXBuf->TXCON)&0x20)
#define		TX_ERROR(psTXBuf)					((psTXBuf->TXCON)&0x10)
#define		SET_TXREQ(psTXBuf)				((psTXBuf->TXCON) |= 0x08)
#define		CLEAR_TXREQ(psTXBuf)				((psTXBuf->TXCON) &= (~(1<<4)))
#define		GET_TXPRIORITY(psTXBuf)			((psTXBuf->TXCON) & 0x03)			
#define		SET_TXPRIORITY(psTXBuf,u8Priority)	((psTXBuf->TXCON) = u8Priority & 0x03)



//	Add some fancy macro definitions for 11-bit identifiers
//	Example usage: regularID = GET_ID(&RX0BUFFER);
#define		GET_TXID(psTXBuf)					(((psTXBuf->SIDH) << 3) + ((psTXBuf->SIDL & 0x0E) >> 5))
#define		SET_TXID(psTXBuf,u16ID)			psTXBuf->SIDH=((u16ID&0x7FF)>>3);psTXBuf->SIDL = ((u16ID & 0x07)<< 5)
#define		SET_TXDLC(psTXBuf,u8Length)		psTXBuf->DLC = (u8Length & 0x0F)

/////////////////////////////////////////////////////////////////////////////////////////
//
//	Function macros for Receive Buffer Control
//

#define		GET_RXID(psRXBuf)				(((unsigned long)(psRXBuf->SIDH)<<3)|(((unsigned long)(psRXBuf->SIDL)>>5)&0x07))
#define 	GET_RXDLC(psRXBuf)				(psRXBuf->DLC & 0x0F)

#define		RX_IS_FULL(psRXBuf)				((psRXBuf->RXCON) & 0x80)
#define		GET_RXMODE(psRXBuf)				(((psRXBuf->RXCON) &0x60) >> 5)
#define		SET_RXMODE(psRXBuf,u8Mode)		(psRXBuf->RXCON = ((u8Mode) & 0x03) << 5)
#define		IS_RTR(psRXBuf)					(((psRXBuf->RXCON) & 0x04) >> 3)
#define		SET_RXDBEN						(RXB0CON |= 0x04)


/////////////////////////////////////////////////////////////////////////////////////////
//
//	Other Hardware based defines
//


//	Set interrupts on receive only
#define	CAN_PIE_INT_MSK	0b00000011

#define	TXCON_ERRORS	0b01110000
#define TXCON_EMPTY		0b00001000

StatusType		CAN_FindSpeed(unsigned char * speed);
StatusType		CAN_SetSpeed(char speed);
StatusType       CAN_enqMsg(CAN_message_tRef toEnqueue);
CAN_message_tRef CAN_deqMsg(void);
StatusType       CopyHard2FrameBuffer(void);
StatusType       CopyFrameBuffer2Hard(pTXBUF_t pTxbuf);
StatusType       WriteCANBuffer(void);
StatusType       ReadCANBuffer(void);
StatusType       CAN_RCV_Register(CAN_message_tRef toEnqueue);
void             CAN_config(void);


#endif /* _CAN_DRV_H_ */

/* End of File : drv_can.h */

⌨️ 快捷键说明

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