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

📄 91x_can.c

📁 STR912的中断控制器的应用例子
💻 C
📖 第 1 页 / 共 3 页
字号:
    CAN->sMsgObj[0].A1R = 0;
    CAN->sMsgObj[0].A2R = (CAN->sMsgObj[0].A2R & 0xE000) | STD_FIXED_ID_ARB(pCanMsg->Id);
  }
  else
  {
    /* extended ID */
    CAN->sMsgObj[0].A1R = EXT_FIXED_ID_ARB_L(pCanMsg->Id);
    CAN->sMsgObj[0].A2R = (CAN->sMsgObj[0].A2R & 0xE000) | CAN_A2R_XTD | EXT_FIXED_ID_ARB_H(pCanMsg->Id);
  }

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

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

  /* request the transmission*/
  CAN->sMsgObj[0].CRR = CAN_CRR_BUSY;
  
  return SUCCESS;
}

/*******************************************************************************
* Function Name  : CAN_BasicReceiveMessage                                   
* Description    : Gets the message in BASIC mode, if received. This mode does
*                  not use the message RAM.                
* Input          : pCanMsg: pointer to the message structure where message is copied.    
* Output         : None                                                      
* Return         : An ErrorStatus enumuration value:
*                         - SUCCESS: Reception OK
*                         - ERROR: No message pending
*******************************************************************************/
ErrorStatus CAN_BasicReceiveMessage(canmsg* pCanMsg)
{
  u32 tmpId;
  
  if ((CAN->sMsgObj[1].MCR & CAN_MCR_NEWDAT) == 0)
  {
    return ERROR;
  }

  CAN->SR &= ~CAN_SR_RXOK;

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

  if ((CAN->sMsgObj[1].A2R & CAN_A2R_XTD) == 0)
  {
    /* standard ID*/
    pCanMsg->IdType = CAN_STD_ID;
    pCanMsg->Id = (CAN->sMsgObj[1].A2R >> 2) & 0x07FF;
  }
  else
  {
    /* extended ID*/
    pCanMsg->IdType = CAN_EXT_ID;
    tmpId = ((u32)(CAN->sMsgObj[1].A2R & 0x1FFF) << 16);
    pCanMsg->Id  = CAN->sMsgObj[1].A1R | tmpId;
  }

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

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

  return SUCCESS;
}

/*******************************************************************************
* Function Name  : CAN_EnterInitMode                                         
* Description    : Switchs the CAN into initialization mode. This function must
*                  be used in conjunction with CAN_LeaveInitMode().                 
* Input          : InitMask: specifies the CAN configuration in normal mode.      
* Output         : None                                                      
* Return         : None                                                          
*******************************************************************************/
void CAN_EnterInitMode(u8 InitMask)
{
  CAN->CR = InitMask | CAN_CR_INIT;
  CAN->SR = 0;					/* reset the status*/
}

/*******************************************************************************
* Function Name  : CAN_LeaveInitMode                                         
* Description    : Leaves the initialization mode (switch into normal mode).
*                  This function must be used in conjunction with CAN_EnterInitMode().  
* Input          : None                                                      
* Output         : None                                                      
* Return         : None                                                      
*******************************************************************************/
void CAN_LeaveInitMode(void)
{
  CAN->CR &= ~(CAN_CR_INIT | CAN_CR_CCE);
}

/*******************************************************************************
* Function Name  : CAN_EnterTestMode                                         
* Description    : Switchs the CAN into test mode. This function must be used in
*                  conjunction with CAN_LeaveTestMode().                            
* Input          : TestMask: specifies the configuration in test modes.     
* Output         : None                                                      
* Return         : None                                                            
*******************************************************************************/
void CAN_EnterTestMode(u8 TestMask)
{
  CAN->CR |= CAN_CR_TEST;
  CAN->TESTR |= TestMask;
}

/*******************************************************************************
* Function Name  : CAN_LeaveTestMode                                         
* Description    : Leaves the current test mode (switch into normal mode).
*                  This function must be used in conjunction with CAN_EnterTestMode().    
* Input          : None                                                      
* Output         : None                                                      
* Return         : None                                                      
*******************************************************************************/
void CAN_LeaveTestMode(void)
{
  CAN->CR |= CAN_CR_TEST;
  CAN->TESTR &= ~(CAN_TESTR_LBACK | CAN_TESTR_SILENT | CAN_TESTR_BASIC);
  CAN->CR &= ~CAN_CR_TEST;
}

/*******************************************************************************
* Function Name  : CAN_ReleaseTxMessage                                      
* Description    : Releases the transmit message object.
* Input          : - msgobj: specifies the Message object number, from 0 to 31.                     
* Output         : None                                                      
* Return         : None                                                                        
*******************************************************************************/
void CAN_ReleaseTxMessage(u32 msgobj)
{
  CAN->sMsgObj[0].CMR = CAN_CMR_CLRINTPND | CAN_CMR_TXRQSTNEWDAT;
  CAN->sMsgObj[0].CRR = 1 + msgobj;
}

