?? lle_mscan.c
字號:
/*******************************************************************************/
/**
Copyright (c) 2007 Freescale Semiconductor
\file lle_MSCAN.c
\brief Driver for basic MSCAN functions
\author Freescale Semiconductor
\author B05114
\version 0.2
\date March/ 2008
*/
/*******************************************************************************/
/** S12X derivative information */
#include <MC9S12XEP100.h>
/** MSCAN definitions */
#include "lle_MSCAN.h"
/*******************************************************************************/
/**
* \brief MSCAN3 configuration, baud rate=500kbps, 16 bit acceptance filters
* \author B05114
* \param void
* \return void
*/
void lle_CAN_Init(void)
{
CAN3CTL0 = 0x01; /* MSCAN in initialization mode */
while (!(CAN3CTL1_INITAK))
; /* Wait for initialization mode acknowledge */
CAN3CTL1_CANE = 1; /* Enable MSCAN module */
CAN3CTL1_CLKSRC = 0; /* Clock source is OSCCLK, CANCLK = 8MHz */
CAN3CTL1_LOOPB = 0; /* Set to 1 for LoopBack Mode, 0 otherwise */
CAN3CTL1_LISTEN = 0; /* Not listen only mode */
/* Baud rate = CANCLK/(Prescaler * time quantas) */
CAN3BTR1_TSEG_10 = 0x04; /* Time Segment 1 = 5 */
CAN3BTR1_TSEG_20 = 0x01; /* Time Segment 2 = 2 */
/* TSEG1 + TSEG2 + SYNCH_SEG = 8 time quantas */
/* Prescaler = CANCLK/(Baud rate * time quantas) = 8MHz/(500kHz * 8) = 2 */
CAN3BTR0_BRP = 0x01; /* Baud rate prescaler = 2 */
CAN3BTR0_SJW = 0x01; /* Sinchronization jump width = 2 clock cycles */
CAN3BTR1_SAMP = 0; /* One sample per bit */
/* Four 16-bit acceptance filters */
CAN3IDAC_IDAM = 0x01;
CAN3IDAR0 = ACC_CODE_ID_HIGH; /* 16 bit Filter 0 */
CAN3IDMR0 = MASK_CODE_ST_ID_HIGH;
CAN3IDAR1 = ACC_CODE_ID_LOW;
CAN3IDMR1 = MASK_CODE_ST_ID_LOW;
CAN3IDAR2 = 0x00; /* 16 bit Filter 1 */
CAN3IDMR2 = MASK_CODE_ST_ID_HIGH;
CAN3IDAR3 = 0x00;
CAN3IDMR3 = MASK_CODE_ST_ID_LOW;
CAN3IDAR4 = 0x00; /* 16 bit Filter 2 */
CAN3IDMR4 = MASK_CODE_ST_ID_HIGH;
CAN3IDAR5 = 0x00;
CAN3IDMR5 = MASK_CODE_ST_ID_LOW;
CAN3IDAR6 = 0x00; /* 16 bit Filter 3 */
CAN3IDMR6 = MASK_CODE_ST_ID_HIGH;
CAN3IDAR7 = 0x00;
CAN3IDMR7 = MASK_CODE_ST_ID_LOW;
CAN3CTL0_INITRQ = 0; ; /* Exit initialization mode request */
while (CAN3CTL1_INITAK)
; /* Wait for normal mode */
while(!(CAN3CTL0_SYNCH))
; /* Wait for CAN synchronization */
CAN3RFLG_RXF = 1; /* Clear receiver flags */
CAN3RIER_RXFIE = 1; /* Enable Full Receive Buffer interrupt */
}
/*******************************************************************************/
/**
* \brief Set MSCAN3 in listen-only mode
* \author B05114
* \param void
* \return void
*/
void lle_CAN_ListenOnly(void)
{
CAN3CTL0_INITRQ = 1; /* MSCAN in initialization mode */
while (!(CAN3CTL1_INITAK))
; /* Wait for initialization mode acknowledge */
CAN3CTL1_LISTEN = 1; /* Set MSCAN module in listen mode */
CAN3CTL0_INITRQ = 0; /* Exit initialization mode request */
while (CAN3CTL1_INITAK)
;
}
/*******************************************************************************/
/**
* \brief CAN frame transmission
* \author B05114
* \param u32ID: Identifier \n
u8Prio: Priority \n
u8Length: Frame size \n
u8TxData: Data array
* \return void
*/
UINT8 lle_CAN_SendFrame(UINT32 u32ID, UINT8 u8Prio, UINT8 u8Length, UINT8 *u8TxData)
{
/* Counter of transmission attempts */
UINT8 u8Attempts = 50;
/* Transmission buffer */
UINT8 u8TxBuffer = {0};
/* Index to data within the transmission buffer */
UINT8 u8Index;
if (!CAN3TFLG) /* Return if Transmit Buffer is full */
return ERR_BUFFER_FULL;
CAN3TBSEL = CAN3TFLG; /* Select lowest empty buffer */
u8TxBuffer = CAN3TBSEL; /* Backup selected buffer */
*((UINT32 *) ((UINT32)(&CAN3TXIDR0)))= u32ID; /* Load Id to IDR Registers */
/* Load data to Data Segment Registers */
for (u8Index=0;u8Index<u8Length;u8Index++) {
*(&CAN3TXDSR0 + u8Index) = u8TxData[u8Index];
}
CAN3TXDLR = u8Length; /* Set Data Length Code */
CAN3TXTBPR = u8Prio; /* Set Priority */
CAN3TFLG = u8TxBuffer; /* Start transmission */
/* Wait for Transmission completion a fixed number of attempts */
while (((CAN3TFLG & u8TxBuffer) != u8TxBuffer) && u8Attempts)
{
u8Attempts --;
}
if (!u8Attempts)
{
/* If attempts counter expires return failure code */
return TX_FAILURE;
}
else
{
/* Indicate sucessfull transmission */
return NO_ERR;
}
}
/*******************************************************************************/
/**
* \brief Read CAN reception buffer
* \author B05114
* \param void
* \return void
*/
void lle_CAN_ReceivedFrame(void)
{
/* Length of received frame */
UINT8 u8Length;
/* Index for extracting/storing received data */
UINT8 u8Index;
/* Reception array */
UINT8 u8RxData[8];
u8Length = (CAN3RXDLR & 0x0F); /* Extract received frame data length */
/* Read and store each of the received data */
for (u8Index=0; u8Index<u8Length; u8Index++)
u8RxData[u8Index] = *(&CAN3RXDSR0 + u8Index);
CAN3RFLG_RXF = 1; /* Clear reception flag */
}
/*******************************************************************************/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -