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

📄 73x_can.c

📁 国外LPC2000系列的一些源程序,请大家快快下载
💻 C
📖 第 1 页 / 共 3 页
字号:
/* Output         : None                                                         */
/* Return         : 1 if transmission was OK, else 0                             */
/* Note           : CAN must be in BASIC mode                                    */
/*********************************************************************************/
u32 CAN_BasicSendMessage(CAN_TypeDef *CANx, canmsg* pCanMsg)
{
	/* clear NewDat bit in IF2 to detect next reception*/
	CANx->sMsgObj[1].MCR &= ~CAN_MCR_NEWDAT;

	CANx->SR &= ~CAN_SR_TXOK;

	CANx->sMsgObj[0].CMR = CAN_CMR_WRRD
  	                       | CAN_CMR_ARB
  	                       | CAN_CMR_CONTROL
  	                       | CAN_CMR_DATAA
  	                       | CAN_CMR_DATAB;

	if (pCanMsg->IdType == CAN_STD_ID)
	{
	  	/* standard ID*/
		CANx->sMsgObj[0].A1R = 0;
		CANx->sMsgObj[0].A2R = (CANx->sMsgObj[0].A2R & 0xE000) | STD_FIXED_ID_ARB(pCanMsg->Id);
	}
	else
	{
  		/* extended ID*/
		CANx->sMsgObj[0].A1R = EXT_FIXED_ID_ARB_L(pCanMsg->Id);
		CANx->sMsgObj[0].A2R = ((CANx->sMsgObj[0].A2R) & 0xE000) | EXT_FIXED_ID_ARB_H(pCanMsg->Id);
	}

	CANx->sMsgObj[0].MCR = (CANx->sMsgObj[0].MCR & 0xFCF0) | pCanMsg->Dlc;

	CANx->sMsgObj[0].DA1R = ((u16)pCanMsg->Data[1]<<8) | pCanMsg->Data[0];
	CANx->sMsgObj[0].DA2R = ((u16)pCanMsg->Data[3]<<8) | pCanMsg->Data[2];
	CANx->sMsgObj[0].DB1R = ((u16)pCanMsg->Data[5]<<8) | pCanMsg->Data[4];
	CANx->sMsgObj[0].DB2R = ((u16)pCanMsg->Data[7]<<8) | pCanMsg->Data[6];

	/* request transmission*/
	CANx->sMsgObj[0].CRR = CAN_CRR_BUSY | (1 + 0);

	return 1;
}

/***********************************************************************************/
/* Function Name  : CAN_BasicReceiveMessage                                        */
/* Description    : Get the message in BASIC mode, if received                     */
/* Input          : -CANx: where x can be 0, 1 or 2 to select the CAN peripheral   */
/*                  -Pointer to the message structure where received data is stored*/
/* Output         : None                                                           */
/* Return         : 1 if reception was OK, else 0 (no message pending)             */
/* Note           : CAN must be in BASIC mode                                      */
/***********************************************************************************/
u32 CAN_BasicReceiveMessage(CAN_TypeDef *CANx, canmsg* pCanMsg)
{
   u32  tmpId0 = 0;
   u32  tmpId1 = 0;

        if ((CANx->sMsgObj[1].MCR & CAN_MCR_NEWDAT) == 0)
		return 0;

	CANx->SR &= ~CAN_SR_RXOK;

	CANx->sMsgObj[1].CMR = CAN_CMR_ARB
  	                       | CAN_CMR_CONTROL
  	                       | CAN_CMR_DATAA
  	                       | CAN_CMR_DATAB;

    if ((CANx->sMsgObj[1].A2R & CAN_A2R_XTD) == 0)
    {
  	  /* standard ID*/
  	  pCanMsg->IdType = CAN_STD_ID;
          pCanMsg->Id = (CANx->sMsgObj[1].A2R >> 2) & 0x07FF;
    }
    else
    {
          /* extended ID*/
  	  pCanMsg->IdType = CAN_EXT_ID;
          tmpId0 = ((u32)CANx->sMsgObj[1].A1R << 11);
          tmpId1 = (((u32)CANx->sMsgObj[1].A2R & 0x0003) << 27);
          pCanMsg->Id = ((CANx->sMsgObj[1].A2R >> 2) & 0x07FF) |tmpId0 | tmpId1;
    }

    pCanMsg->Dlc = CANx->sMsgObj[1].MCR & 0x0F;

    pCanMsg->Data[0] = (u8) CANx->sMsgObj[1].DA1R;
    pCanMsg->Data[1] = (u8)(CANx->sMsgObj[1].DA1R >> 8);
    pCanMsg->Data[2] = (u8) CANx->sMsgObj[1].DA2R;
    pCanMsg->Data[3] = (u8)(CANx->sMsgObj[1].DA2R >> 8);
    pCanMsg->Data[4] = (u8) CANx->sMsgObj[1].DB1R;
    pCanMsg->Data[5] = (u8)(CANx->sMsgObj[1].DB1R >> 8);
    pCanMsg->Data[6] = (u8) CANx->sMsgObj[1].DB2R;
    pCanMsg->Data[7] = (u8)(CANx->sMsgObj[1].DB2R >> 8);

    return 1;
}