/*******************************************************************************
* Function Name  : CAN_ReleaseRxMessage                                      
* Description    : Releases the receive message object.                        
* Input          : - msgobj: specifies the Message object number, from 0 to 31.                      
* Output         : None                                                      
* Return         : None                                                                      
*******************************************************************************/
void CAN_ReleaseRxMessage(u32 msgobj)
{
  CAN->sMsgObj[1].CMR = CAN_CMR_CLRINTPND | CAN_CMR_TXRQSTNEWDAT;
  CAN->sMsgObj[1].CRR = 1 + msgobj;
}

/*******************************************************************************
* Function Name  : CAN_GetMsgReceiveStatus
* Description    : Test the waiting status of a received message
* Input 1        : message object number, from 0 to 31
* Output         : None
* Return         : SET value if the corresponding message object has
*                  received a message waiting to be copied, else RESET
*******************************************************************************/
FlagStatus CAN_GetMsgReceiveStatus(u32 msgobj)
{
  if( msgobj < 16 )
  {
    if ( CAN->ND1R & (1 << msgobj) ) 
        return SET;
    else
        return RESET;
  } 
  else
  {
     if ( CAN->ND2R & (1 << (msgobj - 16) ) )
         return SET;
     else
         return RESET;
  } 
}

/*******************************************************************************
* Function Name  : CAN_GetMsgTransmitRequestStatus
* Description    : Test the request status of a transmitted message
* Input 1        : message object number, from 0 to 31
* Output         : None
* Return         : SET if the corresponding message is requested
*                  to transmit, else RESET
*******************************************************************************/
FlagStatus CAN_GetMsgTransmitRequestStatus(u32 msgobj)
{
   if( msgobj < 16 )
  {
    if ( CAN->TXR1R & (1 << msgobj) ) 
        return SET;
    else
        return RESET;
  } 
  else
  {
     if ( CAN->TXR2R & (1 << (msgobj - 16) ) )
         return SET;
     else
         return RESET;
  }  
}

/*******************************************************************************
* Function Name  : CAN_GetMsgInterruptStatus
* Description    : Test the interrupt status of a message object
* Input 1        : message object number, from 0 to 31
* Output         : None
* Return         : SET if the corresponding message has an
*                  interrupt pending, else RESET
*******************************************************************************/
FlagStatus CAN_GetMsgInterruptStatus(u32 msgobj)
{
   if( msgobj < 16 )
  {
    if ( CAN->IP1R & (1 << msgobj) ) 
        return SET;
    else
        return RESET;
  } 
  else
  {
     if ( CAN->IP2R & (1 << (msgobj - 16) ) ) 
         return SET;
     else
         return RESET;
  }   
}

/*******************************************************************************
* Function Name  : CAN_GetMsgValidStatus
* Description    : Test the validity of a message object (ready to use)
* Input 1        : message object number, from 0 to 31
* Output         : None
* Return         : SET if the corresponding message object is valid
*                  else RESET
*******************************************************************************/
FlagStatus CAN_GetMsgValidStatus(u32 msgobj)
{
  if( msgobj < 16 )
  {
    if ( CAN->MV1R & (1 << msgobj) ) 
        return SET;
    else
        return RESET;
  } 
  else
  {
     if ( CAN->MV2R & (1 << (msgobj - 16) ) ) 
         return SET;
     else
         return RESET;
  }   
}

/*******************************************************************************
* Function Name  : CAN_GetFlagStatus                                          
* Description    : Returns the flasg status of the flag passed in parameter       
* Input          : One of the following parameters:
*                  - CAN_SR_TXOK
*                  - CAN_SR_RXOK
*                  - CAN_SR_EPASS
*                  - CAN_SR_EWARN
*                  - CAN_SR_BOFF          
* Output         : None                                                      
* Return         : 1 if the flag is set else 0                                           
*******************************************************************************/
FlagStatus CAN_GetFlagStatus ( u32 CAN_Flag )
{
  if( CAN->SR & CAN_Flag) 
   {
     return SET;
   }
     else 
      return RESET;
}

/*******************************************************************************
* Function Name  : CAN_GetTransmitErrorCounter                                            
* Description    : Reads the CAN cell transmit error counter        
* Input          : None                      
* Output         : None                                                      
* Return         : Transmit Error Counter value between 0..255                                            
*******************************************************************************/
u32 CAN_GetTransmitErrorCounter ( void )
{
 return( CAN->ERR & 0x00FF );
}

/*******************************************************************************
* Function Name  : CAN_GetReceiveErrorCounter                                            
* Description    : Reads the CAN cell receive error counter        
* Input          : None                      
* Output         : None                                                      
* Return         : Receive Error Counter value between 0..127                                            
*******************************************************************************/
u32 CAN_GetReceiveErrorCounter ( void )
{
 return ( ( CAN->ERR & 0x7F00 ) >> 8);
}

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

⌨️ 快捷键说明

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