/*!
* @file     Fls_Ac.c
*
* @version  1.0.0
*
* @brief    Code file of AUTOSAR Fls access code 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 "Fls.h"
#include "StdRegMacros.h"

/** @addtogroup G32A10xx_StdPeriphDriver
  @{
*/

/** @defgroup FLS_Driver
  * @brief FLS driver modules
  @{
*/

/** @defgroup FLS_Macros
  @{
*/
/**************************************************************************
                  VENDOR ID & AUTOSAR VERSION INFO
**************************************************************************/
/** Vendor ID (vendorId) of the dedicated implementation of this module according to the AUTOSAR vendor list. */
#define FLS_AC_VENDOR_ID                        0x00C1U
/** Major version number of AUTOSAR release on which the appropriate implementation is based on. */
#define FLS_AC_AR_RELEASE_MAJOR_VERSION         0x04U
/** Minor version number of AUTOSAR release on which the appropriate implementation is based on. */
#define FLS_AC_AR_RELEASE_MINOR_VERSION         0x04U
/** Revision version number of AUTOSAR release on which the appropriate implementation is based on. */
#define FLS_AC_AR_RELEASE_REVISION_VERSION      0x00U
/** Major version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define FLS_AC_SW_MAJOR_VERSION                 0x01U
/** Minor version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define FLS_AC_SW_MINOR_VERSION                 0x00U
/** Patch level version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define FLS_AC_SW_PATCH_VERSION                 0x00U

/**@} end of group FLS_Macros */

/**************************************************************************
                    VERSION INFOMATION VERIFICATION
**************************************************************************/
/* Make sure the vendor id is the same between Fls_Ac.c and Fls.h */
#if (FLS_AC_VENDOR_ID != FLS_H_VENDOR_ID)
    #error "Vendor id mismatch between Fls_Ac.c and Fls.h"
#endif
/* Make sure the AUTOSAR major version number is the same between Fls_Ac.c and Fls.h */
#if (FLS_AC_AR_RELEASE_MAJOR_VERSION != FLS_H_AR_RELEASE_MAJOR_VERSION)
    #error "AUTOSAR Major Version Number mismatch between Fls_Ac.c and Fls.h"
#endif
/* Make sure the AUTOSAR minor version number is the same between Fls_Ac.c and Fls.h */
#if (FLS_AC_AR_RELEASE_MINOR_VERSION != FLS_H_AR_RELEASE_MINOR_VERSION)
    #error "AUTOSAR Minor Version Number mismatch between Fls_Ac.c and Fls.h"
#endif
/* Make sure the AUTOSAR revision version number is the same between Fls_Ac.c and Fls.h */
#if (FLS_AC_AR_RELEASE_REVISION_VERSION != FLS_H_AR_RELEASE_REVISION_VERSION)
    #error "AUTOSAR Revision Version Number mismatch between Fls_Ac.c and Fls.h"
#endif
/* Make sure the software major version number is the same between Fls_Ac.c and Fls.h */
#if (FLS_AC_SW_MAJOR_VERSION != FLS_H_SW_MAJOR_VERSION)
    #error "Software Major Version Number mismatch between Fls_Ac.c and Fls.h"
#endif
/* Make sure the software minor version number is the same between Fls_Ac.c and Fls.h */
#if (FLS_AC_SW_MINOR_VERSION != FLS_H_SW_MINOR_VERSION)
    #error "Software Minor Version Number mismatch between Fls_Ac.c and Fls.h"
#endif
/* Make sure the software patch version number is the same between Fls_Ac.c and Fls.h */
#if (FLS_AC_SW_PATCH_VERSION != FLS_H_SW_PATCH_VERSION)
    #error "Software Patch Version Number mismatch between Fls_Ac.c and Fls.h"
#endif

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

/**************************************************************************
                    FUNCTION DECLARATION
**************************************************************************/
#define FLS_START_SEC_CODE_AC
#include "Fls_MemMap.h"

#ifdef G32A_C_IAR
    /* __root forces IAR to emit Fls_ACEntry into default .text and ignores default_function_attributes,
     * so .acfls_code_rom stays empty and Fls_ACLoad copies wrong flash (use explicit location instead). */
    #pragma default_function_attributes=
#endif

