⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lpc177x_8x_can.c

📁 NXPl788上lwip的无操作系统移植,基于Embest开发板
💻 C
📖 第 1 页 / 共 4 页
字号:
/**********************************************************************
* $Id$		lpc177x_8x_can.c			2011-06-02
*//**
* @file		lpc177x_8x_can.c
* @brief	Contains all functions support for CAN firmware library on
*			LPC177x_8x
* @version	1.0
* @date		02. June. 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.
* Permission to use, copy, modify, and distribute this software and its
* documentation is hereby granted, under NXP Semiconductors'
* relevant copyright in the software, without fee, provided that it
* is used in conjunction with NXP Semiconductors microcontrollers.  This
* copyright, permission, and disclaimer notice must appear in all copies of
* this code.
**********************************************************************/

/* Peripheral group ----------------------------------------------------------- */
/** @addtogroup CAN
 * @{
 */
#ifdef __BUILD_WITH_EXAMPLE__
#include "lpc177x_8x_libcfg.h"
#else
#include "lpc177x_8x_libcfg_default.h"
#endif /* __BUILD_WITH_EXAMPLE__ */
#ifdef _CAN
 
/* Includes ------------------------------------------------------------------- */
#include "lpc177x_8x_can.h"
#include "lpc177x_8x_clkpwr.h"

/* 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 LPC_CAN_TypeDef* CAN_GetPointer (uint8_t canId);


static void can_SetBaudrate (LPC_CAN_TypeDef *CANx, uint32_t baudrate);

/*********************************************************************//**
 * @brief 		Setting CAN baud rate (bps)
 * @param[in] 	canId point to LPC_CAN_TypeDef object, should be:
 * 				- LPC_CAN1: CAN1 peripheral
 * 				- LPC_CAN2: CAN2 peripheral
 * @return 		The pointer to CAN peripheral that's expected to use
 ***********************************************************************/
static LPC_CAN_TypeDef* CAN_GetPointer (uint8_t canId)
{
	LPC_CAN_TypeDef* pCan;

	switch (canId)
	{
		case CAN_ID_1:
			pCan = LPC_CAN1;
			break;

		case CAN_ID_2:
			pCan = LPC_CAN2;
			break;

		default:
			pCan = NULL;
			break;
	}

	return pCan;
}


/*********************************************************************//**
 * @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;
	uint32_t CANPclk = 0;
	uint32_t BRP;

	CANPclk = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER);

	result = CANPclk / baudrate;

	/* Calculate suitable nominal time value
	 * NT (nominal time) = (TSEG1 + TSEG2 + 3)
	 * NT <= 24
	 * TSEG1 >= 2*TSEG2
	 */
	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;

			break;
		}
	}

	/* 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]	 canId			 The Id of the expected CAN component
 *
 * @param[in]	baudrate: the value of CAN baudrate will be set (bps)
 * @return 		None
 *********************************************************************/
void CAN_Init(uint8_t canId, uint32_t baudrate)
{
	LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);

	uint16_t i;

	if(canId == CAN_ID_1)
	{
		/* Turn on power and clock for CAN1 */
		CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN1, ENABLE);
	}
	else if(canId == CAN_ID_2)
	{
		/* Turn on power and clock for CAN2 */
		CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN2, ENABLE);
	}
	else
	{
		return;
	}

	pCan->MOD = 1; // Enter Reset Mode
	pCan->IER = 0; // Disable All CAN Interrupts
	pCan->GSR = 0;

	/* Request command to release Rx, Tx buffer and clear data overrun */
	//pCan->CMR = CAN_CMR_AT | CAN_CMR_RRB | CAN_CMR_CDO;
	pCan->CMR = (1 << 1) | (1 << 2) | (1 << 3);

	/* Read to clear interrupt pending in interrupt capture register */
	i = pCan->ICR;
	pCan->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 (pCan, baudrate);
}

/********************************************************************//**
 * @brief		CAN deInit
 * @param[in]	 canId			 The Id of the expected CAN component
 *
 * @return 		None
 *********************************************************************/
void CAN_DeInit(uint8_t canId)
{
	if(canId == CAN_ID_1)
	{
		/* Turn on power and clock for CAN1 */
		CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN1, DISABLE);
	}
	else if(canId == CAN_ID_2)
	{
		/* Turn on power and clock for CAN1 */
		CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN2, DISABLE);
	}

	return;
}

/********************************************************************//**
 * @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(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;

	LPC_CANAF->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;

			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;

			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;

			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;

			entry = 0x00; //reset entry value

			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;

			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;
}
/********************************************************************//**

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -