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

📄 can.c

📁 Infineon单片机XC164CMADC模数转换模块和定时器中断例程
💻 C
📖 第 1 页 / 共 3 页
字号:
// USER CODE END

ubyte CAN_ubDelMsgObj(ubyte ubObjNr)
{
  ubyte ubReturn;

  ubReturn = 0;
  if(!(CAN_HWOBJ[ubObjNr].uwMSGCTR & 0xa200)) // if set RMTPND, TXRQ or NEWDAT
  {
    CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xff7f;     // reset MSGVAL
    ubReturn = 1;
  }
  return(ubReturn);

} //  End of function CAN_ubDelMsgObj


//****************************************************************************
// @Function      void CAN_vReleaseObj(ubyte ubObjNr) 
//
//----------------------------------------------------------------------------
// @Description   This function resets the NEWDAT flag of the selected 
//                RECEIVE OBJECT, so that the CAN controller have access to 
//                it. This function must be called if the function 
//                CAN_ubNewData detects, that new data are present in the 
//                message object and the actual data have been read by 
//                calling the function CAN_vGetMsgObj. 
//
//----------------------------------------------------------------------------
// @Returnvalue   None
//
//----------------------------------------------------------------------------
// @Parameters    ubObjNr: 
//                Number of the message object (0-31)
//
//----------------------------------------------------------------------------
// @Date          2007-5-2
//
//****************************************************************************

// USER CODE BEGIN (ReleaseObj,1)

// USER CODE END

void CAN_vReleaseObj(ubyte ubObjNr)
{
  CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xfdff;       // reset NEWDAT

} //  End of function CAN_vReleaseObj


//****************************************************************************
// @Function      void CAN_vSetMSGVAL(ubyte ubObjNr) 
//
//----------------------------------------------------------------------------
// @Description   This function sets the MSGVAL flag of the selected object. 
//                This is only necessary if the single data transfer mode 
//                (SDT) for the selected object is enabled. If SDT is set to 
//                '1', the CAN controller automatically resets bit MSGVAL 
//                after receiving or tranmission of a frame.
//
//----------------------------------------------------------------------------
// @Returnvalue   None
//
//----------------------------------------------------------------------------
// @Parameters    ubObjNr: 
//                Number of the message object (0-31)
//
//----------------------------------------------------------------------------
// @Date          2007-5-2
//
//****************************************************************************

// USER CODE BEGIN (SetMSGVAL,1)

// USER CODE END

void CAN_vSetMSGVAL(ubyte ubObjNr)
{
  CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xffbf;  // set MSGVAL

} //  End of function CAN_vSetMSGVAL


//****************************************************************************
// @Function      ubyte CAN_ubWriteFIFO(ubyte ubObjNr, TCAN_SWObj *pstObj) 
//
//----------------------------------------------------------------------------
// @Description   This function sets up the next free TRANSMIT message object 
//                which is part of a FIFO. This includes the 8 data bytes, 
//                the identifier (11- or 29-bit) and the data number (0-8 
//                bytes). The direction bit (DIR), the XTD-bit, the NODE bit 
//                and the RMM bit can not be changed. The acceptance mask 
//                register and the Frame Counter remains unchanged. This 
//                function checks whether the choosen message object is still 
//                executing a transmit request, or if the object can be 
//                accessed exclusively. 
//                The structure of the SW message object is defined in the 
//                header file CAN.H (see TCAN_SWObj).
//                Note: 
//                This function can only used for TRANSMIT objects which are 
//                configured for FIFO base functionality. 
//
//----------------------------------------------------------------------------
// @Returnvalue   0: message object is busy (a transfer is active); 1: the 
//                message object was configured and the transmite is 
//                requested; 2: this is not a FIFO base object
//
//----------------------------------------------------------------------------
// @Parameters    ubObjNr: 
//                Number of the FIFO base object
// @Parameters    *pstObj: 
//                Pointer on a message object
//
//----------------------------------------------------------------------------
// @Date          2007-5-2
//
//****************************************************************************

// USER CODE BEGIN (WriteFIFO,1)

// USER CODE END

