I2C based slave device register reading

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
AMSH_1344256
Level 1
Level 1

Hello,

   

I am reading a slave device register value of one byte in a infinite for loop using I2C block. the logic i am using is :

   

I2C_Start();

   

I2C_MasterClearStatus();

   

I2C_MasterSendStart(Device Address,0x00);

   

I2C_MasterWriteByte(address);

   

I2C_MasterSendRestart(Device Address,0xff);

   

rd_Buff==I2C_MasterReadByte(0);

   

I2C_MasterSendStop();

   

I2C_Stop();

   

i am getting value of register in rd_Buff. this is working fine for some time but logic gets hangs at some place and my code is not running further. What may be the reason? I used debugger to check where the code is stoping. I found that when master is generating restart i get stuck for waitng.

   

 

   

kindly suggest.

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Leave away the Master5ClearStatus().

   

Every call returns a status (except the ReadByte() ). The first status != 0 indicates an error, there are #defines to show the reason. So while debugging check the returned values.

   

The I2C_Stop will kill any ongoing transfer, do not use it, call Start() only once in initialization and keep the interface running.

   

 

   

Bob

View solution in original post

0 Likes
3 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I cannot see from your code what the meaning of 0x00 in I2C_MasterSendStart(Device Address,0x00) is, please use the provided #defines. They are different when using different PSoC families afaik.

   

same for I2C_MasterSendRestart(Device Address,0xff) and the 0xff.

   

Lastly: Standard for I2C is: the last byte red from I2C should be NAKed. is the (0) in I2C_MasterReadByte(0) an ACK or a NAK?

   

 

   

Bob

0 Likes
AMSH_1344256
Level 1
Level 1

I have modified my code like this

   

I2C_Start();

   

I2C_MasterClearStatus();

   

I2C_MasterSendStart(0x18, I2C_WRITE_XFER_MODE);

   

I2C_MasterWriteByte(address);

   

I2C_MasterSendRestart(0x18, I2C_READ_XFER_MODE);

   

// start_status=I2C_MasterSendStart(0x18,1);

   

rd_Buff=I2C_MasterReadByte(I2C_NAK_DATA);

   

I2C_MasterSendStop();

   

I2C_MasterClearStatus();

   

I2C_Stop();

   

I found that in I2C_master.c mention  the side effect of function is I2C_MasterReadByte .

   

It is written that function will entered without a 'byte complete' bit set in the I2C_CSR register. It does not exit until it will be set.  So may be I2C_MasterReadByte() is not exiting as register nit is not set?

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Leave away the Master5ClearStatus().

   

Every call returns a status (except the ReadByte() ). The first status != 0 indicates an error, there are #defines to show the reason. So while debugging check the returned values.

   

The I2C_Stop will kill any ongoing transfer, do not use it, call Start() only once in initialization and keep the interface running.

   

 

   

Bob

0 Likes