/**
*   @file    FS26_Task.h
*   @version 1.0.0
*
*   @brief   FS26 task init and runtime
*   @details The FS26 task includes the following:
*            1) Initialize FS26
*            2) Poll FS26 status
*
*   This example uses S32K3 RTD 2.0.0 and FS26 SBC Autosar 4.4 version 1.0.0
*   Note:
*   The FS26 configuration in "CDD_Sbc_fs26_BOARD_InitPeripherals_PBcfg.c" sets the WD window as infinite.
*   This register value is written into FS26 during FS26 init state.
*   So that the WD refresh can be performed any time to decrement the fault counter to zero.
*   After basic FS26 initialization is done, the WD window can be changed to desired value.
*
*   This file contains sample code only. It is not part of the production code deliverables.
*
*   @addtogroup FS26_Task
*   @{
*/

#ifdef __cplusplus
extern "C" {
#endif

#ifndef FS26_TASK_FS26_TASK_H_
#define FS26_TASK_FS26_TASK_H_

/*==================================================================================================
*                                        INCLUDE FILES
* 1) system and project includes
* 2) needed interfaces from external units
* 3) internal and external interfaces from this unit
==================================================================================================*/
#include "Mcal.h"
#include "CDD_Sbc_fs26.h"

/*==================================================================================================
*                              SOURCE FILE VERSION INFORMATION
==================================================================================================*/

/*==================================================================================================
*                                     FILE VERSION CHECKS
==================================================================================================*/

/*==================================================================================================
*                                          CONSTANTS
==================================================================================================*/

/*==================================================================================================
*                                      DEFINES AND MACROS
==================================================================================================*/
/* MISO general device status bits definition - bit31~bit24 in a read/write response */
#define GS_FS_G_MASK	0x20
#define GS_COM_G_MASK	0x10
#define GS_WIO_G_MASK	0x08
#define GS_VSUP_G_MASK	0x04
#define GS_REG_G_MASK	0x02
#define GS_TSD_G_MASK	0x01

/* MACRO to enable STANDBY mode test */
#define ENABLE_STANDBY_MODE_TEST	1
/* MACRO to enable LPOFF mode test */
#define ENABLE_LPOFF_MODE_TEST		0
/* MACRO to enable MCU FCCU EOUT signal to SBC FCCU monitor. */
#define ENABLE_FCCU_MON_TEST		0

/****************************************************************************
 * The FS26 initialization configuration disables WD window. (WD refresh can be performed at anytime)
 * The following MACRO is used to configure and enable the FS26 WD time window after basic initialization.
 * It means WD period 128mS and WD window is 50%. The WD recovery time is 256mS.
 ****************************************************************************/
#define FS26_WD_CONFIGURATION (SBC_FS26_FS_WDW_RECOVERY_256MS|SBC_FS26_FS_WDW_DC_50C_50O|SBC_FS26_FS_WDW_PERIOD_128MS)

/****************************************************************************
 * The following MACRO is used to configure FS26 wakeup sources
 * WAKE1, LDT timer and CSB inputs are enabled for wakeup sources.
 * Wakeup signal polarity are all 0, meaning high voltage wakes FS26.
 ****************************************************************************/
#define FS26_WAKEUP_SOURCE_CFG (SBC_FS26_M_WK1WUEN_MASK|SBC_FS26_M_LDTWUEN_MASK|SBC_FS26_M_CSBWUEN_MASK)


/*==================================================================================================
*                                             ENUMS
==================================================================================================*/

/*==================================================================================================
*                                STRUCTURES AND OTHER TYPEDEFS
==================================================================================================*/
typedef struct
{
	uint16 Sbc_DeviceId;
    uint16 Sbc_ProgId;
    uint16 Sbc_Status;
	uint16 Sbc_Tsd_Flg;
    uint16 Sbc_Tsd_Msk;
    uint16 Sbc_Reg_Flg;
    uint16 Sbc_Reg_Msk;
    uint16 Sbc_Vsup_Flg;
    uint16 Sbc_Vsup_Msk;
    uint16 Sbc_Wio_Flg;
    uint16 Sbc_Wio_Msk;
    uint16 Sbc_Com_Flg;
    uint16 Sbc_Com_Msk;
    uint16 Sbc_Sys_Cfg;
    uint16 Sbc_Tsd_Cfg;
    uint16 Sbc_Reg_Cfg;
    uint16 Sbc_Wio_Cfg;
    uint16 Sbc_Reg_Ctrl1;
    uint16 Sbc_Reg_Ctrl2;
    uint16 Sbc_Amux_Ctrl;
    uint16 Sbc_Ldt_Cfgl;
    uint16 Sbc_Ldt_Cfg2;
    uint16 Sbc_Ldt_Cfg3;
    uint16 Sbc_Ldt_Ctrl;
    uint16 Sbc_Memory0;
    uint16 Sbc_Memory1;
} Sbc_Main_Registers_Struct;

typedef struct
{
	uint16 Sbc_Grl_Flags;
    uint16 Sbc_I_Ovuv_Safe_Reaction1;
    uint16 Sbc_I_Not_Ovuv_Safe_Reaction1;
	uint16 Sbc_I_Ovuv_Safe_Reaction2;
    uint16 Sbc_I_Not_Ovuv_Safe_Reaction2;
    uint16 Sbc_I_Wd_Cfg;
    uint16 Sbc_I_Not_Wd_Cfg;
    uint16 Sbc_I_Safe_Inputs;
    uint16 Sbc_I_Not_Safe_Inputs;
    uint16 Sbc_I_Fssm;
    uint16 Sbc_I_Not_Fssm;
    uint16 Sbc_Wdw_Duration;
    uint16 Sbc_Not_Wdw_Duration;
    uint16 Sbc_Wd_Answer;
    uint16 Sbc_Wd_Token;
    uint16 Sbc_Abist_On_Demand;
    uint16 Sbc_Ovuv_Reg_Status;
    uint16 Sbc_Release_Fs0b_Fs1b;
    uint16 Sbc_Safe_Ios_1;
    uint16 Sbc_Safe_Ios_2;
    uint16 Sbc_Diag_Safety1;
    uint16 Sbc_Diag_Safety2;
    uint16 Sbc_Intb_Mask;
    uint16 Sbc_States;
    uint16 Sbc_Lp_Req;
    uint16 Sbc_Ldt_Lpsel;
} Sbc_FS_Registers_Struct;

/*==================================================================================================
*                                GLOBAL VARIABLE DECLARATIONS
==================================================================================================*/
extern Sbc_Main_Registers_Struct Sbc_Main_Reg;
extern Sbc_FS_Registers_Struct Sbc_FS_Reg;
extern uint32_t Sbc_Int_Flag;
extern uint32_t Sbc_Wd_Flag;


/*==================================================================================================
*                                    FUNCTION PROTOTYPES
==================================================================================================*/

Std_ReturnType FS26_Task_Init(void);
Std_ReturnType FS26_Task_Runtime(void);

Std_ReturnType Sbc_Read_Main_Registers(void);
Std_ReturnType Sbc_Read_FS_Registers(void);

Std_ReturnType Sbc_Clear_Flags(void);
Std_ReturnType Sbc_Read_Interrupt_Flags(void);
Std_ReturnType Sbc_ReadAndClear_Interrupt_Flags(void);
void Sbc_Parse_Interrupt_Flags(void);

Std_ReturnType Sbc_User_Enter_Standby(void);
Std_ReturnType Sbc_User_Enter_Lpoff(void);

#endif /* FS26_TASK_FS26_TASK_H_ */

#ifdef __cplusplus
}
#endif

/** @} */
