#include "common.h" /* include peripheral declarations */
#define printf print_str

#include "i2c.h"
#include "i2c_cfg.h"

#include "pmc_driver.h"

void i2c_low_power(void);
void run_test(void);

void i2c_wakeup_master(unsigned char mst_addr, unsigned char slv_addr);
void i2c_wakeup_slave(unsigned char slv_addr);
void i2c_master_tx_vlpr(unsigned char mst_addr, unsigned char slv_addr);
void i2c_slave_rx_vlpr(unsigned char addr);
void i2c_vlpr_master_tx_test(unsigned char mst_addr, unsigned char slv_addr);
void i2c_vlpr_slave_rx_test(unsigned char slv_addr);

void i2c_wakeup_master(unsigned char mst_addr, unsigned char slv_addr)
{
// Notice:	
// if other address sent first, slave can not wake up from stop mode.	
	uint8 i;
	
	printf("***Mini I2C wakeup test***\r\n");
	printf("***Master side program***\r\n");
    i2c_tx_buffer.tx_index = 0;
    i2c_tx_buffer.rx_index = 0;
    i2c_tx_buffer.data_present = TRUE;
    i2c_tx_buffer.buf[0] = (uint8)(slv_addr&0x0FF);
	for(i=1;i<64;i++)
	{
		i2c_tx_buffer.buf[i] = i;
	}
    
	iic_init(0, mst_addr, 5000);    
    
    i2c_tx_buffer.length = 64;
    // put error slave address first
    i2c_master(0, I2C_TX, slv_addr);
    
    for(i=0;i<0xff;i++)
    	asm("nop");
    
    i2c_tx_buffer.length = 64;
    
	i2c_master(0, I2C_TX, 0xb0);
}

void i2c_wakeup_slave(unsigned char slv_addr)
{
	printf("***Mini I2C wakeup test***\r\n");
	printf("***Slave side program***\r\n");
	iic_init(0, slv_addr, 50000);
	I2C_C1(0) |= I2C_C1_WUEN_MASK; // enable low power wake-up
	I2C_C2(0) |= I2C_C2_RMEN_MASK|I2C_C2_GCAEN_MASK; // enable range&general call
	I2C_RA(0) = (I2C_SLAVE_ADDR + 0x10)<<1;
	printf("\r\nEnter Stop\/wait...\r\n");
#if 0
	SIM_SOPT4 = 0x10; // enable wait.
	_Wait;
#else
	SIM_SOPT4 = 0x20; // enable stop
	_Stop;
#endif	
}

void i2c_vlpr_master_tx_test(unsigned char mst_addr, unsigned char slv_addr)
{
	uint8 i;
	
	i2c_tx_buffer.tx_index = 0;
    i2c_tx_buffer.rx_index = 0;
    i2c_tx_buffer.data_present = TRUE;
    i2c_tx_buffer.buf[0] = (uint8)(slv_addr&0x0FF);
	for(i=1;i<64;i++)
	{
		i2c_tx_buffer.buf[i] = i;
	}	
#if VLPR_TEST		
	enter_vlpr();
#endif
	printf("***Mini I2C vlpr master tx test***");
	iic_init(0, mst_addr, 5000);
	
    i2c_tx_buffer.length = 64;
    i2c_master(0, I2C_TX, slv_addr);	
}

void i2c_vlpr_slave_rx_test(unsigned char slv_addr)
{
	i2c_rx_buffer.length = 64;
#if VLPR_TEST	
	enter_vlpr();
#endif	
	iic_init(0, slv_addr, 5000);
	I2C_F(0) = 0; //follow the clock of master
//	I2C_FLT(0) = 0x0F;
//	I2C_C2(0) |= I2C_C2_SBRC_MASK;
	printf("***Mini I2C vlpr slave rx test***");
}

#define VLP_TEST 2

void i2c_low_power(void) 
{
	int i;
	uint8 slv_addr;
	
	SIM_SCGC1 |= 0xF0; // enable I2C clock gate 
	INTC_CIMR = 26; // I2C0
//	INTC_CIMR = 27; // I2C2
//	INTC_CIMR = 30; // I2C1
//	INTC_CIMR = 31; // I2C3
	slv_addr = I2C_SLAVE_ADDR;
//	i2c_wakeup_master(I2C_MASTER_ADDR, slv_addr);
	i2c_wakeup_slave(I2C_SLAVE_ADDR);

	
//	i2c_vlpr_master_tx_test(I2C_MASTER_ADDR, I2C_SLAVE_ADDR);

	
//	i2c_vlpr_slave_rx_test(I2C_SLAVE_ADDR);
/*	
// slave at wait or stop
#if (VLP_TEST==1) // vlpr
	enter_wait();	
	printf("exit from vlpw\r\n");
#elif (VLP_TEST==2)//vlps
	I2C_C1(0) |= I2C_C1_WUEN_MASK;
#if VLPR_TEST		
	SMC_PMCTRL = SMC_PMCTRL_RUNM(2) | SMC_PMCTRL_STOPM(2);
	SIM_SOPT4 = SIM_SOPT4_STOPE_MASK;
	_Stop;	
#else
// test using irq to wakeup MCU from vlps to run	
//	SIM_SCGC4 |= SIM_SCGC4_IRQ_MASK;
//	MXC_PTBPF4 = 0x05;
//	IRQ_SC = IRQ_SC_IRQACK_MASK;
//	IRQ_SC = IRQ_SC_IRQPE_MASK | IRQ_SC_IRQIE_MASK;
	enable_lpwui();
	enter_vlps();
#endif
	for(i=0;i<0x1000;i++) asm("nop");
	printf("exit from vlps\r\n");
#endif
*/
}

void run_test(void)
{
	/* This function will call the testcase function */
	i2c_low_power();
}

interrupt void irq_isr(void)
{
	IRQ_SC = IRQ_SC_IRQACK_MASK;
}

