/*
 * EepromAccess.h
 *
 *  Created on: Jun 2, 2025
 *      Author: tummalanr
 */

#ifndef USER_ADDONS_EEPROMTEMPERATURE_USERSPACE_EEPROMACCESS_H_
#define USER_ADDONS_EEPROMTEMPERATURE_USERSPACE_EEPROMACCESS_H_

#include <kernel/dpl/TaskP.h>
#include <kernel/dpl/SemaphoreP.h>
#include <kernel/dpl/EventP.h>
#include "SystemStsConfig.h"

/**
 * @brief
 * Eeprom Test Address in Eeprom.
 * page 6 Total 128 Bytes allocated.
 * Starting Address : (6 * 128)        =  768(0x300)
 * Ending Address   : (6 * 128)        =  768(0x300)
 */
#define             EEPROMTESTADDR                  0x00000305u

/**
 * @brief
 * Main Bus Node Identification Information Address in Eeprom.
 * page 7 & 8  Total 256 Bytes allocated.  Required : 128 Bytes.
 * Starting Address : (7 * 128)        =  896(0x380)
 * Ending Address   : (8 * 128) + 128  = 1152(0x480)
 */
#define             MBNIDENTIFICATIONINFOADDR       0x00001A00u

/**
 * @brief
 * Main Bus Node IP-1 Address in Eeprom Main Core port.
 * page 9  Total 128 Bytes allocated.  Required : 8 Bytes.
 * Starting Address : (9 * 128)        = 1152(0x480)
 * Ending Address   : (9 * 128) + 128  = 1280(0x500)
 */
#define             MBNIPADDRESS1INFOADDR       0x00001680u

/**
 * @brief
 * Main Bus Node IP-2 Address in Eeprom CIP Core port-1.
 * page 9  Total 128 Bytes allocated.  Required : 8 Bytes.
 * Starting Address : (9 * 128)        = 1162(0x494)
 * Ending Address   : (9 * 128) + 128  = 1280(0x500)
 */
#define             MBNIPADDRESS2INFOADDR       0x00001688u

/**
 * @brief
 * Main Bus Node IP-3 Address in Eeprom CIP Core port-2.
 * page 9  Total 128 Bytes allocated.  Required : 8 Bytes.
 * Starting Address : (9 * 128)        = 1162(0x48A)
 * Ending Address   : (9 * 128) + 128  = 1280(0x500)
 */
#define             MBNIPADDRESS3INFOADDR       0x00001690u


/**
 * @brief
 * Main Bus Node MAC-1 Address in Eeprom.
 * page 9  Total 128 Bytes allocated.  Required : 10 Bytes.
 * Starting Address : (9 * 128)        = 1185(0x4A1)
 * Ending Address   : (9 * 128) + 128  = 1195(0x4AB)
 */
#define             MBNMACADDRESS1INFOADDR      0x00001A80u

/**
 * @brief
 * Main Bus Node MAC-2 Address in Eeprom.
 * page 9  Total 128 Bytes allocated.  Required : 10 Bytes.
 * Starting Address : (9 * 128)        = 1195(0x4AB)
 * Ending Address   : (9 * 128) + 128  = 1205(0x4AB)
 */
#define             MBNMACADDRESS2INFOADDR      0x00001A94u
/**
 * @brief
 * Main Bus Node MAC-3 Address in Eeprom.
 * page 9  Total 128 Bytes allocated.  Required : 10 Bytes.
 * Starting Address : (9 * 128)        = 1205(0x4B5)
 * Ending Address   : (9 * 128) + 128  = 1214(0x4BE)
 */
#define             MBNMACADDRESS3INFOADDR      0x00001AA4u


/**
 * @brief
 * Main Bus Node PIN Address in Eeprom.
 * page 9  Total 128 Bytes allocated.  Required : 8 Bytes.
 * Starting Address : (9 * 128)        = 1220(0x4C4)
 * Ending Address   : (9 * 128) + 128  = 1228(0x4CB)
 */
#define             MBNPINNUMBERINFOADDR       0x00001D80u


/**
 * @brief
 * Main Bus Node PIASSWORD Address in Eeprom.
 * page 9  Total 128 Bytes allocated.  Required : 10 Bytes.
 * Starting Address : (9 * 128)        = 1229(0x4CC)
 * Ending Address   : (9 * 128) + 128  = 1239(0x4D7)
 */
