/******************************************************************************/
/**
*
* Fls.h
*
* This file contains Macro and data structure declarations of Fls related
* functionalities
* <pre>
*
* MODIFICATION HISTORY:
*
* Ver   Who   Date    Changes
* ----- ---- -------- ----------------------------------------------------------
* 1.0.0 KPIT 01/12/17 First release.
* 1.0.1 KPIT 01/16/17 As per CR 10 banner is updated.
* 1.0.2 KPIT 04/11/17 As per CR11 and CR19
                       Parallel mode is supported.
                       Generic Implementation is Adapted for All Supported Flash
                       Devices.
                       Write Enable Command issued before Entering-4-ByteAdd
                       Mode.
                       Compiler abstraction added for asm and volatile.
* 1.0.3 KPIT 11/11/19  The FLS driver does not support writing less than a configurable 
*                      page size and does not guarantee that a write transfer does not 
*                      exceed a physical page
*                      Fls_MainFunction function will be executed even if Fls module is not initialized.
*                      FLS Supports Macronix flash as well.
*                      Version for external modules checks are removed. 
* </pre>
*
*******************************************************************************/

/*******************************************************************************
**                      MISRA C Rule Violations                               **
*******************************************************************************/


#ifndef FLS_H
#define FLS_H

#ifdef __cplusplus
extern "C"{
#endif
/*******************************************************************************
**                      INCLUDE FILES                                         **
*******************************************************************************/

#include "Std_Types.h"

#if (FLS_DEV_ERROR_DETECT == STD_ON)
#include "Det.h"
#endif
/* Pre-compile/static configuration parameters for FLS  */
#include "Fls_Cfg.h"

/*******************************************************************************
**                      EXPORTED CONSTANTS and MACROS                         **
*******************************************************************************/
#define FLS_VENDOR_ID         39u
#define FLS_MODULE_ID         92u
#define FLS_INSTANCE_ID       1u

#define FLS_AR_RELEASE_MAJOR_VERSION        4u
#define FLS_AR_RELEASE_MINOR_VERSION        2u
#define FLS_AR_RELEASE_REVISION_VERSION     2u

#define FLS_SW_MAJOR_VERSION       1u
#define FLS_SW_MINOR_VERSION       0u
#define FLS_SW_PATCH_VERSION       3u

/*******************************************************************************
**                      DET Error Codes                                       **
*******************************************************************************/
#if (FLS_DEV_ERROR_DETECT == STD_ON)
/* A wrong parameter passed to Fls_Init API. */
#define FLS_E_PARAM_CONFIG                  (uint8)0x01

/* A wrong address passed to the APIs */
#define FLS_E_PARAM_ADDRESS                 (uint8)0x02

/* A wrong length of the address passed to the APIs */
#define FLS_E_PARAM_LENGTH                  (uint8)0x03

/* A null buffer address passed to the APIs */
#define FLS_E_PARAM_DATA                    (uint8)0x04

/* API service is used without driver initialization */
#define FLS_E_UNINIT                        (uint8)0x05

/* API service used when the driver is busy */
#define FLS_E_BUSY                          (uint8)0x06

/* DET error code to report that the erase job has failed */
#define FLS_E_VERIFY_ERASE_FAILED           (uint8)0x07

/* DET error code to report that the block is not erased for a write job */
#define FLS_E_VERIFY_WRITE_FAILED           (uint8)0x08

/*
 * DET error code to report when timeout supervision of a read, write, erase or
 * compare job failed
 */
#define FLS_E_TIMEOUT                       (uint8)0x09

/* API Fls_GetVersionInfo invoked with a null pointer */
#define FLS_E_PARAM_POINTER                 (uint8)0x0A

#define FLS_E_PARAM_HWID                   (uint8)0x0B
#define FLS_E_SYS_RESP_FAILURE             (uint8)0x0C
#define FLS_E_SYS_CMD_FAILURE              (uint8)0x0D
#define FLS_E_INVALID_OPERATION            (uint8)0x0E
/* QSPI */
#define FLS_E_READ_BUSY                    (uint8)0x0F
#define FLS_E_RAF_NOT_CLEAR                (uint8)0x10
#define FLS_E_MSPI_FINISH                  (uint8)0x11
#define FLS_E_PARAM_CMD                    (uint8)0x12
#define FLS_E_FSBL_CONSISTENCE_FAIL        (uint8)0xE3

/* Runtime Errors */
#define FLS_E_ERASE_FAILED                  (uint8)0x1
#define FLS_E_WRITE_FAILED                  (uint8)0x2
#define FLS_E_READ_FAILED                   (uint8)0x3
#define FLS_E_COMPARE_FAILED                (uint8)0x4
#define FLS_E_UNEXPECTED_FLASH_ID           (uint8)0x5



/*******************************************************************************
**                        Service IDs                                         **
*******************************************************************************/

/* Service Id of Fls_Init */
#define FLS_INIT_SID                        (uint8)0x00
/* Service Id of Fls_Erase */
#define FLS_ERASE_SID                       (uint8)0x01
/* Service Id of Fls_Write */
#define FLS_WRITE_SID                       (uint8)0x02
/* Service Id of Fls_Cancel */
#define FLS_CANCEL_SID                      (uint8)0x03
/* Service Id of Fls_GetStatus */
#define FLS_GET_STATUS_SID                  (uint8)0x04
/* Service Id of Fls_GetJobResult */
#define FLS_GET_JOB_RESULT_SID              (uint8)0x05
/* Service Id of Fls_MainFunction */
#define FLS_MAINFUNCTION_SID                (uint8)0x06
/* Service Id of Fls_Read */
#define FLS_READ_SID                        (uint8)0x07
/* Service Id of Fls_Compare */
#define FLS_COMPARE_SID                     (uint8)0x08
/* Service Id of Fls_SetMode */
#define FLS_SET_MODE_SID                    (uint8)0x09
/* Service Id of Fls_GetVersionInfo */
#define FLS_GET_VERSION_INFO_SID            (uint8)0x10
/* Service Id of Fls_BlankCheck */
#define FLS_BLANK_CHECK_SID                 (uint8)0x0A

#endif

#define FLS_ZERO                            0x00UL
#define FLS_ONE                             0x01UL
#define FLS_TWO                             0x02UL
#define FLS_THREE                           0x03UL
#define FLS_FOUR                            0x04UL

/*******************************************************************************
**                      EXPORTED STRUCTURES, ENUMS, TYPEDEFS                  **
*******************************************************************************/

/* Address offset from the configured flash base address to access a certain
 * flash memory area
 */
typedef uint32 Fls_AddressType;

/* Specifies the number of bytes to read/write/erase/compare */
typedef uint32 Fls_LengthType;

/*
Type:Fls_NotifFunctionPtrType
Funtion pointer for JobEnd and JobError Notification 
*/
typedef void (*Fls_NotifFunctionPtrType)(void);

/**
 * Flash controller ID type
 */
typedef uint32 Fls_HwIDType;

/**
 * Flash SPI mode type
 */
typedef uint32 Fls_SPIModeType;

/**
 * Flash speed type
 */
typedef uint32 Fls_SpeedType;

/**
 * Flash read lane type
 */
typedef uint32 Fls_ReadLaneType;

/**
Structure to hold information for sector
*/
typedef struct
{
   /* Start address of writeable sector */
   Fls_AddressType   ulFlsSectorStartaddress;

   /* Number of writeable sectors */
   uint32   ulFlsNumberOfSectors;

}Fls_SectorType;


typedef struct
{
  /** Total Flash size in bytes */
  uint32 ulFlsTotalSize;

  /** Flash sector size in bytes */
  uint32 ulFlsPageSize;

  /** Flash page size in bytes */
  uint32 ulFlsSectorSize;

  /** QSPI mode */
  Fls_SPIModeType   ucFlsQspiMode;

  /** Number of lane for operation */
  Fls_ReadLaneType  ucFlsBusWidth;

  /** Size of flashable sector list */
  uint32 ulFlsSectorListSize; 

  /** Pointer to flashable sector list */
  P2CONST(Fls_SectorType, AUTOMATIC, FLS_CONFIG_DATA) pFlsSectorList;

  /** Clock div value to set baudrate */
  uint32 ucFlsQspiBaudRateDivider;

  /** Flash memory connection mode  */
  uint8 ucFlsConnectionMode;

  /** Flash ID */
  uint32     flsID;

}Fls_MemConfigType;

typedef struct
{

  /* Function pointer for JobEndNotification.*/
  Fls_NotifFunctionPtrType pFlsJobEndNotificationPtr;

  /* Function pointer for JobErrorNotification. */
  Fls_NotifFunctionPtrType pFlsJobErrorNotificationPtr;

}Fls_ConfigType;

/*******************************************************************************
**                      EXPORTED FUNCTIONS                                    **
*******************************************************************************/

#define FLS_START_SEC_CODE_SLOW_ASIL_C
#include "Fls_MemMap.h"

extern FUNC(void, FLS_CODE_SLOW) Fls_Init(
                 P2CONST(Fls_ConfigType, AUTOMATIC, FLS_APPL_CONST) ConfigPtr);

#define FLS_STOP_SEC_CODE_SLOW_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_SLOW_ASIL_C
#include "Fls_MemMap.h"

#if (FLS_VERSION_INFO_API == STD_ON)
extern FUNC(void, FLS_CODE_SLOW) Fls_GetVersionInfo(
         CONSTP2VAR(Std_VersionInfoType, AUTOMATIC, Fls_APPL_DATA) versioninfo);
#endif /* #if (FLS_VERSION_INFO_API == STD_ON)*/

#define FLS_STOP_SEC_CODE_SLOW_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_ASIL_C
#include "Fls_MemMap.h"

#if (FLS_CANCEL_API == STD_ON)
extern FUNC(void, FLS_CODE) Fls_Cancel(void);
#endif /* #if (FLS_CANCEL_API == STD_ON) */

#define FLS_STOP_SEC_CODE_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

extern FUNC(Std_ReturnType, FLS_CODE_FAST) Fls_Erase( 
                                 VAR(Fls_AddressType, AUTOMATIC) TargetAddress,
                                 VAR(Fls_LengthType, AUTOMATIC) Length);

#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

extern FUNC(Std_ReturnType, FLS_CODE_FAST) Fls_Write(
                      VAR(Fls_AddressType, AUTOMATIC) TargetAddress,
                      P2CONST(uint8, AUTOMATIC, FLS_APPL_DATA) SourceAddressPtr,
                      VAR(Fls_LengthType, AUTOMATIC) Length);

#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

#if (FLS_BLANK_CHECK == STD_ON)
extern FUNC(Std_ReturnType, FLS_CODE_FAST) Fls_BlankCheck( 
                          VAR(Fls_AddressType, AUTOMATIC) TargetAddress,
                          VAR(Fls_LengthType, AUTOMATIC) Length);
#endif /* FLS_BLANK_CHECK == STD_ON */

#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"


#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

#if (FLS_GET_STATUS_API == STD_ON)
extern FUNC(MemIf_StatusType, FLS_CODE_FAST) Fls_GetStatus(void);
#endif /* #if (FLS_GET_STATUS_API == STD_ON) */

#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"


#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

#if (FLS_GET_JOB_RESULT_API == STD_ON)
extern FUNC(MemIf_JobResultType, FLS_CODE_FAST) Fls_GetJobResult(void);
#endif /* #if (FLS_GET_JOB_RESULT_API == STD_ON) */

#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

extern FUNC(Std_ReturnType, FLS_CODE_FAST) Fls_Read(
                     VAR(Fls_AddressType, AUTOMATIC) SourceAddress,
                     P2VAR(uint8, AUTOMATIC, FLS_APPL_DATA) TargetAddressPtr,
                     VAR(Fls_LengthType, AUTOMATIC) Length);

#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

#if (FLS_COMPARE_API == STD_ON)
extern FUNC(Std_ReturnType, FLS_CODE_FAST) Fls_Compare(
                     VAR(Fls_AddressType, AUTOMATIC) SourceAddress,
                     P2CONST(uint8, AUTOMATIC, FLS_APPL_DATA) TargetAddressPtr,
                     VAR(Fls_LengthType, AUTOMATIC) Length);
#endif /* #if (FLS_COMPARE_API == STD_ON) */

#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

extern FUNC(void, FLS_CODE_FAST) Fls_SetMode (MemIf_ModeType Mode);

#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"
extern FUNC(void, FLS_CODE_FAST) Fls_MainFunction(void);

#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

/*******************************************************************************
**                      EXPORTED VARIABLES                                     **
*******************************************************************************/

#define FLS_START_SEC_VAR_FAST_CLEARED_ASIL_C_UNSPECIFIED
#include "Fls_MemMap.h"

extern P2CONST(Fls_ConfigType, FLS_VAR_CLEARED, FLS_APPL_DATA) Fls_ConfigPtr;

#define FLS_STOP_SEC_VAR_FAST_CLEARED_ASIL_C_UNSPECIFIED
#include "Fls_MemMap.h"

#define FLS_START_SEC_CONFIG_DATA_ASIL_C_UNSPECIFIED
#include "Fls_MemMap.h"

extern CONST(Fls_ConfigType, FLS_CONFIG_DATA) Fls_Config;

#define FLS_STOP_SEC_CONFIG_DATA_ASIL_C_UNSPECIFIED
#include "Fls_MemMap.h"

#define FLS_START_SEC_CONFIG_DATA_ASIL_C_UNSPECIFIED
#include "Fls_MemMap.h"
extern CONST(Fls_MemConfigType, FLS_CONFIG_DATA) Fls_MemConfig;

#define FLS_STOP_SEC_CONFIG_DATA_ASIL_C_UNSPECIFIED
#include "Fls_MemMap.h"


#define FLS_START_SEC_VAR_CLEARED_ASIL_C_8
#include "Fls_MemMap.h"

#if (FLS_DEV_ERROR_DETECT == STD_ON)
extern VAR(uint8, FLS_VAR_FAST_CLEARED) Fls_ApiId;
#endif /* #if (FLS_DEV_ERROR_DETECT == STD_ON) */

#define FLS_STOP_SEC_VAR_CLEARED_ASIL_C_8
#include "Fls_MemMap.h"

#define FLS_START_SEC_VAR_CLEARED_ASIL_C_8
#include "Fls_MemMap.h"

extern VAR(uint8, FLS_VAR_CLEARED) Fls_CurrentJobHwId;

#define FLS_STOP_SEC_VAR_CLEARED_ASIL_C_8
#include "Fls_MemMap.h"

/*******************************************************************************
**                      VERSION CHECK                                         **
*******************************************************************************/
/* [SWS_BSW_00036] Inter module version check. */
#ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
#if ((FLS_AR_RELEASE_MAJOR_VERSION != STD_AR_RELEASE_MAJOR_VERSION) || \
     (FLS_AR_RELEASE_MINOR_VERSION != STD_AR_RELEASE_MINOR_VERSION))
    #error "Autosar version numbers of Fls.h and Std_Types.h are different."
#endif
#endif

/* SW version check of *.h/c of the own module */
#if ((FLS_SW_MAJOR_VERSION != FLS_SW_MAJOR_VERSION_CFG_H) || \
     (FLS_SW_MINOR_VERSION != FLS_SW_MINOR_VERSION_CFG_H) || \
     (FLS_SW_PATCH_VERSION != FLS_SW_PATCH_VERSION_CFG_H))
    #error "The SW version number of Fls.h and Fls_cfg.h are different."
#endif


#ifdef __cplusplus
}
#endif

#endif /* FLS_H */
/*******************************************************************************
**                               End of File                                  **
*******************************************************************************/
