New question on I2C communicationhttp:
-
- Posts: 8
- Joined: Thu May 27, 2010 10:00 am
New question on I2C communicationhttp:
Guys, I got the I2C working I can read my LTC2309 the first time. The next time i run the code it dose not function. Does any one have any thoughts.
Output first time.
Waiting 2sec to start 'A' to abort
Configured IP = 192.168.1.75
Configured Mask = 255.255.255.1
MAC Address= 00:03:f4:04:4f:28
Value of X 0
We have a 0 on the bus
Value of X 1
Value of X 2
Value of X 3
Value of X 4
Value of X 5
Value of X 6
Value of X 7
Value of X 8
We have a 8 on the bus
Value of X 9
Value of X 10
We have a A on the bus
Value of X 11
Value of X 12
Value of X 13
Value of X 14
Value of X 15
Here
Master SX: ,
Receive successfully
Master RX:0, 0,ff, ff
Receive successfully
Second Run,
Waiting 2sec to start 'A' to abort
Configured IP = 192.168.1.75
Configured Mask = 255.255.255.1
MAC Address= 00:03:f4:04:4f:28
Value of X 0
Value of X 1
Value of X 2
Value of X 3
Value of X 4
Value of X 5
Value of X 6
Value of X 7
Value of X 8
Value of X 9
Value of X 10
Value of X 11
Value of X 12
Value of X 13
Value of X 14
Value of X 15
Here
Failed to read due to error: 4
Failed to read due to error: 4
lly
My Code
#include "predef.h"
#include <stdio.h>
#include <ctype.h>
#include <startnet.h>
#include <autoupdate.h>
#include <dhcpclient.h>
#include <smarttrap.h>
#include <taskmon.h>
#include <NetworkDebug.h>
//#include "i2cmulti.h" //Used for Multi-Master I2C
#include <string.h>
#include "i2cmaster.h"
extern "C" {
void UserMain(void * pd);
}
const char * AppName="lm75";
void UserMain(void * pd) {
InitializeStack();
if (EthernetIP == 0) GetDHCPAddress();
OSChangePrio(MAIN_PRIO);
EnableAutoUpdate();
StartHTTP();
EnableTaskMonitor();
BYTE buffer[I2C_MAX_BUF_SIZE];
BYTE My_WriteBuffer[I2C_MAX_BUF_SIZE];
/* BYTE address; */
BYTE I2CStat;
BYTE Ascii2Byte( char* buf );
#ifndef _DEBUG
EnableSmartTraps();
#endif
#ifdef _DEBUG
InitializeNetworkGDB_and_Wait();
#endif
#ifdef _I2CMASTER_H
I2CInit();
for( int x =0; x<16; x++)
{
iprintf(" Value of X %d \r\n",x);
if( I2CStart( x, I2C_START_READ ) < I2C_TIMEOUT )
iprintf("We have a %X on the bus\r\n", x );
I2CStop();
}
iprintf("Here\r\n" );
/*for(int i=0;i < 1;i++){ */
*My_WriteBuffer =0x40;
I2CStat = I2CSendBuf( 8, My_WriteBuffer,1 );
if( I2CStat == I2C_OK )
{
printf("Master SX: , \r\n");
printf( "Receive successfully\r\n" );
}
else
iprintf( "Failed to read due to error: %d\r\n", I2CStat);
I2CStat = ( I2CReadBuf(0x8, buffer, 16));
I2CStop();
if( I2CStat == I2C_OK )
{
printf("Master RX:%x, %x,%x, %x\r\n", buffer[0], buffer[1],buffer[2], buffer[3]);
printf( "Receive successfully\r\n" );
}
else
iprintf( "Failed to read due to error: %d\r\n", I2CStat);
#endif
// The following function converts the first two ascii chars of a string to a single hex value
}
Output first time.
Waiting 2sec to start 'A' to abort
Configured IP = 192.168.1.75
Configured Mask = 255.255.255.1
MAC Address= 00:03:f4:04:4f:28
Value of X 0
We have a 0 on the bus
Value of X 1
Value of X 2
Value of X 3
Value of X 4
Value of X 5
Value of X 6
Value of X 7
Value of X 8
We have a 8 on the bus
Value of X 9
Value of X 10
We have a A on the bus
Value of X 11
Value of X 12
Value of X 13
Value of X 14
Value of X 15
Here
Master SX: ,
Receive successfully
Master RX:0, 0,ff, ff
Receive successfully
Second Run,
Waiting 2sec to start 'A' to abort
Configured IP = 192.168.1.75
Configured Mask = 255.255.255.1
MAC Address= 00:03:f4:04:4f:28
Value of X 0
Value of X 1
Value of X 2
Value of X 3
Value of X 4
Value of X 5
Value of X 6
Value of X 7
Value of X 8
Value of X 9
Value of X 10
Value of X 11
Value of X 12
Value of X 13
Value of X 14
Value of X 15
Here
Failed to read due to error: 4
Failed to read due to error: 4
lly
My Code
#include "predef.h"
#include <stdio.h>
#include <ctype.h>
#include <startnet.h>
#include <autoupdate.h>
#include <dhcpclient.h>
#include <smarttrap.h>
#include <taskmon.h>
#include <NetworkDebug.h>
//#include "i2cmulti.h" //Used for Multi-Master I2C
#include <string.h>
#include "i2cmaster.h"
extern "C" {
void UserMain(void * pd);
}
const char * AppName="lm75";
void UserMain(void * pd) {
InitializeStack();
if (EthernetIP == 0) GetDHCPAddress();
OSChangePrio(MAIN_PRIO);
EnableAutoUpdate();
StartHTTP();
EnableTaskMonitor();
BYTE buffer[I2C_MAX_BUF_SIZE];
BYTE My_WriteBuffer[I2C_MAX_BUF_SIZE];
/* BYTE address; */
BYTE I2CStat;
BYTE Ascii2Byte( char* buf );
#ifndef _DEBUG
EnableSmartTraps();
#endif
#ifdef _DEBUG
InitializeNetworkGDB_and_Wait();
#endif
#ifdef _I2CMASTER_H
I2CInit();
for( int x =0; x<16; x++)
{
iprintf(" Value of X %d \r\n",x);
if( I2CStart( x, I2C_START_READ ) < I2C_TIMEOUT )
iprintf("We have a %X on the bus\r\n", x );
I2CStop();
}
iprintf("Here\r\n" );
/*for(int i=0;i < 1;i++){ */
*My_WriteBuffer =0x40;
I2CStat = I2CSendBuf( 8, My_WriteBuffer,1 );
if( I2CStat == I2C_OK )
{
printf("Master SX: , \r\n");
printf( "Receive successfully\r\n" );
}
else
iprintf( "Failed to read due to error: %d\r\n", I2CStat);
I2CStat = ( I2CReadBuf(0x8, buffer, 16));
I2CStop();
if( I2CStat == I2C_OK )
{
printf("Master RX:%x, %x,%x, %x\r\n", buffer[0], buffer[1],buffer[2], buffer[3]);
printf( "Receive successfully\r\n" );
}
else
iprintf( "Failed to read due to error: %d\r\n", I2CStat);
#endif
// The following function converts the first two ascii chars of a string to a single hex value
}
Re: New question on I2C communicationhttp:
What happens if you power down the board between running the code each time?
Does it then respond each time and only if you do a reset or rerun the programme will it fail?
Just trying to get an idea if this is a hardware issue. A power down will reset all bus devices.
Once thing I do see is that you are trying to read 16 bytes from the I2C device. Is this correct? I am wondering if this is causing the issue of a stuck bus.
Dave...
Does it then respond each time and only if you do a reset or rerun the programme will it fail?
Just trying to get an idea if this is a hardware issue. A power down will reset all bus devices.
Once thing I do see is that you are trying to read 16 bytes from the I2C device. Is this correct? I am wondering if this is causing the issue of a stuck bus.
Dave...
-
- Posts: 8
- Joined: Thu May 27, 2010 10:00 am
Re: New question on I2C communicationhttp:
Hello,
I was not clear on my post, when I first run the code it works as expected. When I run it a second time it does not respond. I can get it to recover when I power down then power back up. The circuit is simple there are two LTC2309 connected to the I2c buss.
Bob
I was not clear on my post, when I first run the code it works as expected. When I run it a second time it does not respond. I can get it to recover when I power down then power back up. The circuit is simple there are two LTC2309 connected to the I2c buss.
Bob
Re: New question on I2C communicationhttp:
Hi Bob,
Looking at the datasheet for the LTC2309, it sends you 16 bits of data as 2 bytes.
your code:
I2CStat = ( I2CReadBuf(0x8, buffer, 16));
is requesting to receive 16 bytes. Try setting this to 2 and see if this gets it working again. Not sure exactly what the effect of asking the device for more than 2 bytes is but worth a test to see if only asking for 2 works.
The master is expected to NAK after the output of the 2 bytes and because you are requesting 16 it could be confusing it and locking it up.
Cheers,
Dave...
Looking at the datasheet for the LTC2309, it sends you 16 bits of data as 2 bytes.
your code:
I2CStat = ( I2CReadBuf(0x8, buffer, 16));
is requesting to receive 16 bytes. Try setting this to 2 and see if this gets it working again. Not sure exactly what the effect of asking the device for more than 2 bytes is but worth a test to see if only asking for 2 works.
The master is expected to NAK after the output of the 2 bytes and because you are requesting 16 it could be confusing it and locking it up.
Cheers,
Dave...
-
- Posts: 6
- Joined: Tue Dec 01, 2009 10:21 am
Re: New question on I2C communicationhttp:
Hi Dave,
I will give that a try. The reason I added the extra bytes was to make sure I was reading the correct data. My results showed that I had received the correct data the first time I read it.
Thanks
Bob
I will give that a try. The reason I added the extra bytes was to make sure I was reading the correct data. My results showed that I had received the correct data the first time I read it.
Thanks
Bob
Re: New question on I2C communicationhttp:
the only thing that jumps out at me is that it appears you are putting the
device into sleep mode which then requires 200 ms before it will wake up
whenever you next address it. i just did a quick look at the datasheet but
your
*My_WriteBuffer =0x40;
appears to be setting the SLP bit.
if you really want to sleep you'll need some logic to work with that.
they show holding off on sending the stop bit and you can do that
with the i2c lib by adding false as the 4th parameter to I2CSendBuf
and then wait your 200 ms and then call I2CStop.
device into sleep mode which then requires 200 ms before it will wake up
whenever you next address it. i just did a quick look at the datasheet but
your
*My_WriteBuffer =0x40;
appears to be setting the SLP bit.
if you really want to sleep you'll need some logic to work with that.
they show holding off on sending the stop bit and you can do that
with the i2c lib by adding false as the 4th parameter to I2CSendBuf
and then wait your 200 ms and then call I2CStop.
Re: New question on I2C communicationhttp:
Reading more bytes for some devices can lead to lockups. I have seen this in the past.
If you look at your reply, you will see FF, FF as the last 2 bytes indicating no data on the bus (pulled up)
I think Greengene might have spotted the other issue. Try leaving the ADC in normal mode instead of sleep and only request 2 bytes and see if it works then.
Dave...
If you look at your reply, you will see FF, FF as the last 2 bytes indicating no data on the bus (pulled up)
I think Greengene might have spotted the other issue. Try leaving the ADC in normal mode instead of sleep and only request 2 bytes and see if it works then.
Dave...
-
- Posts: 8
- Joined: Thu May 27, 2010 10:00 am
Re: New question on I2C communicationhttp:
Hi Dave,
First I was reading back single ended so I should have been 0x80 instead of 0x04. I called a friend over at ltx and he put me in touch with the product engineer of the2309 and for sleep mode to function the bit has to be high.
I did not want to set the sleep bit. I did modify the to to write then read back right a way. and the results were intermittent.
Waiting 2sec to start 'A' to abort
Configured IP = 192.168.1.75
Configured Mask = 255.255.255.1
MAC Address= 00:03:f4:04:4f:28
Master RX: , 0, 50
Receive successfully
Done:
Waiting 2sec to start 'A' to abort
Configured IP = 192.168.1.75
Configured Mask = 255.255.255.1
MAC Address= 00:03:f4:04:4f:28
Failed to Read due to error: 9
Done:
55.255.1
MAC Address= 00:03:f4:04:4f:28
Failed to Read due to error: 9
#include "predef.h"
#include <stdio.h>
#include <ctype.h>
#include <startnet.h>
#include <autoupdate.h>
#include <dhcpclient.h>
#include <smarttrap.h>
#include <taskmon.h>
#include <NetworkDebug.h>
//#include "i2cmulti.h" //Used for Multi-Master I2C
#include <string.h>
#include "i2cmaster.h"
extern "C" {
void UserMain(void * pd);
}
const char * AppName="lm75";
void UserMain(void * pd) {
InitializeStack();
if (EthernetIP == 0) GetDHCPAddress();
OSChangePrio(MAIN_PRIO);
EnableAutoUpdate();
StartHTTP();
EnableTaskMonitor();
BYTE buffer[I2C_MAX_BUF_SIZE];
BYTE My_WriteBuffer[I2C_MAX_BUF_SIZE];
/* BYTE address; */
BYTE I2CStat;
BYTE Ascii2Byte( char* buf );
#ifndef _DEBUG
EnableSmartTraps();
#endif
#ifdef _DEBUG
InitializeNetworkGDB_and_Wait();
#endif
#ifdef _I2CMASTER_H
I2CInit();
*My_WriteBuffer =0x80;
I2CStat = I2CSendBuf( 8, My_WriteBuffer,1 );
I2CStop();
I2CStat = ( I2CReadBuf(8, buffer, 2));
if( I2CStat == I2C_OK )
{
printf("Master RX: , %x, %x \r\n", buffer[0],buffer[1]);
printf( "Receive successfully\r\n" );
}
else
iprintf( "Failed to Read due to error: %d\r\n", I2CStat);
I2CStop();
iprintf( " Done: \r\n");
#endif
// The following function converts the first two ascii chars of a string to a single hex value
}
Then I tried to set the stop to fail an it never passed.
I2CStat = I2CSendBuf( 8, My_WriteBuffer,1 , bool stop = false);
Dave one other question when writing a address like 10, is it 0x10 or 0xa?
thanks
Bob
First I was reading back single ended so I should have been 0x80 instead of 0x04. I called a friend over at ltx and he put me in touch with the product engineer of the2309 and for sleep mode to function the bit has to be high.
I did not want to set the sleep bit. I did modify the to to write then read back right a way. and the results were intermittent.
Waiting 2sec to start 'A' to abort
Configured IP = 192.168.1.75
Configured Mask = 255.255.255.1
MAC Address= 00:03:f4:04:4f:28
Master RX: , 0, 50
Receive successfully
Done:
Waiting 2sec to start 'A' to abort
Configured IP = 192.168.1.75
Configured Mask = 255.255.255.1
MAC Address= 00:03:f4:04:4f:28
Failed to Read due to error: 9
Done:
55.255.1
MAC Address= 00:03:f4:04:4f:28
Failed to Read due to error: 9
#include "predef.h"
#include <stdio.h>
#include <ctype.h>
#include <startnet.h>
#include <autoupdate.h>
#include <dhcpclient.h>
#include <smarttrap.h>
#include <taskmon.h>
#include <NetworkDebug.h>
//#include "i2cmulti.h" //Used for Multi-Master I2C
#include <string.h>
#include "i2cmaster.h"
extern "C" {
void UserMain(void * pd);
}
const char * AppName="lm75";
void UserMain(void * pd) {
InitializeStack();
if (EthernetIP == 0) GetDHCPAddress();
OSChangePrio(MAIN_PRIO);
EnableAutoUpdate();
StartHTTP();
EnableTaskMonitor();
BYTE buffer[I2C_MAX_BUF_SIZE];
BYTE My_WriteBuffer[I2C_MAX_BUF_SIZE];
/* BYTE address; */
BYTE I2CStat;
BYTE Ascii2Byte( char* buf );
#ifndef _DEBUG
EnableSmartTraps();
#endif
#ifdef _DEBUG
InitializeNetworkGDB_and_Wait();
#endif
#ifdef _I2CMASTER_H
I2CInit();
*My_WriteBuffer =0x80;
I2CStat = I2CSendBuf( 8, My_WriteBuffer,1 );
I2CStop();
I2CStat = ( I2CReadBuf(8, buffer, 2));
if( I2CStat == I2C_OK )
{
printf("Master RX: , %x, %x \r\n", buffer[0],buffer[1]);
printf( "Receive successfully\r\n" );
}
else
iprintf( "Failed to Read due to error: %d\r\n", I2CStat);
I2CStop();
iprintf( " Done: \r\n");
#endif
// The following function converts the first two ascii chars of a string to a single hex value
}
Then I tried to set the stop to fail an it never passed.
I2CStat = I2CSendBuf( 8, My_WriteBuffer,1 , bool stop = false);
Dave one other question when writing a address like 10, is it 0x10 or 0xa?
thanks
Bob
Re: New question on I2C communicationhttp:
Hi Bob,
Quick question. Do you have pullups on the SDA and SCL lines?
As for the 10 question, it depends on whether it is HEX or DECIMAL. If decimal, 0xA would be correct as would just plain old 10. If HEX the 0x10 would be needed.
By the way, the calll to I2CReadBuf and I2CWriteBuf will do the stop for you if you set the BOOLEAN to true or leave it as default. No need for the extra stop call.
Dave...
Quick question. Do you have pullups on the SDA and SCL lines?
As for the 10 question, it depends on whether it is HEX or DECIMAL. If decimal, 0xA would be correct as would just plain old 10. If HEX the 0x10 would be needed.
By the way, the calll to I2CReadBuf and I2CWriteBuf will do the stop for you if you set the BOOLEAN to true or leave it as default. No need for the extra stop call.
Dave...
Re: New question on I2C communicationhttp:
doh! i was reading the command bits bassackards!
maybe it is me that needs the sleep not the device.
maybe it is me that needs the sleep not the device.