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

void i2c_10bit_master_tx_test(unsigned short mst_addr, unsigned short slv_addr);
void i2c_10bit_master_rx_test(unsigned short mst_addr, unsigned short slv_addr);
void i2c_10bit_slave_tx_test(unsigned short mst_addr, unsigned short slv_addr);
void i2c_10bit_slave_rx_test(unsigned short addr);
void i2c_10bit_slave_test(void);
void i2c_10bit(void);
void run_test(void);

void i2c_10bit_master_tx_test(unsigned short mst_addr, unsigned short slv_addr)
{
	uint8 i;
	
	printf("***Mini I2C 10-bit Addr Master TX Test***\r\n");
    i2c_tx_buffer.tx_index = 0;
    i2c_tx_buffer.rx_index = 0;
    i2c_tx_buffer.data_present = TRUE;
    i2c_tx_buffer.length = 64;
    i2c_tx_buffer.buf[0] = (uint8)(I2C_SLAVE_ADDR&0x0FF);
	
	for(i=1;i<64;i++)
	{
		i2c_tx_buffer.buf[i] = i;
	}
	
	i2c_10bit_init(0, mst_addr, 50000);
	i2c_master(0, I2C_10BIT_TX, slv_addr);	
}

void i2c_10bit_master_rx_test(unsigned short mst_addr, unsigned short slv_addr)
{
	printf("***Mini I2C 10-bit Addr Master RX Test***\r\n");	
    i2c_tx_buffer.tx_index = 0;
    i2c_tx_buffer.rx_index = 0;
    i2c_tx_buffer.data_present = TRUE;	
    
    // when using 10-bit address mode, must set tx length to 1,
    // and first byte to be transferred should be 
    // the low 8-bit of 10-bit slave address.
    // when using 7-bit address mode, if do not want to transfer data
    // to slave, just set tx length to 0, 
    // else user can use I2C_TXRX_MODE instead.   
    i2c_tx_buffer.length = 1;
    i2c_tx_buffer.buf[0] = (uint8)(slv_addr&0x0FF);  
    
	i2c_rx_buffer.length = 64;
	
	i2c_10bit_init(0, mst_addr, 50000);
	i2c_master(0, I2C_10BIT_RX, slv_addr);			
}

void i2c_10bit_slave_rx_test(unsigned short addr)
{
	printf("***Mini I2C 10-bit Addr slave RX Test***\r\n");
	i2c_10bit_init(0, addr, 50000);
}

void i2c_10bit_slave_tx_test(unsigned short mst_addr, unsigned short slv_addr)
{
	uint8 i;
	printf("***Mini I2C 10-bit Addr slave TX Test***\r\n");

    i2c_tx_buffer.tx_index = 0;
    i2c_tx_buffer.rx_index = 0;
    i2c_tx_buffer.data_present = TRUE;
    i2c_tx_buffer.length = 64;
    i2c_tx_buffer.buf[0] = (uint8)(mst_addr&0x0FF);

	for(i=1;i<64;i++)
	{
		i2c_tx_buffer.buf[i] = i;
	}    
    
    i2c_10bit_init(0, slv_addr, 50000);	
}

void i2c_10bit(void) 
{
	SIM_SCGC1 |= 0xF0; // enable I2C clock gate 
	INTC_CIMR = 26; // I2C0
	i2c_10bit_master_tx_test(I2C_MASTER_ADDR, I2C_SLAVE_ADDR);
//	i2c_10bit_master_rx_test(I2C_MASTER_ADDR, I2C_SLAVE_ADDR);
//	i2c_10bit_slave_rx_test(I2C_SLAVE_ADDR);
//	i2c_10bit_slave_tx_test(I2C_MASTER_ADDR, I2C_SLAVE_ADDR);
}

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

