/**********************************************************************
* $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->d