It is currently 15 Dec 2018 07:53

All times are UTC + 1 hour




Post new topic Reply to topic  [ 25 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: 16 Aug 2009 06:05 
Offline

Joined: 10 Mar 2007 06:47
Posts: 82
Location: New Zealand
Hello Fellow Programmers
I have a question regarding I2C communications.
I programmed one PIC to work as an I2C master and two PICs which work as slaves.
The system works perfectly until one of the I2C Slaves is taken out of the network. The Master then hangs up.
The system hangs up when it tries to do an I2C Read as I believe that the library sends the request and then waits for a reply from the slave. If the reply does not come it keeps waiting thus hanging up the processor.
In normal operation the slaves will never be taken out of the system but glitches in the communication will make some of the master polls fail (over a long period of time). I need to make the system bulletproof, i.e. it should never hang up.
Is it possible to time out an I2C read connection if the slave does not repond ?
The I2C write works perfectly.
It is possible to implement a watchdog to keep the system going, but then the whole PIC logic will be rest. I want to keep this as a last resort.
Thanks
Rodney


Top
 Profile  
 
 Post subject:
PostPosted: 18 Aug 2009 08:23 
Offline

Joined: 10 Mar 2007 06:47
Posts: 82
Location: New Zealand
Anyone able to help here?


Top
 Profile  
 
 Post subject:
PostPosted: 18 Aug 2009 12:14 
Offline

Joined: 12 May 2007 13:01
Posts: 171
Location: Bristol, UK
Firstly, I am a big fan of I2C and think it is a very clever system allowing multiple devices, Multi-master and clock stretching for slow devices using just 2 wires! Hundreds of millions of I2C devices are operating correctly wordwide and many manufacturers make ICs using I2C so it must be a reliable system but there are no free lunches and the simplicity of a 2 wire connection makes the protocol and firmware complicated.

SPI uses more wires but in my opion must be more robust as the protocol is so simple - the uP will happily do an SPI transfer even with no SPI slave devices connected. Further many SPI devices reset their SPI state machine when the CS line is de-activated and handle the termination of a transfer before all 8 bits have been clocked with no problem.

Although this post is in the mP forum it discusses your question:

http://www.mikroe.com/forum/viewtopic.php?t=16192

You didn't mention the processor you are using but if it is a PIC18, janni's post:

Quote:
Moreover, the PIC18s' I2C state machine is not perfect and in some processors it's not easy to make certain it won't block itself in specific circumstances. Not to mention silicon errors...


I have been using SPI in a harsh industrial environment for nearly three years without a single problem. I changed from using I2C to SPI even though I had to redo the PCB.


Top
 Profile  
 
 Post subject:
PostPosted: 18 Aug 2009 13:54 
Offline

Joined: 30 Apr 2009 02:36
Posts: 239
Location: Canada, Toronto
I wrote my own master & slave code using interrupts (and interrupt on change) to send data (still working on receiving data)... had to make my own protocol, only because I could not stand the thought that I2C freezes the microcontroller at the slightest problem !

I too could not get the I2C to work... and after hours of trying I gave up, you can see my thread here:
http://www.mikroe.com/forum/viewtopic.php?t=20543

I just don't understand how it's possible to freeze the microcontroller (i.e. not return from the function) when there is an error :shock: . Can't it have a timeout ? Can't it just return an error number, and get on with the rest of the code ? I simply don't understand this thing...


Top
 Profile  
 
 Post subject:
PostPosted: 18 Aug 2009 19:26 
Offline

Joined: 10 Mar 2007 06:47
Posts: 82
Location: New Zealand
Hello Folks
Thanks for your feedback. I had a look at the other postings. Interesting.
It would be great if the mikroE team gives us some feedback on their libraries.
Is there a way to get around this microprocessor lock with mikroC I2C libraries?
They are probably the only ones who can give us a straight answer for this.
I made a lot of work with I2C and I am not very keen to redo everything in SPI. I would need to redesign a few PCBs and a lot of software.
Cheers
Rodney


Top
 Profile  
 
 Post subject:
PostPosted: 18 Aug 2009 20:45 
Offline

Joined: 18 Jan 2008 05:32
Posts: 172
Location: Australia
The master should never freeze, as it is the one that is sending the clock pulses. That is the entire point of having a master/slave relationship between the two devices.

If the master sends an ACK clock pulse and the slave fails to ACK, the master should just return a "slave failed to ACK" error.

Just write your own I2C code, there are tons of examples on the net.

_________________
Using; EasyPic4, EasyPIC6, BigPic4, MikroC. MPLab, MPASM.
C since 1991, PIC asm since 1998. Author of many freeware and open-source goodies; www.RomanBlack.com Designer of the www.TalkBotBrain.com talking PIC project.


Top
 Profile  
 
 Post subject:
PostPosted: 19 Aug 2009 07:39 
Offline

Joined: 10 Mar 2007 06:47
Posts: 82
Location: New Zealand
Thanks everybody for the replies.
Seems I need to create my own I2C Master Code.
It is a pity though that we cannot use the ones created by mikroE and that they are not providing feedback on this forum.
mikroC software and libraries are great. This is the only exception so far.
The I2C libraries can be used to learn the protocol, but they cannot be used in the final applications as they are not robust enough.


Top
 Profile  
 
 Post subject:
PostPosted: 19 Aug 2009 07:51 
Offline
mikroElektronika team
User avatar

Joined: 03 Aug 2004 12:59
Posts: 6608
Hi,
Sorry for late reply.
We currently do not support I2C slave mode in our libraries. This is due to the fact that such implementation is project dependent, i.e. it is difficult to generalize such library.

You can, however, search the forum for the posts made by "Andreas". In few occasions he wrote about I2C slave implementation, and he left few useful codes for it.


Top
 Profile  
 
 Post subject:
PostPosted: 19 Aug 2009 08:40 
Offline

Joined: 10 Mar 2007 06:47
Posts: 82
Location: New Zealand
Hello Zristic
Thank you for your reply.
This problem is not with I2C Slave, but I2C Master.
I know that mikroE does not provide I2C Slave libraries.
This issue is with the I2C Master Read library which hangs up the processor if the slave does not reply or the reply is not recognised.
Over a period of time the I2C Master will miss a reply from the slave and hang up the microprocessor.
Is there a way to avoid this happening?
Rodney


Top
 Profile  
 
 Post subject:
PostPosted: 19 Aug 2009 09:23 
Offline
mikroElektronika team
User avatar

Joined: 03 Aug 2004 12:59
Posts: 6608
Well, it is difficult for us to reproduce such problem here, which does not mean that it does not exist.

Is there a way that you use mikroICD? What I have in mind is that you run the program in mikroICD and when it stalls then you should press "Pause" (shortcut F6). The mikroIDE will then open the asm listing and will show at which asm instruction the program stalled. This info will help us to find out why the problem occurs.


Top
 Profile  
 
 Post subject:
PostPosted: 23 Aug 2009 08:50 
Offline

Joined: 10 Mar 2007 06:47
Posts: 82
Location: New Zealand
Hello Zristic

I compiled my project in debug mode and captured the point at which the software freezes.
I am attaching a copy of the ASM code indicating where the code is freezing.
The listing only includes the ASM code for the I2C1_Rd function. This is the standard microC library function.
Please note that this always hangs up the program, not only occasionally. If the I2C Master does not find the I2C Slave it stops. Instead it should return an error and continue executing the code.
Please let me know if you need more information
I look forward for your feedback
Rodney

_I2C1_Rd:
;__Lib_I2C_c34.c,58 ::       
;__Lib_I2C_c34.c,61 ::       
0x046A   0x969E         BCF         PIR1, 3
;__Lib_I2C_c34.c,62 ::       
0x046C   0x86C5         BSF         SSPCON2, 3
;__Lib_I2C_c34.c,64 ::       
L_I2C1_Rd12:
0x046E   0xB69E         BTFSC       PIR1, 3
0x0470   0xD001         BRA         L_I2C1_Rd13
0x0472   0xD7FD         BRA         L_I2C1_Rd12
L_I2C1_Rd13:
;__Lib_I2C_c34.c,66 ::       
0x0474   0xF001CFC9     MOVFF       SSPBUF, R1
;__Lib_I2C_c34.c,67 ::       
0x0478   0x0103         MOVLB       3
0x047A   0x510B         MOVF        FARG_I2C1_Rd_ack, 0, 1
0x047C   0x0A00         XORLW       0
0x047E   0xE102         BNZ         L_I2C1_Rd14
;__Lib_I2C_c34.c,68 ::       
0x0480   0x8AC5         BSF         SSPCON2, 5
0x0482   0xD001         BRA         L_I2C1_Rd15
L_I2C1_Rd14:
;__Lib_I2C_c34.c,70 ::       
0x0484   0x9AC5         BCF         SSPCON2, 5
L_I2C1_Rd15:
;__Lib_I2C_c34.c,72 ::       
0x0486   0x969E         BCF         PIR1, 3
;__Lib_I2C_c34.c,73 ::       
0x0488   0x88C5         BSF         SSPCON2, 4
;__Lib_I2C_c34.c,75 ::       
L_I2C1_Rd16:
0x048A   0xB69E         BTFSC       PIR1, 3
0x048C   0xD001         BRA         L_I2C1_Rd17
0x048E   0xD7FD         BRA         L_I2C1_Rd16        ****** This is where the code freezes ******
L_I2C1_Rd17:
;__Lib_I2C_c34.c,77 ::       
0x0490   0xF000C001     MOVFF       R1, R0
;__Lib_I2C_c34.c,78 ::       
0x0494   0x0012         RETURN      0
; end of _I2C1_Rd
/code


Top
 Profile  
 
 Post subject:
PostPosted: 24 Aug 2009 19:28 
Offline

Joined: 10 Mar 2007 06:47
Posts: 82
Location: New Zealand
Hi Zristic
Did you manage to find why the library is hanging up ?
Cheers
Rodney


Top
 Profile  
 
 Post subject:
PostPosted: 25 Aug 2009 12:26 
Offline
mikroElektronika team
User avatar

Joined: 03 Aug 2004 12:59
Posts: 6608
Hi,
This is the code for I2C_Rd, also please check errata document for P18F8722, note 29:

unsigned short I2C1_Rd(unsigned short ack) {
unsigned short tmp;                                 
  PIR1.SSPIF = 0;                 
  SSPCON2.RCEN = 1;           
  while (!PIR1.SSPIF) ;        // --- here you have the problem   

  tmp = SSPBUF;
  if (ack == 0)
    SSPCON2.ACKDT = 1;
  else
    SSPCON2.ACKDT = 0;
   
  PIR1.SSPIF = 0;                 
  SSPCON2.ACKEN = 1;         
  while (!PIR1.SSPIF) ;           // --- here you have the problem
 
  return tmp;
}



It happens that SSPIF bit is never set, why is this I would not know at this moment.

Just make sure that you read the errata document for P18F8722.


Top
 Profile  
 
 Post subject:
PostPosted: 25 Aug 2009 20:55 
Offline

Joined: 10 Mar 2007 06:47
Posts: 82
Location: New Zealand
Hello Zristic
Thank you for the reply.
Would it be possible for mikroE to fix the library I2C_Rd ?
Rodney


Top
 Profile  
 
 Post subject:
PostPosted: 25 Aug 2009 21:57 
Offline

Joined: 30 Apr 2009 02:36
Posts: 239
Location: Canada, Toronto
I have a better question:

zristic, Is the function you posted the equivalent of what I2C1_Rd() does ?

If yes, then all we have to do is modify the function you posted, remove the while loop, and use our function instead of the one in the mikroE library, right ?

But... what is the while loop checking ? is it checking if the data is sent ? If yes, then we have make a separate function to check this for ourselves, (and add a timeout to it also).


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 25 posts ]  Go to page 1, 2  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: