Neil
2018-10-15 18:38:30 UTC
Anyone here used the PIC32 peripheral library for I2C? I'm having a
hard time with this, and can't find my ICD3 to help diagnose what's
happening.
I'm continuously reading data from a slave I2C device (PIC18F25K42
based), and the master (PIC32MX320F128H) crashes (reboots) regularly.
I'm posting about the maser here...
Flags on reboot only have the POR and BOR flags set. Adding a bit of
bulk capacitance (10uf) does not help. And this is happening with both
Pickit3 power or LiPo w/5V DC-DC power. I also don't see a way to
change BOR threshold voltage as with other PIC series.
Logic analyzer says that it's happening at different points, but always
between I2C start and stop.
I'm at a loss as to what to test next, but perhaps someone can identify
something I'm doing wrong? This is my master code to read data. The
delays were put in earlier when testing w/o error-checking, and seems to
still help to reduce crashes.
HandleI2CErrors() is just a quick function to check and clear
arbitration, TX overflow, and RX overflow errors, and also sets an LED
to alert me, but that LED stays off.
signed int ReadAFEValue(void)
{
signed int result, inVal, retVal;
retVal = 1;
if (PLIB_I2C_BusIsIdle(I2C_ID_1))
{
PLIB_I2C_MasterStart(I2C_ID_1);
// Tell AFE to restart result counter and be ready to send data...
// delay_us(100);
DelayMs(1);
if (PLIB_I2C_TransmitterIsReady(I2C_ID_1))
{
PLIB_I2C_TransmitterByteSend(I2C_ID_1,0x54); //
Slave addr 0x2A in bits 7:1. 1 in LSB = read request, or 0 = write req..
if (PLIB_I2C_TransmitterByteWasAcknowledged(I2C_ID_1))
{
// delay_us(100);
DelayMs(1);
if (PLIB_I2C_TransmitterIsReady(I2C_ID_1))
{
PLIB_I2C_TransmitterByteSend(I2C_ID_1,0xB0);
// Command for set mode to read_data mode
if (PLIB_I2C_TransmitterByteWasAcknowledged(I2C_ID_1))
{
HandleI2CErrors();
// Restart...
// delay_us(200);
DelayMs(1);
if (PLIB_I2C_BusIsIdle(I2C_ID_1))
{
PLIB_I2C_MasterStartRepeat(I2C_ID_1);
HandleI2CErrors();
// Tell AFE we want to read data now...
// delay_us(200);
DelayMs(1);
if (PLIB_I2C_TransmitterIsReady(I2C_ID_1))
{
PLIB_I2C_TransmitterByteSend(I2C_ID_1,0x55); // Slave addr 0x2A in bits
7:1. 1 in LSB = read request, or 0 = write req..
if
(PLIB_I2C_TransmitterByteWasAcknowledged(I2C_ID_1))
{
HandleI2CErrors();
// delay_us(200);
DelayMs(1);
PLIB_I2C_MasterReceiverClock1Byte(I2C_ID_1); // Get first byte back from
AFE.
// delay_us(100);
DelayMs(1);
if
(PLIB_I2C_ReceivedByteIsAvailable(I2C_ID_1))
{
if
(PLIB_I2C_MasterReceiverReadyToAcknowledge(I2C_ID_1))
{
result =
PLIB_I2C_ReceivedByteGet(I2C_ID_1); // TODO -- store this
somewhere
PLIB_I2C_ReceivedByteAcknowledge(I2C_ID_1, true);
// while
(!PLIB_I2C_ReceiverByteAcknowledgeHasCompleted(I2C_ID_1));
}
}
HandleI2CErrors();
// delay_us(200);
DelayMs(1);
PLIB_I2C_MasterReceiverClock1Byte(I2C_ID_1); // Get second byte back
from AFE.
// delay_us(200);
DelayMs(1);
if
(PLIB_I2C_ReceivedByteIsAvailable(I2C_ID_1))
{
if
(PLIB_I2C_MasterReceiverReadyToAcknowledge(I2C_ID_1))
{
inVal =
PLIB_I2C_ReceivedByteGet(I2C_ID_1); // TODO -- store
this somewhere
PLIB_I2C_ReceivedByteAcknowledge(I2C_ID_1, false); // NAK
here is required for last byte
result = result | (inVal
<< 8);
retVal = result;
}
}
HandleI2CErrors();
// DelayMs(1);
// delay_us(200);
DelayMs(1);
while
(PLIB_I2C_TransmitterIsReady(I2C_ID_1) == 0); // TODO
-- countdown and timeout here?
}
else
retVal = -8;
}
else
retVal = -7;
}
else
retVal = -6;
}
else
retVal = -5;
}
else
retVal = -4;
}
else
retVal = -3;
}
else
retVal = -2;
PLIB_I2C_MasterStop(I2C_ID_1);
}
return retVal;
} // ReadAFEValue()
Cheers,
-Neil
hard time with this, and can't find my ICD3 to help diagnose what's
happening.
I'm continuously reading data from a slave I2C device (PIC18F25K42
based), and the master (PIC32MX320F128H) crashes (reboots) regularly.
I'm posting about the maser here...
Flags on reboot only have the POR and BOR flags set. Adding a bit of
bulk capacitance (10uf) does not help. And this is happening with both
Pickit3 power or LiPo w/5V DC-DC power. I also don't see a way to
change BOR threshold voltage as with other PIC series.
Logic analyzer says that it's happening at different points, but always
between I2C start and stop.
I'm at a loss as to what to test next, but perhaps someone can identify
something I'm doing wrong? This is my master code to read data. The
delays were put in earlier when testing w/o error-checking, and seems to
still help to reduce crashes.
HandleI2CErrors() is just a quick function to check and clear
arbitration, TX overflow, and RX overflow errors, and also sets an LED
to alert me, but that LED stays off.
signed int ReadAFEValue(void)
{
signed int result, inVal, retVal;
retVal = 1;
if (PLIB_I2C_BusIsIdle(I2C_ID_1))
{
PLIB_I2C_MasterStart(I2C_ID_1);
// Tell AFE to restart result counter and be ready to send data...
// delay_us(100);
DelayMs(1);
if (PLIB_I2C_TransmitterIsReady(I2C_ID_1))
{
PLIB_I2C_TransmitterByteSend(I2C_ID_1,0x54); //
Slave addr 0x2A in bits 7:1. 1 in LSB = read request, or 0 = write req..
if (PLIB_I2C_TransmitterByteWasAcknowledged(I2C_ID_1))
{
// delay_us(100);
DelayMs(1);
if (PLIB_I2C_TransmitterIsReady(I2C_ID_1))
{
PLIB_I2C_TransmitterByteSend(I2C_ID_1,0xB0);
// Command for set mode to read_data mode
if (PLIB_I2C_TransmitterByteWasAcknowledged(I2C_ID_1))
{
HandleI2CErrors();
// Restart...
// delay_us(200);
DelayMs(1);
if (PLIB_I2C_BusIsIdle(I2C_ID_1))
{
PLIB_I2C_MasterStartRepeat(I2C_ID_1);
HandleI2CErrors();
// Tell AFE we want to read data now...
// delay_us(200);
DelayMs(1);
if (PLIB_I2C_TransmitterIsReady(I2C_ID_1))
{
PLIB_I2C_TransmitterByteSend(I2C_ID_1,0x55); // Slave addr 0x2A in bits
7:1. 1 in LSB = read request, or 0 = write req..
if
(PLIB_I2C_TransmitterByteWasAcknowledged(I2C_ID_1))
{
HandleI2CErrors();
// delay_us(200);
DelayMs(1);
PLIB_I2C_MasterReceiverClock1Byte(I2C_ID_1); // Get first byte back from
AFE.
// delay_us(100);
DelayMs(1);
if
(PLIB_I2C_ReceivedByteIsAvailable(I2C_ID_1))
{
if
(PLIB_I2C_MasterReceiverReadyToAcknowledge(I2C_ID_1))
{
result =
PLIB_I2C_ReceivedByteGet(I2C_ID_1); // TODO -- store this
somewhere
PLIB_I2C_ReceivedByteAcknowledge(I2C_ID_1, true);
// while
(!PLIB_I2C_ReceiverByteAcknowledgeHasCompleted(I2C_ID_1));
}
}
HandleI2CErrors();
// delay_us(200);
DelayMs(1);
PLIB_I2C_MasterReceiverClock1Byte(I2C_ID_1); // Get second byte back
from AFE.
// delay_us(200);
DelayMs(1);
if
(PLIB_I2C_ReceivedByteIsAvailable(I2C_ID_1))
{
if
(PLIB_I2C_MasterReceiverReadyToAcknowledge(I2C_ID_1))
{
inVal =
PLIB_I2C_ReceivedByteGet(I2C_ID_1); // TODO -- store
this somewhere
PLIB_I2C_ReceivedByteAcknowledge(I2C_ID_1, false); // NAK
here is required for last byte
result = result | (inVal
<< 8);
retVal = result;
}
}
HandleI2CErrors();
// DelayMs(1);
// delay_us(200);
DelayMs(1);
while
(PLIB_I2C_TransmitterIsReady(I2C_ID_1) == 0); // TODO
-- countdown and timeout here?
}
else
retVal = -8;
}
else
retVal = -7;
}
else
retVal = -6;
}
else
retVal = -5;
}
else
retVal = -4;
}
else
retVal = -3;
}
else
retVal = -2;
PLIB_I2C_MasterStop(I2C_ID_1);
}
return retVal;
} // ReadAFEValue()
Cheers,
-Neil
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist