/******************************************************************************/
/* Williams Advanced Engineering Limited                     Copyright(C) 2021*/
/* Grove                                                  All rights reserved */
/*                                                                            */
/******************************************************************************/


#include "NVM.h"
#include "MemIf.h"
#include "MemIf_Cfg.h"
#include "FBL_DataM.h"
#include "FBL_DataMCfg.h"


// Removing of compiler warnings and errors because of type re-declaration between hal_stdtypes.h and Std_Types.h
#define _BOOLEAN_DECLARED
#define _UINT8_DECLARED
#define _UINT16_DECLARED
#define _UINT32_DECLARED
#define _UINT64_DECLARED
#define _SINT8_DECLARED
#define _SINT16_DECLARED
#define _SINT32_DECLARED
#define _SINT64_DECLARED
#define _FLOAT32_DECLARED
#define _FLOAT64_DECLARED
#define _STD_RETURN_TYPE_DECLARED
#define _STDVERSION_DECLARED


#include "Ios_Reset.h"
#include "Ios_IntrTimerManagement.h"
//#include "ti_fee.h"

#ifdef RELEASE_BUILD
    #ifdef ECU_RESET_REMOVED_FOR_DEBUG
    #undef ECU_RESET_REMOVED_FOR_DEBUG
    #endif
#else
#define ECU_RESET_REMOVED_FOR_DEBUG 1
#endif

volatile boolean bResetRequest = FALSE;


void fnIosSetResetRequest(void)
{
    bResetRequest = TRUE;
}

void fnIosResetResetRequest(void)
{
    bResetRequest = FALSE;
}


boolean fnIosGetResetRequest(void)
{
    return(bResetRequest == TRUE);
}

void fnIosResetMcu(void)
{
    uint32 ulResetReg = systemREG1->SYSECR;

    ulResetReg |= (0x01 << 15);

    systemREG1->SYSECR = ulResetReg;
}


void fnIosResetRequestMngr(void)
{
    NvM_Rb_StatusType status_NvM;
    MemIf_StatusType stMemIf_en;

    //    NvM_Rb_StatusType NvM_WriteAllStatusflag;
    //    TI_FeeModuleStatusType Status = IDLE;

    //wcu_printf("Reset rqst : %x\n", fnIosGetResetRequest());
    if( fnIosGetResetRequest() == TRUE )
    {

#ifdef ECU_RESET_REMOVED_FOR_DEBUG
        /* Keep counter running otherwise task resetting WDG never triggered */
#else
        // Stop counters and save counter values in NVM
        rtiStopCounter(rtiCOUNTER_BLOCK0);
        rtiStopCounter(rtiCOUNTER_BLOCK1);
#endif
        fnIosSaveRTICountersToNVM();

        /* Non-volatile memory write */
        //        do
        //        {
        //            /* Execute */
        //            Fbl_DataM_MainFunction();
        //            NvM_MainFunction();
        //            TI_Fee_MainFunction();
        //            //(void)MemIf_Rb_MainFunction();
        //            (void)NvM_Rb_GetStatus(&NvM_WriteAllStatusflag);
        //            Status = (TI_FeeModuleStatusType)MemIf_GetStatus(0);
        //        } while ((NvM_WriteAllStatusflag == NVM_RB_STATUS_BUSY)|| (Status != IDLE) || (Fbl_DataM_NvmAllBlocksArePersisted() == FALSE));

        NvM_WriteAll();

        do
        {
            Fbl_DataM_MainFunction();
            NvM_MainFunction();
            MemIf_Rb_MainFunction();

            NvM_Rb_GetStatus(&status_NvM);
            stMemIf_en = MemIf_Rb_GetStatus();
            dwdReset();

        } while ( (status_NvM == NVM_RB_STATUS_BUSY ) || (stMemIf_en == MEMIF_BUSY) || (Fbl_DataM_NvmAllBlocksArePersisted() == FALSE));

#ifdef ECU_RESET_REMOVED_FOR_DEBUG
        //Do not perform reset - but reset the request as if it was done
        fnIosResetResetRequest();
#else
        // Perform reset
        while(1)
        {
            fnIosResetResetRequest();
            //Mcu_PerformReset();
            fnIosResetMcu();
        }
#endif
    }
    else
    {
        // Do nothing;
    }
}

