// © RiceMotion ( Robert Carl Rice ) 2012-2016 - All rights reserved


// This software makes use of tools and libraries obtained from open source projects or released for

// use by relevant hardware manufactures. However, this software is NOT a part of any open source project.

// It is released only on a "need to know" basis for beta testers of the "RiceCNC Interpolation Engine".

// Recipents of this source code must respect the confidential nature of this software and prevent any

// distribution that could result in counterfeit copies of the "RiceCNC Interpolation Engine".


// © RiceMotion ( Robert Carl Rice ) 2012-2016 - All rights reserved

#ifndef MyEEProm_h

#define MyEEProm_h


#include "Energia.h"

#include "IPAddress.h"

#include "netif/etharp.h"



#define UseKey 0xA5  // something not likely to match uninitialized eeProm


class MyEEProm {

public:     

     enum SoftResetReason {

          ResetRequestFromHost,

          HostMessageTimeout,

          MaxPacketRetransmission };


     typedef union {

          uint32_t firstEEPromWord; // force word alignment

          struct {

               byte       useKey;

//               bool  useDHCP; // DHCP hangs

               byte      macAddress      [6];

               byte      myIpAddress     [4];

               byte      routerAddress   [4];

               byte      subnetMask      [4];

               uint16_t  port;

  

               byte      remoteIpAddress [4];

               uint16_t  remote_Port;

               

               SoftResetReason     softResetReason;


               // pad to allow for misaligned word boundary

               byte      padding         [3]; } S; } Un;


     static Un U;

     

     static void

          setUseKey( bool use );


     static void

          readEEProm();


     static void

          writeEEProm();


     static void

          debugEePromCopyDump( String* msgPtr );


     static void

          dumpBytes(

               byte*          buffer,

               int               count,

               String*          msgPtr );


     static SoftResetReason

          softResetReason();


     static void

          recordResetReason(

               SoftResetReason     softResetReason );


     static void

          readMacAddress( byte* mac );


     static void

          setEthernetAddresses(

               char*          data,

               String*          msgPtr );


     static void

          recordConnectionFrom(

               IPAddress     remote_IP,

               uint16_t     remote_port );


     static bool

          remoteIsRecorded (

               IPAddress     remote_IP,

               uint16_t     remote_port );


private:

     static void

          MyEEPROMRead(

               uint32_t     *pui32Data,

               uint32_t     ui32PromAddress,

               uint32_t     ui32Count);


     static uint32_t

          MyEEPROMProgram(

               uint32_t     *pui32Data,

               uint32_t     ui32Address,

               uint32_t     ui32Count);


     static void

          parseIpAddress(

               char*          stringPtr,

               byte*          readByte,

               bool*          change );


     static void

          compare(

               byte*          readByte,

               byte*          writeByte,

               int               length,

               bool*          change );

};


#endif