#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/at45/at45db161e.h"
  #include "drivers/flash/w25q/w25q16jv.h"
  #include "drivers/flash/base/bitbuffer.h"
  #define EXTMEM_HIBERNATE_ENABLED   1

  

  typedef enum 
  {
      extmem_bank_factory,
      extmem_bank_user,
  }
  eExtMem_Bank_t;

  typedef struct
  {
     flash_size_t bankSize;
     flash_size_t bankAddress;
  }
  ExtMem_Bank_Properties_t;

  typedef union
  {
     struct
     {
     flash_size_t factoryBankSize;
     flash_size_t factoryBankAddress;
     flash_size_t userBankSize;
     flash_size_t userBankAddress;
     };

     struct
     {
     ExtMem_Bank_Properties_t factoryBank;
     ExtMem_Bank_Properties_t userBank;
     };

     ExtMem_Bank_Properties_t Banks[ 2 ];
  }
  ExtMem_Banks_Properties_t;

  typedef struct
  {
     bool ( *const 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 ( *const DeInit)( bool bForce );

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

  // для совместимости со старыми модулями, которые используют функции чтения/записи напрямую:
  flash_err_t flash_read( flash_address_t address, __FLASH_BYTE * pBuffer, flash_address_t size );
  flash_err_t flash_write( flash_address_t address, __FLASH_BYTE * pBuffer, flash_address_t size, flash_write_mode_t unused );

  #ifndef EXTMEM_FLASH_C
  extern const ExtMem_Handle_t ExtMemHandle;
  #endif
#endif