I2C_Write blocking waiting for ACK

General discussion on mikroC PRO for ARM.
Author
Message
csturner
Posts: 2
Joined: 31 May 2013 21:02

I2C_Write blocking waiting for ACK

#1 Post by csturner » 31 May 2013 21:19

Hello,

I have scoured the forums looking for a solution to this, but I haven't found any. I am using the STM32 dev board (STM32F407) and I am running in to 2 issues with the I2C Library:
1) If the slave device isn't present and the controller doesn't receive an ACK, the I2C_Read and I2C_Write functions cause the devices to "freeze" (i.e. they are blocking).
2) If the controller is reset mid-message with the slave device, there is no way to reset the bus.

Does anyone have a solution to this?

MikroElektronika - are there any plans to update the I2C library to address this? If not, can I get the source from the library so that I can modify it to address these issues?

Regards,
Cam

User avatar
dejan.odabasic
mikroElektronika team
Posts: 2649
Joined: 30 Apr 2012 14:20

Re: I2C_Write blocking waiting for ACK

#2 Post by dejan.odabasic » 03 Jun 2013 17:06

Hello,

I2C library is implemented according to I2C specifications, unfortunately I2C specification does not specify any timeout conditions - an I2C device can occupy the bus for an arbitrary time period.

Additional obscure I2C problems are listed on this web page:
http://www.i2c-bus.org/obscure-problems/

Please understand that source code of compiler built-in libraries is not public due to the company's policy.
Thank you for understanding.

Best regards.

csturner
Posts: 2
Joined: 31 May 2013 21:02

Re: I2C_Write blocking waiting for ACK

#3 Post by csturner » 03 Jun 2013 17:30

While what you say is certainly true, it isn't very useful for the entire application to lock up when trying to read from a device that isn't present.

It would be much more beneficial if you either:
1) Added a timeout (say 1 second), where if there is no acknowledgement from the slave on the address byte, or any byte for that matter, the read / write functions exit with an error code and allow the application to keep running.
2) Implemented SM bus

As written, your libraries are very dangerous to use. Not all slave devices comply exactly to the spec, and if any failure occurs, the board requires a reset.

Thanks for your response.

User avatar
dejan.odabasic
mikroElektronika team
Posts: 2649
Joined: 30 Apr 2012 14:20

Re: I2C_Write blocking waiting for ACK

#4 Post by dejan.odabasic » 04 Jun 2013 13:07

Hello,

Compiler I2C library is designed to be used with devices which are compatible with I2C bus specification.
Timeout modification that you suggested deviates from the standard, but I have presented your suggestion to our developers and they will consider it.

SM but implementation would require much more customization since there are few major differences when SM bus is compared with I2C bus.
(electrical, timing, protocols and operating modes).

Best regards.

prakob
Posts: 187
Joined: 24 Nov 2012 07:05
Location: Thailand

Re: I2C_Write blocking waiting for ACK

#5 Post by prakob » 04 Jun 2013 17:15

Hi Dejan,

I agree with csturner because there are many suggestion in embedded programming to using loop timeouts to avoid hanging of microcontroller while waiting for hardware operation.

For Examples,

Code: Select all

while (!ADCON1bits.DONE);	// 3. wait for the conversion to complete ###
Such code is potential unreliable, because there are circumstances under which microcontroller may hang if ADC has been incorrectly initialized, we can not sure that ADC's conversion will complete.

About I2C bus specification, I think that the program should not wait for acknowledge bit in while loop.
The application program should not hang in while loop just because of no receiver is present or damaged receiver.


Image

The I2C function should return either Acknowledge (ACK) or Not Acknowledge (NACK) bit instead of hanging and let user to make decision what is the meaning of return bit as the recommendation from attached picture.

Cradit: I2C-bus specification and user manual, NXP

Best Regards

panooli
Posts: 7
Joined: 17 Jan 2012 10:00

Re: I2C_Write blocking waiting for ACK

#6 Post by panooli » 31 Aug 2013 03:56

I have the same problem with the i2c functions for STM32F4xx, which are useless in the current version.

The problem is not only the missing time out, but also the version such as the registers are checked.
It lacks the review whether the hardware reports an error (e.g. "I2C_SR1.AF - Acknowledge failure" if addressed hardware is not exist).
Your comparison of I2Cx_Get_Status with a fixed values results in a endless loop if an error bit is set and a "normal" finish is not coming

As a suggestion for improvement:
1. immediate abort the function with return a error if a hardware-error-bit is set
2. an (configurable) time out as second failsafe for all other errors



PS: The I2C functions for Stellaris M3 does not have this problems

ilferrari
Posts: 195
Joined: 18 Nov 2013 09:09

Re: I2C_Write blocking waiting for ACK

#7 Post by ilferrari » 22 Mar 2016 14:48

