1
\$\begingroup\$

So I've been tackling this problem with integrating I2C functionality into my code, which is documented here on StackOverflow: https://stackoverflow.com/questions/25224541/receiving-undefined-symbols-error-with-xc8-concerning-plib-i2c-functions

TLDR: I2C plib didn't work because PIC12 is not a PIC18, I sorted it out by copying the sources and changing the relevant regs, hoping that it would work...

...and now I've run into another stumbling block. When the program tries to open an I2C connection, it seemingly remains stuck on the StartI2C() method which sets SSPCON2bits.SEN high and as it is so commented in the copied sources waits for it to "automatically be cleared by hardware". I'm running this in MPLABX's simulator and this is where its stuck, I have a PICkit3, but it tells me it can't breakpoint anywhere, so thats useless for actually debugging the part, so I'm assuming whats happening in the simulator is exactly whats happening on the part (which is a poor assumption, but I've got nothing else to really rely on right now).

So I'm guessing theres two questions here, should the ported code be compatible as written since it was written for the MSSP module that is probably copied around in many of their parts?

and

If the SSPCON2bits.SEN bit is supposed to be cleared by hardware automatically, why isn't it being cleared?

All of the relevant code concerning my wrappers and setup are in the linked StackOverflow question. All the relevant code concerning Microchip's I2C plib drivers are located in any install of MPLABX with XC8 under /path/to/xc8/install/directory/version/sources/pic18/plib/i2c/i2c_*.c with the header file being inside /path/to/xc8/install/directory/version/include/plib/i2c.h.

UPDATE: All the code

  • i2c.h - Microchip code modified for PIC12LF1552
    • pastebin.com/wLUJ0xpH
  • i2c.c - Microchip code modified for PIC12LF1552
    • pastebin.com/CTEyZETf
  • plur_i2c.h - My header file for wrapping Microchip's I2C functions
    • pastebin.com/HvphrFC8
  • plur_i2c.c - My source file for wrapping Microchip's I2C functions
    • pastebin.com/F95Kjrzj
  • main.c - Main body of code to be executed (not complete)
    • pastebin.com/NyTkP2pU
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You should not assume that the simulator mimics real hardware exactly, however in this case it may be failing for a similar reason. If SCL or SDA are low during start then a bus collision is assumed, the start is aborted, BCLIF flag is set (which generates an interrupt if enabled) and the MSSP module is reset. If the I2C bus is in a permanent collision state and your program continuously retries then it may appear to be 'stuck' at that point.

SCL and SDA are normally held high with pullup resistors. In the simulator, what is the normal state of the SCL and SDA pins? On your actual hardware, have you checked that your pullups are on the correct pins? What voltages are present? Have you monitored the pins with an oscilloscope or logic analyzer?

Another possibility is that your modified header files are still not compatible, and the MSSP is not being correctly initialized. You could examine all the relevant hardware registers in the simulator to verify that they have the correct values, or post your code here for others to try out.

\$\endgroup\$
17
  • \$\begingroup\$ The SCL and SDA pins a pulled-up to the system reference voltage (3.3 -> 5V depending on whats powering it, i.e: PICkit, battery, or USB) with two 1.5kOhm resistors. The voltage present on the bus is as you would expect, the system reference voltage. I have hooked up a scope to the pins and didn't see any activity, but given whats going on in the simulator and again (poorly) assuming that the exact same thing is happening on the part, it tries to send a START condition, but is hanging at waiting for the SSPCON2bits.SEN to become unset. I'll update the question with the relevant code. \$\endgroup\$
    – James Lui
    Commented Aug 18, 2014 at 14:05
  • \$\begingroup\$ The relevant code has been uploaded to pastebin and appended to the OP. \$\endgroup\$
    – James Lui
    Commented Aug 18, 2014 at 15:21
  • \$\begingroup\$ I don't have a logic analyzer, but I just ordered a Saleae Logic to help verify the transactions better. \$\endgroup\$
    – James Lui
    Commented Aug 18, 2014 at 19:13
  • \$\begingroup\$ Pastebin says the the file 'plur_i2c.h' has been removed. :( \$\endgroup\$ Commented Aug 18, 2014 at 23:10
  • \$\begingroup\$ You are assigning SDA to pin 4 (RA3) with APFCONbits.SDSEL = 1, but in CONFIG you have MCLRE_ON which assigns pin 4 to MCLR. If you have SDA connected to pin 4 then you need to change the config to MCLRE_OFF. If you are using pin 5 (RA2) for SDA then you need to set APFCONbits.SDSEL=0. \$\endgroup\$ Commented Aug 18, 2014 at 23:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.