#define             MODULEPASSWORDINFOADDR     0x00001D88u


/**
 * @brief
 * Main Bus Node OPERATION MODE Address in Eeprom. Service Mode/Operation Mode/Production Mode
 * page 9  Total 128 Bytes allocated.  Required : 5 Bytes.
 * Starting Address : (9 * 128)        = 1240(0x4D8)
 * Ending Address   : (9 * 128) + 128  = 1244(0x4DC)
 */
#define             MBNOPERATIONMODEINFOADDR   0x00001960u

/**
 * @brief
 * Main Bus Node PROTOCOL Address in Eeprom. EthernetIP/Ethercat/Profinet
 * page 9  Total 128 Bytes allocated.  Required : 5 Bytes.
 * Starting Address : (9 * 128)        = 1245(0x4DD)
 * Ending Address   : (9 * 128) + 128  = 1249(0x4E1)
 */
#define             MBNPROTOCOLINFOADDR   0x00001982u



/**
 * @brief
 * Product SHORT Code Information Address in Eeprom.
 * page 10  Total 128 Bytes allocated.  Required : 104 Bytes.
 * Starting Address : (10 * 128)        =  1280(0x500)
 * Ending Address   : (10 * 128) + 128  =  1408(0x568)
 */
#define             PRODUCTSHORTCODEINFOADDR         0x00001B00u


/**
 * @brief
 * Modules (IO and Pressurezone) Discovery Information Address in Eeprom.
 * page 11 & 12  Total 256 Bytes allocated.  Required : 193 Bytes.
 * Starting Address : (11 * 128)        =  1408(0x580)
 * Ending Address   : (12 * 128) + 128  =  1664(0x680)
 */
#define             MODULEDISCOVERYINFOADDR         0x00000580u


#define             VALIDRECORDHEADER       0xEEu

#define             CIP_INST1_IPADDRESS     0x00u
#define             CIP_INST2_IPADDRESS     0x01u
#define             MAINCORE_IPADDRESS      0x02u


#define             MAINCORE_DEFAULT_IPADDRESS      0xc0a80164          //"192.168.1.100"
#define             CIP_INST1_DEFAULT_IPADDRESS     0xc0a8010a          //"192.168.1.10"
#define             CIP_INST2_DEFAULT_IPADDRESS     0xc0a8010a          //"192.168.1.10"

#define             MAINCORE_MACADDRESS      0x00u
#define             CIP_INST1_MACADDRESS     0x01u
#define             CIP_INST2_MACADDRESS     0x02u

#define             MAINCORE_PIN             0x00u
#define             MAINCORE_PSWD            0x01u

/**
 * @brief
 * Ethernet/IP Protocol Used in the Product
 */
#define             CIP_EIP_PROTOCOL         0x01u
/**
 * @brief
 * EtherCat Protocol Used in the Product
 */
#define             CIP_EC_PROTOCOL          0x02u


/**
 * @brief
 * MBN IN OPERATIONAL MODE
 */
#define             MBN_OPRMODE_OPERATIONAL         0x01u
/**
 * @brief
 * MBN IN COMMISIONING MODE
 */
#define             MBN_OPRMODE_COMMISIONING         0x02u
/**
 * @brief
 * MBN IN SERVICE MODE
 */
#define             MBN_OPRMODE_SERVICE              0x03u

#define             CIP_EEPROM_ERROR                0x00000001u         //Later check the position
#define             CIP_TEMPSENSOR_TEST_ERROR       0x00000002u         //Later check the position

/* Event objects, typically these are global's */
EventP_Object gEepromAccessSynchEvent;

#define APP_EEPROMACCS_TASK_STACK_SIZE       (16384u)

void        APP_EepromAccessTask(void* pArgs_p);
AppRET_T    EepromWrite_ModuleDiscoveryInfo(void);
AppRET_T    EepromWrite_MBNIPAddress(uint8_t  instno, uint8_t IsDefault);
AppRET_T    EepromWrite_CIP_ProtocolValue( uint8_t IsDefault);
AppRET_T    EepromWrite_MBNPIN_Value(uint8_t  pinpswd, uint8_t IsDefault);
AppRET_T    EepromWrite_MBN_OperationMode( uint8_t OprMode);


extern system_discoverystatusinfo  app_sys_discvrystsinfo;

#endif /* USER_ADDONS_EEPROMTEMPERATURE_USERSPACE_EEPROMACCESS_H_ */
