#ifndef _FLASH_INTERN_H_
#define _FLASH_INTERN_H_
//----------------------------------------------------------------------------
///
/// \brief  Intern Flash
///
///         See implementation files for further information.
//
/// \date   20190919
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


//---Includes-----------------------------------------------------------------


#include <stdint.h>


//---Own------------------------------

#include <lepto/lepto.h>      // address_t
#include <biwak/flash.h>      // address_t
#if defined( STM32 )
    #include <HALWrapper/stm32_flash.h>  // FLASH_PAGE_SIZE
#else
    #define FLASH_PAGE_SIZE     0x400
#endif

#if defined FLASH_TYPEPROGRAM_WORD
   #define FLASH_32BIT_ACCESS    1
#endif


//---Defines------------------------------------------------------------------



//---Declaration--------------------------------------------------------------


class CFlashIntern
         :public CFlash
{

   private:

   public:
      CFlashIntern( );
      void init();
      ~CFlashIntern();

      uint32_t writeDW(uint32_t address, int data);
      void program(uint32_t address, uint64_t data) const;

#if 0
      virtual int erasePageSize() const override
      {
         #if defined (STM32F4)
            // Really? 16K on STM32F4? 0x4000
            // Na pages at all? fix it to 0x800
            return( 0x800 );
         #else
            return(FLASH_PAGE_SIZE);
         #endif
      }
#endif
      virtual int writeData(address_t address, const void *data, int size) override final;
      //#if defined IS_ENABLED( CONFIG_BIWAK_FLASH_32BIT_COMMANDS )
      
      #if defined FLASH_TYPEPROGRAM_WORD
         virtual int writeData32(address_t address, const void *data, int size);
      #elif defined FLASH_TYPEPROGRAM_DOUBLEWORD
         virtual int writeData64(address_t address, const void *data, int size);
      #endif
      
      virtual int readData(address_t address, void *data, int size) override final;
      virtual int erasePage(address_t pageAddr, int count=1) override final;

      void clearPER() const;
      address_t flashStart() const;
      address_t flashEnd() const;
      int totalSize() const;
      void eraseUserFlash();
      address_t addressToPage( address_t address ) const;

      address_t userStart() const;
      int userSize() const;
};


//---fin----------------------------------------------------------------------
#endif // ? ! _FLASH_INTERN_H_
