/******************************************************************************/
/* Williams Advanced Engineering Limited                     Copyright(C) 2021*/
/* Grove                                                  All rights reserved */
/*                                                                            */
/******************************************************************************/


#ifndef MAINSOURCES_INCLUDE_IOS_FLASH_H_
#define MAINSOURCES_INCLUDE_IOS_FLASH_H_

#include "hal_stdtypes.h"
#include "system.h"
#include "F021.h"

typedef struct Sectors
{
  void * start;
  uint32 length;       // number of 32-bit words
  uint32 bankNumber;
  uint32 sectorNumber;
  uint32 FlashBaseAddress;
} SECTORS;


typedef struct Banks
{ void * start;
  uint32 length;  // number of 32-bit words
  uint32 bankNumber;
  uint32 FlashBaseAddress;
  uint32 numOfSectors;
} BANKS;


typedef enum
{
    FlsJobReqNone = 0,
    FlsJobReqEraseApp,
    FlsJobReqEraseCal,
    FlsJobReqEraseAppAndCal,
    FlsJobReqProgram,
} FlashJobType;

typedef enum
{
    FlsJobComplete = 0,
    FlsJobInit,
    FlsJobEraseSectorStart,
    FlsJobCheckEraseRslt,
    FlsJobEraseBlanckCheck,
    FlsJobFailed
} FlashJobStatusType;


typedef struct
{
    FlashJobType       CurrentFlashJobReq;
    FlashJobStatusType CurrentFlashJobStatus;

}ATTRIBUTE_PACKED FlashMngrStatusType;


#define NUMBEROFSECTORS 27

#define NUMBEROFBANKS 2

#define FLASH_ERASE_TIMEOUT 4000000

#define NUM_FAILED_BLANK_CHECK_ALLOWED 3

#define FLASH_BANK_WIDTH_BITS       128
#define FLASH_BANK_WIDTH_BYTES      16 // 128/8 = 16bytes


extern uint32 FlsApiLoadStart;
extern uint32 FlsApiLoadSize;
extern uint32 FlsApiRunStart;


extern uint32 FlsApiconstLoadStart;
extern uint32 FlsApiconstLoadSize;
extern uint32 FlsApiconstRunStart;

/*------------------------------------------------------------*/
/*          Functions prototypes definition                   */
/*------------------------------------------------------------*/
/* Public */
//extern void fnIosTestFlashJobs(const uint8* bDataToProgram, uint32 ulDataToProgramLengthBytes);
extern void fnIosFlsJobRqstManager(void);
extern void fnIosFlsJobSetRqst(FlashJobType FlashJobReq);
extern FlashJobStatusType fnIosFlsJobGetCurrentJobStatus(void);
extern void fnIosFlsJobResetCurrentJobStatus(void);
extern FlashJobType fnIosFlsJobGetRqst(void);

/* Private */
uint8 Fapi_Init(uint32 ulBankToInit); //, uint32 ucEndBank);
uint8 Fls_PgmFlashEraseCheck(uint32 Start_Address, uint32 Bytes);
uint8 Fls_PgmFlashErase( uint32 ulTargetAddress, uint32 ulLength );
uint8 Fls_PgmFlashWrite( uint32 TargetAddress, const uint8* SourceAddressPtr, uint32 Length );
uint8 Fls_PgmFlashCompare(uint32 SourceAddress, const uint8* TargetAddressPtr, uint32 Length);


#endif /* MAINSOURCES_INCLUDE_IOS_FLASH_H_ */
