/*!
* @file     Crc_Hw.c
*
* @version  1.0.0
*
* @brief    Code file of CRC unit 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 "Crc_Hw.h"
#include "Crc_Reg.h"
#include "StdRegMacros.h"

/** @addtogroup G32A10xx_StdPeriphDriver
  @{
*/

/** @defgroup CRC_Driver
  * @brief CRC driver modules
  @{
*/

/** @defgroup CRC_Macros
  @{
*/
/**************************************************************************
                  VENDOR ID & AUTOSAR VERSION INFO
**************************************************************************/
/** Vendor ID (vendorId) of the dedicated implementation of this module according to the AUTOSAR vendor list. */
#define CRC_HW_VENDOR_ID                        0x00C1U
/** Major version number of AUTOSAR release on which the appropriate implementation is based on. */
#define CRC_HW_AR_RELEASE_MAJOR_VERSION         0x04U
/** Minor version number of AUTOSAR release on which the appropriate implementation is based on. */
#define CRC_HW_AR_RELEASE_MINOR_VERSION         0x04U
/** Revision version number of AUTOSAR release on which the appropriate implementation is based on. */
#define CRC_HW_AR_RELEASE_REVISION_VERSION      0x00U
/** Major version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define CRC_HW_SW_MAJOR_VERSION                 0x01U
/** Minor version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define CRC_HW_SW_MINOR_VERSION                 0x00U
/** Patch level version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define CRC_HW_SW_PATCH_VERSION                 0x00U

/**@} end of group CRC_Macros */

/**************************************************************************
                    VERSION INFOMATION VERIFICATION
**************************************************************************/
/* Make sure the vendor id is the same between Crc_Hw.c and Crc_Hw.h */
#if (CRC_HW_VENDOR_ID != CRC_HW_H_VENDOR_ID)
    #error "Vendor id mismatch between Crc_Hw.c and Crc_Hw.h"
#endif
/* Make sure the AUTOSAR major version number is the same between Crc_Hw.c and Crc_Hw.h */
#if (CRC_HW_AR_RELEASE_MAJOR_VERSION != CRC_HW_H_AR_RELEASE_MAJOR_VERSION)
    #error "AUTOSAR Major Version Number mismatch between Crc_Hw.c and Crc_Hw.h"
#endif
/* Make sure the AUTOSAR minor version number is the same between Crc_Hw.c and Crc_Hw.h */
#if (CRC_HW_AR_RELEASE_MINOR_VERSION != CRC_HW_H_AR_RELEASE_MINOR_VERSION)
    #error "AUTOSAR Minor Version Number mismatch between Crc_Hw.c and Crc_Hw.h"
#endif
/* Make sure the AUTOSAR revision version number is the same between Crc_Hw.c and Crc_Hw.h */
#if (CRC_HW_AR_RELEASE_REVISION_VERSION != CRC_HW_H_AR_RELEASE_REVISION_VERSION)
    #error "AUTOSAR Revision Version Number mismatch between Crc_Hw.c and Crc_Hw.h"
#endif
/* Make sure the software major version number is the same between Crc_Hw.c and Crc_Hw.h */
#if (CRC_HW_SW_MAJOR_VERSION != CRC_HW_H_SW_MAJOR_VERSION)
    #error "Software Major Version Number mismatch between Crc_Hw.c and Crc_Hw.h"
#endif
/* Make sure the software minor version number is the same between Crc_Hw.c and Crc_Hw.h */
#if (CRC_HW_SW_MINOR_VERSION != CRC_HW_H_SW_MINOR_VERSION)
    #error "Software Minor Version Number mismatch between Crc_Hw.c and Crc_Hw.h"
#endif
/* Make sure the software patch version number is the same between Crc_Hw.c and Crc_Hw.h */
#if (CRC_HW_SW_PATCH_VERSION != CRC_HW_H_SW_PATCH_VERSION)
    #error "Software Patch Version Number mismatch between Crc_Hw.c and Crc_Hw.h"
#endif

/* Make sure the vendor id is the same between Crc_Hw.c and Crc_Reg.h */
#if (CRC_HW_VENDOR_ID != CRC_REG_H_VENDOR_ID)
    #error "Vendor id mismatch between Crc_Hw.c and Crc_Reg.h"
