📄 main.c
字号:
/* main.c: FlexCAN program */
/* Description: Transmit one message from FlexCAN A buf. 0 to FlexCAN C buf. 4 */
/* Rev 0.1 Jan 16, 2006 S.Mihalik, Copyright Freescale, 2006. All Rights Reserved */
/* Rev 0.2 Jun 6 2006 SM - changed Flexcan A to C & enabled 64 msg buffers */
/* Rev 0.3 Jun 15 2006 SM - 1. Made globals uninitialized */
/* 2. RecieveMsg function: read CANx_TIMER, removed setting buffer's CODE*/
/* 3. added idle loop code for smoother Nexus trace */
/* 4. modified for newer Freescale header files (r 16) */
/* Rev 0.4 Aug 11 2006 SM - Removed redundant CAN_A.MCR init */
/* Rev 0.5 Jan 31 2007 SM - Removed other redundant CAN_C.MCR init */
/* Rev 0.6 Mar 08 2007 SM - Correced init of MBs - cleared 64 MBs, instead of 63 */
/* Rev 0.7 Jul 20 2007 SM - Changes for MPC5510 */
/* Notes: */
/* 1. MMU not initialized; must be done by debug scripts or BAM */
/* 2. SRAM not initialized; must be done by debug scripts or in a crt0 type file */
#include "mpc563m.h" /* Use proper include file such as mpc5510.h or mpc5554.h */
uint8_t RxCODE; /* Received message buffer code */
uint32_t RxID; /* Received message ID */
uint8_t RxLENGTH; /* Recieved message number of data bytes */
uint8_t RxDATA[8]; /* Received message data string*/
uint32_t RxTIMESTAMP; /* Received message time */
void initCAN_A (void) {
uint8_t i;
CAN_A.MCR.R = 0x5000003F; /* Put in Freeze Mode & enable all 64 message buffers */
CAN_A.CR.R = 0x04DB0006; /* Configure for 8MHz OSC, 100KHz bit time */
for (i=0; i<64; i++) {
CAN_A.BUF[i].CS.B.CODE = 0; /* Inactivate all message buffers */
}
CAN_A.BUF[0].CS.B.CODE = 8; /* Message Buffer 0 set to TX INACTIVE */
/* Use 1 pair of the next four lines of code for MPC551x or MPC555x */
/*SIU.PCR[48].R = 0x0620;*/ /* MPC551x: Configure pad as CNTXA, open drain */
/*SIU.PCR[49].R = 0x0500;*/ /* MPC551x: Configure pad as CNRXA */
SIU.PCR[83].R = 0x0620; /* MPC555x: Configure pad as CNTXA, open drain */
SIU.PCR[84].R = 0x0500; /* MPC555x: Configure pad as CNRXA */
CAN_A.MCR.R = 0x0000003F; /* Negate FlexCAN A halt state for 64 MB */
}
void initCAN_C (void) {
uint8_t i;
CAN_C.MCR.R = 0x5000003F; /* Put in Freeze Mode & enable all 64 message buffers */
CAN_C.CR.R = 0x04DB0006; /* Configure for 8MHz OSC, 100KHz bit time */
/* Use one of the next two lines: */
/* for (i=0; i<64; i++) { */ /* MPC551x, MPC555x except MPC553x: init 64 buffers */
for (i=0; i<32; i++) { /* MPC563x: init 32 message buffers */
CAN_C.BUF[i].CS.B.CODE = 0; /* Inactivate all message buffers */
}
CAN_C.BUF[4].CS.B.IDE = 0; /* MB 4 will look for a standard ID */
CAN_C.BUF[4].ID.B.STD_ID = 555; /* MB 4 will look for ID = 555 */
CAN_C.BUF[4].CS.B.CODE = 4; /* MB 4 set to RX EMPTY */
CAN_C.RXGMASK.R = 0x1FFFFFFF; /* Global acceptance mask */
/* Use 1 pair of the next four lines of code for MPC551x or MPC555x */
/*SIU.PCR[52].R = 0x0620;*/ /* MPC551x: Configure pad as CNTXC, open drain */
/*SIU.PCR[53].R = 0x0500;*/ /* MPC551x: Configure pad as CNRXC */
SIU.PCR[87].R = 0x0E20; /* MPC555x: Configure pad as CNTXC, open drain */
SIU.PCR[88].R = 0x0D00; /* MPC555x: Configure pad as CNRXC */
CAN_C.MCR.R = 0x0000003F; /* Negate FlexCAN C halt state for 64 MB */
}
void TransmitMsg (void) {
uint8_t i;
/* Assumption: Message buffer CODE is INACTIVE */
const uint8_t TxData[] = {"Hello"}; /* Transmit string*/
CAN_A.BUF[0].CS.B.IDE = 0; /* Use standard ID length */
CAN_A.BUF[0].ID.B.STD_ID = 555; /* Transmit ID is 555 */
CAN_A.BUF[0].CS.B.RTR = 0; /* Data frame, not remote Tx request frame */
CAN_A.BUF[0].CS.B.LENGTH = sizeof(TxData) -1 ; /* # bytes to transmit w/o null */
for (i=0; i<sizeof(TxData); i++) {
CAN_A.BUF[0].DATA.B[i] = TxData[i]; /* Data to be transmitted */
}
CAN_A.BUF[0].CS.B.SRR = 1; /* Tx frame (not req'd for standard frame)*/
CAN_A.BUF[0].CS.B.CODE =0xC; /* Activate msg. buf. to transmit data frame */
}
void RecieveMsg (void) {
uint8_t j;
uint16_t dummy;
while (CAN_C.IFRL.B.BUF04I == 0) {}; /* Wait for CAN C MB 4 flag */
RxCODE = CAN_C.BUF[4].CS.B.CODE; /* Read CODE, ID, LENGTH, DATA, TIMESTAMP */
RxID = CAN_C.BUF[4].ID.B.STD_ID;
RxLENGTH = CAN_C.BUF[4].CS.B.LENGTH;
for (j=0; j<RxLENGTH; j++) {
RxDATA[j] = CAN_C.BUF[4].DATA.B[j];
}
RxTIMESTAMP = CAN_C.BUF[4].CS.B.TIMESTAMP;
dummy = CAN_C.TIMER.R; /* Read TIMER to unlock message buffers */
CAN_C.IFRL.B.BUF04I = 1; /* Clear CAN C MB 4 flag */
}
void main(void) {
volatile uint32_t IdleCtr = 0;
initCAN_A(); /* Initialize FlexCAN A & one of its buffers for transmit*/
initCAN_C(); /* Initialize FLEXCAN C & one of its buffers for receive*/
TransmitMsg(); /* Transmit one message from a FlexCAN A buffer */
RecieveMsg(); /* Wait for the message to be recieved at FlexCAN C */
while (1) { /* Idle loop: increment counter */
IdleCtr++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -