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

📄 can1.c

📁 mc68HC12C64的CAN部件例子程序,不错的.
💻 C
📖 第 1 页 / 共 2 页
字号:
/** ###################################################################
**     THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**     Filename  : CAN1.C
**     Project   : Node_B
**     Processor : MC9S12C64CFA16
**     Beantype  : FreescaleCAN
**     Version   : Bean 02.334, Driver 01.21, CPU db: 2.87.339
**     Compiler  : Metrowerks HC12 C Compiler
**     Date/Time : 2006-11-11, 15:02
**     Abstract  :
**         This bean "FreescaleCAN" implements a CAN serial channel.
**     Settings  :
**         CAN channel                 : MSCAN
**
**         Protocol
**             Interrupt priority      : 1
**             Time segment 1          : 7
**             Time segment 2          : 3
**             RSJ                     : 1
**
**             Recieve accept. code    : 0
**             Recieve accept. mask    : 4294967295
**
**         Input interrupt
**             Vector name             : Vcanrx
**             Priority                : 1
**
**         Output interrupt
**             Vector name             : Vcantx
**             Priority                : 1
**
**     Contents  :
**         SetAcceptanceMask - byte CAN1_SetAcceptanceMask(dword AccMask1,dword AccMask2);
**         SendFrame         - byte CAN1_SendFrame(byte BufferNum,dword MessageID,byte FrameType,byte...
**         ReadFrame         - byte CAN1_ReadFrame(dword *MessageID,byte *FrameType,byte *FrameFormat,byte...
**         GetStateTX        - byte CAN1_GetStateTX(void);
**         GetStateRX        - bool CAN1_GetStateRX(void);
**         SendFrameExt      - byte CAN1_SendFrameExt(dword MessageID,byte FrameType,byte Length,byte *Data);
**
**     (c) Copyright UNIS, spol. s r.o. 1997-2005
**     UNIS, spol. s r.o.
**     Jundrovska 33
**     624 00 Brno
**     Czech Republic
**     http      : www.processorexpert.com
**     mail      : info@processorexpert.com
** ###################################################################*/

/* MODULE CAN1. */

#pragma MESSAGE DISABLE C1825
#pragma MESSAGE DISABLE C4301 /* INFORMATION C4301: Inline expansion done for function call */
#pragma OPTION ADD "-Onf"

#include "CAN1.h"
#include "Byte1.h"
#include "Events.h"

#define CAN_STANDARD_FRAME_MAX_ID    2047 /* Max ID of the standard frame */
#define CAN_MAX_DATA_LEN             8 /* Max number of data to be sent in one frame */
#define FULL_RX_BUF                  1 /* RX buffer full           */

#define MB_ID_IDE                    0x00080000UL
#define CAN_TX_MBUFFERS              3 /* Number of TX buffers */
#define CAN_MAX_RX_FIFO              4 /* Max length of the RX fifo */

#define CAN_STATUS_OVERRUN_MASK      2 /* Overrun error flag mask */
#define CAN_STATUS_TX_MASK           12 /* Transmitter error state mask*/
#define CAN_STATUS_RX_MASK           48 /* Receiver error state mask*/
#define CAN_STATUS_BOFF_MASK         12 /* Bus-Off state mask in register */
#define CAN_STATUS_BOFF_EXT_MASK     64 /* Bus-Off state mask in error flag */
#define CAN_STATUS_TX_PASS_MASK      8 /* Transmitter error passive state mask */
#define CAN_STATUS_RX_PASS_MASK      32 /* Receiver error passive state mask */
#define CAN_STATUS_TX_WARN_MASK      4 /* Transmitter warning mask */
#define CAN_STATUS_RX_WARN_MASK      16 /* Receiver warning mask */
#define CAN_STATUS_WAKEUP_MASK       128 /* Wakeup interrupt flag mask */

typedef struct {                       /* Message buffer structure */
          byte IDR0;
          byte IDR1;
          byte IDR2;
          byte IDR3;
          byte Data[CAN_MAX_DATA_LEN];
          byte DLR;
          byte TBPR;
          byte Reserved;
          byte Reserved2;
        }TMsgBuff;                     /* Message buffer structure */

