LPCOpen v2.05 - lpc13xx - weird I2C errors

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
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
     * 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
     * clears I2EN bit via CONCLR
     * Chip_I2CM_SendStart() will set I2EN bit

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_SetBusSpeed(lpcI2C, SPEED_100KHZ);



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:

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.