/**
*   @copyright       © 2026 Microchip Technology Inc.
    Microchip Technology Inc. and its subsidiaries. You may use this
    software and any derivatives exclusively with Microchip products.

    THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
    EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
    WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
    PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
    WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.

    IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
    INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
    WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
    BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
    FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
    ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
    THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.

    MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
    TERMS. 
*
*************************************************************************/

/**
 *  @file       diag_utility_timer.c
 *  @version    1.0
 *  @brief      This file has the implementation for the Timer Utility 
 *		        functions. @see diag_utility_timer.h
 *
 *************************************************************************/

#include <xc.h>
#include <stdint.h>
#include "diag_utility_timer.h"

/*LDRA_EXCLUDE_START 76 D <justification start> The utility functions to be called by the diagnostics or an application. Hence ignored <justification end> */
/*LDRA_EXCLUDE_START 61 D <justification start> The utility functions to be called by the diagnostics or an application. Hence ignored <justification end> */

/* Structures Declarations */
/*LDRA_EXCLUDE 27 D <justification start> variable is retained as global for its usage across files hence violation ignored <justification end> */
const DIAG_UTILITY_TIMER_DATA_LOOKUP timerDataLookup[DIAG_UTILITY_TIMER_TOTAL] =
                    {
                        {0x00001CE0U,0x00000008U,0x00000004U,0x00000000U,0x00000030U,0x00000004U,0x00000000U,0x00008000U,0x000000C4U,0x00010000U,0x00000094U,0x00010000U},//TIMER_TMR1
                        {0x00001CF0U,0x00000008U,0x00000004U,0x00000000U,0x00000030U,0x00000004U,0x00000000U,0x00008000U,0x000000C4U,0x00020000U,0x00000094U,0x00020000U},//TIMER_TMR2
                        {0x00001D00U,0x00000008U,0x00000004U,0x00000000U,0x00000030U,0x00000004U,0x00000000U,0x00008000U,0x000000C4U,0x00040000U,0x00000094U,0x00040000U},//TIMER_TMR3                                
                        {0x00001B00U,0x00000014U,0x00000010U,0x00000000U,0x000000C0U,0x00000006U,0x00000000U,0x00008000U,0x000000C4U,0x00080000U,0x00000094U,0x00080000U},//TIMER_SCCP1
                        {0x00001B30U,0x00000014U,0x00000010U,0x00000000U,0x000000C0U,0x00000006U,0x00000000U,0x00008000U,0x000000C4U,0x00200000U,0x00000094U,0x00200000U},//TIMER_SCCP2
                        {0x00001B60U,0x00000014U,0x00000010U,0x00000000U,0x000000C0U,0x00000006U,0x00000000U,0x00008000U,0x000000C4U,0x00800000U,0x00000094U,0x00800000U},//TIMER_SCCP3
                        {0x00001B90U,0x00000014U,0x00000010U,0x00000000U,0x000000C0U,0x00000006U,0x00000000U,0x00008000U,0x000000C4U,0x02000000U,0x00000094U,0x02000000U},//TIMER_SCCP4
                        {0x00001BC0U,0x00000014U,0x00000010U,0x00000000U,0x000000C0U,0x00000006U,0x00000000U,0x00008000U,0x000000CCU,0x40000000U,0x0000009CU,0x40000000U},//TIMER_SCCP5
                        {0x00001BF0U,0x00000014U,0x00000010U,0x00000000U,0x000000C0U,0x00000006U,0x00000000U,0x00008000U,0x000000D0U,0x00000001U,0x000000A0U,0x00000001U},//TIMER_SCCP6
                        {0x00001C20U,0x00000014U,0x00000010U,0x00000000U,0x000000C0U,0x00000006U,0x00000000U,0x00008000U,0x000000D0U,0x00000004U,0x000000A0U,0x00000004U},//TIMER_SCCP7
                        {0x00001C50U,0x00000014U,0x00000010U,0x00000000U,0x000000C0U,0x00000006U,0x00000000U,0x00008000U,0x000000D0U,0x00000010U,0x000000A0U,0x00000010U},//TIMER_SCCP8
                        {0x00001C80U,0x00000014U,0x00000010U,0x00000000U,0x000000C0U,0x00000006U,0x00000000U,0x00008000U,0x000000D0U,0x00000040U,0x000000A0U,0x00000040U},//TIMER_MCCP9                                
                    };