/** @defgroup FLS_Fuctions
  @{
*/

#ifdef G32A_C_DS6
    __attribute__((used)) 
FUNC(void, FLS_CODE) Fls_ACEntry( CONST(uint32, AUTOMATIC) BaseAddr, P2FUNC(void, FLS_CODE, CallBackPtr)( void )
                                 #if (STD_ON == FLS_TIMEOUT_SUPERVISION_EN)
                                , P2VAR(uint32, AUTOMATIC, FLS_CODE) TimeoutACPtr
                                 #endif
                                );
#elif defined(G32A_C_IAR)
#pragma location=".acfls_code_rom"
FUNC(void, FLS_CODE) Fls_ACEntry( CONST(uint32, AUTOMATIC) BaseAddr, P2FUNC(void, FLS_CODE, CallBackPtr)( void )
                                 #if (STD_ON == FLS_TIMEOUT_SUPERVISION_EN)
                                , P2VAR(uint32, AUTOMATIC, FLS_CODE) TimeoutACPtr
                                 #endif
                                );
#elif defined(G32A_C_LINARO)
FUNC(void, FLS_CODE) Fls_ACEntry( CONST(uint32, AUTOMATIC) BaseAddr, P2FUNC(void, FLS_CODE, CallBackPtr)( void )
                                 #if (STD_ON == FLS_TIMEOUT_SUPERVISION_EN)
                                , P2VAR(uint32, AUTOMATIC, FLS_CODE) TimeoutACPtr
                                 #endif
                                ) __attribute__ ((section (".acfls_code_rom")));
#else
FUNC(void, FLS_CODE) Fls_ACEntry( CONST(uint32, AUTOMATIC) BaseAddr, P2FUNC(void, FLS_CODE, CallBackPtr)( void )
                                 #if (STD_ON == FLS_TIMEOUT_SUPERVISION_EN)
                                , P2VAR(uint32, AUTOMATIC, FLS_CODE) TimeoutACPtr
                                 #endif
                                );
#endif

/**************************************************************************
                    GLOBAL FUNCTION
**************************************************************************/
/*!
* @brief            write/erase access code of position independent.
*
* @param[in]        BaseAddr        Base Address of register
* @param[in]        CallBackPtr     pointer to callback api
* @param[in]        TimeoutACPtr    the timeout value of program/erase operation
* @param[out]       None
* @param[in,out]    None
*
* @retval  None
*
* @note    Non-AUTOSAR support function
*/
#ifdef G32A_C_IAR
#pragma location=".acfls_code_rom"
#endif
FUNC(void, FLS_CODE) Fls_ACEntry( CONST(uint32, AUTOMATIC) BaseAddr, P2FUNC(void, FLS_CODE, CallBackPtr)( void )
                                 #if (STD_ON == FLS_TIMEOUT_SUPERVISION_EN)
                                , P2VAR(uint32, AUTOMATIC, FLS_CODE) TimeoutACPtr
                                 #endif
                                )
{
    /* start write/erase operation */
    REG_BIT_SET32(BaseAddr + (uint32)0x10U , FMC_CTRL2_STA_MASK);
    /* wait until write/erase completion or timeout */
    while((1U == REG_BIT_GET32(BaseAddr + (uint32)0x0CU , FMC_STS_BUSYF_MASK))
           #if (STD_ON == FLS_TIMEOUT_SUPERVISION_EN)
            && (0U < *TimeoutACPtr)
           #endif
         )
    {
        #if (STD_ON == FLS_TIMEOUT_SUPERVISION_EN)
        (*TimeoutACPtr)--;
        #endif
    }

    if(FMC_STS_OCF_MASK == REG_BIT_GET32(FMC_STS_REG_ADDR, FMC_STS_OCF_MASK))
    {
        REG_BIT_SET32(FMC_STS_REG_ADDR, FMC_STS_OCF_MASK);
    }
    else
    {
        /* do nothing */
    }

    if (NULL_PTR != CallBackPtr)
    {
        CallBackPtr();
    }
    else
    {
        /* do nothing */
    }
}

/**@} end of group FLS_Fuctions */

#define FLS_STOP_SEC_CODE_AC
#include "Fls_MemMap.h"

#ifdef __cplusplus
    }
#endif

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