#include "Ifx_Types.h"
#include "IfxCpu_Intrinsics.h"
#include "IfxScuWdt.h"
#include "Ifx_reg.h"
#include "IfxCpu_Irq.h"
#include <I2c/I2c/IfxI2c_I2c.h>

#include "Test_Time.h"

#pragma GCC optimize "-O0" /* Disable optimisation for debugging */

/* used globally */
static IfxI2c_I2c i2c;                        /* i2c handle */
static IfxI2c_I2c_Device I2CportExp;    	  /* Port expander device handler */
static IfxI2c_I2c_Device I2Ceeprom;    		  /* EEPROM device handle */
uint8 I2Cdata[128];                           /* data buffer */

/* create config structure */
IfxI2c_I2c_Config config; 					/* I2C module Config */
IfxI2c_I2c_deviceConfig I2CportExpConfig;  	/* Device Config */
IfxI2c_I2c_deviceConfig I2CeepromConfig;   	/* /Device Config */

/* configure pins */
const IfxI2c_Pins pins = {    &IfxI2c0_SCL_P02_5_INOUT, /* Clock Pin */
							  &IfxI2c0_SDA_P02_4_INOUT, /* Data pin */
							  IfxPort_PadDriver_cmosAutomotiveSpeed1};

uint8 I2c_Buffer[128];
uint8 I2c_Address = (0x20<<1) + 0x00; /* I2C Slave Address (shifted 1 bit to convert from 8 to 7-bit address) */
uint8 readPORTA[2] = {0x0A}; 	/* Cmd to read PORTA of the port expander */
uint8 Out_Data[2] = {0x00,0x00};/* Data which sets all PORT A pins to outputs */
uint8 data[2] = {0x0A,0x01}; 	/* Data to be sent{InternalRegAddr,PinStateByte} */
uint8 recvData[2]; 				/* Array to recieve data into */
uint8 EEPROMsend[3] = {0x00,0x05,0x55};
uint8 EEPROMrecv[2] = {0x00,0x05};

/*** Main Loop ***/
int core0_main (void)
{

	IfxI2c_I2c_deviceConfig I2CportExpConfig; /* Device Config */
	IfxI2c_I2c_deviceConfig I2CeepromConfig;  /* Device Config */

    /*
     * !!WATCHDOG0 AND SAFETY WATCHDOG ARE DISABLED HERE!!
     * Enable the watchdog in the demo if it is required and also service the watchdog periodically
     */
    IfxScuWdt_disableCpuWatchdog (IfxScuWdt_getCpuWatchdogPassword ());
    IfxScuWdt_disableSafetyWatchdog (IfxScuWdt_getSafetyWatchdogPassword ());

    Test_InitTime(); /* Initialise Delays */

    /* fill structure with default values and Module address */
    IfxI2c_I2c_initConfig(&config, &MODULE_I2C0); /* Fill the I2C module Config with default values */

    config.pins = &pins; 		/* Set Clock and Data pins */
    config.baudrate = 200000;   /* 100 kHz */

    /* Initialise module */
    IfxI2c_I2c_initModule(&i2c, &config); /* Initialise the module */

    /* Port Expander Setup */

    /* fill structure with default values and i2c Handler */
    IfxI2c_I2c_initDeviceConfig(&I2CportExpConfig, &i2c); /* Create default config for a specific slave device */

    I2CportExpConfig.deviceAddress = 0x20<<1; /* Set the slave device address */

    /* Initialise the i2c device handle */
    IfxI2c_I2c_initDevice(&I2CportExpConfig, &I2CportExp); /* Initialise the I2C device */

    /* EEPROM Setup */

    /* fill structure with default values and i2c Handler */
    IfxI2c_I2c_initDeviceConfig(&I2CeepromConfig, &i2c); /* Create default config for a specific slave device */

    I2CeepromConfig.deviceAddress = (0x50 << 1); /* Set the slave device address */

    /* Initialise the i2c device handle */
    IfxI2c_I2c_initDevice(&I2CeepromConfig, &I2Ceeprom); /* Initialise the I2C device */


    while(IfxI2c_I2c_write(&I2CportExp, Out_Data, sizeof(Out_Data)) == IfxI2c_I2c_Status_nak) /* Keep writing until the slave responds  */
    { ; } /* Set all PORTA pins to outputs  */

	while(1)
	{
		while(IfxI2c_I2c_write(&I2CportExp, data, sizeof(data)) == IfxI2c_I2c_Status_nak) /* Keep writing until the slave responds */
		{ ; } /* Write data[1] to PORTA */

		while(IfxI2c_I2c_write(&I2Ceeprom, EEPROMsend, 3) == IfxI2c_I2c_Status_nak) /* Keep writing until the slave responds */
		{ ; } /* Poll the EEPROM to see if an ACK is received */

		while(IfxI2c_I2c_write(&I2Ceeprom, EEPROMrecv, 2) == IfxI2c_I2c_Status_nak) /* Keep writing until the slave responds */
		{ ; } /* Poll the EEPROM to see if an ACK is received */

		while(IfxI2c_I2c_read(&I2Ceeprom, recvData, 1) == IfxI2c_I2c_Status_nak) /* Keep writing until the slave responds */
		{ ; } /* Poll the EEPROM to see if an ACK is received */

		data[1]++; /* Increment the value sent to the port by 1 */

	}

    return (1);
}