/********************************************************************************/
/* Function Name  : CAN_EnterInitMode                                           */
/* Description    : Switch the CAN into initialization mode                     */
/* Input          : -CANx: where x can be 0, 1 or 2 to select the CAN peripheral*/
/*                  -Any binary value formed from the CAN_CR_xxx defines        */
/* Output         : None                                                        */
/* Return         : None                                                        */
/* Note           : CAN_LeaveInitMode must be called when all is done           */
/********************************************************************************/
void CAN_EnterInitMode(CAN_TypeDef *CANx, u8 mask)
{
	CANx->CR = mask | CAN_CR_INIT;
	CANx->SR = 0;					/* reset the status*/
}

/********************************************************************************/
/* Function Name  : CAN_LeaveInitMode                                           */
/* Description    : Leave the initialization mode (switch into normal mode)     */
/* Input          : CANx: where x can be 0, 1 or 2 to select the CAN peripheral */
/* Output         : None                                                        */
/* Return         : None                                                        */
/********************************************************************************/
void CAN_LeaveInitMode(CAN_TypeDef *CANx)
{
	CANx->CR &= ~(CAN_CR_INIT | CAN_CR_CCE);
}

/********************************************************************************/
/* Function Name  : CAN_EnterTestMode                                           */
/* Description    : Switch the CAN into test mode                               */
/* Input          : -CANx: where x can be 0, 1 or 2 to select the CAN peripheral*/
/*                  -Any binary value formed from the CAN_TST_xxx defines       */
/* Output         : None                                                        */
/* Return         : None                                                        */
/* Note           : CAN_LeaveTestMode must be called when all is done           */
/********************************************************************************/
void CAN_EnterTestMode(CAN_TypeDef *CANx, u8 mask)
{
	CANx->CR |= CAN_CR_TEST;
	CANx->TESTR |= mask;
}

/*******************************************************************************/
/* Function Name  : CAN_LeaveTestMode                                          */
/* Description    : Leave the current test mode (switch into normal mode)      */
/* Input          : CANx: where x can be 0, 1 or 2 to select the CAN peripheral*/
/* Output         : None                                                       */
/* Return         : None                                                       */
/*******************************************************************************/
void CAN_LeaveTestMode(CAN_TypeDef *CANx)
{
	CANx->CR |= CAN_CR_TEST;
	CANx->TESTR &= ~(CAN_TESTR_LBACK | CAN_TESTR_SILENT | CAN_TESTR_BASIC);
	CANx->CR &= ~CAN_CR_TEST;
}

