/*
 * AppDebugLog.c
 *
 *  Created on: Nov 7, 2025
 *      Author: tummalanr
 */





#include "FreeRTOS.h"
#include "task.h"
#include </kernel/dpl/CycleCounterP.h>

#include "AppGlobal.h"
#include "BoardInitExtern.h"


char* GetTaskState(uint8_t taskstate)
{
    switch(taskstate)
    {
        case    0u:
                    return("Running");
        case    1u:
                    return("Ready");

        case    2u:
                    return("Blocked");

        case    3u:
                    return("Suspended");

        case    4u:
                    return("Deleted");

        case    5u:
                    return("Invalid");

        default:
                    return("Unknown");
    }
}


void Display_TaskInfo(TaskHandle_t taskhndl, uint8_t alltasks)
{
        //uint8_t tskno = 0u;
        TaskStatus_t xTaskDetails;

        if(alltasks == 0u)
        {
                /* Check the handle is not NULL. */
                configASSERT( taskhndl );

                /* Use the handle to obtain further information about the task. */
                vTaskGetInfo( /* The handle of the task being queried. */
                             taskhndl,
                            /* The TaskStatus_t structure to complete with information
                               on xTask. */
                            &xTaskDetails,
                            /* Include the stack high water mark value in the
                               TaskStatus_t structure. */
                            pdTRUE,
                            /* Include the task state in the TaskStatus_t structure. */
                            eInvalid );

                // Print task details
                #ifdef Debug_Verbose
                    DebugP_log("Priority    FreeStack    SatckBase        State        Name \r\n");
                    DebugP_log("%u          %u             %#x                %s           %s\r\n", xTaskDetails.uxCurrentPriority, xTaskDetails.usStackHighWaterMark,xTaskDetails.pxStackBase,GetTaskState(xTaskDetails.eCurrentState), xTaskDetails.pcTaskName);

                    /*DebugP_log("Task Name: %s\r\n", xTaskDetails.pcTaskName);
                    DebugP_log("Task Priority: %u\r\n", xTaskDetails.uxCurrentPriority);
                    DebugP_log("Task State: %d\r\n", xTaskDetails.eCurrentState);
                    DebugP_log("Free Stack Space: %u\n", xTaskDetails.usStackHighWaterMark);*/
                #endif
        }// if(alltasks == 0u)
        else
        {

            char Buf[1000U];
              vTaskList(Buf);
              DebugP_log("%s\r\n", Buf);

              vTaskGetRunTimeStatistics(Buf,1000u);
              DebugP_log("%s\r\n", Buf);

        }//else
}


/*!
 *  <!-- Description: -->
 *
 *  \brief
  * App_errorHandler function.
  * prints the  Error Code
  *
  * @param error code - code value.
  * @retval None
  */
void App_errorHandler(AppRET_T errorCode)
{
    #ifdef Debug_Verbose
        DebugP_log("Error..%d\r\n",errorCode);
    #endif

    while(1)
    {
        (void)errorCode;
    };

}

void StartRuntimeCpuCycles(uint32_t *cycleCount)
{
    CycleCounterP_reset(); /* enable and reset CPU cycle counter */
    *cycleCount = CycleCounterP_getCount32(); /* get CPU cycle count */

}

void EndRuntimeCpuCycles(uint32_t cycleCount)
{
    cycleCount = CycleCounterP_getCount32() - cycleCount; /* get CPU cycle count and calculate diff, we dont expect any overflow for this short duration */
    odrdupdtcpucyclcnt[odrdupdtcpucyclcntindx++] = cycleCount;
    if(odrdupdtcpucyclcntindx >= 1000u)
        odrdupdtcpucyclcntindx = 0u;

}
