/*
 *  Copyright (C) 2022-2025 Texas Instruments Incorporated
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *    Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 *    Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the
 *    distribution.
 *
 *    Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

 /**
 *  \file     rti_app_main.c
 *
 *  \brief    This file contains RTI Example test code.
 *
 *  \details  RTI tests
 **/

/*===========================================================================*/
/*                         Include files                                     */
/*===========================================================================*/
#include "rti_app_main.h"
#include "dpl_interface.h"
#include <drivers/sciclient.h>
#include <sdl/sdl_esm.h>
#include <sdl/sdl_rti.h>
#include "ti_drivers_open_close.h"
#include "ti_board_open_close.h"
#include<drivers/soc/am62ax/soc.h>
/*===========================================================================*/
/*                         Declarations                                      */
/*===========================================================================*/
/* None */

/*===========================================================================*/
/*                         Macros                                            */
/*===========================================================================*/
/* None */

/*===========================================================================*/
/*                         Internal function declarations                    */
/*===========================================================================*/
void test_sdl_rti_baremetal_test_app (void);
/*===========================================================================*/
/*                         Global Variables                                  */
/*===========================================================================*/
sdlRtiTest_t  sdlRtiTestList[] = {
    {SDL_RTI_exampleTest, "RTI EXAMPLE TEST" ,     SDL_APP_TEST_NOT_RUN },
    {NULL,             "TERMINATING CONDITION",  SDL_APP_TEST_NOT_RUN }
};

#if defined (SOC_AM62X)
#if defined (M4F_CORE)
SDL_ESM_config RTI_Test_esmInitConfig_wkup =
{
    .esmErrorConfig = {0u, 3u}, /* Self test error config */
    .enableBitmap = {0xffffffffu, 0xff0fffffu, 0x7fffffffu, 0x00000007u,
                },
     /**< All events enable: except timer and self test  events, and Main ESM output */
    /* Temporarily disabling vim compare error as well*/
    .priorityBitmap = {0xffffffffu, 0xff0fffffu, 0x7fffffffu, 0x00000007u,
                        },
    /**< All events high priority: except timer, selftest error events, and Main ESM output */
    .errorpinBitmap = {0xffffffffu, 0xff0fffffu, 0x7fffffffu, 0x00000007u,
                      },
    /**< All events high priority: except timer, selftest error events, and Main ESM output */
};
#endif
#if defined (R5F_CORE)
static SDL_ESM_config RTI_Test_esmInitConfig_Main =
{
 .esmErrorConfig = {0u, 3u}, /* Self test error config */
 .enableBitmap = {0x00000000u, 0x000000e0u, 0x00000000u, 0x00000000u,
                  0x00000000u, 0x00000008u,
                 },
      /**< All events enable: except timer and self test  events, */
     /*    and Main ESM output.Configured based off esmErrorConfig to test high or low priorty events.*/
 .priorityBitmap = {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u,
                    0x00000000u, 0x00000008u,
                   },
     /**< Configured based off esmErrorConfig to test high or low priorty events. */
  .errorpinBitmap = {0x00000000u, 0x000000e0u, 0x00000000u, 0x00000000u,
                     0x00000000u, 0x00000008u,
                    },
    /**< All events high priority:  */
};
#endif
#endif
#if defined (SOC_AM62AX) || defined (SOC_AM62PX) || defined (SOC_AM62DX) || defined (SOC_J722S)
static SDL_ESM_config RTI_Test_esmInitConfig_wkup =
{
    .esmErrorConfig = {0u, 3u}, /* Self test error config */
	.enableBitmap = {0x00000007u, 0x00000000u, 0x00200000u, 0x00000000u,
					  0x00000000u, 0x00000000u,
					 },
	/**< All events enable: except timer and self test  events, */
	/*    and Main ESM output.Configured based off esmErrorConfig to test high or low priorty events.*/
	.priorityBitmap = {0x00000007u, 0x00000000u, 0x00200000u, 0x00000000u,
						0x00000000u, 0x00000000u,
					   },
	/**< Configured based off esmErrorConfig to test high or low priorty events. */
	.errorpinBitmap = {0x00000007u, 0x00000000u, 0x00200000u, 0x00000000u,
						 0x00000000u, 0x00000000u,
						},
	/**< All events high priority:  */
};
#endif
#if defined (SOC_AM275X)
static SDL_ESM_config RTI_Test_esmInitConfig_wkup =
{
    .esmErrorConfig = {0u, 3u}, /* Self test error config */
	.enableBitmap = {0x00000007u, 0x00000000u, 0x00400000u, 0x00000000u,
					  0x00000000u, 0x00000000u,
					 },
	/**< All events enable: except timer and self test  events, */
	/*    and Main ESM output.Configured based off esmErrorConfig to test high or low priorty events.*/
	.priorityBitmap = {0x00000007u, 0x00000000u, 0x00400000u, 0x00000000u,
						0x00000000u, 0x00000000u,
					   },
	/**< Configured based off esmErrorConfig to test high or low priorty events. */
	.errorpinBitmap = {0x00000007u, 0x00000000u, 0x00400000u, 0x00000000u,
						 0x00000000u, 0x00000000u,
						},
	/**< All events high priority:  */
};
#endif

