//-----------------------------------------------------------------------------
///
/// \brief  Flash
///
///         internal flash.
///
/// \date   20150416
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//-----------------------------------------------------------------------------


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


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

#include <lepto/log.h>
#include <biwak/biwak.h>         // exception
#include <biwak/flash.h>
// #include <stm/stm32_flash.h>       // dac wants that


//---Implementation------------------------------------------------------------


extern char _start_user_flash;


CFlash::CFlash( address_t address, int size )
   :m_size( size )
{
   m_userAddress = m_startAddress = address;
}


CFlash::~CFlash()
{
   // Foo
}


/// \brief  Aquires some flash for user use
///
///         The flash is reservated starting from the end of the flash. This
///         guarantees reservated flash to be at se same address when the
///         application size changes.
address32_t CFlash::acquireMemory(int size)
{
   address_t acquiredPtr;

   lDebugAssert( ( size % (address_t)erasePageSize()) == 0 );

   acquiredPtr=m_userAddress;
   
   // No overflow of originally reserveds size
   lAssert( m_userAddress + size <= m_startAddress + m_size );
   m_userAddress+=size;

   return( acquiredPtr );
}


int CFlash::erasePageCount() const
{
    return(
        size() / erasePageSize()
    );
}


int CFlash::availableSize() const
{
   return ( ( m_startAddress + m_size) - m_userAddress );
}


//---fin-----------------------------------------------------------------------
