/**
* @file    Mcu_Lld.h
*==================================================================================================
*   Project              : YTMicro AUTOSAR 4.4.0 MCAL
*   Platform             : ARM
*   Peripheral           : Mcu_Lld
*   Dependencies         : none
*
*   Autosar Version      : V4.4.0
*   Autosar Revision     : ASR_REL_4_4_REV_0000
*   SW Version           : V0.8.0
*
*   (c) Copyright 2020-2026 Yuntu Microelectronics co.,ltd.
*   All Rights Reserved.
==================================================================================================*/

#ifndef MCU_LLD_H
#define MCU_LLD_H

#ifdef __cplusplus
extern "C" {
#endif

/*==================================================================================================
 *                                          INCLUDE FILES
==================================================================================================*/
#include "Mcu_Types.h"

/*==================================================================================================
 *                                 HEADER FILE VERSION INFORMATION
==================================================================================================*/
#define MCU_LLD_VENDOR_ID                      (180)
#define MCU_LLD_AR_RELEASE_MAJOR_VERSION       (4)
#define MCU_LLD_AR_RELEASE_MINOR_VERSION       (4)
#define MCU_LLD_AR_RELEASE_REVISION_VERSION    (0)
#define MCU_LLD_SW_MAJOR_VERSION               (0)
#define MCU_LLD_SW_MINOR_VERSION               (8)
#define MCU_LLD_SW_PATCH_VERSION               (0)
/*==================================================================================================
 *                                       FILE VERSION CHECKS
==================================================================================================*/
/* Mcu_Types.h version check */
#if (MCU_LLD_VENDOR_ID != MCU_VENDOR_ID)
#error "Mcu_Lld.h and Mcu_Types.h have different vendor ids"
#endif

#if ((MCU_LLD_AR_RELEASE_MAJOR_VERSION != MCU_AR_RELEASE_MAJOR_VERSION) || \
     (MCU_LLD_AR_RELEASE_MINOR_VERSION != MCU_AR_RELEASE_MINOR_VERSION) || \
     (MCU_LLD_AR_RELEASE_REVISION_VERSION != MCU_AR_RELEASE_REVISION_VERSION) \
    )
#error "AutoSar Version Numbers of Mcu_Lld.h and Mcu_Types.h are different"
#endif

#if ((MCU_LLD_SW_MAJOR_VERSION != MCU_SW_MAJOR_VERSION) || \
     (MCU_LLD_SW_MINOR_VERSION != MCU_SW_MINOR_VERSION) || \
     (MCU_LLD_SW_PATCH_VERSION != MCU_SW_PATCH_VERSION) \
    )
#error "Software Version Numbers of Mcu_Lld.h and Mcu_Types.h are different"
#endif 

/*==================================================================================================
 *                                        DEFINES AND MACROS
==================================================================================================*/

/*==================================================================================================
 *                                        EXTERNAL CONSTANTS
==================================================================================================*/
/*==================================================================================================
 *                                              ENUMS
==================================================================================================*/
/*==================================================================================================
 *                                       FUNCTION PROTOTYPES
==================================================================================================*/

/**
 * @brief         Initialize/clear a RAM section with its configured default value.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Non Reentrant
 *                Available via: Mcu_Lld.h
 *
 * @param[in]     ConfigPtr: Pointer to the struct containing information about the RAM section,
 *                including its base address and the default value to be set.
 * @return        void
 */
void Mcu_Lld_SetRamSection(const Mcu_RamSectionConfigType *ConfigPtr);

/**
 * @brief         Configure Reset Control Unit (RCU) reset pin filter and related reset settings.
 *                This function sets MCU Reset and Control Unit (RCU) reset configuration.
 *                The hardware reset pin can be optionally filtered by configuring the filter enable,
 *                filter stabilization time, and filter counter value before triggering a Reset.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Non Reentrant
 *                Available via: Mcu_Lld.h
 *
 * @param[in]     ConfigPtr: A pointer to the structure containing RCU reset configurations
 * @return        void
 */
void Mcu_Lld_SetRcu(const Mcu_ResetConfigType *ConfigPtr);

/**
 * @brief         Return current PLL lock/ready status.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Reentrant
 *                Available via: Mcu_Lld.h
 *
 * @return        Mcu_PllStatusType PLL lock/distribution status.
 */
Mcu_PllStatusType Mcu_Lld_GetPllStatus(void);

/**
 * @brief         Set MCU power mode (standby, powerdown, normal) and apply peripheral power state changes.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Non Reentrant
 *                Available via: Mcu_Lld.c
 *
 * @param[in]     McuMode: Power mode selection for MCU.
 * @return        void
 */
void Mcu_Lld_SetMode(Mcu_PowerModeType McuMode);

/**
 * @brief         Configure all indexed peripheral clocks (IPC) and per-peripheral clock registers
 *                as specified in configuration.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Non Reentrant
 *                Available via: Mcu_Lld.h
 *
 * @param[in]     ConfigPtr: Pointer to the configuration structure for the peripheral clock. It
 *                contains the list of IPC configurations and the number of IPCs to be configured.
 * @return        void
 */