extern int32_t SDL_ESM_applicationCallbackFunction(SDL_ESM_Inst esmInstType,
                                                   SDL_ESM_IntType esmIntType,
                                                   uint32_t grpChannel,
                                                   uint32_t index,
                                                   uint32_t intSrc,
                                                   void *arg);
static uint32_t arg;
/*===========================================================================*/
/*                   Local Function definitions                              */
/*===========================================================================*/

#define RTI_NUM_DEVICES 1
uint32_t RTI_devices[RTI_NUM_DEVICES] =
{
#if defined (SOC_AM62X) || defined (SOC_AM62AX) || defined (SOC_AM62PX) || defined (SOC_AM62DX) || defined (SOC_J722S)
    TISCI_DEV_MCU_RTI0,
#elif defined (SOC_AM275X)
    TISCI_DEV_WKUP_RTI0,
#endif
};

static int32_t sdlApp_initRTI(void)
{
    int32_t status = SDL_PASS;
    uint32_t i;

    for (i = 0; i < RTI_NUM_DEVICES; i++)
    {
        /* Power up RTI */
        status = Sciclient_pmSetModuleState(RTI_devices[i],
                                            TISCI_MSG_VALUE_DEVICE_SW_STATE_ON,
                                            TISCI_MSG_FLAG_AOP,
                                            SystemP_WAIT_FOREVER);

        if (status != SDL_PASS)
        {
            DebugP_log("   RTI Sciclient_pmSetModuleState 0x%x ...FAILED: retValue %d\r\n",
                        RTI_devices[i], status);
        }
    }

    return status;
}


static int32_t sdlApp_dplInit(void)
{
    SDL_ErrType_t ret = SDL_PASS;

    ret = SDL_TEST_dplInit();
    if (ret != SDL_PASS)
    {
        DebugP_log("Error: Init Failed\r\n");
    }

    return ret;
}

/*===========================================================================*/
/*                         Function definitions                              */
/*===========================================================================*/

