📄 lpc17xx_can.c.svn-base
字号:
/**********************************************************************
* $Id$ lpc17xx_can.c 2011-03-09
*//**
* @file lpc17xx_can.c
* @brief Contains all functions support for CAN firmware library on LPC17xx
* @version 3.3
* @date 09. March. 2011
* @author NXP MCU SW Application Team
*
* Copyright(C) 2011, NXP Semiconductor
* All rights reserved.
*
***********************************************************************
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* products. This software is supplied "AS IS" without any warranties.
* NXP Semiconductors assumes no responsibility or liability for the
* use of the software, conveys no license or title under any patent,
* copyright, or mask work right to the product. NXP Semiconductors
* reserves the right to make changes in the software without
* notification. NXP Semiconductors also make no representation or
* warranty that such application will be suitable for the specified
* use without further testing or modification.
**********************************************************************/
/* Peripheral group ----------------------------------------------------------- */
/** @addtogroup CAN
* @{
*/
/* Includes ------------------------------------------------------------------- */
#include "lpc17xx_can.h"
#include "lpc17xx_clkpwr.h"
/* If this source file built with example, the LPC17xx FW library configuration
* file in each example directory ("lpc17xx_libcfg.h") must be included,
* otherwise the default FW library configuration file must be included instead
*/
#ifdef __BUILD_WITH_EXAMPLE__
#include "lpc17xx_libcfg.h"
#else
#include "lpc17xx_libcfg_default.h"
#endif /* __BUILD_WITH_EXAMPLE__ */
#ifdef _CAN
/* Private Variables ---------------------------------------------------------- */
/** @defgroup CAN_Private_Variables CAN Private Variables
* @{
*/
FunctionalState FULLCAN_ENABLE;
/* Counts number of filters (CAN message objects) used */
uint16_t CANAF_FullCAN_cnt = 0;
uint16_t CANAF_std_cnt = 0;
uint16_t CANAF_gstd_cnt = 0;
uint16_t CANAF_ext_cnt = 0;
uint16_t CANAF_gext_cnt = 0;
/* End of Private Variables ----------------------------------------------------*/
/**
* @}
*/
/* Private Variables ---------------------------------------------------------- */
static void can_SetBaudrate (LPC_CAN_TypeDef *CANx, uint32_t baudrate);
/*********************************************************************//**
* @brief Setting CAN baud rate (bps)
* @param[in] CANx point to LPC_CAN_TypeDef object, should be:
* - LPC_CAN1: CAN1 peripheral
* - LPC_CAN2: CAN2 peripheral
* @param[in] baudrate: is the baud rate value will be set
* @return None
***********************************************************************/
static void can_SetBaudrate (LPC_CAN_TypeDef *CANx, uint32_t baudrate)
{
uint32_t result = 0;
uint8_t NT, TSEG1, TSEG2, BRFail;
uint32_t CANPclk = 0;
uint32_t BRP;
CHECK_PARAM(PARAM_CANx(CANx));
if (CANx == LPC_CAN1)
{
CANPclk = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_CAN1);
}
else
{
CANPclk = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_CAN2);
}
result = CANPclk / baudrate;
/* Calculate suitable nominal time value
* NT (nominal time) = (TSEG1 + TSEG2 + 3)
* NT <= 24
* TSEG1 >= 2*TSEG2
*/
BRFail = 1;
for(NT=24;NT>0;NT=NT-2)
{
if ((result%NT)==0)
{
BRP = result / NT - 1;
NT--;
TSEG2 = (NT/3) - 1;
TSEG1 = NT -(NT/3) - 1;
BRFail = 0;
break;
}
}
if(BRFail)
while(1); // Failed to calculate exact CAN baud rate
/* Enter reset mode */
CANx->MOD = 0x01;
/* Set bit timing
* Default: SAM = 0x00;
* SJW = 0x03;
*/
CANx->BTR = (TSEG2<<20)|(TSEG1<<16)|(3<<14)|BRP;
/* Return to normal operating */
CANx->MOD = 0;
}
/* End of Private Functions ----------------------------------------------------*/
/* Public Functions ----------------------------------------------------------- */
/** @addtogroup CAN_Public_Functions
* @{
*/
/********************************************************************//**
* @brief Initialize CAN peripheral with given baudrate
* @param[in] CANx pointer to LPC_CAN_TypeDef, should be:
* - LPC_CAN1: CAN1 peripheral
* - LPC_CAN2: CAN2 peripheral
* @param[in] baudrate: the value of CAN baudrate will be set (bps)
* @return None
*********************************************************************/
void CAN_Init(LPC_CAN_TypeDef *CANx, uint32_t baudrate)
{
uint32_t temp;
uint16_t i;
CHECK_PARAM(PARAM_CANx(CANx));
if(CANx == LPC_CAN1)
{
/* Turn on power and clock for CAN1 */
CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN1, ENABLE);
/* Set clock divide for CAN1 */
}
else
{
/* Turn on power and clock for CAN1 */
CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN2, ENABLE);
/* Set clock divide for CAN2 */
}
CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_CAN1, CLKPWR_PCLKSEL_CCLK_DIV_2);
CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_CAN2, CLKPWR_PCLKSEL_CCLK_DIV_2);
CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_ACF, CLKPWR_PCLKSEL_CCLK_DIV_2);
CANx->MOD = 1; // Enter Reset Mode
CANx->IER = 0; // Disable All CAN Interrupts
CANx->GSR = 0;
/* Request command to release Rx, Tx buffer and clear data overrun */
//CANx->CMR = CAN_CMR_AT | CAN_CMR_RRB | CAN_CMR_CDO;
CANx->CMR = (1<<1)|(1<<2)|(1<<3);
/* Read to clear interrupt pending in interrupt capture register */
temp = CANx->ICR;
CANx->MOD = 0;// Return Normal operating
//Reset CANAF value
LPC_CANAF->AFMR = 0x01;
//clear ALUT RAM
for (i = 0; i < 512; i++) {
LPC_CANAF_RAM->mask[i] = 0x00;
}
LPC_CANAF->SFF_sa = 0x00;
LPC_CANAF->SFF_GRP_sa = 0x00;
LPC_CANAF->EFF_sa = 0x00;
LPC_CANAF->EFF_GRP_sa = 0x00;
LPC_CANAF->ENDofTable = 0x00;
LPC_CANAF->AFMR = 0x00;
/* Set baudrate */
can_SetBaudrate (CANx, baudrate);
}
/********************************************************************//**
* @brief CAN deInit
* @param[in] CANx pointer to LPC_CAN_TypeDef, should be:
* - LPC_CAN1: CAN1 peripheral
* - LPC_CAN2: CAN2 peripheral
* @return None
*********************************************************************/
void CAN_DeInit(LPC_CAN_TypeDef *CANx)
{
CHECK_PARAM(PARAM_CANx(CANx));
if(CANx == LPC_CAN1)
{
/* Turn on power and clock for CAN1 */
CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN1, DISABLE);
}
else
{
/* Turn on power and clock for CAN1 */
CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN2, DISABLE);
}
}
/********************************************************************//**
* @brief Setup Acceptance Filter Look-Up Table
* @param[in] CANAFx pointer to LPC_CANAF_TypeDef
* Should be: LPC_CANAF
* @param[in] AFSection the pointer to AF_SectionDef structure
* It contain information about 5 sections will be install in AFLUT
* @return CAN Error could be:
* - CAN_OBJECTS_FULL_ERROR: No more rx or tx objects available
* - CAN_AF_ENTRY_ERROR: table error-violation of ascending numerical order
* - CAN_OK: ID is added into table successfully
*********************************************************************/
CAN_ERROR CAN_SetupAFLUT(LPC_CANAF_TypeDef* CANAFx, AF_SectionDef* AFSection)
{
uint8_t ctrl1,ctrl2;
uint8_t dis1, dis2;
uint16_t SID, ID_temp,i, count = 0;
uint32_t EID, entry, buf;
uint16_t lowerSID, upperSID;
uint32_t lowerEID, upperEID;
CHECK_PARAM(PARAM_CANAFx(CANAFx));
CANAFx->AFMR = 0x01;
/***** setup FullCAN Table *****/
if(AFSection->FullCAN_Sec == NULL)
{
FULLCAN_ENABLE = DISABLE;
}
else
{
FULLCAN_ENABLE = ENABLE;
for(i=0;i<(AFSection->FC_NumEntry);i++)
{
if(count + 1 > 64)
{
return CAN_OBJECTS_FULL_ERROR;
}
ctrl1 = AFSection->FullCAN_Sec->controller;
SID = AFSection->FullCAN_Sec->id_11;
dis1 = AFSection->FullCAN_Sec->disable;
CHECK_PARAM(PARAM_CTRL(ctrl1));
CHECK_PARAM(PARAM_ID_11(SID));
CHECK_PARAM(PARAM_MSG_DISABLE(dis1));
entry = 0x00; //reset entry value
if((CANAF_FullCAN_cnt & 0x00000001)==0)
{
if(count!=0x00)
{
buf = LPC_CANAF_RAM->mask[count-1];
ID_temp = (buf & 0xE7FF); //mask controller & identifier bits
if(ID_temp > ((ctrl1<<13)|SID))
{
return CAN_AF_ENTRY_ERROR;
}
}
entry = (ctrl1<<29)|(dis1<<28)|(SID<<16)|(1<<27);
LPC_CANAF_RAM->mask[count] &= 0x0000FFFF;
LPC_CANAF_RAM->mask[count] |= entry;
CANAF_FullCAN_cnt++;
if(CANAF_FullCAN_cnt == AFSection->FC_NumEntry) //this is the lastest FullCAN entry
count++;
}
else
{
buf = LPC_CANAF_RAM->mask[count];
ID_temp = (buf >>16) & 0xE7FF;
if(ID_temp > ((ctrl1<<13)|SID))
{
return CAN_AF_ENTRY_ERROR;
}
entry = (ctrl1<<13)|(dis1<<12)|(SID<<0)|(1<<11);
LPC_CANAF_RAM->mask[count] &= 0xFFFF0000;
LPC_CANAF_RAM->mask[count]|= entry;
count++;
CANAF_FullCAN_cnt++;
}
AFSection->FullCAN_Sec = (FullCAN_Entry *)((uint32_t)(AFSection->FullCAN_Sec)+ sizeof(FullCAN_Entry));
}
}
/***** Setup Explicit Standard Frame Format Section *****/
if(AFSection->SFF_Sec != NULL)
{
for(i=0;i<(AFSection->SFF_NumEntry);i++)
{
if(count + 1 > 512)
{
return CAN_OBJECTS_FULL_ERROR;
}
ctrl1 = AFSection->SFF_Sec->controller;
SID = AFSection->SFF_Sec->id_11;
dis1 = AFSection->SFF_Sec->disable;
//check parameter
CHECK_PARAM(PARAM_CTRL(ctrl1));
CHECK_PARAM(PARAM_ID_11(SID));
CHECK_PARAM(PARAM_MSG_DISABLE(dis1));
entry = 0x00; //reset entry value
if((CANAF_std_cnt & 0x00000001)==0)
{
if(CANAF_std_cnt !=0 )
{
buf = LPC_CANAF_RAM->mask[count-1];
ID_temp = (buf & 0xE7FF); //mask controller & identifier bits
if(ID_temp > ((ctrl1<<13)|SID))
{
return CAN_AF_ENTRY_ERROR;
}
}
entry = (ctrl1<<29)|(dis1<<28)|(SID<<16);
LPC_CANAF_RAM->mask[count] &= 0x0000FFFF;
LPC_CANAF_RAM->mask[count] |= entry;
CANAF_std_cnt++;
if(CANAF_std_cnt == AFSection->SFF_NumEntry)//if this is the last SFF entry
count++;
}
else
{
buf = LPC_CANAF_RAM->mask[count];
ID_temp = (buf >>16) & 0xE7FF;
if(ID_temp > ((ctrl1<<13)|SID))
{
return CAN_AF_ENTRY_ERROR;
}
entry = (ctrl1<<13)|(dis1<<12)|(SID<<0);
LPC_CANAF_RAM->mask[count] &= 0xFFFF0000;
LPC_CANAF_RAM->mask[count] |= entry;
count++;
CANAF_std_cnt++;
}
AFSection->SFF_Sec = (SFF_Entry *)((uint32_t)(AFSection->SFF_Sec)+ sizeof(SFF_Entry));
}
}
/***** Setup Group of Standard Frame Format Identifier Section *****/
if(AFSection->SFF_GPR_Sec != NULL)
{
for(i=0;i<(AFSection->SFF_GPR_NumEntry);i++)
{
if(count + 1 > 512)
{
return CAN_OBJECTS_FULL_ERROR;
}
ctrl1 = AFSection->SFF_GPR_Sec->controller1;
ctrl2 = AFSection->SFF_GPR_Sec->controller2;
dis1 = AFSection->SFF_GPR_Sec->disable1;
dis2 = AFSection->SFF_GPR_Sec->disable2;
lowerSID = AFSection->SFF_GPR_Sec->lowerID;
upperSID = AFSection->SFF_GPR_Sec->upperID;
/* check parameter */
CHECK_PARAM(PARAM_CTRL(ctrl1));
CHECK_PARAM(PARAM_CTRL(ctrl2));
CHECK_PARAM(PARAM_MSG_DISABLE(dis1));
CHECK_PARAM(PARAM_MSG_DISABLE(dis2));
CHECK_PARAM(PARAM_ID_11(lowerSID));
CHECK_PARAM(PARAM_ID_11(upperSID));
entry = 0x00;
if(CANAF_gstd_cnt!=0)
{
buf = LPC_CANAF_RAM->mask[count-1];
ID_temp = buf & 0xE7FF;
if((ctrl1 != ctrl2)||(lowerSID > upperSID)||(ID_temp > ((ctrl1<<13)|lowerSID)))
{
return CAN_AF_ENTRY_ERROR;
}
}
entry = (ctrl1 << 29)|(dis1 << 28)|(lowerSID << 16)| \
(ctrl2 << 13)|(dis2 << 12)|(upperSID << 0);
LPC_CANAF_RAM->mask[count] = entry;
CANAF_gstd_cnt++;
count++;
AFSection->SFF_GPR_Sec = (SFF_GPR_Entry *)((uint32_t)(AFSection->SFF_GPR_Sec)+ sizeof(SFF_GPR_Entry));
}
}
/***** Setup Explicit Extend Frame Format Identifier Section *****/
if(AFSection->EFF_Sec != NULL)
{
for(i=0;i<(AFSection->EFF_NumEntry);i++)
{
if(count + 1 > 512)
{
return CAN_OBJECTS_FULL_ERROR;
}
EID = AFSection->EFF_Sec->ID_29;
ctrl1 = AFSection->EFF_Sec->controller;
// check parameter
CHECK_PARAM(PARAM_ID_29(EID));
CHECK_PARAM(PARAM_CTRL(ctrl1));
entry = (ctrl1 << 29)|(EID << 0);
if(CANAF_ext_cnt != 0)
{
buf = LPC_CANAF_RAM->mask[count-1];
// EID_temp = buf & 0x0FFFFFFF;
if(buf > entry)
{
return CAN_AF_ENTRY_ERROR;
}
}
LPC_CANAF_RAM->mask[count] = entry;
CANAF_ext_cnt ++;
count++;
AFSection->EFF_Sec = (EFF_Entry *)((uint32_t)(AFSection->EFF_Sec)+ sizeof(EFF_Entry));
}
}
/***** Setup Group of Extended Frame Format Identifier Section *****/
if(AFSection->EFF_GPR_Sec != NULL)
{
for(i=0;i<(AFSection->EFF_GPR_NumEntry);i++)
{
if(count + 2 > 512)
{
return CAN_OBJECTS_FULL_ERROR;
}
ctrl1 = AFSection->EFF_GPR_Sec->controller1;
ctrl2 = AFSection->EFF_GPR_Sec->controller2;
lowerEID = AFSection->EFF_GPR_Sec->lowerEID;
upperEID = AFSection->EFF_GPR_Sec->upperEID;
//check parameter
CHECK_PARAM(PARAM_CTRL(ctrl1));
CHECK_PARAM(PARAM_CTRL(ctrl2));
CHECK_PARAM(PARAM_ID_29(lowerEID));
CHECK_PARAM(PARAM_ID_29(upperEID));
entry = 0x00;
if(CANAF_gext_cnt != 0)
{
buf = LPC_CANAF_RAM->mask[count-1];
// EID_temp = buf & 0x0FFFFFFF;
if((ctrl1 != ctrl2) || (lowerEID > upperEID) || (buf > ((ctrl1 << 29)|(lowerEID << 0))))
{
return CAN_AF_ENTRY_ERROR;
}
}
entry = (ctrl1 << 29)|(lowerEID << 0);
LPC_CANAF_RAM->mask[count++] = entry;
entry = (ctrl2 << 29)|(upperEID << 0);
LPC_CANAF_RAM->mask[count++] = entry;
CANAF_gext_cnt++;
AFSection->EFF_GPR_Sec = (EFF_GPR_Entry *)((uint32_t)(AFSection->EFF_GPR_Sec)+ sizeof(EFF_GPR_Entry));
}
}
//update address values
LPC_CANAF->SFF_sa = ((CANAF_FullCAN_cnt + 1)>>1)<<2;
LPC_CANAF->SFF_GRP_sa = LPC_CANAF->SFF_sa + (((CANAF_std_cnt+1)>>1)<< 2);
LPC_CANAF->EFF_sa = LPC_CANAF->SFF_GRP_sa + (CANAF_gstd_cnt << 2);
LPC_CANAF->EFF_GRP_sa = LPC_CANAF->EFF_sa + (CANAF_ext_cnt << 2);
LPC_CANAF->ENDofTable = LPC_CANAF->EFF_GRP_sa + (CANAF_gext_cnt << 3);
if(FULLCAN_ENABLE == DISABLE)
{
LPC_CANAF->AFMR = 0x00; // Normal mode
}
else
{
LPC_CANAF->AFMR = 0x04;
}
return CAN_OK;
}
/********************************************************************//**
* @brief Add Explicit ID into AF Look-Up Table dynamically.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -