?? mscan.c
字號:
//////////////////////////////////////////////////////////////////////////////
//
// Sample for SofTec Microsystems SK-S12XDP512-A Starter Kit
// (Freescale code: EVB9S12XDP512)
//
// ---------------------------------------------------------------------------
//
// Copyright (c) 2005 SofTec Microsystems
// http://www.softecmicro.com/
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// NOTES:
//
// - The following parameters are calculated according to application note
// AN1798.
// - Only standard identifiers are used.
//
//////////////////////////////////////////////////////////////////////////////
//
// fNBT = 125000 bit/s
// CANCLK = 4000000 Hz
// tBUS = 25 ns (0.5 m * 5*10^-9 sm^-1)
// tTx = 500 ns
// tRx = 500 ns
//
// NBT = 10
// Tq = 0.75ns
// P = 3
// SYNC_SEG = 1Tq
// PROP_SEG = 3Tq
// PHASE_SEG1 = 3Tq
// PHASE_SEG2 = 3Tq
// RJW = 3
// OscToll = 1.181102%
//
//////////////////////////////////////////////////////////////////////////////
#include "mc9s12xdp512.h"
#include "mscan.h"
//////////////////////////////////////////////////////////////////////////////
// Variables
//////////////////////////////////////////////////////////////////////////////
unsigned char *can_periph[5] = {
&CAN0CTL0,
&CAN1CTL0,
&CAN2CTL0,
&CAN3CTL0,
&CAN4CTL0
};
//////////////////////////////////////////////////////////////////////////////
// MSCAN Peripheral Initialization
//////////////////////////////////////////////////////////////////////////////
void MSCANInit(unsigned char can_num)
{
unsigned char *can_pt;
can_pt = can_periph[can_num];
// If MSCAN peripheral is not in Initialization Mode, enables the Inizialization Mode Request
if(!(can_pt[CANCTL1]&CANCTL1_INITAK_MASK))
{
can_pt[CANCTL0] = CANCTL0_INITRQ_MASK;
while(!(can_pt[CANCTL1]&CANCTL1_INITAK_MASK))
;
}
// Enables MSCAN peripheral and chooses Oscillator Clock, Loop Disabled and Normal Operation
can_pt[CANCTL1] = 0x80;
// Configures SJW = 3Tq and Prescaler = 3
can_pt[CANBTR0] = 0x82;
// Configures One Sample, Time Segment 1 = 6Tq and Time Segment 2 = 3Tq
can_pt[CANBTR1] = 0x25;
// Disables all the Filters
can_pt[CANIDMR_1B+0] = 0xFF;
can_pt[CANIDMR_1B+1] = 0xFF;
can_pt[CANIDMR_1B+2] = 0xFF;
can_pt[CANIDMR_1B+3] = 0xFF;
can_pt[CANIDMR_2B+0] = 0xFF;
can_pt[CANIDMR_2B+1] = 0xFF;
can_pt[CANIDMR_2B+2] = 0xFF;
can_pt[CANIDMR_2B+3] = 0xFF;
// Restarts MSCAN peripheral and waits for Initialization Mode exit
can_pt[CANCTL0] = 0x00;
while(can_pt[CANCTL1]&CANCTL1_INITAK_MASK)
;
// Waits for MSCAN synchronization with the CAN bus
while(!(can_pt[CANCTL0]&CANCTL0_SYNCH_MASK))
;
}
//////////////////////////////////////////////////////////////////////////////
// MSCAN Send Message Routine
//////////////////////////////////////////////////////////////////////////////
Bool MSCANSendMsg(unsigned char can_num, struct can_msg msg)
{
unsigned char n_tx_buf = 0, i;
unsigned char *can_pt;
can_pt = can_periph[can_num];
if(msg.len > 8)
return(FALSE);
if(!(can_pt[CANCTL0]&CANCTL0_SYNCH_MASK))
return(FALSE);
while(!(can_pt[CANTFLG]&MaskOR(n_tx_buf)))
n_tx_buf = (n_tx_buf == MAX_TX_BUFFERS)? 0: (unsigned char)(n_tx_buf + 1);
can_pt[CANTBSEL] = MaskOR(n_tx_buf);
can_pt[CANTXIDR+0] = (unsigned char)(msg.id>>3);
can_pt[CANTXIDR+1] = (unsigned char)(msg.id<<5);
if(msg.RTR)
can_pt[CANTXIDR+1] |= 0x10;
for(i = 0; i < msg.len; i++)
can_pt[CANTXDSR+i] = msg.data[i];
can_pt[CANTXDLR] = msg.len;
can_pt[CANTXTBPR] = msg.prty;
can_pt[CANTFLG] = MaskOR(n_tx_buf);
return(TRUE);
}
//////////////////////////////////////////////////////////////////////////////
// MSCAN Get Message Routine
//////////////////////////////////////////////////////////////////////////////
Bool MSCANGetMsg(unsigned char can_num, struct can_msg *msg)
{
unsigned char i;
unsigned char *can_pt;
can_pt = can_periph[can_num];
if(!(can_pt[CANRFLG]&CANRFLG_RXF_MASK))
return(FALSE);
if(can_pt[CANRXIDR+1]&0x08)
return(FALSE);
msg->id = ((can_pt[CANRXIDR+0]<<3)&0x0700) | (unsigned char)(can_pt[CANRXIDR+0]<<3) | (unsigned char)(can_pt[CANRXIDR+1]>>5);
if(can_pt[CANRXIDR+1]&0x10)
msg->RTR = TRUE;
else
msg->RTR = FALSE;
msg->len = can_pt[CANRXDLR];
for(i = 0; i < msg->len; i++)
msg->data[i] = can_pt[CANRXDSR+i];
can_pt[CANRFLG] = CANRFLG_RXF_MASK;
return(TRUE);
}
//////////////////////////////////////////////////////////////////////////////
// MSCAN Check for Received Message Routine
//////////////////////////////////////////////////////////////////////////////
Bool MSCANCheckRcvdMsg(unsigned char can_num)
{
unsigned char *can_pt;
can_pt = can_periph[can_num];
if(can_pt[CANRFLG]&CANRFLG_RXF_MASK)
return(TRUE);
return(FALSE);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -