//-----------------------------------------------------------------------------
///
/// \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_i2c.hpp>
#include <biwak/i2c.hpp>
#include <biwak/i2c_slave.h>
#include <biwak/timer.hpp>             // usleep
#include <HALWrapper/stm32_i2c.h>      // dac wants that


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


CFlashI2c::CFlashI2c( CI2c &_i2c, int address )
   :CI2cSlave(_i2c, address)
   ,CFlash(0, 128)
{
   m_erasePageSize = m_writePageSize = 8;
   return;
}

CFlashI2c::~CFlashI2c()
{

}

int CFlashI2c::erasePage(address_t page, int count /*=1*/ ) /* virtual */
{
   // Nothing to be done
   return(0);
}

int CFlashI2c::writeData(address_t address, const void *data, int size) /* virtual */
{
   int sta;
   const uint8_t* bData=(const uint8_t *)data;
   const int pageSize=writePageSize();
   int partSize;

   do
   {
      // The remaining size to the end of an page; if none: pageSize
      partSize=pageSize-(address % pageSize);
      partSize=MIN( partSize, size);
      
      // Writing can take a lot of time
      sta=waitBusy( ) ? 2 : 0;
      if(!sta)
      {
         sta=i2cWriteData(address, bData, partSize);
      }
      address+=partSize;
      bData+=partSize;
      size-=partSize;
   } while( size && (!sta) );

   return(sta);
}

int CFlashI2c::readData(address_t address, void *data, int size) /* virtual */
{
   int sta=0;
   sta=waitBusy( );
   if(!sta)
   {
      sta=i2cReadData( address, data, size);
   }
   //lAssert(!sta);

   return(sta);
}


void CFlashI2c::dump()
{
   int status;
   char cdata;

#if 0
   printf("Dump @0x%X width=%d:\n", getSlaveAddress(), getAddressWidth() == EAddressSize::_16Bits ? 16 : 8 );
   printf("    | -0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -A -B -C -D -E -F\n");
   printf("----|------------------------------------------------\n");
   for(int i1=0; i1< 8; i1++)
   {
      printf(" %X- | ", i1);
      for(int i2=0; i2<0x10; i2++)
      {
         status=i2cReadData( (i1*0x10)+i2, &cdata, 1);
         if(status)
         {
            printf("-- ");
         }
         else
         {
            printf("%02X ", cdata);
         }
      }
      printf("\n");
   }
#else
   printf("{\n");
   for(int i1=0; i1< 8; i1++)
   {
      for(int i2=0; i2<0x10; i2++)
      {
         status=i2cReadData( (i1*0x10)+i2, &cdata, 1);
         if(status)
         {
            printf("-- ");
         }
         else
         {
            printf("0x%02X, ", cdata);
         }
      }
      printf("\n");
   }
   printf("};\n");
#endif

   return;
}


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