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

📄 toucan.c

📁 can总线c语言 canopen 源程序
💻 C
📖 第 1 页 / 共 5 页
字号:
}/************************************************************************** TouCAN_Tx - Transmits the CAN message** This function transmits the CAN Id and data currently in the channel of* the specified controller on the device. The mode of the channel must be* WNCAN_CHN_TRANSMIT or WNCAN_CHN_RTR_REQUESTER** RETURNS:        OK or ERROR*   * ERRNO:          S_can_illegal_channel_no*                 S_can_illegal_config**/static STATUS TouCAN_Tx( struct WNCAN_Device *pDev, UCHAR channelNum ){    struct canAccess *pcanAccess;        TouCAN            pTouCanRegs;    TouCANBuf         pTouCanBufs;    volatile TouCAN_StandardMsgType *buffer;        STATUS            retCode = ERROR; /* pessimistic */        volatile USHORT   len=0;        if (channelNum >= TOUCAN_MAX_MSG_OBJ)    {        errnoSet(S_can_illegal_channel_no);        goto exit;    }    else if((pDev->pCtrl->chnMode[channelNum] != WNCAN_CHN_TRANSMIT) &&        (pDev->pCtrl->chnMode[channelNum] != WNCAN_CHN_RTR_REQUESTER))    {        errnoSet(S_can_illegal_config);                goto exit;    }        /*    * Get pointer to TouCAN registers and channels.        */      pcanAccess = (struct canAccess *)pDev->pCtrl->csData;    pTouCanRegs = (TouCAN)pcanAccess->pTouCanRegs;          pTouCanBufs = (TouCANBuf)pcanAccess->pTouCanBufs;           buffer = &pTouCanBufs->can_MSG[channelNum];                 /*    * Check if tx request is set before transmitting     * only a write access will deactivate a transmit     * buffer, so this is safe    */       if((buffer->Control & 0x00c0) == 0x00c0)    {        errnoSet(S_can_busy);        goto exit;    }        /*    * Make a copy of the length before writing to control field    * of channel. This is done since we want to clear any previous    * control code setting.    */    len = buffer->Control & 0x000f;                     /*    * Request a transmission and write length if channel is marked    * as a transmit or remote requester channel    */                buffer->Control = (TOUCAN_TX | len);                                    retCode = OK;           exit:       return retCode;}/************************************************************************** TouCAN_SetGlobalRxFilter - set the global receive filter** This function sets the global HW filter mask for incoming messages on the * device controller. If the controller does not have a global filter this* function is a no-op. If the ext parameter is TRUE, the mask value applies* to extended messages; otherwise, the mask values applies to standard * messages. A value of "0" for a particular bit position of the mask means * don't care (i.e. the incoming message Id could have a value of zero or * one for this bit position); otherwise, a value of 1 means the * corresponding bit of the message Id must match identically.** WARNING: Only one global filter exists on the controller. This is shared* by standard and extended identifiers. These routines cannot be called from* an interrupt level.** RETURNS: OK ** ERRNO:   N/A**/static STATUS TouCAN_SetGlobalRxFilter( struct WNCAN_Device *pDev, long maskVal,  BOOL  ext ){    struct canAccess *pcanAccess;    TouCAN     pTouCANRegs;        ULONG      mask;    ULONG      temp;    USHORT     regMCR;        /*    * Get pointer to TouCAN registers and channels.        */        pcanAccess = (struct canAccess *)pDev->pCtrl->csData;    pTouCANRegs = (TouCAN)pcanAccess->pTouCanRegs;            /*    Check if controller is in configuration change enable mode    if not set HALT bit    */    regMCR = pTouCANRegs->can_MCR;        if((regMCR & TOUCAN_HALT) != TOUCAN_HALT)        TouCANSetHaltFrz(pTouCANRegs);              if (ext == TRUE)    {        /* Adjust mask bit 28 to be the MSB */        temp = (ULONG)maskVal;        temp = temp << 3;                /* create the holes (the 0x8) between mask bits 18 and 17 */        /* Put mask bits 17 to 0 in place */        mask = (temp & TOUCAN_M17TO0) >> 2;                /* Put mask bits 28 to 18 in place */        mask |= (temp & TOUCAN_M28TO18);            }    else    {        temp = (ULONG)maskVal;        /* Adjust mask bit 28 to be the MSB */              mask = (temp << 21) & 0xffe00000;    }        /* Place 01 between mask bits 18 and 17 */    mask |= 0x00080000;        pTouCANRegs->can_RxGlobalMask = mask;        /* restore controller mode */    if((regMCR & TOUCAN_HALT) != TOUCAN_HALT)        TouCANResetHaltFrz(pTouCANRegs);                return OK;}/************************************************************************** TouCAN_GetGlobalRxFilter - Gets the global HW filter mask programmed.** This function return the programmed value in the Global filter resgiter* Based on the value of ext passed to the function, this function reads the * appropriate format of the global mask, and returns the value in the specified,* extended or standard identifier format.* If the controller does not have a global mask, a value of -1 is returned.** RETURNS: long: mask** ERRNO:   N/A**/static long TouCAN_GetGlobalRxFilter( struct WNCAN_Device *pDev, BOOL   ext ){    struct canAccess *pcanAccess;    TouCAN            pTouCanRegs;        ULONG             mask, val;        /*    * Get pointer to TouCAN registers and channels.        */        pcanAccess = (struct canAccess *)pDev->pCtrl->csData;    pTouCanRegs = (TouCAN)pcanAccess->pTouCanRegs;            mask = pTouCanRegs->can_RxGlobalMask;        if(ext == TRUE)    {                /* get bits 17:0 */        val = (mask & TOUCAN_GetM17TO0) << 2;                /* get bits 28 to 18*/        val |= mask & TOUCAN_M28TO18;                /* adjust bit positions */        val = val >> 3;    }    else    {        /* get bits 28 to 18*/        val = (mask & TOUCAN_M28TO18) >> 21;            }        return (long)val; }/************************************************************************** TouCAN_SetLocalMsgFilter -  sets the TouCAN message object 14/15 filter*                           * Sets the HW filter mask for message object 14/15. Message object 14 and 15* incorporates a hardware buffer to store incoming messages, thereby allowing* the CPU more time to process messages. Because the functionality of message* object 15 is specific to the TouCAN, this function is not part of the CAN common * interface. This function is provided here for developers who specifically* require the functionality of message object 14/15. The mask values applies * to both standard and extended messages. A value of "0" for a particular * bit position of the mask means don't care (i.e. the incoming message Id * could have a value of zero or one for this bit position); otherwise, a * value of 1 means the corresponding bit of the message Id must match * identically.** RETURNS: OK, ERROR*   * ERRNO: S_can_illegal_channel_no**/static STATUS TouCAN_SetLocalMsgFilter( struct WNCAN_Device *pDev, /* CAN device pointer */ UCHAR channel,             /* identifies filter 14/ 15*/  long  maskVal,             /* filter mask value  */ BOOL  ext ){    struct canAccess *pcanAccess;    TouCAN            pTouCanRegs;        TouCANBuf         pTouCanBufs;    volatile TouCAN_StandardMsgType *buffer;    ULONG             mask;    ULONG             temp;            UCHAR             tempRead;     STATUS            retCode=OK;        if(channel<14)    {        errnoSet(S_can_illegal_channel_no);             retCode=ERROR;        goto exit;    }            /*    * Get pointer to TouCAN registers and channels.        */        pcanAccess = (struct canAccess *)pDev->pCtrl->csData;    pTouCanRegs = (TouCAN)pcanAccess->pTouCanRegs;        pTouCanBufs = (TouCANBuf)pcanAccess->pTouCanBufs;                   buffer = &pTouCanBufs->can_MSG[channel];                 /*Read channel control field to lock the channel*/    tempRead = buffer->Control;        if (ext)    {        /* Adjust mask bit 28 to be the MSB */        temp = (ULONG)maskVal;        temp = temp << 3;                /* create the holes (the 0x8) between mask bits 18 and 17 */        /* Put mask bits 17 to 0 in place */        mask = (temp & TOUCAN_M17TO0) >> 2;                /* Put mask bits 28 to 18 in place */        mask |= (temp & TOUCAN_M28TO18);            }    else    {        temp = (ULONG)maskVal;        /* Adjust mask bit 28 to be the MSB */              mask = (temp << 21) & 0xffe00000;    }        /* Place 01 between mask bits 18 and 17 */    mask |= 0x00080000;        switch (channel) {    case TOUCAN_RXBUF14_MASK:        pTouCanRegs->can_RxBuff14Mask = mask;        break;    case TOUCAN_RXBUF15_MASK:        pTouCanRegs->can_RxBuff15Mask = mask;    }        /*    * Release lock on channel by reading the timer    */    timerRead = pTouCanRegs->can_TIMER;  exit:    return retCode;}/************************************************************************** TouCAN_GetLocalMsgFilter - get local message object filter** This function returns the programmed value in the local filter resgiter* Based on the value of ext passed to the function, this function reads the * appropriate format of the local mask, and returns the value in the specified,* extended or standard identifier format.* The channel argument identifies the local filter associated with the particular* channel number** RETURNS: long:mask or -1 on error *   * ERRNO:   S_can_hwfeature_not_available*          (for channels not supporting local filters)**/static long TouCAN_GetLocalMsgFilter( struct WNCAN_Device *pDev, UCHAR channel,             /* identifies filter 14/ 15*/  BOOL   ext ){    struct canAccess *pcanAccess;    TouCAN            pTouCanRegs;        ULONG             mask;    ULONG             val=0xffffffff;        /*    * Get pointer to TouCAN registers and channels.        */        pcanAccess = (struct canAccess *)pDev->pCtrl->csData;    pTouCanRegs = (TouCAN)pcanAccess->pTouCanRegs;            switch (channel) {    case TOUCAN_RXBUF14_MASK:        mask = pTouCanRegs->can_RxBuff14Mask;        break;    case TOUCAN_RXBUF15_MASK:        mask = pTouCanRegs->can_RxBuff15Mask;        break;    default:        /*channel number does not have a local filter*/        errnoSet(S_can_hwfeature_not_available);        goto exit;    }        if(ext == TRUE)    {                /* get bits 17:0 */        val = (mask & TOUCAN_GetM17TO0) << 2;                /* get bits 28 to 18*/        val |= mask & TOUCAN_M28TO18;                /* adjust bit positions */        val = val >> 3;    }    else    {        /* get bits 28 to 18*/        val = (mask & TOUCAN_M28TO18) >> 21;            }    exit:    return (long)val; }/************************************************************************** TouCAN_IsMessageLost - Test if message is lost** This function tests if the current message data in the channel overwrote * the old message data before CAN_ReadData() was called. Valid only for * channels with mode = WNCAN_CHN_RECEIVE or WNCAN_CHN_RTR_REQUESTER** RETURNS:        0 if FALSE, 1 if TRUE, or -1 if ERROR** ERRNO:          S_can_illegal_channel_no, S_can_illegal_config**/static int TouCAN_IsMessageLost( struct WNCAN_Device *pDev, UCHAR channelNum ){        struct canAccess *pcanAccess;       TouCANBuf         pTouCanBufs;    TouCAN            pTouCanRegs;    volatile TouCAN_StandardMsgType *buffer;    int               retCode = -1; /* pessimistic */    USHORT            buffCtrl;        if (channelNum >= TOUCAN_MAX_MSG_OBJ)    {        errnoSet(S_can_illegal_channel_no);        goto exit;    }        if((pDev->pCtrl->chnMode[channelNum] != WNCAN_CHN_RECEIVE) &&        (pDev->pCtrl->chnMode[channelNum] != WNCAN_CHN_RTR_REQUESTER))    {        errnoSet(S_can_illegal_config);                goto exit;    }        /*    * Get pointer to TouCAN registers and channels.        */    pcanAccess = (struct canAccess *)pDev->pCtrl->csData;    pTouCanRegs = (TouCAN)pcanAccess->pTouCanRegs;        pTouCanBufs = (TouCANBuf)pcanAccess->pTouCanBufs;                   buffer = &pTouCanBufs->can_MSG[channelNum];                 /*Read channel control field to lock the channel*/    buffCtrl = buffer->Control;        if((buffer->Control & TOUCAN_BUFFER_OVERRUN) == TOUCAN_BUFFER_OVERRUN)    {                               retCode = 1;            }    else        retCode = 0;            /*        * Release lock on channel by reading the timer    */    timerRead = pTouCanRegs->can_TIMER;     exit:    return retCode;}/************************************************************************** TouCAN_ClearMessageLost - Clear message lost indication 

⌨️ 快捷键说明

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