/*
 * Hal_Fls.h
 *
 *  Created on: 2022Äê4ÔÂ6ÈÕ
 *      Author: Zhengchao
 */

#ifndef HAL_FLS_H_
#define HAL_FLS_H_

#include "hal_adapter.h"
#include "AppFunclib.h"
#include "AppGlobalVar.h"
#include "Hal_Wdg.h"
#define EN_SUPPORT_APP_B

typedef uint32 tLogicalAddr;

typedef struct
{
    tLogicalAddr xBlockStartLogicalAddr; /* block start logical addr */
    tLogicalAddr xBlockEndLogicalAddr;   /* block end logical addr */
} BlockInfo_t;

#define LOGICAL_START_ADDR           0U

#define NUMBER_OF_INTERNAL_SECTOR    128U
#define INTERNAL_SECTOR_SIZE         0x1000U


#define APP_B_START_ADDR             0U
#define APP_B_END_ADDR               NUMBER_OF_INTERNAL_SECTOR*INTERNAL_SECTOR_SIZE

#define APP_FLAST_START_PHY_ADDR    (0x80000u)
#define APP_VECTOR_TABLE_OFFSET (0x200u) /* Vector table offset from gs_astBlockNumA/B */
#define RESET_HANDLER_OFFSET    (4u)     /* From top vector table to reset handle */
#define RESET_HANDLER_ADDR_LEN  (4u)     /* Pointer length or reset handler length */
/* Flash finger print length */
#define FL_FINGER_PRINT_LENGTH  (17u)

/* Program data buffer max length */
#define MAX_FLASH_DATA_LEN (512u)

#if (defined EN_SUPPORT_APP_B)


typedef enum
{
    CONTROLLER_SELF = 1,
	CONTROLLER_BLE,
	CONTROLLER_B,
	CONTROLLER_C,
	CONTROLLER_D,
	CONTROLLER_E,
	CONTROLLER_F,
}ControllerType;

typedef struct
{
    /* Flash programming successful? If programming successful, the value set TRUE, else set FALSE */
    uint8 isFlashProgramSuccessfull;

    /* Is erase flash successful? If erased flash successful, set the TRUE, else set the FALSE. */
    uint8 isFlashErasedSuccessfull;

    /* Is Flash struct data valid? If written set the value is TRUE, else set the valid FALSE */
    uint8 isFlashStructValid;

    /* Indicate APP Counter. Before download. */
    uint8 appCnt;

    /* Is the App self update? If selfUpdate set the value is TRUE, else set the value FALSE*/
    ControllerType controllerName;

    /* Flag if finger print buffer */
    uint8 aFingerPrint[FL_FINGER_PRINT_LENGTH];

    /* Reset handler length */
    uint32 appStartAddrLen;

    /* APP Start address - reset handler */
    uint32 appStartAddr;

    /* APP Length - reset handler */
    uint32 appLength;

    /* Count CRC */
    uint32 crc;
}tAppFlashStatus;

typedef struct
{
    /* Current process start address */
    uint32 appVectoTableStartAddr;

    /* Current process length */
    uint32 appLength;

    /* Received CRC value */
    uint32 receivedCRC;

} tAppDownloadType;

#endif

MemIf_JobResultType Hal_FlsWrite(Fls_AddressType u32TargetAddress, uint8 * pSourceAddressPtr, Fls_LengthType u32Length, uint32 timeOutMs);

MemIf_JobResultType Hal_FlsRead(Fls_AddressType u32TargetAddress, uint8 * pSourceAddressPtr, Fls_LengthType u32Length, uint32 timeOutMs);

MemIf_JobResultType Hal_FlsErase(Fls_AddressType u32TargetAddress, uint32 eraseInternalSectorNum, uint32 timeOutMs);

void Hal_FlsGetAppVectorTableStartAddr(uint32 *appVectorTableStartAddr);

void Hal_SetAppInfo(uint32 appLength, uint32 appReceviedCRC,ControllerType controllerName);

uint8 Hal_FlsCheckIsTransferSucceed(void);

extern void CRC_HAL_CreatSoftwareCrc(const uint8_t *i_pucDataBuf, const uint32_t i_ulDataLen, uint32_t *m_pCurCrc);

//extern void CreatSoftwareCrc16(const uint8 *i_pDataBuf, const uint32 i_dataLen, uint32 *m_pCurCrc);
#endif /* HAL_FLS_H_ */