ubyte CAN_ubWriteFIFO(ubyte ubObjNr, TCAN_SWObj *pstObj)
{
  ubyte i,j;
  ubyte ubReturn;

  ubReturn = 2;

  if((CAN_HWOBJ[ubObjNr].uwCANPTR & 0x0700) == 0x0200)  // if FIFO base object 
  {
    j = aubFIFOWritePtr[ubObjNr / 2];

    ubReturn = 0;
    if((CAN_HWOBJ[j].uwMSGCTR & 0x3000) == 0x1000)      // if reset TXRQ 
    {
      // CANPTRn(new) = CANPTR(old) & ~(uword)FSIZEn | (CANPTRn(old) + 1) & FZIZEn
      aubFIFOWritePtr[ubObjNr / 2] = ((aubFIFOWritePtr[ubObjNr / 2] & ~(uword)(CAN_HWOBJ[ubObjNr].uwCANFCR & 0x001f)) |
                                     ((aubFIFOWritePtr[ubObjNr / 2] + 1) & (CAN_HWOBJ[ubObjNr].uwCANFCR & 0x001f)));

      CAN_HWOBJ[j].uwMSGCTR = 0xf9ff;         // set CPUUPD, reset NEWDAT 

      if(CAN_HWOBJ[j].uwMSGCFG & 0x0004)      // if extended identifier
      {
        CAN_HWOBJ[j].ulCANAR   = pstObj->ulID ;
      }
      else                                    // if standard identifier
      {
        CAN_HWOBJ[j].ulCANAR   = pstObj->ulID << 18;
      }

      CAN_HWOBJ[j].uwMSGCFG  = (CAN_HWOBJ[j].uwMSGCFG & 0x000f) | 
                               (pstObj->uwMsgCfg & 0x00f0); // set DLC

      for(i = 0; i < (pstObj->uwMsgCfg & 0x00f0) >> 4; i++)
      {
        CAN_HWOBJ[j].ubData[i] = pstObj->ubData[i];
      }
      CAN_HWOBJ[j].uwMSGCTR  = 0x66bf;        // set TXRQ, NEWDAT, MSGVAL 
                                              // reset CPUUPD, RMTPND
      ubReturn = 1;
    }
  }
  return(ubReturn);

} //  End of function CAN_ubWriteFIFO


//****************************************************************************
// @Function      ubyte CAN_ubReadFIFO(ubyte ubObjNr, TCAN_SWObj *pstObj) 
//
//----------------------------------------------------------------------------
// @Description   This function reads the next RECEIVE message object which 
//                is part of a FIFO. It checks whether the selected RECEIVE 
//                OBJECT has received a new message. If so the forwarded SW 
//                message object is filled with the content of the HW message 
//                object and the functions returns the value "1". The 
//                structure of the SW message object is defined in the header 
//                file CAN.H (see TCAN_SWObj).
//                Note: 
//                This function can only used for RECEIVE objects which are 
//                configured for FIFO base functionality. 
//                Be sure that no interrupt is enabled for the FIFO objects. 
//
//----------------------------------------------------------------------------
// @Returnvalue   0: the message object has not received a new message; 1: 
//                the message object has received a new message; 2: this is 
//                not a FIFO base object; 3: a previous message was lost; 4: 
//                the received message is corrupted
//
//----------------------------------------------------------------------------
// @Parameters    ubObjNr: 
//                Number of the FIFO base object
// @Parameters    *pstObj: 
//                Pointer on a message object to be filled by this function
//
//----------------------------------------------------------------------------
// @Date          2007-5-2
//
//****************************************************************************

// USER CODE BEGIN (ReadFIFO,1)

// USER CODE END

