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

📄 gateway.h

📁 CAN 网关原代码,汽车电子中的总线网关
💻 H
字号:
/* Gateway application */

#define NULL  ((void *) 0)

//#define LITTLE_ENDIAN

/* types of peripherals */
enum periph_types {
	MsCan,
	Lin
};

/* descriptor of timer */
typedef struct {
	unsigned char load_value;
	unsigned char counter;
} tTimer;

/* descriptor of signal destination */
typedef const struct {
	unsigned int frame_no:10;
	unsigned int position:6;
} tSignalDestDescr;

/* descriptor of signal */
typedef const struct {
	unsigned int position:6;
	unsigned int size:6;
	unsigned int dests_no:4;
} tSignalDescr;

/* member of the RxTable array */
typedef struct {
	unsigned int ID:11;					     /* ID of the frame */
	unsigned int RxTimerPrescaler:5; /* prescaller of the Rx Timer */
	tSignalDescr * pSignalDescr;     /* pointer to Signal descriptors */
	unsigned int DataSize:4;         /* expected data size for this frame */
	unsigned int SignalCount:8;      /* number of signals */
	tTimer RxTimer;                  /* Rx timeout */
	unsigned int byte_copy:1;        /* signals specify numbers and positions of bytes rather than bits - this is to speed up processiong of 1:1 frames */
	unsigned int RxToutFailEn:1;     /* Enable of the Fail bit assertion on Rx timeout; the bit is asseted in Tx frame poited to by first destionation of the first signal */
  unsigned int RxToutFailPos:6;    /* position of the fail bit */
  unsigned int RxToutReload:4;     /* the RxTimeoutHandler is only called after specified number of timeouts; 0 means never */
  unsigned int RxToutCntr:4;       /* counter of timeouts */
} tRxFrmDescr;

/* member of the TxTable array */
typedef struct {
	unsigned int ID:11;					     /* ID of the frame */
	unsigned int node:5;             /* node number */
	tTimer TxTimer;                  /* Tx periodic timer */
  unsigned char * Data;            /* pointer to the data buffer */
	unsigned int TxTimerPrescaler:5; /* prescaller of the Rx Timer */
	unsigned int DataSize:4;         /* data size for this frame */
  unsigned int TxonRx:1;           /* transmit frame on data reception */
  unsigned int TxonDataChg:1;      /* transmit frame on data change */
  unsigned int TxScheduled:1;      /* transmission of frame is scheduled, but frame not transmitted yet */ 
} tTxFrmDescr;

/* Node descriptor */
/* describes the end peripheral and provides pointers to the rest of the descriptors */
typedef struct {
	unsigned int periph_addr;	    /* address of the peripheral (CAN) or its descriptor (LIN) */
	tRxFrmDescr* rx_idx_start_p;  /* pointer to the first ID belonging to this peripheral */
	unsigned int rx_idx_cnt;		  /* number of IDs from the Rx look-up table which belong to this peripheral */
	unsigned char periph_type;		/* identifies the peripheral type (used when transmitting to select the apropriate transmit function) */
  unsigned char TxBufferAdd;    /* index *after* the last entry in the buffer (place new entry and then increment) */
  unsigned char TxBufferTake;   /* index *to* the first entry in the buffer (if TxBufferTake!=TxBufferAdd take the entry and than increment the index) */
  unsigned char TxBufferSize;   /* size of the TxBuffer */
  tTxFrmDescr** TxBuffer;       /* transmit buffer containing pointer to frames to be transmitted */
} tNodeDescr;

#define XLIN_TIMER_STOP	0x7F

typedef struct {
	tSCI* pSCI;	          	/* pointer to the SCI peripheral */
	unsigned int checksum;  /* checksum */
	unsigned char data[8];	/* buffer for data being received */
	unsigned char Id;	  	  /* LIN identifier to be transmitted */
	unsigned char dir:1;		/* direction: Rx = 0 ; Tx = 1 */
	unsigned char len:4;		/* length */
	unsigned char state;	  /* state of the state machine */
	signed char timer;		  /* 127 - do not count, <=0 - timeout, >0 && <127 - count down */
  unsigned char* data_p;  /* pointer to the data currently Tx/Rx */
} tLINnode;