void Mcu_Lld_SetPeripheralClock(const Mcu_PeripheralClockConfigType *ConfigPtr);

/**
 * @brief         Top-level initialization routine to initialize clocks (SIRC, FIRC, FXOSC, PLL,
 *                dividers) according to the provided clock configuration.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Non Reentrant
 *                Available via: Mcu_Lld.h
 *
 * @param[in]     ConfigPtr: ConfigPtr Pointer to the configuration data.
 * @return        Std_ReturnType E_OK when initialization succeeded, E_NOT_OK otherwise.
 */

Std_ReturnType Mcu_Lld_InitClock(const Mcu_ClockConfigType *ConfigPtr);

/**
 * @brief         Convert raw reset bits to a higher-level Mcu_ResetType and return it.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Reentrant
 *                Available via: Mcu_Lld.h
 *
 * @return Mcu_ResetType Decoded reset reason.
 */
Mcu_ResetType Mcu_Lld_GetResetValue(void);

/**
 * @brief         Return the current computed core clock frequency using the provided clock configuration.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Reentrant
 *                Available via: Mcu_Lld.h
 *
 * @param[in]     ConfigPtr: Parameter description
 * @return        uint32 Core clock frequency in Hz.
 */
uint32 Mcu_Lld_GetCoreClockFrequency(const Mcu_ClockConfigType *ConfigPtr);

/**
 * @brief         Calculate and return PLL-based core clock frequency using provided clock configuration.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Reentrant
 *                Available via: Mcu_Lld.h
 *
 * @param[in]     ConfigPtr: ConfigPtr A pointer to the configuration data.
 * @return        uint32 PLL output frequency in Hz (implementation dependent).
 */
uint32 Mcu_Lld_GetPllClockFrequency(const Mcu_ClockConfigType *ConfigPtr);

/**
 * @brief         Apply a complete clock source configuration (select sources, PLL distribution,
 *                dividers as needed).
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Non Reentrant
 *                Available via: Mcu_Lld.h
 *
 * @param[in]     ConfigPtr: ConfigPtr A pointer to the configuration data.
 * @return        Std_ReturnType E_OK on success, E_NOT_OK on failure.
 */
Std_ReturnType Mcu_Lld_SetClockSource(const Mcu_ClockConfigType *ConfigPtr);


/**
 * @brief         Read raw reset source/status bits from reset controller (RCU).
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Reentrant
 *                Available via: Mcu_Lld.h
 *
 * @return        Mcu_RawResetType Bitfield of raw reset source/status.
 */
Mcu_RawResetType Mcu_Lld_GetResetRawValue(void);

/**
 * @brief         Configure Power Control Unit (PCU) registers (LVD, BOR, CSR, CTRL) per the provided
 *                config.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Non Reentrant
 *                Available via: Mcu_Lld.h
 *
 * @param[in]     ConfigPtr: ConfigPtr A pointer to the PCU configuration data.
 * @return        void
 */
void Mcu_Lld_SetPcu(const Mcu_PcuConfigType *ConfigPtr);

/**
 * @brief         Interrupt handler to process PCU channel interrupts and call configured notifications.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Non Reentrant
 *                Available via: Mcu_Lld.h
 *
 * @return void
 */
void Mcu_Lld_Pcu_ChannelIrqProcess(void);

/**
 * @brief         Clock Monitor Unit interrupt handler; clears status and reports failures if configured.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Non Reentrant
 *                Available via: Mcu_Lld.h
 *
 * @return void
 */
void Mcu_Lld_Cmu_ChannelIrqProcess(void);


#if (MCU_NO_PLL == STD_OFF)
/**
 * @brief         Enable CMU channel 2 specifically (helper used when PLL present).
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Non Reentrant
 *                Available via: Mcu_Lld.c
 *
 * @return        void
 * @note          When the PLL is enabled but not used as the system clock, once the CMU2 is enabled,
 *                Mcu_InitClock will enable CMU before PLL is locked, which may lead to a chip reset.
 */
void Mcu_Lld_SetCmuChannel2(void);
#endif


#if (MCU_GET_SXOSC_STATUS_API == STD_ON)
/**
 * @brief         Get the status of the SXOSC (secondary oscillator). Returns MCU_SXOSC_VALID if the
 *                SXOSC is valid, otherwise MCU_SXOSC_INVALID.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Reentrant
 *                Available via: Mcu_Lld.h
 *
 * @return Mcu_SxoscStatusType MCU_SXOSC_VALID if SXOSC is valid, MCU_SXOSC_INVALID otherwise.
 */
Mcu_SxoscStatusType Mcu_Lld_GetSxoscStatus(void);
#endif

/**
 * @brief         Perform a full system reset by writing the required key to the system control block
 *                (SCB) AIRCR register.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Non Reentrant
 *                Available via: Mcu_Lld.h
 *
 * @return void
 */
void Mcu_Lld_SystemReset(void);

#ifdef __cplusplus
}
#endif

#endif /* End of file Mcu_Lld.h */


