Questions about Software I2C Library

Discuss with MikroElektronika software developers about current library development.
Post Reply
Author
Message
srogers
Posts: 24
Joined: 25 Aug 2012 02:15
Location: Webster, NY, USA

Questions about Software I2C Library

#1 Post by srogers » 11 Mar 2013 17:35

The Software 12C Library has some confusing statements and concepts on which I'd like some clarification.

1. The help states that the library uses time-based activities, so interrupts need to be disabled when using the functions in the library.

2. However, the library has a Soft_I2C_Break method that one must call from within an interrupt routine. How does one get an interrupt routine to get called with interrupts disabled?

3. The example for the Soft_I2C_Break method shows the setup of Timer0 to cause an interrupt every 1 ms, and through a counter, times out after 20 milliseconds calling the Soft_I2C_Break method. In the example, one enables all interrupts, calls Soft_I2C_Init, and then disables all interrupts. Does the Soft_I2C_Init routine called with interrupts enabled detect enabled interrupts and does the Software I2C library work with that information in subsequent calls to the library to prevent blocking calls on Soft_I2C_* functions?

4. I have determined that the Timer0 interrupt occurs once every 1 ms (for Fosc/4 = 8 MHz) and that a global variable, counter, holds the count of 1 ms interrupts detected so far. Does the code calling the the various Soft I2C library functions that might block need to clear the counter and to enable interrupts before calling the the blocking function? If not, how does the Soft_I2C library know that the Soft_I2C_Break function in in play, and how does the Soft I2C Library know to clear the counter global variable at the start of a blocking call?

Could someone enlighten me on how this all works?

None if this information is clear from the Help file for the library.

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: Questions about Software I2C Library

#2 Post by janko.kaljevic » 26 Jun 2013 13:48

Hello,

I apologize for the late reply.

Soft I2C is very sensitive to timings and it is important not to have interrupts during its operations.
Or if you have interrupt, keep them very short.

Soft_I2C_Break function will allow you to break from the Soft I2C routines when they get stuck from any reason.
The best would be to call it from timer interrupt, when desired amount of time passes. Of course, when interrupts are disabled, timer interrupt will not trigger.
But you can disable other interrupts and leave timer interrupt on during I2C operations.

Soft_I2C library functions do not change interrupt settings. So you will have to enable timer interrupt before calling I2C functions if you want to use antiblocking mechanism.

When Soft_I2C_Break is called it sets antiblocking flag, which will cause breaking from while loop (waiting for the I2C slave).
Before calling functions that may cause blocking this flag is internally reset.

Best regards.

Post Reply

Return to “Library Development Discussion”