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

📄 can.c

📁 TQ公司的STK16x开发系统的源码
💻 C
📖 第 1 页 / 共 2 页
字号:

  can_cms_buf[cmsnr].canstatus = 0;

  return (TRUE);
}

/*==========================================================================*
* extern available functions:
*===========================================================================*/

/*--------------------------------------------------------------------------*
* void can_init(BYTE node, BYTE baud)
*---------------------------------------------------------------------------*
* FT: init CAN bus
* EP: node = node number (0..NODEMAX)
*     baud = baud rate (BPS50, BPS125, BPS250, BPS500)
* RV: -
* GP: BPSxxx, I_CAN_BTR0_xxx, canreg
*---------------------------------------------------------------------------*/
void can_init(BYTE node, BYTE baud)
{ int cmsnr;
  int msgbuf;

  /* init CPU C167: */
  XP0IC = I_XP0IC;
  IEN = 1;

  /* start of initialization mode: */
  canreg[CAN_CR] = 0x41;                    /* allow configuration changes */
  canreg[CAN_SR] = I_CAN_SR;
  canreg[CAN_IPC] = I_CAN_IPC;  /* IPC = 000b --> CAN1/CAN2 -> P4.4 .. P4.7 */ 

  switch(baud)
  {
    case BPS50 : canreg[CAN_BTR0] = I_CAN_BTR0_50;
		 break;
    case BPS125: canreg[CAN_BTR0] = I_CAN_BTR0_125;
		 break;
    case BPS250: canreg[CAN_BTR0] = I_CAN_BTR0_250;
		 break;
    case BPS500: canreg[CAN_BTR0] = I_CAN_BTR0_500;
		 break;
  }
	
  canreg[CAN_BTR1] = I_CAN_BTR1;
  canreg[CAN_CR]   = 0x01;
  
  /* init message control register: */
  for(msgbuf=1; msgbuf<16; msgbuf++)
  {
    canreg[msgbuf * MSGOFF + CAN_MSG_CTR0]  = 0x55;
    canreg[msgbuf * MSGOFF + CAN_MSG_CTR1]  = 0x55;
  }

  /* global mask standard: */
  canreg[CAN_GMS0] = I_CAN_GMS0;        
  canreg[CAN_GMS1] = I_CAN_GMS1;

  /* global mask extended: */
  canreg[CAN_GML0] = I_CAN_GML0;
  canreg[CAN_GML1] = I_CAN_GML1;
  canreg[CAN_GML2] = I_CAN_GML2;
  canreg[CAN_GML3] = I_CAN_GML3;

  /* message 15 mask: */
  canreg[CAN_M15M0] = I_CAN_M15M0;
  canreg[CAN_M15M1] = I_CAN_M15M1;
  canreg[CAN_M15M2] = I_CAN_M15M2;
  canreg[CAN_M15M3] = I_CAN_M15M3;

  /* end of initialization mode: */
  canreg[CAN_CR] = I_CAN_CR & 0xFE;       /* end of configuration changes */

  /* init variables: */
  for (cmsnr=0; cmsnr<CAN_CMS_MAX; cmsnr++)
    can_cms_init(cmsnr, node);
}

/*--------------------------------------------------------------------------*
* int can_write(int cmsnr, CAN_DATA_T *buf)
*---------------------------------------------------------------------------*
* FT: write CAN message
* EP: cmsnr = CAN message number
*     buf   = message buffer
* RV: TRUE = function succesfully; FALSE = function failed
* GP: -
*---------------------------------------------------------------------------*/
int can_write(int cmsnr, CAN_DATA_T *buf)
{ static int loop;
  static int msgbuf;

  /* check parameter: */
  if (can_chk_cmsbuf(cmsnr) == FALSE)
    return (FALSE);

  msgbuf = cmsnr + 1;

  /* CPU is updating data: */
  canreg[msgbuf * MSGOFF + CAN_MSG_CTR1] = 0xFA;
	
  /* store data in the CAN register an CMS data: */
  for (loop=0; loop<8; loop++)
  {
    can_cms_buf[cmsnr].data[loop] 
      = canreg[msgbuf * MSGOFF + CAN_MSG_DATA1 + loop] 
		  = buf->data[loop];            
  }

  /* end of CPU updating data: */
  canreg[msgbuf * MSGOFF + CAN_MSG_CTR1] = 0xF7;
	
  /* if the transmit buffer will initialized, the output message will
   * be activated after the 1st written message to the data register.
   * This avoids wrong data on a rtr command. */
  if (can_cms_buf[cmsnr].status == INITIALIZED) 
  {     
    canreg[msgbuf * MSGOFF + CAN_MSG_CTR0] = IW_CAN_MSG_CTR0; 
    canreg[msgbuf * MSGOFF + CAN_MSG_CTR1] = IW_CAN_MSG_CTR1;
    can_cms_buf[cmsnr].status = NEWDATA;
  }

  /* when EVENT, then send data immediately: */
  if( can_cms[cmsnr].control == EVENT)
    canreg[msgbuf * MSGOFF + CAN_MSG_CTR1] = 0xEF;  

  return (TRUE);
}