dejan.odabasic wrote:I2C library is implemented according to I2C specifications, unfortunately I2C specification does not specify any timeout conditions - an I2C device can occupy the bus for an arbitrary time period.
I think this is a misunderstanding of what is quoted there. We are talking about what the receiver does on the ninth clock pulse - there is no need to wait or timeout. The master clocks out the ninth bit and the ACK gets logged in hardware and should be returned back to the user. It is impossible to do acknowledge polling, which is a normal write procedure with I2C eeproms.

I think the library design is wrong here - it should just return the ACK value and definitely not hang. I hope ME will take another look at this.

Val Gretchev
Posts: 19
Joined: 16 Apr 2012 22:01
Location: Whitby, Ontario, Canada

Re: I2C_Write blocking waiting for ACK

#8 Post by Val Gretchev » 23 Mar 2016 14:09

Without a time-division-multiplexing operating system, all tasks in your code should be written as state machines. That is also true for using I2C which can hang up the system if something goes wrong. Also, avoid using Delay_ms(); which stalls the processor for the duration of the delay.

I posted an article on LibStock you may be able to adapt for your needs. See:
http://libstock.mikroe.com/projects/vie ... tm32-board

Val Gretchev

ITDesigns
Posts: 13
Joined: 15 Sep 2014 07:10

Re: I2C_Write blocking waiting for ACK

#9 Post by ITDesigns » 18 May 2016 02:16

dejan.odabasic wrote:Hello,

Compiler I2C library is designed to be used with devices which are compatible with I2C bus specification.
Timeout modification that you suggested deviates from the standard, but I have presented your suggestion to our developers and they will consider it.

SM but implementation would require much more customization since there are few major differences when SM bus is compared with I2C bus.
(electrical, timing, protocols and operating modes).

Best regards.
OK Dejan
Its 3 years later - do we / will we have a timeout function for the I2C ?
This is not a question of whether it deviates from the I2C specification -
- Its a matter of simple practicality and good coding practice.
How can I report a problem if an I2C device has failed ?

Well I can't if it goes in to an infinite loop !!

Its great to see Val's solution but this really ought to be part of the library functions.
Thanks Val, I'll try and cobble something together in Pascal and post it

Regards

Geoff

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: I2C_Write blocking waiting for ACK

#10 Post by filip » 19 May 2016 15:13

Hi,

Unfortunately, this is not yet implemented in the I2C library, so you should still use the solution given on this page.

Regards,
Filip.

ITDesigns
Posts: 13
Joined: 15 Sep 2014 07:10

Re: I2C_Write blocking waiting for ACK

#11 Post by ITDesigns » 30 Jun 2016 06:47

filip wrote:Hi,

Unfortunately, this is not yet implemented in the I2C library, so you should still use the solution given on this page.

Regards,
Filip.
Unfortunately, I am working in mikroPascal, so I can't use the solution given on this page. . . .

Regards

Geoff

ITDesigns
Posts: 13
Joined: 15 Sep 2014 07:10

Re: I2C_Write blocking waiting for ACK

#12 Post by ITDesigns » 06 Jul 2016 05:26

Hi

Why is
I2Cn_Start a function
I2C_Read a procedure
I2C_Write a function ?

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: I2C_Write blocking waiting for ACK

#13 Post by filip » 06 Jul 2016 14:05

Hi,

I2Cn_Start returns 0 if I2C started OK and -1 if there is an error.
I2C_Read and Write both should be procedures, for some reason the Write is a function, but it only returns 0 in any case.

Thank you for spotting this, I will present this to our developer.s

Regards,
Filip.

ITDesigns
Posts: 13
Joined: 15 Sep 2014 07:10

Re: I2C_Write blocking waiting for ACK

#14 Post by ITDesigns » 12 Jul 2016 02:42

Hi
I have posted a solution to this problem for mikroPascal on libstock :-
1468287584_i2c_library_func_mikropascal_arm
I hope it is useful to anyone else who has had this problem.
I should be relatively easy to convert to mikroC

Geoff

Synaps3
Posts: 17
Joined: 29 Sep 2013 22:23

Re: I2C_Write blocking waiting for ACK

#15 Post by Synaps3 » 26 Jan 2017 11:12

Could someone please point me to the I2C code for mikroC for PIC that works around this problem?

I am starting to realize the laziness of the mikroC team. This has been an issue for years and instead they rely on users to submit code to fix problems with their own software. Add an optional timeout. It shouldn't be that hard.

And the whole thing about this not being part of the I2C standard - Timeout has nothing to do with the standard, it is a side issue that is fixed by good coding practice. That is not an excuse. How do you expect your software to be used in professional products when it goes into infinite loop on error?

Post Reply

Return to “mikroC PRO for ARM General”