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

📄 can.c

📁 ST7 Mcu CAN setting and program control for learning
💻 C
📖 第 1 页 / 共 2 页
字号:
        CAN_CIER = 0x00; 
        CAN_CPSR = CAN_CTRL_PG;         
        CAN_CEIER = 0x00;
    }
    CanRestorePg();
}

/******************************************************************************
Function Name     :  CanCanInterruptRestore
Input Parameters  :  None
Output Parameters :  None
Description       :  This service enables all CAN interrupts by changing the 
                     CAN interrupt control flags
******************************************************************************/
void CanCanInterruptRestore( void)
{
    CanSavePg();
    {
        CAN_CIER = CIER_WKUIE | CIER_FOVIE | CIER_FFIE |CIER_FMPIE |CIER_TMEIE;
        
        CAN_CPSR = CAN_CTRL_PG;
        CAN_CEIER = CEIER_ERRIE|CEIER_LECIE|CEIER_BOFIE|CEIER_EPVIE|CEIER_EWGIE;
        
    } 
    CanRestorePg();
}

/******************************************************************************
Function Name     :  CanTransmit
Input Parameters  :  tx_handle - Selected transmit Handle
Output Parameters :  KCANTXOK - If the transmit request is accepted by the CAN 
                     driver.
                     KCANTXFAILED- If the transmit request is not accepted by 
                     the CAN driver.
Description       :  This service initiates the transmission within the CAN 
                     controller for the CAN message referenced by <tx_handle>. 
Comments          :  This service shall not be called when the CAN driver is in
                     stop or sleep mode.
******************************************************************************/
canuint8 CanTransmit( CanTransmitHandle tx_handle)
{  
    canuint8 idx;
 
    CanGlobalInterruptDisable();
    CanSavePg();
    if(tx_handle < NO_OF_TX_HANDLES)
    {
      if(CAN_CTPR & CTPR_TME0)
      {
         Can_MailBox_Handle[0] = tx_handle;
         CAN_CPSR = CAN_TXMB0_PG;
      }
      else
      if(CAN_CTPR & CTPR_TME1)
      {
         Can_MailBox_Handle[1] = tx_handle;
         CAN_CPSR = CAN_TXMB1_PG;
      }
      else
      {
        CanRestorePg();
        CanGlobalInterruptRestore();
        return(KCANTXFAILED);
      }
     
      CAN_MDLC = Tx_Dlc[tx_handle];
      if(CAN_MDLC <= MAX_DLC_LEN)
      {
        CAN_MIDR01 = Tx_Stdid[tx_handle];
        if(CAN_MIDR01 & EXT_ID_MASK)
        {
          CAN_MIDR23 = Tx_Extid[tx_handle];
        } 
        Tx_Ptr = Tx_Data_Ptr[tx_handle];
        for(idx=0;idx<CAN_MDLC;idx++)
        {
          CAN_MDAR[idx] = Tx_Ptr[idx];
        }
        CAN_MCSR |= MCSR_TXRQ;                   /* Transmit Request */
        CanRestorePg();
        CanGlobalInterruptRestore(); 
        return(KCANTXOK);
      }
    }
    CanRestorePg();
    CanGlobalInterruptRestore();
    return(KCANFAILED);
} 

/******************************************************************************
Function Name     :  CanCancelTransmit
Input Parameters  :  tx_handle - Selected transmit Handle
Output Parameters :  None
Description       :  This service cancels a transmit request by making an Abort
                     Request. 
*******************************************************************************/
void CanCancelTransmit(CanTransmitHandle tx_handle)
{
    CanGlobalInterruptDisable();
    CanSavePg();
    if(Can_MailBox_Handle[0] == tx_handle) 
    {
        Can_MailBox_Handle[0] = CANMSGCANCEL;
        CAN_CPSR = CAN_TXMB0_PG;
        CAN_MCSR |= MCSR_ABRQ;                      /* make abort request */
    }
    else if( Can_MailBox_Handle[1] == tx_handle)
    {
        Can_MailBox_Handle[1] = CANMSGCANCEL;
        CAN_CPSR = CAN_TXMB1_PG;
        CAN_MCSR |= MCSR_ABRQ;
    }
    CanRestorePg();
    CanGlobalInterruptRestore();
}

/******************************************************************************
Function Name     :  CanMsgTransmit
Input Parameters  :  txData - Pointer to structure which contains about CAN-Id,
                     CAN-DLC,CAN-Frame Data.
Output Parameters :  KCANTXOK - Request is accepted by CAN driver.
                     KCANTXFAILED - Request is not accepted by CAN driver. 
Description       :  This service initiates the transmission for the message 
                     referenced by <txData>.
Comments          :  This service shall not be called when the CAN driver is in
                     stop or sleep mode.
******************************************************************************/
canuint8 CanMsgTransmit(tCanMsgOject *txData)
{
    canuint8 idx;
    CanGlobalInterruptDisable();
    CanSavePg();
    if(CAN_CTPR & CTPR_TME0)
    {
      Can_MailBox_Handle[0] = CANMSGTRANSMIT;
      CAN_CPSR = CAN_TXMB0_PG;
    }
    else
    {
      CanRestorePg();
      CanGlobalInterruptRestore();
      return(KCANTXFAILED);
    }
    CAN_MDLC = txData->dlc;
    if(CAN_MDLC <= MAX_DLC_LEN)
    {
     CAN_MIDR01 = txData->stdid; 
     if(CAN_MIDR01 & EXT_ID_MASK)
     {
         CAN_MIDR23 = txData->extid;
     }
     for(idx=0;idx<CAN_MDLC;idx++)
     {
         CAN_MDAR[idx] = txData->data[idx];
     }
     CAN_MCSR |= MCSR_TXRQ;                   /* Transmit Request */
    }
    else
    {
     CanRestorePg();
     CanGlobalInterruptRestore();
     return(KCANTXFAILED);
    }
    CanRestorePg();
    CanGlobalInterruptRestore(); 
    return(KCANTXOK); 
} 

/******************************************************************************
Function Name     :  CanCancelMsgTransmit
Input Parameters  :  None
Output Parameters :  None
Description       :  This service cancels a transmit request requested by the 
                     service CanMsgTransmit( ). 
******************************************************************************/
void CanCancelMsgTransmit( void)
{
    CanGlobalInterruptDisable();
    CanSavePg();
    if(Can_MailBox_Handle[0] == CANMSGTRANSMIT) 
    {
        Can_MailBox_Handle[0] = CANMSGCANCEL;
        CAN_CPSR = CAN_TXMB0_PG;
        CAN_MCSR |= MCSR_ABRQ;
    }
    CanRestorePg();
    CanGlobalInterruptRestore();
}

