/* Setup I2C */
static void setupI2CMaster(void)
{
	/* Enable I2C clock and reset I2C peripheral */
	Chip_I2C_Init(LPC_I2C_PORT);

	/* Setup clock rate for I2C */
	Chip_I2C_SetClockDiv(LPC_I2C_PORT, I2C_CLK_DIVIDER);
	
	/* Setup I2CM transfer rate */
	Chip_I2CM_SetBusSpeed(LPC_I2C_PORT, I2C_BITRATE);

//	/* Enable I2C master interface */
	//Chip_I2CM_Enable(LPC_I2C_PORT);
}
/****************************************************************************************************
 * @fn      i2cs_setup_slave
 *          Helper routine to set up slave
 *
 ***************************************************************************************************/
static void i2cs_setup_slave(i2cs_slave_t *pSlaveSetup)
{
    uint32_t optimalDev;
    /* Limit usable slave address indexes to the maximum the controller can support */
    if (pSlaveSetup->SlaveIndex <= I2C_SLV_ADDR_NUM) {
        Chip_I2CS_SetSlaveAddr(I2C_HOSTIF, pSlaveSetup->SlaveIndex, (pSlaveSetup->slaveAddr & I2C_SLV_ADDR_MSK));
        /* Clear interrupt status */
        Chip_I2CS_ClearStatus(I2C_HOSTIF, I2C_STAT_SLVDESEL);

        if (pSlaveSetup->EnableSlave == 0) {
            Chip_I2CS_DisableSlaveAddr(I2C_HOSTIF, pSlaveSetup->SlaveIndex);
        }
        else {
            Chip_I2C_EnableInt(I2C_HOSTIF, I2C_INTENSET_SLVPENDING | I2C_INTENSET_SLVDESEL);
        }
    }

    optimalDev = Chip_Clock_GetAsyncSyscon_ClockRate()/I2C_SLV_PCLK_FREQ;
    Chip_I2C_SetClockDiv( I2C_HOSTIF, optimalDev );
    /* Enable I2C slave interface */
    Chip_I2CS_Enable(I2C_HOSTIF);
}
/****************************************************************************************************
 * @fn      I2C_HardwareSetup
 *          Configures the GPIOs and h/w interface for the I2C bus
 *
 * @param   busId - I2C bus identifier in case multiple buses are supported
 *
 * @return  true if successful, false otherwise.
 *
 ***************************************************************************************************/
osp_bool_t I2C_HardwareSetup( I2C_TypeDef *busId )
{
    if (busId == LPC_I2C0)
    {
        if (sI2C_Bus1Initialized)
        {
            return true;
        }
        /* Configure the I2C interface in Master mode with the given speed */
        Chip_IOCON_PinMuxSet(LPC_IOCON, I2C_SENSOR_BUS_SCL_PIN);
        Chip_IOCON_PinMuxSet(LPC_IOCON, I2C_SENSOR_BUS_SDA_PIN);

        Chip_I2C_Init(busId); /* Enables clock and resets the peripheral */

        /* setup speed and config. as Master */
        Chip_I2C_SetClockDiv( busId, I2C_MASTER_CLOCK_DIV );
        Chip_I2CM_SetBusSpeed( busId, I2C_MCLOCK_SPEED );
        /* Reset master state machine */
        Chip_I2CM_Disable( busId );
        Chip_I2CM_Enable( busId );

        /* Enable interrupt for pending status */
        Chip_I2C_EnableInt( busId, I2C_INTENSET_MSTPENDING );

        I2C_Master_Initialise();

        /* Configure TWI interrupts */
        NVIC_SetPriority( I2C_SENSOR_BUS_IRQn, I2C_SENSOR_BUS_INT_PRIORITY );
        NVIC_EnableIRQ( I2C_SENSOR_BUS_IRQn );           //enable I2C isr


        sI2C_Bus1Initialized = true;

        return true;
    }
    return false;
}
TempSensDig::TempSensDig(int deviceNumber, uint32_t speed) {
	if(deviceNumber == 0) {
		device = LPC_I2C0;
		Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 22, IOCON_DIGMODE_EN | I2C_MODE);
		Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 23, IOCON_DIGMODE_EN | I2C_MODE);
		Chip_SWM_EnableFixedPin(SWM_FIXED_I2C0_SCL);
		Chip_SWM_EnableFixedPin(SWM_FIXED_I2C0_SDA);
	}
	else {
		// currently we support only I2C number 0
	}
	/* Enable I2C clock and reset I2C peripheral - the boot ROM does not
	   do this */
	Chip_I2C_Init(device);

	/* Setup clock rate for I2C */
	Chip_I2C_SetClockDiv(device, I2C_CLK_DIVIDER);

	/* Setup I2CM transfer rate */
	Chip_I2CM_SetBusSpeed(device, speed);

	/* Enable Master Mode */
	Chip_I2CM_Enable(device);
}