uint32_t DIAG_UTIL_TIMER_GetPeriodCount(DIAG_UTILITY_TIMER_INSTANCE tmrInstance)
{
    uint32_t baseAddress = timerDataLookup[tmrInstance].baseAddress;
    uint32_t offsetAddress = timerDataLookup[tmrInstance].periodOffsetAddress;

    /*LDRA_EXCLUDE 440 S <justification start> Accessing SFR address through pointers, hence violation ignored <justification end> */
    uint32_t *prdReg = (uint32_t *)(baseAddress + offsetAddress);

    return ((uint32_t)(*prdReg));
}

uint32_t DIAG_UTIL_TIMER_GetTMRValue(DIAG_UTILITY_TIMER_INSTANCE tmrInstance)
{
    uint32_t baseAddress = timerDataLookup[tmrInstance].baseAddress;
    uint32_t offsetAddress = timerDataLookup[tmrInstance].timerOffsetAddress;

    /*LDRA_EXCLUDE 440 S <justification start> Accessing SFR address through pointers, hence violation ignored <justification end> */
    uint32_t *tmrReg = (uint32_t *)(baseAddress + offsetAddress);

    return ((uint32_t)*tmrReg);
}

uint32_t DIAG_UTIL_TIMER_GetPrescaler(DIAG_UTILITY_TIMER_INSTANCE tmrInstance)
{
    
    uint32_t baseAddress = timerDataLookup[tmrInstance].baseAddress;
    uint32_t offsetAddress = timerDataLookup[tmrInstance].prescalerOffsetAddress;
    
    /*LDRA_EXCLUDE 440 S <justification start> Accessing SFR address through pointers, hence violation ignored <justification end> */
    uint32_t *prescalerReg = (uint32_t *)(baseAddress + offsetAddress);
    
    uint32_t setting = (*prescalerReg & (timerDataLookup[tmrInstance].prescalerMask));
    
    setting = (setting>>(timerDataLookup[tmrInstance].prescalerDataPosition));
    
    return setting;
}

void DIAG_UTIL_TIMER_SetPeriodCount(DIAG_UTILITY_TIMER_INSTANCE tmrInstance, uint32_t prVal)
{
    uint32_t baseAddress = timerDataLookup[tmrInstance].baseAddress;
    uint32_t offsetAddress = timerDataLookup[tmrInstance].periodOffsetAddress;

    /*LDRA_EXCLUDE 440 S <justification start> Accessing SFR address through pointers, hence violation ignored <justification end> */
    uint32_t *prdReg = (uint32_t *)(baseAddress + offsetAddress);
     
    *prdReg = (uint32_t)prVal;
}

void DIAG_UTIL_TIMER_SetPrescaler(DIAG_UTILITY_TIMER_INSTANCE tmrInstance, uint32_t preScaleSetting)
{
    uint32_t setting = (uint32_t)preScaleSetting;
    uint32_t baseAddress = timerDataLookup[tmrInstance].baseAddress;
    uint32_t offsetAddress = timerDataLookup[tmrInstance].prescalerOffsetAddress;

    /*LDRA_EXCLUDE 440 S <justification start> Accessing SFR address through pointers, hence violation ignored <justification end> */
    uint32_t *prescalerReg = (uint32_t *)(baseAddress + offsetAddress);
    
    setting = (setting<<(timerDataLookup[tmrInstance].prescalerDataPosition));
    *prescalerReg = ((*prescalerReg) & (~(timerDataLookup[tmrInstance].prescalerMask)));
    *prescalerReg |= setting;
}

void DIAG_UTIL_TIMER_SetTMRValue(DIAG_UTILITY_TIMER_INSTANCE tmrInstance, uint32_t tmrVal)
{
    uint32_t baseAddress = timerDataLookup[tmrInstance].baseAddress;
    uint32_t offsetAddress = timerDataLookup[tmrInstance].timerOffsetAddress;

    /*LDRA_EXCLUDE 440 S <justification start> Accessing SFR address through pointers, hence violation ignored <justification end> */
    uint32_t *tmrReg = (uint32_t *)(baseAddress + offsetAddress);
  
    *tmrReg = (uint32_t)tmrVal;
}

