#ifndef EXTMEM_FLASH_H
#define EXTMEM_FLASH_H
  #include <string.h>
  #include <stdbool.h>
  #include "stm32l1xx_hal.h"
  #include "drivers/spi/spi.h"
  #include "drivers/flash/at45db321d.h"

  #define EXTMEM_HIBERNATE_ENABLED   1

  typedef struct
  {
      #if AT45DB321D_POWER_MANAGEMENT
      void ( *SetPinPowerOn)(); 
      void ( *SetPinPowerOff)();
      #endif
      #if AT45DB321D_RESET_MANAGEMENT                                         
      void ( *ResetPinAssert)(); 
      void ( *ResetPinRelease)();
      #endif
      #if AT45DB321D_HW_WR_PROTECT
      void ( *HwWrProtPinAssert)(); 
      void ( *HwWrProtPinRelease)();
      #endif
  }
  ExtMemPins_Handle_t;
  
  extern const ExtMemPins_Handle_t ExtMemPinsHandle;


  typedef enum 
  {
      extmem_bank_factory,
      extmem_bank_user,
  }
  eExtMem_Bank_t;

  typedef struct
  {
     flash_size_t factoryBankSize;
     flash_size_t userBankSize;
  }
  ExtMem_Bank_Properties_t;

  typedef struct
  {
     bool ( *Init)();     
     bool ( *Write)( flash_address_t address, __FLASH_BYTE * pBuffer, flash_address_t size );  
     bool ( *Read)( flash_address_t address, __FLASH_BYTE * pBuffer, flash_address_t size );
     bool ( *RangeCheck_Read)( flash_address_t address, flash_address_t size );  
     bool ( *RangeCheck_Write)( flash_address_t address, flash_address_t size ); 
     bool ( *BankProtect)( eExtMem_Bank_t bankId, bool protectStatus );
     bool ( *CheckBankProtect)( eExtMem_Bank_t bankId, bool * pActualProtectStatus );
     bool ( *DeInit)( bool bForce );

     const flash_properties_t * pFlashProperties;
     const ExtMem_Bank_Properties_t * pBanksProperties;     
  }
  ExtMem_Handle_t;

  extern const ExtMem_Handle_t ExtMemHandle;
#endif