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

📄 hartlol.c

📁 HART协议编程例程
💻 C
📖 第 1 页 / 共 2 页
字号:
  { strHrtComSM.byErr |= byInErr;
    /* check for error within addressing range */
    if (strHrtComSM.byCount < HRT_SHORTF_LEN_POS)
      /* shortest header has 2 char */
      strHrtComSM.bAddrErr = TRUE;
    if (strHrtComSM.byCount == (HRT_SHORTF_LEN_POS + 1))
      strHrtComSM.bAddrErr = TRUE;
    else
    { if (strHrtComSM.byData[HRT_DELIMITER_POS] == (HRT_LONG_ADDR | HRT_STX))
      { /* long stx frame */
        if (strHrtComSM.byCount < HRT_LONGF_LEN_POS)
          /* shortest header 6 characters */
          strHrtComSM.bAddrErr = TRUE;
        else
        { 
          if (strHrtComSM.byCount == (HRT_LONGF_LEN_POS + 1))
            strHrtComSM.bAddrErr = TRUE;
          if (strHrtComSM.byData[HRT_LONGF_CMD_POS] == HRT_CMD_GET_UNIQUE_ID_BY_TAG)
          { /* addressed by tag name */
            if (strHrtComSM.byCount < HRT_MIN_FRAME_BY_TAG)
              strHrtComSM.bAddrErr = TRUE;
          }
        }  
      }
    }                
  }      
}

          /*############*/
static VOID GetFirstChar(VOID)
          /*############*/
{ strHrtComSM.byCount = 0;
  strHrtComSM.bAddrErr = FALSE;
  strHrtComSM.byErr = HRT_ERR_NONE;
  GetNextChar();
}

          /*####################*/
static VOID BuildDefaultResponse(VOID)
          /*####################*/
{ BYTE byIndex;
  BYTE byCheck;
  BYTE e;

  /* change stx into ack */
  strHrtComSM.byData[HRT_DELIMITER_POS] |= HRT_ACK;
  /* initialize checksum */
  byCheck = strHrtComSM.byData[HRT_DELIMITER_POS];
  /* clear burst mode bit */
  strHrtComSM.byData[HRT_ADDR_POS] &= (UCHR)(~HRT_DEV_IN_BURST_BIT);
  /* assume short address */
  byIndex = HRT_SHORTF_CMD_POS;
  /* build checksum */
  byCheck ^= strHrtComSM.byData[HRT_ADDR_POS];
  if (strHrtComSM.byData[HRT_DELIMITER_POS] & HRT_LONG_ADDR)
  { /* change to long address */
    byIndex = HRT_LONGF_CMD_POS;
    /* build checksum */
    for (e=(HRT_ADDR_POS+1);e<HRT_LONGF_CMD_POS;e++)
      byCheck ^= strHrtComSM.byData[e];
  }
  /* build checksum, command */
  byCheck ^= strHrtComSM.byData[byIndex++];
  /* Set default data len to responses only */
  strHrtComSM.byData[byIndex++] = 2;
  /* build checksum */
  byCheck ^= 2;
  /* set position of response 1 */
  strHrtComSM.byPosRsp1 = byIndex;
  /* report eventual errors in response 1 */
  if (strHrtComSM.byErr)
    strHrtComSM.byData[byIndex] = strHrtComSM.byErr;
  else   
    /* use device busy as the default response */
    strHrtComSM.byData[byIndex] = HRT_RSPCODE_DEV_BUSY;
  /* check for special commands / (Uoppss!) only if no error */
  if (!(strHrtComSM.byErr))
  { if (strHrtAppIf.byStatus == HRT_APP_IDLE)
    { switch (strHrtComSM.byData[byIndex-2])
      { case HRT_CMD_DO_RESET:
          /* do not report busy if reset is requested */
          strHrtComSM.byData[byIndex] = HRT_RSPCODE_OK;
          break;
        default:
          break;
      }
    }
  }
  byCheck ^= strHrtComSM.byData[byIndex++];
  /* response 2 */
  strHrtComSM.byData[byIndex++] = (BYTE)(m_ucResponse2);
  byCheck ^= m_ucResponse2;
  /* place checksum at end of frame */
  strHrtComSM.byData[byIndex++] = byCheck;
  /* set the length of the frame to be sent */
  strHrtComSM.byCount = byIndex;
}

          /*##########*/
static BOOL IsValidDel(VOID)
          /*##########*/
{ switch (byInChar)
  { case 0x02:
    case 0x82:
    case 0x01:
    case 0x81:
    case 0x06:
    case 0x86:
      return TRUE;
    default:
      return FALSE;  
  }
}

          /*##############*/
static VOID CopyFrameToApp(VOID)
          /*##############*/
{ BYTE byIndex;
  /* Set master type */
  if (strHrtComSM.byData[HRT_ADDR_POS] & HRT_MASTER_ADDR_BIT)
    strHrtAppIf.byMasterType = HRT_PRIM_MASTER;
  else
    strHrtAppIf.byMasterType = HRT_SEC_MASTER;
  /* Set index pointer to command position */
  if (strHrtComSM.byData[HRT_DELIMITER_POS] & HRT_LONG_ADDR)
    /* long frame */
    byIndex = HRT_LONGF_CMD_POS;
  else
    /* short frame */
    byIndex = HRT_SHORTF_CMD_POS;
  /* set the command */
  strHrtAppIf.bySrvID = strHrtComSM.byData[byIndex++];
  /* set the data length */
  strHrtAppIf.byDataLen = strHrtComSM.byData[byIndex];
  if (strHrtAppIf.byDataLen > HRT_MAX_DATA_BUFFER)
  { strHrtAppIf.byDataLen = HRT_MAX_DATA_BUFFER;
  }
  if (strHrtAppIf.byDataLen)
  { /* there is data to copy */
    memcpy(&strHrtAppIf.byData[0],
           &strHrtComSM.byData[byIndex+1],
           strHrtAppIf.byDataLen);
  }
}   

          /*##################*/
static VOID BuildValidResponse(T_APP_INTF* pstrAppIntf)
          /*##################*/
{ PBYTE pbyBuf;
  BYTE  byCheck;
  BYTE  byDatLen;
  BYTE  e;    
      
  /* Set correct byte count in frame */
  strHrtComSM.byData[strHrtComSM.byPosRsp1 - 1] = (BYTE)(2 + pstrAppIntf->byDataLen);
  pbyBuf = &strHrtComSM.byData[strHrtComSM.byPosRsp1];
  /* insert response 1 */
  *pbyBuf++ = pstrAppIntf->byResponse1;
  /* save response 2 */
  m_ucResponse2 = (BYTE)(pstrAppIntf->byResponse2);
  /* insert response 2 */
  *pbyBuf++ = (BYTE)(pstrAppIntf->byResponse2);
  /* copy the data into the frame */
  if (pstrAppIntf->byDataLen)
  { memcpy(pbyBuf,&pstrAppIntf->byData[0],pstrAppIntf->byDataLen);
  }
  byCheck = 0;
  byDatLen = (BYTE)(strHrtComSM.byPosRsp1 + pstrAppIntf->byDataLen + 2);
  /* build the checksum */
  for (e=0;e<byDatLen;e++)
  { byCheck ^= strHrtComSM.byData[e];
  }
  /* insert checksum in frame*/
  strHrtComSM.byData[byDatLen] = byCheck;
  /* set length of frame to be sent */
  strHrtComSM.byCount = (BYTE)(byDatLen+1);
  /* reset cold start flag for requesting master */
  pstrAppIntf->byColdStartFlag = FALSE;
}

          /*##########*/
static VOID CopyStrApp(VOID)
          /*##########*/
{ BYTE byMasterType;
  /*
   * Copy the result of the application into a standby buffer
   */  
  byMasterType = strHrtAppIf.byMasterType;
  astrHrtSrvBuf[byMasterType].byStatus = strHrtAppIf.byStatus;
  astrHrtSrvBuf[byMasterType].bySrvID = strHrtAppIf.bySrvID;
  astrHrtSrvBuf[byMasterType].bWriteProtected = strHrtAppIf.bWriteProtected;
  astrHrtSrvBuf[byMasterType].byResponse1 = strHrtAppIf.byResponse1;
  astrHrtSrvBuf[byMasterType].byResponse2 = strHrtAppIf.byResponse2;
  astrHrtSrvBuf[byMasterType].byDataLen = strHrtAppIf.byDataLen;
  if (strHrtAppIf.byDataLen)
    memcpy(&astrHrtSrvBuf[byMasterType].byData[0],
           &strHrtAppIf.byData[0],
           strHrtAppIf.byDataLen);
}

          /*#####################*/
