Created Thu, 02 Jan 2014 21:27:47 +0000 by bperrybap
Thu, 02 Jan 2014 21:27:47 +0000
Matt and I have been looking at a Wire library issue. The summary is that if you call begin() more than once the library can hang. You may say "well just don't do that". Well it isn't that easy. This is because some libraries that use i2c call the Wire.begin() function for the user. So if more than one of these i2c using libraries is used, then the system will hang in the Wire code as soon as any master operation is done after the second call to Wire.begin()
I tracked it down to an initailzation sequence in the twi_init() code in the twi.c module. It appears that if you set the ON and STREN bits at the same time after the i2c module is already enabled, then as soon as a master operation is done, the i2c module drops the i2c clock low and will wait forever. It appears to be stuck in a permanent clock stretch.
Further details can be found in the comments of this pull notice: [url]https://github.com/chipKIT32/chipKIT32-MAX/pull/396[/url]
It would be nice to get some information from a pic32 i2c module expert as the behavior I'm seeing is currently outside my understanding of the data sheet.
--- bill
Sat, 08 Feb 2014 16:24:44 +0000
What about adding something like this to the beginning of twi_init():
static bool initialized = false;
if( initialized == true ) return;
initialized = true;
Its kind of ghetto, but may get you through your problem.
Jacob
Sat, 08 Feb 2014 18:29:30 +0000
majenko added something similar into the begin() code in the twowire class and it has been merged into the mainline code so the problem won't show up in future releases. (See the github pull referenced above).
But this really doesn't solve the problem. It merely prevents it. There is a comment stating as much in the new codel, but things like this tend to be forgotten over time.
I was contacted by microchip. We discussed it a bit, I need to follow-up with them to see if there is any additional information or an actual known solution available.
--- bill