enum LINstate {
	sendSync, sendId, transmitData, receiveData, 	/* Tx & Rx states */
	idleState, 	/* idle - no operation in progress, buffer empty */
	bitError,		/* bit error (HW problem) */
};

/* the following macro calculates the double parity as required for LIN ID */
#define LIN_ID(id) ((id&0x3f)|( ((id&1)^((id&2)>>1)^((id&4)>>2)^((id&16)>>4))<<6 )|( (((id&2)>>1)^((id&8)>>3)^((id&16)>>4)^((id&32)>>5)^1)<<7 ))

/* XGATE prototypes */
#pragma DATA_SEG __RPAGE_SEG XGATE_DATA
extern tNodeDescr NodeDescrs[GATEWAY_NODE_CNT];
#if (GATEWAY_RX_FRM_CNT>0)
  extern tRxFrmDescr RxTable[GATEWAY_RX_FRM_CNT];
#endif
#if (GATEWAY_TX_FRM_CNT>0)
  extern tTxFrmDescr TxTable[GATEWAY_TX_FRM_CNT];
#endif
#if (GATEWAY_LIN_NODE_CNT>0) 
  extern tLINnode LinNodeDescrs[GATEWAY_LIN_NODE_CNT];
#endif 
#pragma DATA_SEG DEFAULT

#pragma CODE_SEG XGATE_CODE
tRxFrmDescr* RxFindFrmId(int FrmId, tNodeDescr* node);  /* prototype for function which finds a frame in RxTable */
void FrameTransmit(tTxFrmDescr* frame_p);         /* prototype for routine which schedules frame for transmission */
interrupt void GatewayTick(void);                 /* prototype for the periodic tick ISR */
interrupt void MsCanTxEmptyIsr(tNodeDescr* node); /* prototype for MsCan Tx Empty ISR */
interrupt void MsCanRxFullIsr(tNodeDescr* node);  /* prototype for MsCan Rx Full ISR */
interrupt void XgateXmitLINChar(tLINnode *node);  /* prototype for LIN statemachine processor */
void LinFrameSetup(tNodeDescr *node);             /* prototype for routine which sets-up LIN frame for Tx/Rx */
void GatewayRxTimeout(tRxFrmDescr *rframe_p);     /* prototype for routine which handles timeout situations */
void interrupt LinSciIsr(tNodeDescr *node);       /* prototype for Lin SCI interrupt service routine */
void interrupt LinTimeoutTick(void);              /* prototype for Lin timeout management */
void RxTimeoutHandler(tRxFrmDescr *frame_p);      /* user function for handling timeouts on frame receptions */
void RxHandler(tRxFrmDescr *frame_p, tNodeDescr *node);       /* user function for handling frame receptions */
void RxInvalidHandler(tRxFrmDescr *frame_p, tNodeDescr *node);/* user function for handling incorrect frame receptions */
void CopySignals(unsigned char *buffer, tRxFrmDescr *frame_p);/* copies signals from a buffer into their destinations */
int CopySignalChkBE(tSignalDestDescr *dest, unsigned char *srcp, char src_pos, signed char size); /* signal copy in Big Endian */
int CopySignalChk(tSignalDestDescr *dest, unsigned char *srcp, char src_pos, signed char size);   /* signal copy in Little Endian */
int CopyDataChk(unsigned char * src, unsigned char * dest, int count);  /* byte copy */
void BitAssign(int value, char position, unsigned char *buffer);        /* assigns 0 or 1 to a bit (signal of length 1) */

#pragma CODE_SEG DEFAULT

/* CPU prototypes */
void SetIntPrio(char channel, char prio);         /* sets interrupt priority */
void GatewayInit(unsigned int bus_freq);          /* initialises the gateway */
void InitMsCan(tMSCAN volatile *can);             /* initialises MsCan for 500kb/s with 4MHz crystal */
void InitSci(tSCI volatile *pSCI, unsigned int bus_freq);  /* initialises Sci for 19200 baud (Lin) */
void InitPit(unsigned int bus_freq);              /* initialises Pit channel 0 (10ms tick) and 1 (Lin timeout tick) */

⌨️ 快捷键说明

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