void DIAG_UTIL_TIMER_EnableTimer(DIAG_UTILITY_TIMER_INSTANCE tmrInstance)
{
    uint32_t baseAddress = timerDataLookup[tmrInstance].baseAddress;
    uint32_t offsetAddress = timerDataLookup[tmrInstance].timerEnableOffsetAddress; 

    /*LDRA_EXCLUDE 440 S <justification start> Accessing SFR address through pointers, hence violation ignored <justification end> */
    uint32_t *enableReg = (uint32_t *)(baseAddress + offsetAddress);

    *enableReg |= timerDataLookup[tmrInstance].timerEnableMask;
}

void DIAG_UTIL_TIMER_EnableInterrupt(DIAG_UTILITY_TIMER_INSTANCE tmrInstance)
{
	/*LDRA_EXCLUDE 440 S <justification start> Accessing SFR address through pointers, hence violation ignored <justification end> */
    uint32_t *intEnableReg = (uint32_t *)timerDataLookup[tmrInstance].interruptEnableAddress;
	/*LDRA_EXCLUDE 440 S <justification start> Accessing SFR address through pointers, hence violation ignored <justification end> */
	uint32_t *intFlagReg = (uint32_t *)timerDataLookup[tmrInstance].interruptFlagAddress;

    *intFlagReg &= ~(timerDataLookup[tmrInstance].interruptFlagMask);
    *intEnableReg |= timerDataLookup[tmrInstance].interruptEnableMask;
}

void DIAG_UTIL_TIMER_DisableTimer(DIAG_UTILITY_TIMER_INSTANCE tmrInstance)
{
    uint32_t baseAddress = timerDataLookup[tmrInstance].baseAddress;
    uint32_t offsetAddress = timerDataLookup[tmrInstance].timerEnableOffsetAddress;

    /*LDRA_EXCLUDE 440 S <justification start> Accessing SFR address through pointers, hence violation ignored <justification end> */
    uint32_t *enableReg = (uint32_t *)(baseAddress + offsetAddress);

    *enableReg &= ~(timerDataLookup[tmrInstance].timerEnableMask);
}

void DIAG_UTIL_TIMER_DisableInterrupt(DIAG_UTILITY_TIMER_INSTANCE tmrInstance)
{
	/*LDRA_EXCLUDE 440 S <justification start> Accessing SFR address through pointers, hence violation ignored <justification end> */
    uint32_t *intEnableReg = (uint32_t *)timerDataLookup[tmrInstance].interruptEnableAddress;
	/*LDRA_EXCLUDE 440 S <justification start> Accessing SFR address through pointers, hence violation ignored <justification end> */
	uint32_t *intFlagReg = (uint32_t *)timerDataLookup[tmrInstance].interruptFlagAddress;

    *intFlagReg &= ~(timerDataLookup[tmrInstance].interruptFlagMask);
    *intEnableReg &= ~(timerDataLookup[tmrInstance].interruptEnableMask);
}

uint32_t DIAG_UTIL_TIMER_GetInterruptEnableStatus(DIAG_UTILITY_TIMER_INSTANCE tmrInstance)
{
    /*LDRA_EXCLUDE 440 S <justification start> Accessing SFR address through pointers, hence violation ignored <justification end> */	
    uint32_t *intEnableReg = (uint32_t *)timerDataLookup[tmrInstance].interruptEnableAddress;
    uint32_t value = 0U;
    uint32_t status = 0U;

    value = (*intEnableReg & (uint32_t)timerDataLookup[tmrInstance].interruptEnableMask);


    if(value != 0U)
    {
        status = 0x01U;
    }
    else
    {
        status = 0x00U;
    }
    return status;
}

uint32_t DIAG_UTIL_TIMER_GetInterruptFlagStatus(DIAG_UTILITY_TIMER_INSTANCE tmrInstance)
{
    /*LDRA_EXCLUDE 440 S <justification start> Accessing SFR address through pointers, hence violation ignored <justification end> */	
    uint32_t *intFlagReg = (uint32_t *)timerDataLookup[tmrInstance].interruptFlagAddress;
    uint32_t value = 0U;

    value = (*intFlagReg & (uint32_t)timerDataLookup[tmrInstance].interruptFlagMask);

    return value;
}
/*LDRA_EXCLUDE_END 76 D */
/*LDRA_EXCLUDE_END 61 S */