/********************************************************************************/
/* Function Name  : CAN_ReleaseTxMessage                                        */
/* Description    : Release the transmit message object                         */
/* Input          : -CANx: where x can be 0, 1 or 2 to select the CAN peripheral*/
/*                  -Message object number, from 0 to 31                        */
/* Output         : None                                                        */
/* Return         : None                                                        */
/* Note           : assume that message interface 0 is free                     */
/********************************************************************************/
void CAN_ReleaseTxMessage(CAN_TypeDef *CANx, u32 msgobj)
{
	CANx->sMsgObj[0].CMR = CAN_CMR_CLRINTPND | CAN_CMR_TXRQSTNEWDAT;
	CANx->sMsgObj[0].CRR = 1 + msgobj;
}

/********************************************************************************/
/* Function Name  : CAN_ReleaseRxMessage                                        */
/* Description    : Release the receive message object                          */
/* Input          : -CANx: where x can be 0, 1 or 2 to select the CAN peripheral*/
/*                  -Message object number, from 0 to 31                        */
/* Output         : None                                                        */
/* Return         : None                                                        */
/* Note           : assume that message interface 1 is free                     */
/********************************************************************************/
void CAN_ReleaseRxMessage(CAN_TypeDef *CANx, u32 msgobj)
{
	CANx->sMsgObj[1].CMR = CAN_CMR_CLRINTPND | CAN_CMR_TXRQSTNEWDAT;
	CANx->sMsgObj[1].CRR = 1 + msgobj;
}

/********************************************************************************/
/* Function Name  : CAN_IsMessageWaiting                                        */
/* Description    : Test the waiting status of a received message               */
/* Input          : -CANx: where x can be 0, 1 or 2 to select the CAN peripheral*/
/*                  -Message object number, from 0 to 31                        */
/* Output         : None                                                        */
/* Return         : A non-zero value if the corresponding message object has    */
/*                  received a message waiting to be copied, else 0             */
/********************************************************************************/
u32 CAN_IsMessageWaiting(CAN_TypeDef *CANx, u32 msgobj)
{
	return (msgobj < 16 ? CANx->ND1R & (1 << msgobj) : CANx->ND2R & (1 << (msgobj-16)));
}

/********************************************************************************/
/* Function Name  : CAN_IsTransmitRequested                                     */
/* Description    : Test the request status of a transmitted message            */
/* Input          : -CANx: where x can be 0, 1 or 2 to select the CAN peripheral*/
/*                  -Message object number, from 0 to 31                        */
/* Output         : None                                                        */
/* Return         : A non-zero value if the corresponding message is requested  */
/*                  to transmit, else 0                                         */
/********************************************************************************/
u32 CAN_IsTransmitRequested(CAN_TypeDef *CANx, u32 msgobj)
{
	return (msgobj < 16 ? CANx->TXR1R & (1 << msgobj) : CANx->TXR2R & (1 << (msgobj-16)));
}

/**********************************************************************************/
/* Function Name  : CAN_IsInterruptPending                                        */
/* Description    : Test the interrupt status of a message object                 */
/* Input          : -CANx: where x can be 0, 1 or 2 to select the CAN peripheral  */
/*                  -Message object number, from 0 to 31                          */
/* Output         : None                                                          */
/* Return         : A non-zero value if the corresponding message has an interrupt*/
/*                  pending, else 0                                               */
/**********************************************************************************/
u32 CAN_IsInterruptPending(CAN_TypeDef *CANx, u32 msgobj)
{
	return (msgobj < 16 ? CANx->IP1R & (1 << msgobj) : CANx->IP2R & (1 << (msgobj-16)));
}

/**********************************************************************************/
/* Function Name  : CAN_IsObjectValid                                             */
/* Description    : Test the validity of a message object (ready to use)          */
/* Input          : -CANx: where x can be 0, 1 or 2 to select the CAN peripheral  */
/*                  -Message object number, from 0 to 31                          */
/* Output         : None                                                          */
/* Return         : A non-zero value if the corresponding message object is valid,*/
/*                  else 0                                                        */
/**********************************************************************************/
u32 CAN_IsObjectValid(CAN_TypeDef *CANx, u32 msgobj)
{
	return (msgobj < 16 ? CANx->MV1R & (1 << msgobj) : CANx->MV2R & (1 << (msgobj-16)));
}


/******************* (C) COPYRIGHT 2005 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

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