#endif
/* Make sure the AUTOSAR major version number is the same between Crc_Hw.c and Crc_Reg.h */
#if (CRC_HW_AR_RELEASE_MAJOR_VERSION != CRC_REG_H_AR_RELEASE_MAJOR_VERSION)
    #error "AUTOSAR Major Version Number mismatch between Crc_Hw.c and Crc_Reg.h"
#endif
/* Make sure the AUTOSAR minor version number is the same between Crc_Hw.c and Crc_Reg.h */
#if (CRC_HW_AR_RELEASE_MINOR_VERSION != CRC_REG_H_AR_RELEASE_MINOR_VERSION)
    #error "AUTOSAR Minor Version Number mismatch between Crc_Hw.c and Crc_Reg.h"
#endif
/* Make sure the AUTOSAR revision version number is the same between Crc_Hw.c and Crc_Reg.h */
#if (CRC_HW_AR_RELEASE_REVISION_VERSION != CRC_REG_H_AR_RELEASE_REVISION_VERSION)
    #error "AUTOSAR Revision Version Number mismatch between Crc_Hw.c and Crc_Reg.h"
#endif
/* Make sure the software major version number is the same between Crc_Hw.c and Crc_Reg.h */
#if (CRC_HW_SW_MAJOR_VERSION != CRC_REG_H_SW_MAJOR_VERSION)
    #error "Software Major Version Number mismatch between Crc_Hw.c and Crc_Reg.h"
#endif
/* Make sure the software minor version number is the same between Crc_Hw.c and Crc_Reg.h */
#if (CRC_HW_SW_MINOR_VERSION != CRC_REG_H_SW_MINOR_VERSION)
    #error "Software Minor Version Number mismatch between Crc_Hw.c and Crc_Reg.h"
#endif
/* Make sure the software patch version number is the same between Crc_Hw.c and Crc_Reg.h */
#if (CRC_HW_SW_PATCH_VERSION != CRC_REG_H_SW_PATCH_VERSION)
    #error "Software Patch Version Number mismatch between Crc_Hw.c and Crc_Reg.h"
#endif

#ifdef ENABLE_INTERMODULE_MCAL_CHECK
    #if(STD_ON == CRC_DEV_ERR_DETECT_ENABLE)
        /* Make sure the AUTOSAR major version number is the same between Crc_Hw.c and StdRegMacros.h */
        #if (CRC_HW_AR_RELEASE_MAJOR_VERSION != STD_REG_MACROS_H_AR_RELEASE_MAJOR_VERSION)
            #error "AUTOSAR Major Version Number mismatch between Crc_Hw.c and StdRegMacros.h"
        #endif
        /* Make sure the AUTOSAR minor version number is the same between Crc_Hw.c and StdRegMacros.h */
        #if (CRC_HW_AR_RELEASE_MINOR_VERSION != STD_REG_MACROS_H_AR_RELEASE_MINOR_VERSION)
            #error "AUTOSAR Minor Version Number mismatch between Crc_Hw.c and StdRegMacros.h"
        #endif
    #endif
#endif

/**************************************************************************
                                GLOBAL FUNCTION
**************************************************************************/
#define CRCU_START_SEC_CODE
#include "Crc_MemMap.h"

/** @defgroup CRC_Fuctions
  @{
*/

/*!
* @brief Reset CRC_DATA register
*
* @param[in]       None
* @param[out]      None
* @param[inout]    None
*/
FUNC(void, CRCU_CODE) Crc_Hw_ResetDATA(void)
{
    REG_BIT_SET32(CRC_CTRL_REG_ADDR, CRC_CTRL_RST_MASK);
}

/*!
* @brief Selects the reverse operation to be performed on input data
*
* @param[in]       Mode     Target input reverse mode:
*                           CRC_REVERSE_INPUT_DATA_NO  : Bit order not affected
*                           CRC_REVERSE_INPUT_DATA_8B  : Bit reversal done by byte
*                           CRC_REVERSE_INPUT_DATA_16B : Bit reversal done by half-word
*                           CRC_REVERSE_INPUT_DATA_32B : Bit reversal done by word
* @param[out]      None
* @param[inout]    None
*/
FUNC(void, CRCU_CODE) Crc_Hw_SelectReverseInputData(VAR(Crc_ReverseInputDataType, AUTOMATIC) Mode)
{
    /* Reset REVI bit */
    REG_BIT_CLEAR32(CRC_CTRL_REG_ADDR, CRC_CTRL_REVI_MASK);
    /* Set REVI bit */
    REG_BIT_SET32(CRC_CTRL_REG_ADDR, ((uint32)Mode << CRC_CTRL_REVI_SHIFT));
}

