/**
 * @file    Fls_Lld.h
 * @version
 *
 * @brief   AUTOSAR Fls driver interface
 * @details API implementation for FLS driver
 *
 * @addtogroup FLS_MODULE
 * @{
 */
/*==================================================================================================
 *   Project              : YTMicro AUTOSAR 4.4.0 MCAL
 *   Platform             : ARM
 *   Peripheral           : Fls
 *   Dependencies         : none
 *
 *   Autosar Version      : V4.4.0
 *   Autosar Revision     : ASR_REL_4_4_REV_0000
 *   Autosar Conf.Variant :
 *   SW Version           : V0.8.0
 *
 *
 *   (c) Copyright 2020-2026 Yuntu Microelectronics co.,ltd.
 *   All Rights Reserved.
==================================================================================================*/
/*==================================================================================================
==================================================================================================*/

#ifndef FLS_DRV_H
#define FLS_DRV_H

#ifdef __cplusplus
extern "C" {
#endif


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

/*==================================================================================================
 *                               SOURCE FILE VERSION INFORMATION
==================================================================================================*/
#define FLS_DRV_VENDOR_ID                      (180)
#define FLS_DRV_AR_RELEASE_MAJOR_VERSION       (4)
#define FLS_DRV_AR_RELEASE_MINOR_VERSION       (4)
#define FLS_DRV_AR_RELEASE_REVISION_VERSION    (0)
#define FLS_DRV_SW_MAJOR_VERSION               (0)
#define FLS_DRV_SW_MINOR_VERSION               (8)
#define FLS_DRV_SW_PATCH_VERSION               (0)


/*==================================================================================================
 *                                     FILE VERSION CHECKS
==================================================================================================*/
/* Check if source file and FLS configuration header file are of the same vendor */
#if (FLS_DRV_VENDOR_ID != FLS_TYPES_VENDOR_ID)
#error "Fls_Drv.h and Fls_Types.h have different vendor ids"
#endif

/* Check if source file and FLS configuration header file are of the same Autosar version */
#if ((FLS_DRV_AR_RELEASE_MAJOR_VERSION != FLS_TYPES_AR_RELEASE_MAJOR_VERSION) || \
     (FLS_DRV_AR_RELEASE_MINOR_VERSION != FLS_TYPES_AR_RELEASE_MINOR_VERSION) || \
     (FLS_DRV_AR_RELEASE_REVISION_VERSION != FLS_TYPES_AR_RELEASE_REVISION_VERSION) \
    )
#error "AutoSar Version Numbers of Fls_Drv.h and Fls_Types.h are different"
#endif

/* Check if source file and FLS configuration header file are of the same software version */
#if ((FLS_DRV_SW_MAJOR_VERSION != FLS_TYPES_SW_MAJOR_VERSION) || \
     (FLS_DRV_SW_MINOR_VERSION != FLS_TYPES_SW_MINOR_VERSION) || \
     (FLS_DRV_SW_PATCH_VERSION != FLS_TYPES_SW_PATCH_VERSION) \
    )
#error "Software Version Numbers of Fls_Drv.h and Fls_Types.h are different"
#endif