/******************************************************************************
Function Name     :  CanRx_ISR
Input Parameters  :  None
Output Parameters :  None
Description       :  This service handles the receive FIFO interrupt. Received 
                     message (CAN-ID, CAN-DATA) is copied into the Rx buffer 
                     corresponding to the message received.  
******************************************************************************/
#ifdef _HIWARE_                        /* test for HIWARE Compiler*/ 
#pragma TRAP_PROC SAVE_REGS            /* additional registers will be saved */
#else                                  
#ifdef _COSMIC_                        /* test for Cosmic Compiler */
@interrupt                           
#else
#error "Unsupported Compiler!"         /* Compiler Defines not found! */ 
#endif
#endif
void CanRx_ISR(void)
{
    static canuint8 filter_match_index,data_len=0;
    canuint8 idx;
 
    CanGlobalInterruptDisable();
    CanSavePg();    
    if(CAN_CRFR & CRFR_FOVR)
    {
     CAN_CRFR |= CRFR_FOVR;             /* clear the FIFO FOVR bit */
    }                
    else if(CAN_CRFR & CRFR_FULL)
    {
        CAN_CRFR |= CRFR_FULL;         /* clear the FIFO FULL bit */      
    }                
    while(CAN_CRFR & CRFR_FMP01)
    {
      CAN_CPSR = CAN_FIFO_PG;               /* Select Rx_FIFO page */
      filter_match_index = CAN_MFMI;
      if(filter_match_index < NO_OF_RX_HANDLES)
      {
       if((Can_Ind_Flags[filter_match_index/8])&
                (Ind_Mask[filter_match_index % 8]))
       {
         Can_Ovf_Flags[filter_match_index/8] |= 
                         Ovf_Mask[filter_match_index%8]; 
       }
       data_len = CAN_MDLC;
       if((data_len == Rx_Dlc[filter_match_index]) && (data_len != 0))
       {
         Rx_Stdid[filter_match_index] = CAN_MIDR01;
         Rx_Extid[filter_match_index] = CAN_MIDR23;
         Can_Ind_Flags[filter_match_index/8] |= 
                  Ind_Mask[filter_match_index%8];
         Rx_Ptr = Rx_Data_Ptr[filter_match_index];
         for(idx=0;idx<data_len;idx++)
         {
            Rx_Ptr[idx]= CAN_MDAR[idx];
         }
        }
        if((CAN_CRFR & CRFR_FMP01)== 0x02)
        {
          while((CAN_CMSR & CMSR_REC) && (CAN_CDGR & CDGR_RX));
        }
        CAN_CRFR |= CRFR_RFOM;
      }
    }
    CanRestorePg();
    CanGlobalInterruptRestore();
}

/******************************************************************************
Function Name     :  CanTx_ISR
Input Parameters  :  None
Output Parameters :  None
Description       :  This service handles the wakeup, error and transmit 
                     mailbox empty interrupts. 
******************************************************************************/
#ifdef _HIWARE_                      /* test for HIWARE Compiler*/ 
#pragma TRAP_PROC SAVE_REGS          /* additional registers will be saved */
#else
#ifdef _COSMIC_                      /* test for Cosmic Compiler */
@interrupt                           
#else
#error "Unsupported Compiler!"       /* Compiler Defines not found! */ 
#endif
#endif
void CanTx_ISR(void)
{
    canuint8 handle;
    if(CAN_CMSR & CMSR_ERRI)
    {
      CAN_CMSR = CMSR_ERRI;
    }
    if(CAN_CMSR & CMSR_WKUI)
    {
      CAN_CMSR = CMSR_WKUI;
    }
    if(CAN_CTSR & CTSR_TXOK0)
    {
      CAN_CTSR |= CTSR_RQCP0; /* clear the request completed 
                                 request for mailbox0 */
         /*The right side operator in a conditional statement should not have 
         any side effect ie. it should not contain any volatile variable*/
      if((Can_MailBox_Handle[0] != CANMSGCANCEL)&& (Can_MailBox_Handle[0] != CANMSGTRANSMIT))
      {
         handle = Can_MailBox_Handle[0];
         Can_Conf_Flags[handle/8] |= Conf_Mask[handle % 8];
      }   
      Can_MailBox_Handle[0] = MAILBOXFREE;
    }
    else if(CAN_CTSR & CTSR_TXOK1)
    {
      CAN_CTSR |= CTSR_RQCP1;    
      if(Can_MailBox_Handle[1] != CANMSGCANCEL)
      {
         handle = Can_MailBox_Handle[1];
         Can_Conf_Flags[handle/8] |= Conf_Mask[handle % 8];
      }   
      Can_MailBox_Handle[1] = MAILBOXFREE;
    }
    else if(CAN_CTSR & CTSR_RQCP0)
    {
      CAN_CTSR = CTSR_RQCP0;
      Can_MailBox_Handle[0] = MAILBOXFREE;
    } 
    else if(CAN_CTSR & CTSR_RQCP1)
    {
      CAN_CTSR = CTSR_RQCP1;
      Can_MailBox_Handle[1] = MAILBOXFREE;
    } 
}
/************** (c) 2003  ST Microelectronics ****************** END OF FILE */

⌨️ 快捷键说明

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