


#include "sys_common.h"
#include "reg_system.h"
#include "sci.h"
#include "spi.h"
#include "rti.h"
#include "stdio.h"
#include "types.h"
//#include "drv.h"

#ifndef ECMP_H_
#define ECMP_H_
/******************************************************************************
 *                           The Defines
 *****************************************************************************/

#define RD_DEV_ID             0x0600U   //Device ID module register
#define RD_DEV_REV            0x0C00U   //Device Revision module register
#define RD_SAFETY_STAT1       0x2400U   //SAFETY_STAT1 module register
#define RD_SAFETY_STAT2       0xC500U   //SAFETY_STAT2 module register
#define RD_SAFETY_STAT3       0xA300U   //SAFETY_STAT3 module register
#define RD_SAFETY_STAT4       0xA500U   //SAFETY_STAT4 module register
#define RD_SAFETY_STAT5       0xC000U   //SAFETY_STAT5 module register
#define RD_SAFETY_ERR_CFG     0x3000U   //SAFETY_ERR_CFG module register
#define RD_SAFETY_ERR_STAT    0xAA00U   //SAFETY_ERR_STAT module register
#define RD_SAFETY_PWD_THR_CFG 0x3900U   //SAFETY_PWD_THR_CFG module register

#define RD_SAFETY_CHECK_CTRL  0x4400U
#define RD_SAFETY_BIST_CTRL   0x3C00U
#define RD_WD_OPEN_WIN_CFG    0x2E00U
#define RD_WD_CLOSE_WIN_CFG   0x0500U
#define RD_WDT_TOKEN          0x3600U
#define RD_WDT_STATUS         0x4E00U
#define RD_DEV_STAT           0x1100U
#define RD_VMON_STAT_1        0x1200U
#define RD_VMON_STAT_2        0xA600U
#define RD_SENS_CTRL          0x5600U
#define RD_SAFETY_FUNC_CFG    0x3A00U
#define RD_SAFE_CFG_CRC       0x5A00U
#define RD_DIAG_CFG_CTRL      0xDD00U
#define RD_DIAG_MUX_SEL       0xAC00U
#define RD_SAFETY_ERR_PWM_H   0xD700U
#define RD_SAFETY_ERR_PWM_L   0x5900U
#define RD_WDT_TOKEN_FDBCK    0x7800U

//write definitions
#define SW_UNLOCK             0xBBU //0xBBAAU
#define SW_LOCK               0xBDU //0xBD55U
#define WR_CAN_STBY           0x7DU
#define WR_ERR_CFG            0xDBU
#define WR_ERR_STAT           0xA9U
#define WR_PWD_CFG            0x99U
#define WR_CHK_CTRL           0x93U
#define WR_BIST_CTRL          0x9FU
#define WR_OPEN_WD            0xEDU
#define WR_CLOSE_WD           0x09U
#define WR_ANSWR              0xE1U
#define WR_SENS               0x7BU
#define WR_FUNC_CFG           0x35U
#define WR_CRC                0x63U
#define WR_DIAG_CFG           0xCCU
#define WR_MUX                0xC9U
#define WR_PWM_H              0xD8U
#define WR_PWM_L              0x7EU
#define WR_TOKEN              0x77U

#define SCITXREADY (scilinREG->FLR & SCI_TX_INT)
#define SCIRXREADY (scilinREG->FLR & SCI_RX_INT)
#define SCITD (scilinREG->TD)
#define SCIRD (scilinREG->RD)



/******************************************************************************
 *                           The Typedefs
 *****************************************************************************/
/** @enum WDWindow_t
*   @brief WD window status
*/
typedef enum
{
    ECMP_WINDOW_OPEN,
    ECMP_WINDOW_CLOSE
} WDWindow_t;

/** @enum ecmpIsInActiveState_t
*   @brief ecmp state
*/
typedef enum
{
    ECMP_INACTIVE,
    ECMP_ENADRV,
    ECMP_SETWATD,
    ECMP_CLRERR,
    ECMP_ACTIVE
} ecmpIsInActiveState_t;


/** @enum command_t
*   @brief for command line branches
*/
typedef enum
{
    ECMP_CLEAR,
    ECMP_ERRORPIN,
    ECMP_ERRORPININIT,
    ECMP_READ,
    ECMP_WRITE,
    ECMP_WDOPCL,
    ECMP_WDQA,
    ECMP_WDOC,
    ECMP_HELP,
    ECMP_STATE,
    ECMP_WDTFB,
    ECMP_TEST,
    ECMP_WRITEACCESS,
    ECMP_ESM,
    ECMP_GOTOACTIVESTATE,
    ECMP_UNKNOWN
} command_t;

/** @enum command_t
*   @brief used to store variables - to be modified
*/
typedef struct
{
	command_t command;
	command_t secondcommand;
	command_t thirdcommand;
	command_t storage;
} commandline_t;


/******************************************************************************
 *                           The Structs
 *****************************************************************************/
typedef struct
{
//	uint16 ecmpReadRegAlias[27];
//	char8 **ecmpReg;
//	uint16 ecmpModifyRegAlias1[15];
//	uint16 ecmpModifyRegAlias2[15];
//	uint16 EcmpRegisterValues[27 / sizeof(uint16)];
//	uint32 ecmRegisterArrayLength;
//	uint32 ecmpRegisterLength;
//	WDWindow_t wdWindow;
//	uint32 trgPulseOpen;
//	uint32 trgPulseClose;
//	ecmpIsInActiveState_t ecmpIsInActiveState;
//	uint32 conversion;
//	uint16 SPI2_RXDATA;
//	sint32 SP2_Complete;
//	sint32 Err_Count;
//	sint32 Curr_Sta;
//	uint32 k;
//	uint32 index;
//	uint8 value;
//	uint8 cnt;
//	uint16 answer[8];
//	uint32 token;
//	uint32 tOpen;
//	uint32 tClose;
//	uint8 dataStr[80];
//	commandline_t commandlinememory;
//	sint8 str[128];
//	sint16 length;
//	va_list argList;
//	uint8 c_get;
	uint8 DiagFlag;
} ECMP_Obj,*ECMP_handle;

//typedef ECMP_Obj *ECMP_handle;

/******************************************************************************
 *                           The Functions
 *****************************************************************************/
uint8 GetChar(void *pMemory);
void PrintStr(void *pMemory, char8 *text);
uint16 PrintMenu(void *pMemory);
ECMP_handle ECMPInit(void *pMemory, const size_t numBytes);
void ECMPSetup(void *pMemory);
void MonitorClock();
uint16 ecmpStayinDIAGState(void *pMemory);
uint32 ecmpModifyRegister(void *pMemory, uint16 regname, uint8 regbits, uint8 setbit);
uint16 ecmpReadRegister(void *pMemory, uint16 regname);
void ecmpSetRegister(void *pMemory, uint16 regname, uint16 regbits);
uint16 ecmpGetWriteAccess(void *pMemory);
void ecmpWdStart(void *pMemory);
void ecmpWdQaSendAnswer(void *pMemory, uint8 configure, uint32 openwin, uint32 closewin);
void ecmpWdQaInit(void *pMemory, uint32_t Topen, uint32_t Tclose);
void ecmpWdQaSetOpenWinAnswr(void *pMemory);
void ecmpWdQaSetCloseWinAnswr(void *pMemory);
uint16 ecmpGetWdToken(void *pMemory);
uint16 ecmpWdIsTimeOut(void *pMemory);
uint16 ecmpCalcWdAnswr(void *pMemory, uint16 token);
uint8 ecmpGetWdAnswrCnt(void *pMemory);
uint8 ecmpWdFailCount(void *pMemory);



#endif /* ECMP_H_ */
