#ifndef BIWAK_BRINGUP_H_
#define BIWAK_BRINGUP_H_
//----------------------------------------------------------------------------
///
/// \brief  Test I2C-EEPROM
///
///         See implementation files for further information.
///
/// \date   20220312
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


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


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

#include <biwak/spi_slave.h>
#include <biwak/flash_i2c.hpp>
#include <lepto/log.h>


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


class CBringUpObsolete
{
   public:
      static bool i2cTest( CFlashI2c &flashI2c );
      static bool i2cDump( CFlashI2c &flashI2c );
};

bool CBringUpObsolete::i2cTest( CFlashI2c &flashI2c )
{
   // Write data and read it back again
   auto i2cWriteReadTest = [flashI2c](CFlashI2c &_flashI2c, uint32_t val){
      uint32_t data;
      data=val;
      _flashI2c.writeData(0, &data, sizeof(data));
      data=0;
      _flashI2c.readData(0, &data, sizeof(data));
      return(data);
   };

   uint32_t intro;

   intro=0x12345678;
   lAssert(intro == i2cWriteReadTest(flashI2c, intro));
   intro=0xFEDCBA98;
   lAssert( intro == i2cWriteReadTest(flashI2c, intro));

   puts("I2C-Flash-Test: ok");

   return(1);
}

char iToN(int value)
{
   if( (value>=0) && (value<=9))
   {
      return('0'+ value);
   }

   return('A' + (value-0xA));
}

bool CBringUpObsolete::i2cDump( CFlashI2c &flashI2c )
{
   uint32_t intro=0x12345678;
   flashI2c.writeData( 0x44, &intro, sizeof(intro));

   puts("   -0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -A -B -C -D -E -F");
   puts("   -----------------------------------------------");
   for(int i1=0; i1<0x8; i1++)
   {
      char buf[]="0-|";
      buf[0]+=i1;
      fputs(buf, stdout);
      for(int i2=0; i2<0x10; i2++)
      {
         char buf[]="00 ";
         char data;
         flashI2c.readData( (i1*0x10)+ i2, &data, sizeof(data));
         buf[0]=iToN( (data>>4) & 0xF);
         buf[1]=iToN( (data>>0) & 0xF);
         fputs(buf, stdout);
      }
      fputs("\n", stdout);
   }
   return(1);
}


//---fin----------------------------------------------------------------------
#endif // ? ! BIWAK_BRINGUP_H_
