/*
 * CustomInit.c
 *
 *  Created on: Apr 4, 2025
 *      Author: tummalanr
 */



#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdarg.h>
#include <kernel/dpl/ClockP.h>
#include <inttypes.h>
#include <kernel/dpl/DebugP.h>
#include <kernel/dpl/CpuIdP.h>
#include <drivers/hw_include/cslr_soc.h>
#include <ti_drivers_open_close.h>
#include <ti_board_open_close.h>

#include    "EepromAccessExtern.h"
#include    "SharedMemoryExterns.h"
#include    "IPCDataSyncExterns.h"


/**
 * MBN Post Error status (Both Power On self test and Init Sections).
 */
uint32_t    cipmbnposterr = 0u;

AppRET_T CheckRunningCoreId(void)
{
    CSL_ArmR5CPUInfo cpuInfo;

    /* Get Core ID Info.*/
    CSL_armR5GetCpuID(&cpuInfo);
    //DebugP_log("[CIP]Core Id :%d_%d\r\n",cpuInfo.grpId,cpuInfo.cpuID);
    if( (cpuInfo.grpId != (uint32_t)CSL_ARM_R5_CLUSTER_GROUP_ID_0 ) && (cpuInfo.cpuID == CSL_ARM_R5_CPU_ID_0) ) /* R5SS1-0 */
    {
        #ifdef Debug_Verbose
            DebugP_log("[CIP] Application Running On Wrong Core!!!\r\n");
        #endif

        return AppExecCore_Err;
    }

    return AppRET_OK;
}

/*!
 *  <!-- Description: -->
 *
 *  \brief
 *
 *   Application Process Initialization.
 *
 *  <!-- Parameters and return values: -->
 *  \param[in]  :   None
 *  \return     :      on success 0 otherwise Error.
 */
char Custom_App_Init(void)
{
    char        retsts = 1u;
    uint32_t    status;
    AppRET_T    sts = AppRET_OK;

//    Drivers_open();
//
//    status = Board_driversOpen();
//    DebugP_assert(status==SystemP_SUCCESS);

    //DebugP_log("%s [%" PRId64 " ms]\r\n",__func__, (ClockP_getTimeUsec()/1000u) );

    /*Check Core Id.*/
    sts = CheckRunningCoreId();
    if(sts != AppRET_OK) {
        return sts;
    }

    sts = App_ShrdMem_Init();
    if(sts != AppRET_OK) {
        retsts = 1u;
        cipmbnposterr |= MBN_CIPCore_DataSynchInitErr;
    }

    sts = App_EepromTemp_TaskInit();      //later remove from here already below
    if(sts != AppRET_OK) {
            retsts = 1u;
            cipmbnposterr |= MBN_CIPCore_EEPROMInitErr;
    }

    sts = App_CANCoreDataSync_Init();
    if(sts != AppRET_OK) {
            retsts = 1u;
            cipmbnposterr |= MBN_CIPCore_CANSynchInitErr;
    }



    return retsts;
}
