/*!
* @file     SchM_Can.c
*
* @version  1.0.0
*
* @brief    This source file includes the variables and functions implementation of the Rte driver
*
* @attention
*
* Project Name      : AUTOSAR MCAL
*
* Platform          : Arm
*
* Autosar Standard  : Classic Platform 4.4.0
*
* Compiled Version  : G32A10xx_MCAL_1_0_0_01-Otc-25
*
* Copyright (C) 2026 Geehy Semiconductor
*
* You may not use this file except in compliance with the
* GEEHY COPYRIGHT NOTICE (Geehy Semiconductor Software License Agreement).
*
* The program is only for reference, which is distributed in the hope
* that it will be usefull and instructional for customers to develop
* their software. Unless required by applicable law or agreed to in
* writing, the program is distributed on an "AS IS" BASIS, WITHOUT
* ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
* See the Geehy Semiconductor Software License Agreement for the governing permissions
* and limitations under the License.
*/

#ifdef __cplusplus
extern "C"{
#endif

#include "Std_Types.h"
#include "Mcal.h"
#include "SchM_Can.h"

/** @addtogroup G32A10xx_StdPeriphDriver
  @{
*/

/** @defgroup RTE_Driver
  * @brief RTE driver modules
  @{
*/

/** @defgroup RTE_Macros
  @{
*/
/***********************************************************************************************************************
                                    VENDOR ID & AUTOSAR VERSION INFO
***********************************************************************************************************************/
/** Major version number of AUTOSAR release on which the appropriate implementation is based on. */
#define SCHM_CAN_AR_RELEASE_MAJOR_VERSION       0x04U
/** Minor version number of AUTOSAR release on which the appropriate implementation is based on. */
#define SCHM_CAN_AR_RELEASE_MINOR_VERSION       0x04U
/** Revision version number of AUTOSAR release on which the appropriate implementation is based on. */
#define SCHM_CAN_AR_RELEASE_REVISION_VERSION    0x00U
/** Major version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define SCHM_CAN_SW_MAJOR_VERSION               0x01U
/** Minor version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define SCHM_CAN_SW_MINOR_VERSION               0x00U
/** Patch level version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define SCHM_CAN_SW_PATCH_VERSION               0x00U

/**********************************************************************************************************************
                                            MACRO DEFINITION
***********************************************************************************************************************/
#ifdef MCAL_PLATFORM_ARM
    #if (MCAL_PLATFORM_ARM == MCAL_ARM_AARCH64)
        #define ISR_STATE_ON(msr)           (((uint32)(msr) & (uint32)(ISR_STATE_BITS_MASK)) != (uint32)3)
        #define ISR_STATE_BITS_MASK         ((uint32)0x00000002UL)   /** DAIF bit I and F */
    #else
        #define ISR_STATE_ON(msr)           (((uint32)(msr) & (uint32)(ISR_STATE_BITS_MASK)) == (uint32)0)

        #ifdef MCAL_ENABLE_USER_MODE_SUPPORT
            #define ISR_STATE_BITS_MASK     ((uint32)0x000000FFUL)   /** BASEPRI[7:0] mask */
        #else
            #define ISR_STATE_BITS_MASK     ((uint32)0x00000001UL)   /** PRIMASK bit 0 */
        #endif
    #endif
#endif

/**********************************************************************************************************************
                                                LOCAL VARIABLES
***********************************************************************************************************************/
#define CAN_EXCLUSIVE_AREA_COUNT    15U

/**@} end of group RTE_Macros */

/** @defgroup RTE_Variable
  @{
*/

/** The MSR state when entering the exclusive area */
static uint32 s_msrState[CAN_EXCLUSIVE_AREA_COUNT];

/** The msr variable guard */
static volatile uint32 s_msrGuard[CAN_EXCLUSIVE_AREA_COUNT];

/**@} end of group RTE_Variable */

/** @defgroup RTE_Fuctions
  @{
*/

/**********************************************************************************************************************
                                              LOCAL FUNCTION DECLARATIONS
***********************************************************************************************************************/
LOCAL_INLINE FUNC(void, RTE_CODE) Can_EnterCriticalSection(uint8 Index);
LOCAL_INLINE FUNC(void, RTE_CODE) Can_ExitCriticalSection(uint8 Index);

#ifndef G32A_C_COSMIC
static FUNC(uint32, RTE_CODE) Can_SchmReadMsrReg(void);
#endif

/**********************************************************************************************************************
                                                  GLOBAL FUNCTIONS
***********************************************************************************************************************/
/*!
* @brief         Enter can critical section.
*
* @param[in]     None
* @param[out]    None
* @param[in,out] None
*
*/
FUNC(void, RTE_CODE) SchM_Enter_Can_CAN_CRITICAL_SECTION0(void)
{
    Can_EnterCriticalSection(0);
}

/*!
* @brief         Exit can critical section.
*
* @param[in]     None
* @param[out]    None
* @param[in,out] None
*
*/
FUNC(void, RTE_CODE) SchM_Exit_Can_CAN_CRITICAL_SECTION0(void)
{
    Can_ExitCriticalSection(0);
}

FUNC(void, RTE_CODE) SchM_Enter_Can_CAN_CRITICAL_SECTION1(void)
{
    Can_EnterCriticalSection(1);
}

FUNC(void, RTE_CODE) SchM_Exit_Can_CAN_CRITICAL_SECTION1(void)
{
    Can_ExitCriticalSection(1);
}

FUNC(void, RTE_CODE) SchM_Enter_Can_CAN_CRITICAL_SECTION2(void)
{
    Can_EnterCriticalSection(2);
}

FUNC(void, RTE_CODE) SchM_Exit_Can_CAN_CRITICAL_SECTION2(void)
{
    Can_ExitCriticalSection(2);
}

FUNC(void, RTE_CODE) SchM_Enter_Can_CAN_CRITICAL_SECTION3(void)
{
    Can_EnterCriticalSection(3);
}

FUNC(void, RTE_CODE) SchM_Exit_Can_CAN_CRITICAL_SECTION3(void)
{
    Can_ExitCriticalSection(3);
}

FUNC(void, RTE_CODE) SchM_Enter_Can_CAN_CRITICAL_SECTION4(void)
{
    Can_EnterCriticalSection(4);
}

FUNC(void, RTE_CODE) SchM_Exit_Can_CAN_CRITICAL_SECTION4(void)
{
    Can_ExitCriticalSection(4);
}

FUNC(void, RTE_CODE) SchM_Enter_Can_CAN_CRITICAL_SECTION5(void)
{
    Can_EnterCriticalSection(5);
}

FUNC(void, RTE_CODE) SchM_Exit_Can_CAN_CRITICAL_SECTION5(void)
{
    Can_ExitCriticalSection(5);
}

FUNC(void, RTE_CODE) SchM_Enter_Can_CAN_CRITICAL_SECTION6(void)
{
    Can_EnterCriticalSection(6);
}

FUNC(void, RTE_CODE) SchM_Exit_Can_CAN_CRITICAL_SECTION6(void)
{
    Can_ExitCriticalSection(6);
}

FUNC(void, RTE_CODE) SchM_Enter_Can_CAN_CRITICAL_SECTION7(void)
{
    Can_EnterCriticalSection(7);
}

FUNC(void, RTE_CODE) SchM_Exit_Can_CAN_CRITICAL_SECTION7(void)
{
    Can_ExitCriticalSection(7);
}

FUNC(void, RTE_CODE) SchM_Enter_Can_CAN_CRITICAL_SECTION8(void)
{
    Can_EnterCriticalSection(8);
}

FUNC(void, RTE_CODE) SchM_Exit_Can_CAN_CRITICAL_SECTION8(void)
{
    Can_ExitCriticalSection(8);
}

FUNC(void, RTE_CODE) SchM_Enter_Can_CAN_CRITICAL_SECTION9(void)
{
    Can_EnterCriticalSection(9);
}

FUNC(void, RTE_CODE) SchM_Exit_Can_CAN_CRITICAL_SECTION9(void)
{
    Can_ExitCriticalSection(9);
}

FUNC(void, RTE_CODE) SchM_Enter_Can_CAN_CRITICAL_SECTION10(void)
{
    Can_EnterCriticalSection(10);
}

FUNC(void, RTE_CODE) SchM_Exit_Can_CAN_CRITICAL_SECTION10(void)
{
    Can_ExitCriticalSection(10);
}

FUNC(void, RTE_CODE) SchM_Enter_Can_CAN_CRITICAL_SECTION11(void)
{
    Can_EnterCriticalSection(11);
}

FUNC(void, RTE_CODE) SchM_Exit_Can_CAN_CRITICAL_SECTION11(void)
{
    Can_ExitCriticalSection(11);
}

FUNC(void, RTE_CODE) SchM_Enter_Can_CAN_CRITICAL_SECTION12(void)
{
    Can_EnterCriticalSection(12);
}

FUNC(void, RTE_CODE) SchM_Exit_Can_CAN_CRITICAL_SECTION12(void)
{
    Can_ExitCriticalSection(12);
}

FUNC(void, RTE_CODE) SchM_Enter_Can_CAN_CRITICAL_SECTION13(void)
{
    Can_EnterCriticalSection(13);
}

FUNC(void, RTE_CODE) SchM_Exit_Can_CAN_CRITICAL_SECTION13(void)
{
    Can_ExitCriticalSection(13);
}

FUNC(void, RTE_CODE) SchM_Enter_Can_CAN_CRITICAL_SECTION14(void)
{
    Can_EnterCriticalSection(14);
}

FUNC(void, RTE_CODE) SchM_Exit_Can_CAN_CRITICAL_SECTION14(void)
{
    Can_ExitCriticalSection(14);
}

/**********************************************************************************************************************
                                                  LOCAL FUNCTIONS
***********************************************************************************************************************/
/*!
* @brief         Enter can critical section.
*
* @param[in]     Index    section number
* @param[out]    None
* @param[in,out] None
*/
LOCAL_INLINE FUNC(void, RTE_CODE) Can_EnterCriticalSection(uint8 Index)
{
    if (s_msrGuard[Index] == 0UL)
    {
#ifdef MCAL_ENABLE_USER_MODE_SUPPORT
        s_msrState[Index] = Mcal_Trusted_Call_Return(Can_SchmReadMsrReg);
#else
        s_msrState[Index] = Can_SchmReadMsrReg();
#endif
        if (ISR_STATE_ON(s_msrState[Index]))
        {
            SuspendAllInterrupts();
#ifdef G32A_C_DS5
            ASM_KEYWORD(" nop ");
#endif
        }
        else
        {
            /* Intentionally Empty */
        }

    }
    else
    {
        /* Intentionally Empty */
    }

    s_msrGuard[Index] = s_msrGuard[Index] + 1UL;
}

/*!
* @brief         Exit can critical section.
*
* @param[in]     Index    section number
* @param[out]    None
* @param[in,out] None
*/
LOCAL_INLINE FUNC(void, RTE_CODE) Can_ExitCriticalSection(uint8 Index)
{
    s_msrGuard[Index] = s_msrGuard[Index] - 1UL;

    if ((ISR_STATE_ON(s_msrState[Index])) &&
        (s_msrGuard[Index] == 0UL))
    {
        ResumeAllInterrupts();
#ifdef G32A_C_DS5
        ASM_KEYWORD(" nop ");
#endif
    }
    else
    {
        /* Intentionally Empty */
    }
}

#ifdef G32A_C_IAR
/*!
* @brief           This function is used to get the MSR register value (32 bits).
*
* @param[in]       None
* @param[out]      None
* @param[in,out]   None
*
* @retval  The value of the MSR register
*/
static FUNC(uint32, RTE_CODE) Can_SchmReadMsrReg(void)
{
    register uint32 regVal;

    #if ((defined MCAL_ENABLE_USER_MODE_SUPPORT) && (!defined MCAL_M0PLUS_CORE_SUPPORT))
        __asm volatile( " mrs %0, basepri " : "=r" (regVal) );
    #else
        __asm volatile( " mrs %0, primask " : "=r" (regVal) );
    #endif

    return (uint32)regVal;
}
#endif

#ifdef G32A_C_DS5
/*!
* @brief           This function is used to get the MSR register value (32 bits).
*
* @param[in]       None
* @param[out]      None
* @param[in,out]   None
*
* @retval  The value of the MSR register
*/
static FUNC(uint32, RTE_CODE) Can_SchmReadMsrReg(void)
{
    register uint32 regVal;

    #if (MCAL_PLATFORM_ARM == MCAL_ARM_AARCH64)
        __asm volatile( " mrs %x0, DAIF " : "=r" (regVal) );
    #else
        #ifdef MCAL_ENABLE_USER_MODE_SUPPORT
        __asm volatile( " mrs %0, basepri " : "=r" (regVal) );
        #else
        __asm volatile( " mrs %0, primask " : "=r" (regVal) );
        #endif
    #endif

    return (uint32)regVal;
}
#endif

#if (defined(G32A_C_GREENHILLS) || defined(G32A_C_DIAB) || defined(G32A_C_CODEWARRIOR))
/*!
* @brief           This function is used to get the MSR register value (32 bits).
*
* @param[in]       None
* @param[out]      None
* @param[in,out]   None
*
* @retval  The value of the MSR register
*/
#ifdef MCAL_PLATFORM_ARM
    #if (MCAL_PLATFORM_ARM == MCAL_ARM_AARCH64)
        ASM_KEYWORD static FUNC(uint32, RTE_CODE) Can_SchmReadMsrReg(void)
        {
            mrs x0, S3_3_c4_c2_1
        }
    #else
        ASM_KEYWORD static FUNC(uint32, RTE_CODE) Can_SchmReadMsrReg(void)
        {
        #if ((defined MCAL_ENABLE_USER_MODE_SUPPORT) && (!defined MCAL_M0PLUS_CORE_SUPPORT))
            mrs r0, BASEPRI
        #else
            mrs r0, PRIMASK
        #endif
        }
    #endif
#endif
#endif

#ifdef G32A_C_LINARO
/*!
* @brief           This function is used to get the MSR register value (32 bits).
*
* @param[in]       None
* @param[out]      None
* @param[in,out]   None
*
* @retval  The value of the MSR register
*/
static FUNC(uint32, RTE_CODE) Can_SchmReadMsrReg(void)
{
    register uint32 regVal;

    #if (MCAL_PLATFORM_ARM == MCAL_ARM_AARCH64)
        __asm volatile( " mrs %x0, DAIF " : "=r" (regVal) );
    #elif (defined MCAL_M0PLUS_CORE_SUPPORT)
        __asm volatile( " mrs %0, primask " : "=r" (regVal) );
    #else
        #ifdef MCAL_ENABLE_USER_MODE_SUPPORT
        __asm volatile( " mrs %0, basepri " : "=r" (regVal) );
        #else
        __asm volatile( " mrs %0, primask " : "=r" (regVal) );
        #endif
    #endif

    return (uint32)regVal;
}
#endif

#ifdef _G32A_C_HITECH_
/*!
* @brief           This function is used to get the MSR register value (32 bits).
*
* @param[in]       None
* @param[out]      None
* @param[in,out]   None
*
* @retval  The value of the MSR register
*/
static FUNC(uint32, RTE_CODE) Can_SchmReadMsrReg(void)
{
    uint32 resVal;
    __asm volatile("mfmsr %0" : "=r" (resVal) :);
    return resVal;
}
#endif

/**@} end of group RTE_Fuctions */

/** @defgroup RTE_Macros
  @{
*/

#ifdef G32A_C_COSMIC
/** This macro is used to get the MSR register value (32 bits). */
#define Can_SchmReadMsrReg()    ASM_KEYWORD("mfmsr r3")
#endif

/**@} end of group RTE_Macros */

#ifdef __cplusplus
}
#endif

/**@} end of group RTE_Driver */
/**@} end of group G32A10xx_StdPeriphDriver */
