|
|
View previous topic :: View next topic |
Author |
Message |
benoitstjean
Joined: 30 Oct 2007 Posts: 566 Location: Ottawa, Ontario, Canada
|
PIC24EP - I2C bus locking-up qustion again [Solved] |
Posted: Mon Feb 05, 2024 11:55 am |
|
|
Device: PIC24EP512GP806
Compiler: 5.026 (new one on order!!)
This is somewhat of a similar issue to an earlier question I posted which I honestly did not have time to look into for other reasons.
So here's the situation:
I have a FW that I run on two very similar circuits. One has sensors tied to I2C, one does not. The one with the sensors obviously has pull-up resistors on the I2C bus and initializes the sensors just fine. The one without the sensors does not have pull-ups (floating... I know, my bad, will correct on next version).
For the FW to differentiate between what circuit it's booting on, I want it to be based off if I can initialize the I2C devices or not...
So I figured I'd be smart and perform a read of the SDA and SCL pins so that if they both return '1', it means they are high therefore the resistors are present therefore the sensors are present therefore I can initialize them. Then if the pins are low, it means there aren't any sensors so I can just skip the initialization.
But it's not that simple: for some reason, when SDA and SCL pins are read, on both ciruits, the input() function returns 0! Even on the one with the pull-ups! But the worst part is that althgouh they return 0, the board with sensors initializes just fine and the one without locks-up.
I completely understand that having floating inputs is a bad idea but this is not even part of the problem since the input(SDA) and input(SCL) both return 0 even with pull-up resistors and the worst part is that the sensors initialize!
Any other idea or suggestion?
I've read through my previous post on this but it didn't help resolve the issue.
Thanks again,
Ben
Last edited by benoitstjean on Mon Feb 05, 2024 12:55 pm; edited 1 time in total |
|
|
benoitstjean
Joined: 30 Oct 2007 Posts: 566 Location: Ottawa, Ontario, Canada
|
|
Posted: Mon Feb 05, 2024 12:35 pm |
|
|
Alrighty, I think it works!
Went back through my initial message from months ago and this is what I changed to the code:
Code: | Original line:
#use i2c( MASTER, FAST, sda=PIN_G3, scl=PIN_G2, FORCE_HW )
New line:
#use i2c( MASTER, FAST, sda=PIN_G3, scl=PIN_G2, FORCE_HW, noinit ) // Force I2C to NOT initialize with noinit flag
main()
{
//....
// Force SDA and SCL pins low
output_low( PIN_G3 );
output_low( PIN_G2 );
delay_ms( 100 );
// Force them to float
output_float( PIN_G3 );
output_float( PIN_G2 );
// Here, it appears that when resistors are present, both input() functions return TRUE as opposed
// to when sensors are not present, they both return false
bool TEST_SDA = input( PIN_G3 );
bool TEST_SCL = input( PIN_G2 );
// Check if pins returned TRUE
if( TEST_SDA == TRUE && TEST_SCL == TRUE )
{
// Sensors are present, initialize I2C
i2c_init( TRUE );
}
else
{
// Sensors are not present
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Tue Feb 06, 2024 1:58 am |
|
|
Well done.
I was going to post this morning suggesting no_init, but you found it.
Key is that on old chips the I/O lines are still left connected to the
processor even when you use a peripheral. This is why you have to set
the TRIS to suit the peripheral involved. On many of the newer chips,
the normal I/O functions are disabled when the peripheral is enabled.
Hence what you were seeing.
You could possibly make this even safer, by enabling the internal pull-down
on these pins. This is typically only about 80KR, so won't interfere with
normal operation, but will ensure they read as 0 on the chip where the
lines as 'floating'. |
|
|
benoitstjean
Joined: 30 Oct 2007 Posts: 566 Location: Ottawa, Ontario, Canada
|
|
Posted: Tue Feb 06, 2024 7:23 am |
|
|
Oh geez, didn't think of the pull downs! Will do that for sure.
Thanks!
Ben |
|
|
|
|
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 vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|