📄 mscan.c
字号:
/*******************************************************************************/
/**
Copyright (c) 2007 Freescale Semiconductor
\file MSCAN.c
\brief Driver for basic MSCAN functions
\author Freescale Semiconductor
\author B05114
\version 0.1
\date January/ 2008
*/
/*******************************************************************************/
/** Derivative information */
#include "derivative.h"
/** MSCAN definitions */
#include "MSCAN.h"
extern UINT8 RxData[8]; /* Reception array */
/*******************************************************************************/
/**
* \brief MSCAN configuration, baud rate = 500kbps, 16 bit acceptance filters
* \author B05114
* \param void
* \return void
*/
void MSCAN_Init(void)
{
CANCTL0_INITRQ = 1; /* MSCAN in initialization mode */
while (!(CANCTL1_INITAK))
; /* Wait for initialization mode acknowledge */
CANCTL1_CANE = 1; /* Enable MSCAN module */
CANCTL1_CLKSRC = CAN_CLK; /* Clock source selection */
CANCTL1_LOOPB = 0; /* Set to 1 for LoopBack Mode, 0 otherwise */
CANCTL1_LISTEN = 0; /* Not listen only mode */
CANBTR1_TSEG_10 = TIME_SEGM1; /* Time Segment 1 */
CANBTR1_TSEG_20 = TIME_SEGM2; /* Time Segment 2 */
CANBTR0_BRP = CAN_PRESC; /* Baud rate prescaler */
CANBTR0_SJW = CAN_SYNJW; /* Sinchronization jump width */
CANBTR1_SAMP = 0; /* One sample per bit */
CANIDAC_IDAM = 0x01; /* Four 16-bit acceptance filters */
CANIDAR0 = ACC_CODE_ID_HIGH; /* 16 bit Filter 0 */
CANIDMR0 = MASK_CODE_ST_ID_HIGH;
CANIDAR1 = ACC_CODE_ID_LOW;
CANIDMR1 = MASK_CODE_ST_ID_LOW;
CANIDAR2 = 0x00; /* 16 bit Filter 1 */
CANIDMR2 = MASK_CODE_ST_ID_HIGH;
CANIDAR3 = 0x00;
CANIDMR3 = MASK_CODE_ST_ID_LOW;
CANIDAR4 = 0x00; /* 16 bit Filter 2 */
CANIDMR4 = MASK_CODE_ST_ID_HIGH;
CANIDAR5 = 0x00;
CANIDMR5 = MASK_CODE_ST_ID_LOW;
CANIDAR6 = 0x00; /* 16 bit Filter 3 */
CANIDMR6 = MASK_CODE_ST_ID_HIGH;
CANIDAR7 = 0x00;
CANIDMR7 = MASK_CODE_ST_ID_LOW;
CANCTL0_INITRQ = 0; /* Exit initialization mode request */
while (CANCTL1_INITAK)
; /* Wait for normal mode */
while(!(CANCTL0_SYNCH))
; /* Wait for CAN synchronization */
CANRFLG_RXF = 1; /* Clear receiver flags */
CANRIER_RXFIE = 1; /* Enable Full Receive Buffer interrupt */
}
/*******************************************************************************/
/**
* \brief CAN frame transmission
* \author B05114
* \param CAN_ID: Identifier \n
Msg_Prio: Priority \n
Msg_Length: Frame size \n
CANTxData: Data array
* \return void
*/
UINT8 MSCAN_SendFrame(UINT32 CAN_ID, UINT8 Msg_Prio, UINT8 Msg_Length, UINT8 *CANTxData)
{
/* Transmission buffer */
UINT8 TxBuffer = {0};
/* Index to data within the transmission buffer */
UINT8 Index;
if (!CANTFLG) /* Retrun if Transmit Buffer is full */
return ERR_BUFFER_FULL;
CANTBSEL = CANTFLG; /* Select lowest empty buffer */
TxBuffer = CANTBSEL; /* Backup selected buffer */
*((UINT32 *) ((UINT32)(&CANTIDR0)))= CAN_ID; /* Load Id to IDR Registers */
/* Load data to Data Segment Registers */
for (Index=0;Index<Msg_Length;Index++) {
*(&CANTDSR0 + Index) = CANTxData[Index];
}
CANTDLR = Msg_Length; /* Set Data Length Code */
CANTTBPR = Msg_Prio; /* Set Priority */
CANTFLG = TxBuffer; /* Start transmission */
while ( (CANTFLG & TxBuffer) != TxBuffer)
; /* Wait for Transmission completion */
return NO_ERR;
}
/*******************************************************************************/
/**
* \brief CAN reception interrupt service routine
* \author B05114
*/
interrupt void CANRx_ISR(void)
{
/* Length of received frame */
UINT8 Msg_Length;
/* Index for extracting/storing received data */
UINT8 Index;
Msg_Length = (CANRDLR & 0x0F); /* Extract received frame data length */
/* Read and store each of the received data */
for (Index=0; Index < Msg_Length; Index++)
RxData[Index] = *(&CANRDSR0 + Index);
CANRFLG_RXF = 1; /* Clear reception flag */
}
/*******************************************************************************/
/*******************************************************************************/
/* */
/* All software, source code, included documentation, and any implied know-how */
/* are property of Freescale Semiconductor and therefore considered */
/* CONFIDENTIAL INFORMATION. */
/* */
/* This confidential information is disclosed FOR DEMONSTRATION PURPOSES ONLY. */
/* */
/* All Confidential Information remains the property of Freescale Semiconductor*/
/* and will not be copied or reproduced without the express written permission */
/* of the Discloser, except for copies that are absolutely necessary in order */
/* to fulfill the Purpose. */
/* */
/* Services performed by FREESCALE in this matter are performed AS IS and */
/* without any warranty. CUSTOMER retains the final decision relative to the */
/* total design and functionality of the end product. */
/* */
/* FREESCALE neither guarantees nor will be held liable by CUSTOMER for the */
/* success of this project. */
/* */
/* FREESCALE disclaims all warranties, express, implied or statutory including,*/
/* but not limited to, implied warranty of merchantability or fitness for a */
/* particular purpose on any hardware, software ore advise supplied to the */
/* project by FREESCALE, and or any product resulting from FREESCALE services. */
/* */
/* In no event shall FREESCALE be liable for incidental or consequential */
/* damages arising out of this agreement. CUSTOMER agrees to hold FREESCALE */
/* harmless against any and all claims demands or actions by anyone on account */
/* of any damage,or injury, whether commercial, contractual, or tortuous, */
/* rising directly or indirectly as a result of the advise or assistance */
/* supplied CUSTOMER in connectionwith product, services or goods supplied */
/* under this Agreement. */
/* */
/*******************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -