/**********************************************************************
 * Copyright (c) 1998 - 2000 by Borst Automation, Fachingen
 * All rights reserved
 *
 * Filename:  Hart.h
 * Project:   HART
 * Date:      Aug-22-99
 * Author(s): W. Borst
 *
 * Revision:
 * Date:      Name:       Version:  Description:
 * Dec-03-98  W. Borst    -----     Initial Draft
 * Aug-22-99  W. Borst    1.1       Changes for Project fl005/99
 * Jul-25-00  W. Borst    1.2       Changes for Project li001/00
 *
 * Description: HART related declarations
 *
 **********************************************************************/

/**********************************************************************
 * Constant definitions                                               *
 **********************************************************************/
/*
 * Delimiters
 */
#define HRT_STX   0x02          /* Master to Slave */
#define HRT_ACK   0x06          /* Slave to Master */
#define HRT_BURST 0x01          /* Burst from Slave */

/*
 * Addressing type flag
 */
#define HRT_SHORT_ADDR 0x00
#define HRT_LONG_ADDR  0x80

/*
 * Master types
 */
#define HRT_PRIM_MASTER         1
#define HRT_SEC_MASTER          0

/*
 * Special characters
 */
#define HRT_PREAMBLE       0xff
#define HRT_BROADCAST_ADDR 0x00

/*
 * LL management commands
 */
#define HRT_CMD_GET_UNIQUE_ID_BY_SHORT 0  /* command  0 */
#define HRT_CMD_SET_POLL_ADDRESS       6  /* command  6 */
#define HRT_CMD_GET_UNIQUE_ID_BY_TAG  11  /* command 11 */
#define HRT_CMD_DO_RESET              42  /* command 42 */

/*
 * Time out definitions
 */     
#define HRT_NO_APP_POLLS    3       /* Latest time for the slave to 
                                       respond as a multiple of 
                                       HRT_TO_APP_CYCLE */
#ifdef VC_STUDIO
  /* For PC simulation a longer gap time out is required */
  #define HRT_TO_GAP          6       /* 30 ms GAP to be sure on win pc
                                         multiple of 5 ms */
#else
  #define HRT_TO_GAP          3       /* 15 ms GAP to be sure on win pc
                                         multiple of 5 ms */
#endif
#define HRT_TO_APP_CYCLE   10       /* Cycle time for polling the 
                                       application as a multiple of 
                                       5 ms */
#define HRT_TO_RTSON_DELAY  1       /* Wait for transmit after RTS off */
#define HRT_TO_RTSOFF_DELAY 1       /* Wait for transmit after RTS off */

/*
 * Bit masks 
 */
#define HRT_DELIMITER_TYPE_MASK   0x07
#define HRT_MASK_OUT_HI_ADDR_BITS 0x3f

/*
 * Addressing bits
 */
#define HRT_MASTER_ADDR_BIT  0x80
#define HRT_DEV_IN_BURST_BIT 0x40

/*
 * Address sizes
 */
#define HRT_SHORT_ADDR_SIZE 1
#define HRT_LONG_ADDR_SIZE  5

/*
 * Other data sizes
 */
#define HRT_TAG_NAME_SIZE 0x06

/*
 * Possible frame sizes
 */
#define HRT_MIN_FRAME_ABS     5 /* No hart frame shorter than this */
#define HRT_MIN_FRAME_LONG    9 /* No long hart frame shorter than this */
#define HRT_MIN_FRAME_BY_TAG 15 /* Minimum amount for command 11 */

/*
 * Number of preambles
 */
#define HRT_NO_PREAMBLES         5

/*
 * Frame information positions
 */
#define HRT_DELIMITER_POS        0
#define HRT_ADDR_POS             1
#define HRT_SHORTF_CMD_POS       2
#define HRT_LONGF_CMD_POS        6
#define HRT_SHORTF_LEN_POS       3
#define HRT_LONGF_LEN_POS        7
#define HRT_SHORTF_REQDATA_POS   4
#define HRT_LONGF_REQDATA_POS    8
#define HRT_SHORTF_RSPCODE1_POS  4
#define HRT_LONGF_RSPCODE1_POS   8
#define HRT_SHORTF_RSPCODE2_POS  5
#define HRT_LONGF_RSPCODE2_POS   9
#define HRT_SHORTF_RSPDATA_POS   6
#define HRT_LONGF_RSPDATA_POS   10


/*
 * Bit definitions for the device status
 */
