/*
 * DataSynchInit.c
 *
 *  Created on: Nov 11, 2025
 *      Author: tummalanr
 */


#include <inttypes.h>
#include <kernel/dpl/AddrTranslateP.h>

#include "FreeRTOS.h"
#include "event_groups.h"

#include "SharedMemDataSynch.h"
#include "AppGlobal.h"
#include "SharedMemoryGlobalObjs.h"
#include "ShmDataSyncExterns.h"
#include "BoardInitExtern.h"
#include "CIPCoreExterns.h"
#include "AppExterns.h"



void memset_volatile(volatile void *s, char c, uint32_t n) {
  volatile char *p = s;
  while (n-- > 0) {
    *p++ = c;
  }
}

AppRET_T DataSync_ProcessInit(void)
{
        int32_t             status;


        #ifdef Debug_Verbose2
            DebugP_log("[[CAN] 1- %s [%" PRId64 " ms]\r\n",__func__,(ClockP_getTimeUsec()/1000u));
        #endif

        #ifdef Debug_Verbose2
            App_Struct_Union_Size();
        #endif

        /*Initialize the Shared Memory Objects*/
        memset_volatile(&system_discovery_status, 0x00, sizeof(system_discovery_status));

        /* Get address after translation translate*/
        spinlockBaseAddr = (uint32_t) AddrTranslateP_getLocalAddr(SPINLOCK_BASE_ADDR);

        /*Semaphore used in GetSpinLock for Accessing System Info Data*/
        status = SemaphoreP_constructMutex(&System_Info_mutexObj);
        DebugP_assert(status==SystemP_SUCCESS);

        /*Semaphore used in GetSpinLock for Accessing IO Modules Data*/
        status = SemaphoreP_constructMutex(&Cyclic_IOModules_mutexObj);
        DebugP_assert(status==SystemP_SUCCESS);
        status = SemaphoreP_constructMutex(&ACyclic_IOModules_mutexObj);
        DebugP_assert(status==SystemP_SUCCESS);

        /*Semaphore used in GetSpinLock for Accessing PZx ISEM-DIAG/DIAGVALVE/PROP Modules Data*/
        status = SemaphoreP_constructMutex(&Cyclic_PZx_mutexObj);
        DebugP_assert(status==SystemP_SUCCESS);
        status = SemaphoreP_constructMutex(&ACyclic_PZx_mutexObj);
        DebugP_assert(status==SystemP_SUCCESS);




        /* This task is created  for processing IPC to/from CIP for IO Modules System Configuration Parameters.*/
        gSystemInfoTask = xTaskCreateStatic( APP_SystemInfoTask,    /* Pointer to the function that implements the task. */
                                            "SystemInfoTask",       /* Text name for the task.  This is to facilitate debugging only. */
                                            SystemInfo_TASK_SIZE,   /* Stack depth in units of StackType_t typically uint32_t on 32b CPUs */
                                            NULL,                   /* We are not using the task parameter. */
                                            SystemInfo_TASK_PRI,    /* task priority, 0 is lowest priority, configMAX_PRIORITIES-1 is highest */
                                            gSystemInfoTaskStack,   /* pointer to stack base */
                                            &gSystemInfoTaskObj );  /* pointer to statically allocated task object memory */
        configASSERT(gSystemInfoTask != NULL);


        return AppRET_OK;
}
