【原】飞思卡尔DZ60的CAN驱动
0赞
发表于 11/8/2011 11:14:07 AM
阅读(4793)
发个之前的CAN驱动,大家看看!
typedef struct
{
u16 id;
u8 cmd;
u8 data_len : 4;
u8 ack : 4;
u16 data_addr;
u8 data[4];
u8 len;
} cal_msg;
// Functions
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
// MSCANInit
// --------------------------------------------------------------------------------------
// MSCAN Peripheral Initialization
/////////////////////////////////////////////////////////////////////////////////////////
void CAN_Init(void)
{
char s,p;
char t_sg1, t_sg2;
if (!CANCTL0&0x01) CANCTL0 =0x01;
while (!CANCTL1&0x01) ;
s = (SJW-1)<<6;
p = (BRP-1);
// Configures SJW and Tq clock Baud Rate Prescaler
CANBTR0 = (s|p);
t_seg1 = (TSEG1-1);
t_seg2 = (TSEG2-1)<<4;
while (!CANCTL1&0x01) ;
s = (SJW-1)<<6;
p = (BRP-1);
// Configures SJW and Tq clock Baud Rate Prescaler
CANBTR0 = (s|p);
t_seg1 = (TSEG1-1);
t_seg2 = (TSEG2-1)<<4;
CANBTR1 = (t_sg1 | t_sg2);
// Disables all the Filters
CANIDMR0 = 0xFF;
CANIDMR1 = 0xFF;
CANIDMR2 = 0xFF;
CANIDMR3 = 0xFF;
CANIDMR4 = 0xFF;
CANIDMR5 = 0xFF;
CANIDMR6 = 0xFF;
CANIDMR7 = 0xFF;
// Disables all the Filters
CANIDMR0 = 0xFF;
CANIDMR1 = 0xFF;
CANIDMR2 = 0xFF;
CANIDMR3 = 0xFF;
CANIDMR4 = 0xFF;
CANIDMR5 = 0xFF;
CANIDMR6 = 0xFF;
CANIDMR7 = 0xFF;
// Enable MSCAN and normal operation and select the oscillator clock as MSCAN clock source
CANCTL1 = 0x80;
// Active MSCAN Normal Operation
CANCTL0 = 0x00;
// Wait until the MSCAN operates normally
while(CANCTL1&0x01) ;
// Wait until MSCAN is synchronized to the CAN bus
while(!(CANCTL0&0x10));
}
CANCTL1 = 0x80;
// Active MSCAN Normal Operation
CANCTL0 = 0x00;
// Wait until the MSCAN operates normally
while(CANCTL1&0x01) ;
// Wait until MSCAN is synchronized to the CAN bus
while(!(CANCTL0&0x10));
}
//--------------------------------jie shou
Bool cal_rxMsg(cal_msg *MsgPtr)
{
uchar i;
Bool cal_rxMsg(cal_msg *MsgPtr)
{
uchar i;
// Checks for received messages
if(!(CANRFLG&0x01)) return(FALSE);
// Checks if message has standard identifier
if(CANRIDR1_IDE) return(FALSE);
if(!(CANRFLG&0x01)) return(FALSE);
// Checks if message has standard identifier
if(CANRIDR1_IDE) return(FALSE);
// Reads message
MsgPtr->can_msg.id = (unsigned int)(CANRIDR0<<3) | (unsigned char)(CANRIDR1>>5);
if (MsgPtr->can_msg.id != CAL_ID)
{
CANRFLG = 0x01;
return (FALSE);
}
// Read Data Length
MsgPtr->can_msg.len = CANRDLR_DLC;
MsgPtr->can_msg.id = (unsigned int)(CANRIDR0<<3) | (unsigned char)(CANRIDR1>>5);
if (MsgPtr->can_msg.id != CAL_ID)
{
CANRFLG = 0x01;
return (FALSE);
}
// Read Data Length
MsgPtr->can_msg.len = CANRDLR_DLC;
for(i = 0; i <8; i++) MsgPtr->can_msg.data[i] = *((&CANRDSR0)+i);
CANRFLG = 0x01;
return(TRUE);
}
//-------------------------------fashou
Bool cal_txMsg(cal_msg *MsgPtr)
{
return(TRUE);
}
//-------------------------------fashou
Bool cal_txMsg(cal_msg *MsgPtr)
{
uchar n_tx_buf, i;
if(MsgPtr->can_msg.len > MAX_LEN) return(FALSE);
if(MsgPtr->can_msg.len > MAX_LEN) return(FALSE);
if(!(CANCTL0&0x10)) return(FALSE);
n_tx_buf = 0;
CANTBSEL=CANTFLG;
n_tx_buf=CANTBSEL;
if(!n_tx_buf) return(FALSE);
CANTBSEL=CANTFLG;
n_tx_buf=CANTBSEL;
if(!n_tx_buf) return(FALSE);
// Write Identifier
CANTIDR0 = (uchar)(MsgPtr->can_msg.id>>3);
CANTIDR1 = (uchar)(MsgPtr->can_msg.id<<5);
CANTIDR1_IDE = 0;//标准ID时,SRR 和IDE 都为0
CANTIDR1_SRR = 0;//0 数据帧;1 远程帧
for(i = 0; i < 8; i++) *((&CANTDSR0)+i) = MsgPtr->can_msg.data[i];
// Write Data Length
CANTDLR = MsgPtr->can_msg.len;
// Clear TXx Flag (buffer ready to transmission)
CANTFLG = n_tx_buf;
return(TRUE);
}
CANTIDR0 = (uchar)(MsgPtr->can_msg.id>>3);
CANTIDR1 = (uchar)(MsgPtr->can_msg.id<<5);
CANTIDR1_IDE = 0;//标准ID时,SRR 和IDE 都为0
CANTIDR1_SRR = 0;//0 数据帧;1 远程帧
for(i = 0; i < 8; i++) *((&CANTDSR0)+i) = MsgPtr->can_msg.data[i];
// Write Data Length
CANTDLR = MsgPtr->can_msg.len;
// Clear TXx Flag (buffer ready to transmission)
CANTFLG = n_tx_buf;
return(TRUE);
}
