LPCOpen v2.05 - lpc13xx - weird I2C errors

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LPCOpen v2.05 - lpc13xx - weird I2C errors

579 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by nidalpres on Fri Apr 03 19:46:58 MST 2015
I have a C++ project which has a cpp file with a function that goes like this:

void I2C_Init()
{
    /* Initialize I2C
     * - enable I2C clock via SYSAHBCLKCTRL
     * - resets I2C peripheral via clocking I2C pin in PRESETCTRL
     */
    Chip_I2CM_Init(LPC_I2C);
    /*
     * Sets SCLH and SCLL based on PCLK (which is same as core clock)
     * and given speed
     */
    Chip_I2CM_SetBusSpeed(LPC_I2C, SPEED_100KHZ);
    /*
     * Clears SI, STO, STA and AA bits
     * via CONCLR
     */
    Chip_I2CM_ResetControl(LPC_I2C);
    /*
     * clears I2EN bit via CONCLR
     * Chip_I2CM_SendStart() will set I2EN bit
     */
    Chip_I2CM_Disable(LPC_I2C);
}


It works like a charm, I2C initializes and I can communicate with my I2C device.

If I change it to this (note how instead passing LPC_I2C to functions I pass a variable with that value):

void I2C_Init()
{
    LPC_I2C_T *lpcI2C = LPC_I2C;

    Chip_I2CM_Init(lpcI2C);

    Chip_I2CM_SetBusSpeed(lpcI2C, SPEED_100KHZ);

    Chip_I2CM_ResetControl(lpcI2C);

    Chip_I2CM_Disable(lpcI2C);
}



all I get after calling Chip_I2CM_XferBlocking() (or anything else for that matter) is xfer.status == I2CM_STATUS_BUS_ERROR.

Also, weird, in second case (when using variable), LPCXpresso IDE reports connection error when uploading code to target (via debug in LPCXpresso IDE).
I have to disconnect LPCXpresso 1347 board from USB, connect it back again and uploading code now works.

Also, weird, in second case (when using variable), if I set breakpoint on my call to Chip_I2CM_XferBlocking() and then just press F8 (continue run) when execution hits the breakpoint, suddenly there are no more I2CM_STATUS_BUS_ERRORs and I get readouts from the sensor connected to LPCXpresso 1347 board.

Remove variable lpcI2C and pass LPC_I2C value directly to those calls in I2C_Init(), and all works fantastically from the start, no need to reset board by reconnecting it, code upload works right away, code reads data from sensor from the start.

My question:
W.
T.
F.
???

Excuse my french, I just spent like 3 hours figuring out what is going on and at the end narrowed it down to what I described in here.
Now I'm just puzzled, amazed and bewildered.

If anyone has any comments, please do.

I'll go now and have a beer. Or two. Or three.
Labels (1)
0 Kudos
Reply
1 Reply

534 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by nidalpres on Fri Apr 03 19:49:34 MST 2015
Btw. those code samples are not code samples, both of those I2C_Init() functions are literally the way I pasted them in that post.

And now, to that beer...

0 Kudos
Reply