typedef union {
          dword dw;
          struct {
            byte b0;
            byte b1;
            byte b2;
            byte b3;
          }b;
        }DwordSwap;

#pragma DATA_SEG CAN1_DATA
#pragma CODE_SEG CAN1_CODE

static volatile byte SerFlag;          /* Internal driver flags */

/*
** ===================================================================
**     Method      :  CAN1_GetStateRX (bean FreescaleCAN)
**
**     Description :
**         Returns a value of the reception complete flag.
**     Parameters  : None
**     Returns     :
**         ---             - The value of the receiver complete flag
**                           of the given buffer. 
**                           Possible values:
**                           FALSE - message buffer is empty
**                           TRUE - message buffer isn't empty
** ===================================================================
*/
byte CAN1_GetStateRX(void)
{
  return ((SerFlag & FULL_RX_BUF) != 0)? (byte)1 : (byte)0; /* Return status of the RX buffer */
}

/*
** ===================================================================
**     Method      :  CAN1_SendFrame (bean FreescaleCAN)
**
**     Description :
**         Sends the frame via the CAN device. Using this method the
**         user can send own message to the CAN bus. This method
**         allows to specify CAN buffer number, message ID, data to
**         be sent and frame type (DATA_FRAME/REMOTE_FRAME).
**     Parameters  :
**         NAME            - DESCRIPTION
**         BufferNum       - Number of the buffer.
**         MessageID       - Identification of the
**                           message - ID. Message ID can be
**                           specified in the STANDARD format
**                           (default) or the EXTENDED format. The
**                           most significant bit in the ID is set to
**                           specify EXTENDED format. Predefined
**                           macro CAN_EXTENDED_FRAME_ID can be used
**                           (ID "bitwise or" CAN_EXTENDED_FRAME_ID)
**                           to mark ID as extended. If the most
**                           significant bit of ID is clear, STANDARD
**                           format is used.
**         FrameType       - Type of frame
**                           DATA_FRAME - data frame
**                           REMOTE_FRAME - remote frame
**         Length          - The length of the frame in bytes
**                           (0..8)
**       * Data            - Pointer to data
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_DISABLED - This bean is disabled by
**                           user
**                           ERR_VALUE - Some parameter is out of
**                           possible range
**                           ERR_TXFULL - Transmition buffer is full.
** ===================================================================
*/
byte CAN1_SendFrame(byte BufferNum,dword MessageID,byte FrameType,byte Length,byte *Data)
{
  byte i;                              /* Temorary variables */
  byte bufmask=((word)1 << BufferNum); /* Buffer mask */
  TMsgBuff *MsgBuff;
  dword tmpId;

  if (((MessageID & CAN_EXTENDED_FRAME_ID) == 0) && (MessageID > CAN_STANDARD_FRAME_MAX_ID)) { /* Is the standard ID greater that 2047? */
    return ERR_VALUE;                  /* If yes then error */
  }
  if ((BufferNum > (CAN_TX_MBUFFERS - 1)) || (Length > CAN_MAX_DATA_LEN)) { /* Is BufferNum greater than CAN_MAXBUFF or Length greater than CAN_MAX_DATA_LEN? */
    return ERR_VALUE;                  /* If yes then error */
  }
  if (FrameType > REMOTE_FRAME) {      /* Is FrameType other than REMOTE_FRAME or DATA_FRAME */
    return ERR_VALUE;                  /* If yes then error */
  }
  if (!(CANTFLG & bufmask)) {          /* Is the transmit buffer full? */
    return ERR_TXFULL;                 /* If yes then error */
  }
  CANTBSEL = bufmask;                  /* Select requested transmit buffer */
  MsgBuff = (TMsgBuff *)&CANTXIDR0;
  EnterCritical();                     /* Disable global interrupts */
  if (MessageID & CAN_EXTENDED_FRAME_ID) {
    tmpId = (((MessageID & 0x1FFC0000UL) << 3) | 0x00180000UL | ((MessageID & 0x0003FFFFUL) << 1)); /* Extended frame */
  }
  else {
    tmpId = MessageID << 21;           /* Standard frame */
  }
  MsgBuff->IDR3 = ((DwordSwap *)&tmpId)->b.b3; /* Store the message ID */
  MsgBuff->IDR2 = ((DwordSwap *)&tmpId)->b.b2;
  MsgBuff->IDR1 = ((DwordSwap *)&tmpId)->b.b1;
  MsgBuff->IDR0 = ((DwordSwap *)&tmpId)->b.b0;
  if (FrameType == DATA_FRAME) {       /* Is it a data frame? */
    for (i=0; i<Length; i++) {
      MsgBuff->Data[i] = Data[i];      /* Store data to the transmit register */
    }
    if (MessageID & CAN_EXTENDED_FRAME_ID) { /* Is it the extended frame? */
      MsgBuff->IDR3 &= 254;            /* If no then set message type as "data frame" */
    }
    else {
      MsgBuff->IDR1 &= 239;            /* If yes then set message type as "data frame" */
    }
  }
  else {                               /* Remote frame */
    if (MessageID & CAN_EXTENDED_FRAME_ID) { /* Is it the extended frame? */
      MsgBuff->IDR3 |= 1;              /* If yes then set message type as "remote frame" */
    }
    else {
      MsgBuff->IDR1 |= 16;             /* If yes then set message type as "remote frame" */
    }
  }
  MsgBuff->DLR = Length;               /* Set the length of the message */
  MsgBuff->TBPR = 0;                   /* Set the priority (high) */
  CANTFLG = bufmask;                   /* Start transmission */
  ExitCritical();                      /* Enable global interrupts */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  CAN1_SendFrameExt (bean FreescaleCAN)
**
**     Description :
**         Sends a frame. This method automatically selects a free
**         transmit buffer for data transmission. The user cannot
**         specify a transmit buffer.
**     Parameters  :
**         NAME            - DESCRIPTION
**         MessageID       - Identification of the
**                           message - ID. Message ID can be
**                           specified in the STANDARD format
**                           (default) or the EXTENDED format. The
**                           most significant bit in the ID is set to
**                           specify EXTENDED format. Predefined
**                           macro CAN_EXTENDED_FRAME_ID can be used
**                           (ID "bitwise or" CAN_EXTENDED_FRAME_ID)
**                           to mark ID as extended. If the most
**                           significant bit of ID is clear, STANDARD
**                           format is used.
**         FrameType       - Type of frame
**                           DATA_FRAME - data frame
**                           REMOTE_FRAME - remote frame
**         Length          - The length of the frame in bytes
**                           (0..8)
**       * Data            - Pointer to data
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_DISABLED - This bean is disabled by
**                           user
**                           ERR_VALUE - Some parameter is out of
**                           possible range
** ===================================================================
*/
byte CAN1_SendFrameExt(dword MessageID,byte FrameType,byte Length,byte *Data)
{
  byte i;                              /* Temorary variables */
  TMsgBuff *MsgBuff;
  dword tmpId;

  if (((MessageID & CAN_EXTENDED_FRAME_ID) == 0) && (MessageID > CAN_STANDARD_FRAME_MAX_ID)) { /* Is the standard ID greater that 2047? */
    return ERR_VALUE;                  /* If yes then error */
  }
  if (Length > CAN_MAX_DATA_LEN) {     /* Is the message length greater than 8?*/
    return ERR_VALUE;                  /* If yes then error */
  }
  if (FrameType > REMOTE_FRAME) {      /* Is FrameType other than REMOTE_FRAME or DATA_FRAME */
    return ERR_VALUE;                  /* If yes then error */
  }
  if (!(CANTFLG & 7)) {                /* Are all transmit buffers full? */
    return ERR_TXFULL;                 /* If yes then error */
  }
  CANTBSEL = CANTFLG;                  /* Find any empty transmit buffer */
  MsgBuff = (TMsgBuff *)&CANTXIDR0;
  EnterCritical();                     /* Disable global interrupts */
  if (MessageID & CAN_EXTENDED_FRAME_ID) {
    tmpId = (((MessageID & 0x1FFC0000UL) << 3) | 0x00180000UL | ((MessageID & 0x0003FFFFUL) << 1)); /* Extended frame */
  }
  else {
    tmpId = MessageID << 21;           /* Standard frame */
  }

⌨️ 快捷键说明

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