ubyte CAN_ubReadFIFO(ubyte ubObjNr, TCAN_SWObj *pstObj)
{
  ubyte i,j;
  ubyte ubReturn;

  ubReturn = 2;

  if((CAN_HWOBJ[ubObjNr].uwCANPTR & 0x0700) == 0x0200)  // if FIFO base object 
  {
    j = aubFIFOReadPtr[ubObjNr / 2];

    ubReturn = 0;
    if((CAN_HWOBJ[j].uwMSGCTR & 0x0300) == 0x0200)    // if NEWDAT 
    {
      CAN_HWOBJ[j].uwMSGCTR = 0xfdff;                 // clear NEWDAT

      // CANPTRn(new) = CANPTR(old) & ~(uword)FSIZEn | (CANPTRn(old) + 1) & FZIZEn
      aubFIFOReadPtr[ubObjNr / 2] = ((aubFIFOReadPtr[ubObjNr / 2] & ~(uword)(CAN_HWOBJ[ubObjNr].uwCANFCR & 0x001f)) |
                                    ((aubFIFOReadPtr[ubObjNr / 2] + 1) & (CAN_HWOBJ[ubObjNr].uwCANFCR & 0x001f)));

      // check if the previous message was lost 
      if((CAN_HWOBJ[j].uwMSGCTR & 0x0c00) == 0x0800)  // if set MSGLST 
      {
        CAN_HWOBJ[j].uwMSGCTR = 0xf7ff;               // reset MSGLST 
        return(3);
      }

      for(i = 0; i < (CAN_HWOBJ[j].uwMSGCFG & 0x00f0) >> 4; i++)
      {
        pstObj->ubData[i] = CAN_HWOBJ[j].ubData[i];
      }

      if(CAN_HWOBJ[j].uwMSGCFG & 0x04)        // if extended identifier
      {
        pstObj->ulID   = CAN_HWOBJ[j].ulCANAR;
      }
      else                                    // if standard identifier 
      {
        pstObj->ulID   = CAN_HWOBJ[j].ulCANAR >> 18;
      }

      pstObj->uwMsgCfg  = CAN_HWOBJ[j].uwMSGCFG;

      // check if the message was corrupted 
      if((CAN_HWOBJ[j].uwMSGCTR & 0x0300) == 0x0200)  // if NEWDAT 
      {
        CAN_HWOBJ[j].uwMSGCTR = 0xfdff;               // clear NEWDAT
        return(4);
      }
      ubReturn = 1;
    }
  }
  return(ubReturn);

} //  End of function CAN_ubReadFIFO


//****************************************************************************
// @Function      ubyte CAN_ubFlushReadFIFO(ubyte ubObjNr) 
//
//----------------------------------------------------------------------------
// @Description   This function flushes the selected RECEIVE FIFO. After a 
//                lost or corrupted message in the receive FIFO the hardware 
//                and software FIFO-pointers are no longer synchron. 
//                Note: 
//                This function can only used for RECEIVE objects which are 
//                configured for FIFO base functionality. 
//
//----------------------------------------------------------------------------
// @Returnvalue   0: this is not a FIFO base object; 1: the receive FIFO was 
//                flushed; 
//
//----------------------------------------------------------------------------
// @Parameters    ubObjNr: 
//                Number of the FIFO base object
//
//----------------------------------------------------------------------------
// @Date          2007-5-2
//
//****************************************************************************

// USER CODE BEGIN (FlushReadFIFO,1)

// USER CODE END

ubyte CAN_ubFlushReadFIFO(ubyte ubObjNr)
{
  ubyte j;
  ubyte ubReturn;

  ubReturn = 0;

  if((CAN_HWOBJ[ubObjNr].uwCANPTR & 0x0700) == 0x0200)  // if FIFO base object 
  {
    aubFIFOReadPtr[ubObjNr / 2] = ubObjNr;
    CAN_HWOBJ[ubObjNr].uwCANPTR = (CAN_HWOBJ[ubObjNr].uwCANPTR & ~(uword)0x001F) | ubObjNr;

    for(j = ubObjNr; j < ubObjNr + (CAN_HWOBJ[ubObjNr].uwCANFCR & 0x001f); j++)
    {
      CAN_HWOBJ[j].uwMSGCTR = 0xf5ff;                   // reset MSGLST, NEWDAT 
    }
    // make sure that no new frame was received
    CAN_HWOBJ[ubObjNr].uwCANPTR = (CAN_HWOBJ[ubObjNr].uwCANPTR & ~(uword)0x001F) | ubObjNr;
    CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xf5ff;               // reset MSGLST, NEWDAT 

    ubReturn = 1;
  }
  return(ubReturn);

} //  End of function CAN_ubFlushReadFIFO



// USER CODE BEGIN (CAN_General,10)

// USER CODE END

⌨️ 快捷键说明

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