I have to do Write and Read to PCA2129 using I2C peripheral of TMS320dm6446.
I am checking the data being transmitted out from I2C of TMS320DM6446 on Oscilloscope which is not matching with the actual data written in the code.
Pls suggest what could be the issue ?
The code being used is as under:
#include "stdio.h"
#include "davincievm.h"
#include "davincievm_uart.h"
#include <csl_spi.h>
#include <csl_spiAux.h>
#include <davincievm_gpio.h>
#include "stdlib.h"
#include "davincievm.h"
#include "string.h"
void main( void )
{
Uint32 i;
Uint32 jj;
unsigned char add_regI2c=0,I2C_len=0,I2C_SLAddr, cntRD;
DAVINCIEVM_GPIO_init();
PINMUX0 &=0xffff7fE0;
DAVINCIEVM_GPIO_setDirection(12, 0);
DAVINCIEVM_GPIO_setOutput(12, 1);
PINMUX1 &=0xffffff7f;
PINMUX1 |=0x00000080;
DAVINCIEVM_I2C_reset();
add_regI2c=0x03;
//I2C_len = 0x02;
//I2C_SLAddr = 0xA2;
while(1)
{
DAVINCIEVM_I2C_write(0xA2, &add_regI2c, 3);
DAVINCIEVM_I2C_read(0xA3, &RDsec, 0x01);
cntRD++;
}
}
//Following files are added as header/source/include
///////////////////////////////////////////////////////////////////////////////////////////////////////
/*
* Copyright 2005 by Spectrum Digital Incorporated.
* All rights reserved. Property of Spectrum Digital Incorporated.
*
* Not for distribution.
*/
/*
* I2C implementation
*
*/
#include "davincievm_i2c.h"
/* ------------------------------------------------------------------------ *
* *
* DAVINCIEVM_I2C_init( ) *
* *
* Enable and initalize the I2C module *
* *
* The I2C clk is set to run at 20 KHz *
* *
* ------------------------------------------------------------------------ */
Int16 DAVINCIEVM_I2C_init( )
{
#ifdef ARM_SIDE
CSL_Status status;
CSL_I2cClkSetup i2c_clksetup;
CSL_I2cHwSetup i2c_hwsetup;
CSL_I2cParam i2c_param;
CSL_i2cInit( 0 );
i2c_handle = CSL_i2cOpen( &i2c_obj, 0, &i2c_param, &status );
i2c_clksetup.prescalar = 26;
i2c_clksetup.clklowdiv = 20;
i2c_clksetup.clkhighdiv = 20;
i2c_hwsetup.mode = 1; // 0: Slave mode 1: Master mode
i2c_hwsetup.dir = 0; // 0: Rx mode 1: Tx mode
i2c_hwsetup.addrMode = 0; // 0: 7-bit mode 1: 10-bit mode//0
i2c_hwsetup.sttbyteen = 0; // 0: Normal mode 1: Start byte mode //0
i2c_hwsetup.ownaddr = 1; // #: Own address//0
i2c_hwsetup.ackMode = 0; // 0: ACK mode 1: NACK mode
i2c_hwsetup.runMode = 0; // 0: No Free run mode 1: Free run mode//1
i2c_hwsetup.repeatMode = 0; // 0: No repeat mode 1: Repeat mode
i2c_hwsetup.loopBackMode = 0; // 0: No loopback 1: Loopback mode//0
i2c_hwsetup.freeDataFormat = 0; // 0: No Free data fmt 1: Free data fmt
i2c_hwsetup.resetMode = 1; // 0: Reset 1: Out of reset//0
i2c_hwsetup.bcm = 0; // 0: Not compatible 1: Compatible
i2c_hwsetup.inten = 0; // #: Intr enable mask
i2c_hwsetup.clksetup = &i2c_clksetup;
status = CSL_i2cHwSetup( i2c_handle, &i2c_hwsetup );
CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_OUTOFRESET, 0 );
return 0;
#elif DSP_SIDE
I2C_ICMDR = 0; // Reset I2C
I2C_ICPSC = 26; // Config prescaler for 27MHz
I2C_ICCLKL = 5; // Config clk LOW for 50kHz
I2C_ICCLKH = 5; // Config clk HIGH for 50kHz
I2C_ICMDR |= ICMDR_IRS; // Release I2C from reset
return 0;
#endif
}
/* ------------------------------------------------------------------------ *
* *
* DAVINCIEVM_I2C_close( ) *
* *
* ------------------------------------------------------------------------ */
Int16 DAVINCIEVM_I2C_close( )
{
#ifdef ARM_SIDE
CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_RESET, 0 );
CSL_i2cClose( i2c_handle );
return 0;
#elif DSP_SIDE
I2C_ICMDR = 0; // Reset I2C
return 0;
#endif
}
/* ------------------------------------------------------------------------ *
* *
* DAVINCIEVM_I2C_reset( ) *
* *
* *
* *
* ------------------------------------------------------------------------ */
Int16 DAVINCIEVM_I2C_reset( )
{
DAVINCIEVM_I2C_close( );
DAVINCIEVM_I2C_init( );
return 0;
}
/* ------------------------------------------------------------------------ *
* *
* DAVINCIEVM_I2C_write( i2caddr, data, len ) *
* *
* I2C write in Master mode *
* *
* i2caddr <- I2C slave address *
* data <- I2C data ptr *
* len <- # of bytes to write *
* *
* ------------------------------------------------------------------------ */
Int16 DAVINCIEVM_I2C_write( Uint16 i2caddr, Uint8* data, Uint16 len )
{
Uint16 i;
Int32 timeout = 0x20000;
Int32 timecount = 0;
#ifdef ARM_SIDE
Uint16 response;
CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_SET_DATA_COUNT, &len );
CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_SET_SLAVE_ADDR, &i2caddr );
CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_DIR_TRANSMIT, 0 );
CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_START, 0 );
DAVINCIEVM_wait( 10 );
for ( i = 0 ; i < len ; i++ )
{
CSL_i2cWrite( i2c_handle, &data[i] );
timecount = 0;
do
{
CSL_i2cGetHwStatus( i2c_handle, CSL_I2C_QUERY_TX_RDY, &response );
timecount++;
if ( timecount >= timeout )
{
DAVINCIEVM_I2C_reset( );
return 1000;
}
} while( response == 0 );
}
//CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_STOP, 0 );
return 0;
#elif DSP_SIDE
I2C_ICCNT = len; // Set len
I2C_ICSAR = i2caddr; // Set I2C slave address
I2C_ICMDR = ICMDR_STT // Config for master write
| ICMDR_TRX
| ICMDR_MST
| ICMDR_IRS
| ICMDR_FREE
;
DAVINCIEVM_wait( 10 ); // Short delay
for ( i = 0 ; i < len ; i++ )
{
I2C_ICDXR = data[i]; // Write
timecount = 0;
do
{
timecount++;
if ( timecount >= timeout )
{
DAVINCIEVM_I2C_reset( );
return 1000;
}
} while ( ( I2C_ICSTR & ICSTR_ICXRDY ) == 0 );// Wait for Tx Ready
}
I2C_ICMDR |= ICMDR_STP; // Generate STOP
return 0;
#endif
}
/* ------------------------------------------------------------------------ *
* *
* DAVINCIEVM_I2C_read( i2caddr, data, len ) *
* *
* I2C read in Master mode *
* *
* i2caddr <- I2C slave address *
* data <- I2C data ptr *
* len <- # of bytes to write *
* *
* ------------------------------------------------------------------------ */
Int16 DAVINCIEVM_I2C_read( Uint16 i2caddr, Uint8* data, Uint16 len )
{
Uint16 i;
Int32 timeout = 0x20000;
Int32 timecount = 0;
#ifdef ARM_SIDE
Uint16 response;
CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_SET_DATA_COUNT, &len );
CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_SET_SLAVE_ADDR, &i2caddr );
CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_DIR_RECEIVE, 0 );
CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_START, 0 );
DAVINCIEVM_wait( 10 );
for ( i = 0 ; i < len ; i++ )
{
timecount = 0;
do
{
CSL_i2cGetHwStatus( i2c_handle, CSL_I2C_QUERY_RX_RDY, &response );
timecount++;
if ( timecount >= timeout )
{
DAVINCIEVM_I2C_reset( );
return 1000;
}
} while( response == 0 );
CSL_i2cRead( i2c_handle, &data[i] );
}
//CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_STOP, 0 );
return 0;
#elif DSP_SIDE
I2C_ICCNT = len; // Set len
I2C_ICSAR = i2caddr; // Set I2C slave address
I2C_ICMDR = ICMDR_STT // Config for master read
| ICMDR_MST
| ICMDR_IRS
| ICMDR_FREE
;
for ( i = 0 ; i < len ; i++ )
{
timecount = 0;
do
{
timecount++;
if ( timecount >= timeout )
{
DAVINCIEVM_I2C_reset( );
return 1000;
}
} while ( ( I2C_ICSTR & ICSTR_ICRRDY ) == 0 );// Wait for Rx Ready
data[i] = I2C_ICDRR; // Read
}
I2C_ICMDR |= ICMDR_STP; // Generate STOP
return 0;
#endif
}
/* ------------------------------------------------------------------------ *
* *
* DAVINCIEVM_I2C_read_variable( i2caddr, data, len ) *
* *
* I2C read in Master mode *
* *
* i2caddr <- I2C slave address *
* data <- I2C data ptr *
* len <- # of bytes to write *
* *
* ------------------------------------------------------------------------ */
Int16 DAVINCIEVM_I2C_read_variable( Uint16 i2caddr, Uint8* data, Uint16 len )
{
Uint16 i;
Int32 timeout = 0x20000;
Int32 timecount = 0;
#ifdef ARM_SIDE
Uint16 response;
CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_SET_DATA_COUNT, &len );
CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_SET_SLAVE_ADDR, &i2caddr );
CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_DIR_RECEIVE, 0 );
CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_START, 0 );
DAVINCIEVM_wait( 10 );
for ( i = 0 ; i < len ; i++ )
{
if ( i == 1 )
{
len = data[0];
CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_SET_DATA_COUNT, &len );
}
timecount = 0;
do
{
CSL_i2cGetHwStatus( i2c_handle, CSL_I2C_QUERY_RX_RDY, &response );
timecount++;
if ( timecount >= timeout )
{
DAVINCIEVM_I2C_reset( );
return 1000;
}
} while( response == 0 );
CSL_i2cRead( i2c_handle, &data[i] );
}
//CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_STOP, 0 );
return 0;
#elif DSP_SIDE
I2C_ICCNT = len; // Set len
I2C_ICSAR = i2caddr; // Set I2C slave address
I2C_ICMDR = ICMDR_STT // Config for master read
| ICMDR_MST
| ICMDR_IRS
| ICMDR_FREE
;
for ( i = 0 ; i < len ; i++ )
{
if ( i == 1 )
{
len = data[0];
I2C_ICCNT = len; // Set len
}
timecount = 0;
do
{
timecount++;
if ( timecount >= timeout )
{
DAVINCIEVM_I2C_reset( );
return 1000;
}
} while ( ( I2C_ICSTR & ICSTR_ICRRDY ) == 0 );// Wait for Rx Ready
data[i] = I2C_ICDRR; // Read
}
I2C_ICMDR |= ICMDR_STP; // Generate STOP
return 0;
#endif
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
* Copyright 2005 by Spectrum Digital Incorporated.
* All rights reserved. Property of Spectrum Digital Incorporated.
*
* Not for distribution.
*/
/*
* I2C header file
*
*/
#ifndef I2C_
#define I2C_
#include "davincievm.h"
#ifdef ARM_SIDE
#include "csl_i2c.h"
static CSL_I2cHandle i2c_handle;
static CSL_I2cObj i2c_obj;
#elif DSP_SIDE
#endif
/* ------------------------------------------------------------------------ *
* *
* I2C Prototypes *
* *
* ------------------------------------------------------------------------ */
Int16 DAVINCIEVM_I2C_init ( );
Int16 DAVINCIEVM_I2C_close( );
Int16 DAVINCIEVM_I2C_write( Uint16 slaveaddr, Uint8* data, Uint16 len );
Int16 DAVINCIEVM_I2C_read ( Uint16 slaveaddr, Uint8* data, Uint16 len );
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//more I2C related header files are added in include EX: csl_i2c.h etc.