/*--------------------------------------------------------------------------*
* int can_read(int cmsnr, CAN_DATA_T *buf)
*---------------------------------------------------------------------------*
* FT: read CAN message object
* EP: cmsnr = CAN message number
*     buf   = message buffer
* RV: TRUE = function succesfully; FALSE = function failed
* GP: -
*---------------------------------------------------------------------------*/
int can_read(int cmsnr, CAN_DATA_T *buf)
{ int loop;

  /* check parameter: */
  if (can_chk_cmsbuf(cmsnr) == FALSE)
    return (FALSE);
    
  /* copy status and check it: */
  if ((buf->status = can_cms_buf[cmsnr].status) == EMPTY)
    return (FALSE);

  if ((buf->status = can_cms_buf[cmsnr].status) == INITIALIZED)
    return (FALSE);

  do
  {
    /* sign data buffer as empty: */
    can_cms_buf[cmsnr].status = EMPTY;

    /* copy data: */
    for (loop=0; loop<8; loop++)
      buf->data[loop] = can_cms_buf[cmsnr].data[loop];

    /* meanwhile copying data, the interrupt can write new data *
     * to the buffer. If this happens, copy the buffer again    */
  } 
  while (can_cms_buf[cmsnr].status != EMPTY);

  return (TRUE);
}

/*--------------------------------------------------------------------------*
* void can2_interrupt(void) interrupt XP0INT
*---------------------------------------------------------------------------*
* FT: CAN interrupt routine, copy data from CAN controller to CMS object
* EP: -
* RV: -
* GP: can_cms_buf, canreg, CPU register
*---------------------------------------------------------------------------*/
void can2_interrupt(void) interrupt XP0INT
{ static int idata loop;

  static BYTE idata ir;         /* ir = interrupt register        */
  static BYTE idata msgbuf;     /* Messagebuffernr der IRQ source */
  static BYTE idata cmsnr;      /* Messagebuffernr im Speicher    */
  static BYTE idata sr;         /* sr = status register           */ 
  
  while (canreg[CAN_IR])                     /* handle all CAN interrupts */
  {
    ir = canreg[CAN_IR];

    if (ir == 0x01)                     /* handle status change interrupt */
    {
      /* interrupt source:
       * Error Warning Status -> ignored
       * Busoff Status        -> ignored */
      sr = canreg[CAN_SR];
    }

    if (ir == 0x02)                        /* handle message-15 interrupt */
    {
      canreg[CAN_MSG15 + CAN_MSG_CTR0] = 0xFD;           /* update INTPND */
    }

    if (ir > 0x02)                 /* handle all other message interrupts */
    {
      msgbuf = ir - 2;                                 /* register access */
      cmsnr  = ir - 3;                    /* access to CAN message buffer */
			
      canreg[msgbuf * MSGOFF + CAN_MSG_CTR0] = 0xFD;     /* update INTPND */

      for (loop=0; loop<8; loop++)
      {      
	can_cms_buf[cmsnr].data[loop] = canreg[msgbuf * MSGOFF + CAN_MSG_DATA1 
					       + loop]; 
      } 

      if (can_cms_buf[cmsnr].status == NEWDATA)
	can_cms_buf[cmsnr].status = OVERLOAD;
			
      if (can_cms_buf[cmsnr].status == EMPTY)
	can_cms_buf[cmsnr].status = NEWDATA;
			
      if( can_cms_buf[cmsnr].status == INITIALIZED)
	can_cms_buf[cmsnr].status = NEWDATA;
			
      can_cms_buf[cmsnr].canstatus = canreg[CAN_SR];
    }
  }
}

⌨️ 快捷键说明

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