static VOID DataLinkLayerFunction(VOID)
          /*#####################*/
{ switch(byHrtToState)
  { case HRT_TO_STATE_GAP:
      FbHartVerify();
      /* GAP time out indicates receive ready */
      strHrtComSM.byState = HRT_APP_IDLE;
      if (FrameIsForMe())
      { M_HRT_DISABLE_RCV;
        if (!strHrtComSM.byErr)
        { /* there is no communication error, valid frame! */
      	  if (strHrtAppIf.byStatus == HRT_APP_DONE)
      	  { /* application hands over a service to the communication */
            CopyStrApp();
            /* Free the application interface */
            strHrtAppIf.byStatus = HRT_APP_IDLE;
          }
          //check if new command
          if (astrHrtSrvBuf[strHrtComSM.byMasterType].bySrvID != strHrtComSM.byCmd)
	        { /* reset service buffer status if new command */
            /* old command no longer of interest */
            astrHrtSrvBuf[strHrtComSM.byMasterType].byStatus = HRT_APP_IDLE;
	        } 
          if (astrHrtSrvBuf[strHrtComSM.byMasterType].byResponse1)
	        { /* reset service buffer status if there was a negative
               response before */
            if (astrHrtSrvBuf[strHrtComSM.byMasterType].byResponse1 != HRT_RSPCODE_DEV_BUSY)
              astrHrtSrvBuf[strHrtComSM.byMasterType].byStatus = HRT_APP_IDLE;
	        } 
          /* check if app should do next service */
          if ((strHrtAppIf.byStatus == HRT_APP_IDLE) &&
              (astrHrtSrvBuf[strHrtComSM.byMasterType].byStatus == HRT_APP_IDLE)
             )
          {
            CopyFrameToApp(); //copy data before building default response
            //work to do for the application
            strHrtAppIf.byStatus = HRT_APP_BUSY;
          }
          BuildDefaultResponse();
          /* check if immediate response can be sent */
          if (astrHrtSrvBuf[strHrtComSM.byMasterType].byStatus == HRT_APP_DONE)
	        { BuildValidResponse(&astrHrtSrvBuf[strHrtComSM.byMasterType]);
            if (strHrtAppIf.byStatus != HRT_APP_LOCKED) 
              FbHartSendStream(&strHrtComSM.byData[0],strHrtComSM.byCount);
            astrHrtSrvBuf[strHrtComSM.byMasterType].byStatus = HRT_APP_IDLE;
          }
          else
          { /* wait to send response */
            strHrtComSM.byPollCount = HRT_NO_APP_POLLS;
            StartTimeOut(HRT_TO_APP_CYCLE,HRT_TO_STATE_WAIT_APP);
          }
        }
        else
        { /* Send out the default response immediately if there was a
             communication error */
          BuildDefaultResponse();
          if (strHrtAppIf.byStatus != HRT_APP_LOCKED)
          { FbHartSendStream(&strHrtComSM.byData[0],strHrtComSM.byCount);
          }
        }
      }
      else
      { /* Reset com state state machine if this was not a frame
           for me */
        strHrtComSM.byState = HRT_APP_IDLE;
      }
      break;
    case HRT_TO_STATE_WAIT_APP:
      if (strHrtAppIf.byStatus == HRT_APP_DONE)
      { /* Get the application result */
        CopyStrApp();
        /* Reset application interface */
        strHrtAppIf.byStatus = HRT_APP_IDLE;
      }
      if (astrHrtSrvBuf[strHrtComSM.byMasterType].byStatus == HRT_APP_DONE)
      { /* Send out the response together with the applications data */
        BuildValidResponse(&astrHrtSrvBuf[strHrtComSM.byMasterType]);
        /* Reset the time out */
        StopTimeOut();
        /* Send out data stream */
        if (strHrtAppIf.byStatus != HRT_APP_LOCKED)
          FbHartSendStream(&strHrtComSM.byData[0],strHrtComSM.byCount);
        astrHrtSrvBuf[strHrtComSM.byMasterType].byStatus = HRT_APP_IDLE;
      }
      else
      { if (!(strHrtComSM.byPollCount--))
        { /* Send the default response anyhow if application locked
             comm process */
          StopTimeOut();
          if (strHrtAppIf.byStatus != HRT_APP_LOCKED)
            FbHartSendStream(&strHrtComSM.byData[0],strHrtComSM.byCount);
          /* Reset the time out */
        }
        else
        { /* Wait another 50 ms for the application */
          StartTimeOut(HRT_TO_APP_CYCLE,HRT_TO_STATE_WAIT_APP);
        }
      } 
      break;
  }
}

⌨️ 快捷键说明

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