void test_sdl_rti_baremetal_test_app (void)
{
    /* Declarations of variables */
    int32_t    testResult = SDL_APP_TEST_PASS;
    int32_t    i, result;
    void *ptr = (void *)&arg;
    DebugP_log("\r\n RTI Example Test Application\r\n");

	/* Init dpl */
    sdlApp_dplInit();
    /* Init the RTI modules */
    sdlApp_initRTI();
#if defined (SOC_AM62X)
#if defined (M4F_CORE)
    /* Initialize WKUP ESM module */
    result = SDL_ESM_init(SDL_ESM_INST_WKUP_ESM0, &RTI_Test_esmInitConfig_wkup, SDL_ESM_applicationCallbackFunction, ptr);
#endif
#if defined (R5F_CORE)
	/* Initialize MAIN ESM module */
    result = SDL_ESM_init(SDL_ESM_INST_MAIN_ESM0, &RTI_Test_esmInitConfig_Main, SDL_ESM_applicationCallbackFunction, ptr);
#endif
#endif
#if defined (SOC_AM62AX) || defined (SOC_AM62PX) || defined (SOC_AM62DX) || defined (SOC_AM275X) || defined (SOC_J722S)
	/* Initialize MAIN ESM module */
    result = SDL_ESM_init(SDL_ESM_INST_WKUP_ESM0, &RTI_Test_esmInitConfig_wkup, SDL_ESM_applicationCallbackFunction, ptr);
#endif
    if (result != SDL_PASS)
    {
        /* print error and quit */
#if defined (SOC_AM62X)
#if defined (M4F_CORE)
        DebugP_log("RTI_Test_init: Error initializing WKUP ESM: result = %d\r\n", result);
#endif
#if defined (R5F_CORE)
		DebugP_log("RTI_Test_init: Error initializing MAIN ESM: result = %d\r\n", result);
#endif
#endif
#if defined (SOC_AM62AX) || defined (SOC_AM62PX) || defined (SOC_AM62DX) || defined (SOC_AM275X) || defined (SOC_J722S)
		DebugP_log("RTI_Test_init: Error initializing WKUP ESM: result = %d\r\n", result);
#endif
	}
    else
    {
#if defined (SOC_AM62X)
#if defined (M4F_CORE)
        DebugP_log("\r\nRTI_Test_init: Init WKUP ESM complete \r\n\r\n");
#endif
#if defined (R5F_CORE)
		DebugP_log("\r\nRTI_Test_init: Init MAIN ESM complete \r\n\r\n");
#endif
#endif
#if defined (SOC_AM62AX) || defined (SOC_AM62PX) || defined (SOC_AM62DX) || defined (SOC_AM275X) || defined (SOC_J722S)
		DebugP_log("\r\nRTI_Test_init: Init WKUP ESM complete \r\n\r\n");
#endif

    }

    for ( i = 0; sdlRtiTestList[i].testFunction != NULL; i++)
    {
        testResult = sdlRtiTestList[i].testFunction();
        sdlRtiTestList[i].testStatus = testResult;
    }

    testResult = SDL_APP_TEST_PASS;
    for ( i = 0; sdlRtiTestList[i].testFunction != NULL; i++)
    {
        if (sdlRtiTestList[i].testStatus != SDL_APP_TEST_PASS)
        {
            DebugP_log("Test Name: %s  FAILED \r\n", sdlRtiTestList[i].name);
            testResult = SDL_APP_TEST_FAILED;
            break;
        }
        else
        {
            DebugP_log("Test Name: %s  PASSED \r\n", sdlRtiTestList[i].name);
        }
    }

    if (testResult == SDL_APP_TEST_PASS)
    {
        DebugP_log("\r\n All tests have passed. \r\n");
    }
    else
    {
        DebugP_log("\r\n Few/all tests Failed \r\n");
    }
}
static void McuEsm_enableResetSignal(void)
{
    /* Unlock partition 6 of MCU domain (MCU_RTI0 routes to WKUP_ESM0, need MCU_CTRL_MMR config) */
    SOC_unlockAllMMR();  

    /* Read CTRLMMR_MCU_RST_CTRL register */
    uint32_t stat = HW_RD_REG32(CSL_MCU_CTRL_MMR0_CFG0_BASE + CSL_MCU_CTRL_MMR_CFG0_RST_CTRL);

    /* Set MCU_ESM_ERROR_RST_EN_Z [BIT 17] to '0' (low active) to enable SoC reset on MCU ESM error */
    stat &= ~(CSL_MCU_CTRL_MMR_CFG0_RST_CTRL_MCU_ESM_ERROR_RST_EN_Z_MASK);

    /* Write back CTRLMMR_MCU_RST_CTRL register */
    HW_WR_REG32(CSL_MCU_CTRL_MMR0_CFG0_BASE + CSL_MCU_CTRL_MMR_CFG0_RST_CTRL, stat);

}

int32_t sdl_rti_example_uc1_main(void)
{
    McuEsm_enableResetSignal();
    test_sdl_rti_baremetal_test_app();

    /* Stop the test and wait here */
    return 0;
}

/* Nothing past this point */