/*!
* @brief Enable or disable the reverse operation on output data
*
* @param[in]       Status     whether to enable the output reverse function :
*                             TRUE  : enable the output reverse function
*                             FALSE : disable the output reverse function
* @param[out]      None
* @param[inout]    None
*/
FUNC(void, CRCU_CODE) Crc_Hw_OperationReverseOutputData(VAR(boolean, AUTOMATIC) Status)
{
    if (TRUE == Status)
    {
        REG_BIT_SET32(CRC_CTRL_REG_ADDR, CRC_CTRL_REVO_MASK);
    }
    else
    {
        REG_BIT_CLEAR32(CRC_CTRL_REG_ADDR, CRC_CTRL_REVO_MASK);
    }
}

/*!
* @brief This function is used to set CRC_INITVAL register value
*
* @param[in]       InitValue    Target value to CRC_INITVAL register
* @param[out]      None
* @param[inout]    None
*/
FUNC(void, CRCU_CODE) Crc_Hw_WriteInitRegister(VAR(uint32, AUTOMATIC) InitValue)
{
    REG_WRITE32(CRC_INITVAL_REG_ADDR, InitValue);
}

/*!
* @brief This function is used to calculate crc value according to a uint32 value
*
* @param[in]       Data    Target value to used calculate crc value
* @param[out]      None
* @param[inout]    None
*
* @retval CRC value
*/
FUNC(uint32, CRCU_CODE) Crc_Hw_CalculateCRC(VAR(uint32, AUTOMATIC) Data)
{
    REG_WRITE32(CRC_DATA_REG_ADDR, Data);

    return REG_READ32(CRC_DATA_REG_ADDR);
}

/*!
* @brief This function is used to calculate crc value according to a uint32 value block
*
* @param[in]       pBuffer         Target value block pointer to used calculate crc value
* @param[in]       BufferLength    Element number, each element is a number of type uint32
* @param[out]      None
* @param[inout]    None
*
* @retval CRC value
*/
FUNC(uint32, CRCU_CODE) Crc_Hw_CalculateBlockCRC(P2CONST(uint32, AUTOMATIC, CRC_APPL_DATA) pBuffer,
                                                     VAR(uint32, AUTOMATIC) BufferLength)
{
    VAR(uint32, AUTOMATIC) index = 0;

    for (index = 0; index < BufferLength; index++)
    {
        REG_WRITE32(CRC_DATA_REG_ADDR, pBuffer[index]);
    }

    return REG_READ32(CRC_DATA_REG_ADDR);
}

/*!
* @brief This function is used to get CRC_DATA register value
*
* @param[in]       None
* @param[out]      None
* @param[inout]    None
*
* @retval CRC_DATA register value
*/
FUNC(uint32, CRCU_CODE) Crc_Hw_ReadCRC(void)
{
    return REG_READ32(CRC_DATA_REG_ADDR);
}

/*!
* @brief This function is used to set CRC_INDATA register value
*
* @param[in]       IDValue    Target value to set CRC_INDATA register
* @param[out]      None
* @param[inout]    None
*/
FUNC(void, CRCU_CODE) Crc_Hw_WriteIDRegister(VAR(uint32, AUTOMATIC) IDValue)
{
    REG_WRITE32(CRC_INDATA_REG_ADDR, IDValue);
}

/*!
* @brief This function is used to get CRC_INDATA register value
*
* @param[in]       None
* @param[out]      None
* @param[inout]    None
*
* @retval CRC_INDATA register value
*/
FUNC(uint32, CRCU_CODE) Crc_Hw_ReadIDRegister(void)
{
    return REG_READ32(CRC_INDATA_REG_ADDR);
}

/**@} end of group CRC_Fuctions */

#define CRCU_STOP_SEC_CODE
#include "Crc_MemMap.h"

#ifdef __cplusplus
}
#endif

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