/*==================================================================================================
 *                                       DEFINES AND MACROS
==================================================================================================*/
#define FLS_SECTOR_ERASE_COMMAND_CODE     (0x10u)
#if defined (CPU_YTM32B1HA0)
#define FLS_PAGE_PROGRAM64_COMMAND_CODE   (0x01u)
#define FLS_PAGE_PROGRAM256_COMMAND_CODE  (0x02u)
#define FLS_ERASE_RETRY_COMMAND_CODE      (0x14u)
#elif defined (CPU_YTM32B1MC0)
#define FLS_PAGE_PROGRAM_COMMAND_CODE     (0x01u)
#define FLS_ERASE_RETRY_COMMAND_CODE      (0x10u)
#define FEATURE_EFM_SECTOR_ERASE_RETRY_TIMING 400u
#elif defined (CPU_YTM32B1MD2)
#define FLS_PAGE_PROGRAM_COMMAND_CODE     (0x03u)
#elif defined (CPU_YTM32B1ME1)
#define FLS_PAGE_PROGRAM_COMMAND_CODE     (0x01u)
#else
#define FLS_PAGE_PROGRAM_COMMAND_CODE     (0x02u)
#define FLS_ERASE_RETRY_COMMAND_CODE      (0x17u)
#endif
#if defined (CPU_YTM32B1MC0)||defined (CPU_YTM32B1MD1)||defined(CPU_YTM32B1ME0)|| defined (CPU_YTM32B1HA0)
#define FLS_LOAD_AES_KEY_CMD_CODE         (0x20u)
#elif defined (CPU_YTM32B1MD2)|| defined (CPU_YTM32B1ME1)
#define FLS_LOAD_AES_KEY_CMD_CODE         (0x30u)
#else
#error "Wrong MCU part number or no MCU part number selected!"
#endif
#define FLS_EFM_CMDUNLOCK_KEY             (0xFD9573F5U)
#if defined (CPU_YTM32B1MC0)||defined (CPU_YTM32B1MD2)||defined (CPU_YTM32B1ME1)
#define FLS_ERR_FLAGS_MASK                (EFM_STS_CMD_FAIL_MASK|\
                                        EFM_STS_ACCERR_MASK|\
                                        EFM_STS_UNRECOVERR_MASK)
#elif defined (CPU_YTM32B1HA0)
#define FLS_ERR_FLAGS_MASK                (EFM_STS_FAIL_MASK|\
                                        EFM_STS_ACCERR_MASK|\
                                        EFM_STS_UNRECOVERR_MASK|\
                                        EFM_STS_CI_UNRECOVERR_MASK)
#else
#define FLS_ERR_FLAGS_MASK                (EFM_STS_FAIL_MASK|\
                                        EFM_STS_ACCERR_MASK|\
                                        EFM_STS_UNRECOVERR_MASK)
#endif
#if (FLS_WEBIT_AVAILABLE==STD_ON)
#if defined (CPU_YTM32B1HA0)
#define FLS_ADDR_SELECTED_FLAG_MASK           (EFM_STS_SET_ADDR_MASK)   /* addr selected flag */
#elif defined (CPU_YTM32B1MC0)||defined (CPU_YTM32B1MD2)
#define FLS_ADDR_SELECTED_FLAG_MASK           (EFM_STS_ARRAY_SELECTED_MASK)   /* addr selected flag */
#else
#endif
#endif
/**
* @brief the number of bytes uses to compare (1 byte).
*
*/
#define FLS_SIZE_1BYTE      1U

/**
* @brief the number of bytes uses to compare (2 bytes).
*
*/
#define FLS_SIZE_2BYTE      2U

/**
* @brief the number of bytes uses to compare (4 bytes).
*
*/
#define FLS_SIZE_4BYTE      4U
/*==================================================================================================
 *                                         EXTERNAL CONSTANTS
==================================================================================================*/

/*==================================================================================================
 *                                             ENUMS
==================================================================================================*/
/*==================================================================================================
 *                                      GLOBAL VARIABLES
==================================================================================================*/

/*==================================================================================================
 *                                 PUBLIC API FUNCTIONS
==================================================================================================*/

 /**
 * @brief         Initialize the flash low level driver.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Non Reentrant
 *                Available via: Fls_Lld.c
 *
 * @param[in]     SectorProtCfg  Pointer to the sector protection configuration.
 * @return        Fls_Lld_StatusType  Status of the initialization.
 */
Fls_Lld_StatusType Fls_Lld_Init(Fls_SectorProtectType const *SectorProtCfg);

 /**
 * @brief         Erase a sector of flash memory.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Non Reentrant
 *                Available via: Fls_Lld.c
 *
 * @param[in]     StartAddress  Start address of the sector to be erased.
 * @param[in]     Async         TRUE: Erase asynchronously.
 *                              FALSE: Erase synchronously.
 * @return        Fls_Lld_StatusType  Status of the erase operation.
 */
Fls_Lld_StatusType Fls_Lld_SectorErase(Fls_AddressType StartAddress, boolean Async);

 /**
 * @brief         Read data from flash memory.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Non Reentrant
 *                Available via: Fls_Lld.c
 *
 * @param[in]     StartAddress  Start address of the data to be read.
 * @param[in]     Length        Number of bytes to read.
 * @param[out]    TargetAddressPtr  Pointer to the target address where the data will be stored.
 * @return        Fls_Lld_StatusType  Status of the read operation.
 */