#define HRT_FIELD_DEVICE_MALFUNCTION 0x80
#define HRT_CONFIGURATION_CHANGED    0x40
#define HRT_COLD_START               0x20
#define HRT_MORE_STATUS_AVAILABLE    0x10
#define HRT_ANALOG_OUTPUT_FIXED      0x08
#define HRT_ANALOG_OUTPUT_SATURATED  0x04
#define HRT_NON_PV_OUT_OF_LIMITS     0x02
#define HRT_PV_OUT_OF_LIMITS         0x01

/*
 * Standard response codes
 */
#define HRT_RSPCODE_DEV_BUSY           0x20
#define HRT_RSPCODE_CMD_NOT_IMPL       0x40
#define HRT_RSPCODE_OK                 0x00

/*
 * Error code flags
 */
#define HRT_ERR_BUFFER_OVERFLOW      0x82
#define HRT_ERR_LONG_PARITY          0x88
#define HRT_ERR_FRAMING              0x90
#define HRT_ERR_OVERRUN              0xa0
#define HRT_ERR_VERT_PARITY          0xc0
#define HRT_ERR_SUMMARY              0x80
#define HRT_ERR_NONE                 0x00

/*
 * Bit definitions for special HART stati
 */
#define HRT_CONF_CHANGED    0x10
#define HRT_OUTPUT_FIX      0x01

/*
 * For the preambles
 */
#define HRT_MIN_NUMBER_PREAMBLES_HART_MODE   5

/*
 * Default frame sizes
 */
#define HRT_DEFAULT_SHORT_FRAME_LEN   7
#define HRT_DEFAULT_LONG_FRAME_LEN   11 

/*
 * Buffer sizes
 */
#define HRT_MAX_DATA_BUFFER     27
#define HRT_MAX_IO_BUFFER       HRT_MAX_DATA_BUFFER + 29
  /* 20 Preambles,Del,UniqueID,CMD,Len,Data,Chk == 29 */

#define HRT_MAX_NUMBER_PREAMBLES_HART_MODE 20
#define HRT_OFFSET_PREAMBLES_HART_MODE      2

/**********************************************************************
 * Enum declarators                                               *
 **********************************************************************/
/*
 * Application handshake
 */
#define HRT_APP_IDLE   (BYTE) 0  /* nothing to do */
#define HRT_APP_BUSY   (BYTE) 1  /* application has to do it */
#define HRT_APP_DONE   (BYTE) 2  /* response to be send */
#define HRT_APP_LOCKED (BYTE) 3  /* response done receiver locked
                             note: response not really sent */

/*
 * I/O state machine states
 */
#define HRT_IO_IDLE            0 /* nothing to do */
#define HRT_WAIT_NEXT_PREAMBLE 1 /* Wait for second preamble */
#define HRT_RCV_WAIT_DEL       2 /* waiting for a delimiter */
#define HRT_RCV_READING        3 /* reading frame */
#define HRT_SEND_PENDING       4 /* about to send data */
#define HRT_SENDING            5 /* sending data */
#define HRT_IO_LOCKED          6 /* IO completely locked by application */

/*
 * Time out states
 */
#define HRT_TO_STATE_NONE              0
#define HRT_TO_STATE_GAP               1
#define HRT_TO_STATE_WAIT_APP          2
#define HRT_TO_STATE_WAIT_RTS_ON       3
#define HRT_TO_STATE_WAIT_RTS_OFF      4

/*
 * Standard response codes
 */
#define HRT_CMD_NOT_IMPL       64
#define HRT_IN_WR_PROT_MODE     7
#define HRT_TOO_FEW_DATA_BYTES  5
#define HRT_INVALID_SELECTION   2

/**********************************************************************
 * Structure definitions                                              *
 **********************************************************************/
/*
 * I/O state machine
 */
typedef struct tagComSM
{ BYTE byState;
  BYTE byCount;
  BYTE byMasterType;
  BYTE byPollCount;
  BYTE byCmd;
  BOOL bAddrErr;
  BYTE byErr;
  BYTE byErrPos;
  BYTE byPosRsp1;
  BYTE byValidPreambs;
  BYTE byData[HRT_MAX_IO_BUFFER];
} T_COM_SM;

/*
 * Application interface
 */
typedef struct tagAppIntf
{ BYTE bySrvID;
  BYTE byDataLen;
  BYTE byData[HRT_MAX_DATA_BUFFER];
  BYTE byStatus;
  BYTE byMasterType;
  BOOL bWriteProtected;
  BYTE byResponse1;
  BYTE byResponse2;
  BYTE byColdStartFlag;
} T_APP_INTF;

