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

extern void _i2c_delay_1bit(void);

void i2c_smb_basic(void);
void run_test(void);

void i2c_smb_master_basic_tx(unsigned char channel, unsigned char mst_addr, unsigned char slv_addr);
void i2c_smb_slave_basic_rx(unsigned char channel, unsigned char addr);

void i2c_smb_master_basic_rx(unsigned char channel, unsigned char mst_addr, unsigned char slv_addr);
void i2c_smb_slave_basic_tx(unsigned char channel, unsigned char mst_addr, unsigned char slv_addr);

void i2c_smb_master_basic_tx(unsigned char channel, unsigned char mst_addr, unsigned char slv_addr)
{
	uint8 i;
	printf("***Mini I2C SMB 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)(slv_addr&0x0FF);
	
	for(i=1;i<64;i++)
	{
		i2c_tx_buffer.buf[i] = i;
	}
	
	smbus_init(channel, mst_addr, I2C_SMB_MST_SEC_ADDR, 50000);
	i2c_master(channel, I2C_TX, slv_addr);	
}

void i2c_smb_slave_basic_rx(unsigned char channel, unsigned char addr)
{
	int i;
	printf("***Mini I2C SMB slave RX Test***\r\n");
	i2c_rx_buffer.length = 64;
	smbus_init(channel, addr, I2C_SMB_DEF_ADDR, 50000);		

	while(i2c_rx_buffer.rx_index < i2c_rx_buffer.length);
	for(i=0;i<i2c_rx_buffer.rx_index;)
	{
		printf("%02x ", i2c_rx_buffer.buf[i]);
		if((++i)%16 == 0)
		{
			printf("\r\n");
		}
	}
}

void i2c_smb_master_basic_rx(unsigned char channel, unsigned char mst_addr, unsigned char slv_addr)
{
	uint8 i;
	
	printf("***Mini I2C SMB master RX 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 = 1;
    i2c_tx_buffer.buf[0] = (uint8)(slv_addr&0x0FF);
	
    i2c_rx_buffer.length = 64;
    
    smbus_init(channel, mst_addr, I2C_SMB_MST_SEC_ADDR, 50000);
	i2c_master(channel, I2C_RX, slv_addr);	
}

void i2c_smb_slave_basic_tx(unsigned char channel, unsigned char mst_addr, unsigned char slv_addr)
{
    uint8 i;
    printf("***Mini I2C SMB 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;
	}
    
    smbus_init(channel, slv_addr, I2C_SMB_SLV_SEC_ADDR, 50000); 	
}

void i2c_smb_basic(void) 
{
	EnableInterrupts;
	SIM_SCGC1 |= 0xF0; // enable I2C clock gate 
	INTC_CIMR = 26; // I2C0
//	INTC_CIMR = 27; // I2C2
//	INTC_CIMR = 30; // I2C1
//	INTC_CIMR = 31; // I2C3	
	
	i2c_smb_master_basic_tx(0, I2C_MASTER_ADDR, I2C_SMB_DEF_ADDR);
//	i2c_smb_slave_basic_rx(0, I2C_SLAVE_ADDR);

//	i2c_smb_master_basic_rx(0, I2C_MASTER_ADDR, I2C_SMB_SLV_SEC_ADDR);
//	i2c_smb_slave_basic_tx(0, I2C_MASTER_ADDR, I2C_SLAVE_ADDR);
}

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