Fls_Lld_StatusType Fls_Lld_ReadFlash(Fls_AddressType StartAddress, Fls_LengthType Length, uint8 *TargetAddressPtr);

/**
 * @brief         Compare data in flash memory.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Non Reentrant
 *                Available via: Fls_Lld.c
 *
 * @param[in]     StartAddress     Start address of the data to be compared.
 * @param[in]     Length           Number of bytes to compare.
 * @param[in]     SourceAddressPtr Pointer to the source data for comparison.
 *                                 If NULL_PTR is passed, a blank check is performed.
 * @return        Fls_Lld_StatusType  Status of the compare operation.
 */
Fls_Lld_StatusType Fls_Lld_CompareFlash(Fls_AddressType StartAddress, Fls_LengthType Length, const uint8 *SourceAddressPtr);

/**
* @brief          Process ongoing erase or write hardware job.
*                 In case Async Operation is ongoing this function will complete the following job:
*                 - Erase
*                 - Erase on Interleaved sectors
*                 - Write
*                 - Erase blank Check
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Non Reentrant
 *                Available via: Fls_Lld.c
 *
 * @return        void
*/
void Fls_Lld_MainFunction(void);

/**
* @brief          IP wrapper sector write function.Route the write job to appropriate low level IP function.
*                 Route the write job to appropriate low level IP function.
* @autosar        N/A
*
* @details        Service ID: --
*                 Sync/Async: Synchronous
*                 Reentrancy: Non Reentrant
*                 Available via: Fls_Lld.c
*
* @param[in]      DestAddr - Destination address
* @param[in]      Length - Length of data to be written
* @param[in]      JobDataSrcPtr - Pointer to data source
* @param[in]      AsynchFlag - Asynchronous or synchronous job
* @return         Fls_Lld_ReturnType
*/
Fls_Lld_ReturnType Fls_Lld_SectorWrite(const Fls_AddressType DestAddr, const Fls_LengthType Length, \
                                       const uint8 *JobDataSrcPtr, const boolean AsynchFlag);

/**
 * @brief         Abort a program or erase operation.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Non Reentrant
 *                Available via: Fls_Lld.c
 *
 * @return        Fls_Lld_ReturnType
 */
Fls_Lld_StatusType Fls_Lld_Cancel(void);

/**
 * @brief            This function will Translate the return code from drv to Fls_Lld_ReturnType.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Non Reentrant
 *                Available via: Fls_Lld.c
 *
 * @param[in]     ReturnCode       Return code from EFM IP.
 * @return        Fls_Lld_ReturnType
 */
Fls_Lld_ReturnType Fls_Lld_TranslateReturnCode(Fls_Lld_StatusType ReturnCode);
/**
 * @brief           This function will get the physical sector size.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Non Reentrant
 *                Available via: Fls_Lld.c
 *
 * @param[in]     DestAddr         Start address of the data to be written.
 * @return        uint16
*/
uint16 Fls_Lld_GetPhycSectorSize(Fls_AddressType DestAddr);

/**
 * @brief         Get the one bit ecc error flag.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Non Reentrant
 *                Available via: Fls_Lld.c
 *
 * @return        boolean  TRUE: One bit ecc error occurred.
 *                         FALSE: No one bit ecc error occurred.
 */
boolean Fls_Lld_GetOneBitEccErrFlag(void);

 /**
 * @brief         Get the two bit ecc error flag.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Non Reentrant
 *                Available via: Fls_Lld.c
 *
 * @return        boolean  TRUE: Two bit ecc error occurred.
 *                         FALSE: No two bit ecc error occurred.
 */
boolean Fls_Lld_GetTwoBitEccErrFlag(void);

 /**
 * @brief         Get the ecc error address.
 * @autosar       N/A
 *
 * @details       Service ID: --
 *                Sync/Async: Synchronous
 *                Reentrancy: Non Reentrant
 *                Available via: Fls_Lld.c
 *
 * @return        Fls_AddressType  Ecc error address.
 */
Fls_AddressType Fls_Lld_GetEccErrAddr(void);

#ifdef __cplusplus
}
#endif

#endif /* FLS_DRV_H */

/** @} */

