I2C driver for LPC11u67

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

I2C driver for LPC11u67

516 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by elgarbe on Fri Jul 10 06:44:58 MST 2015
Hi, I'm working on reading a LM92 digital temp sensor.
I'm using this code:

#include <board.h>
#include "LM92.h"

/*****************************************************************************
 * Private types/enumerations/variables
 ****************************************************************************/

/* I2CM transfer record */
static I2CM_XFER_T  i2cmXferRec;

/* 1Mbps I2C bit-rate */
#define I2C_BITRATE         (400000)
#define I2C_RD_CMD_BIT      (0x01)
#define I2C_FASTPLUS_BIT    (0)
#define I2C_ADDR       (0x48)

static volatile int intErrCode;

/*****************************************************************************
 * Public types/enumerations/variables
 ****************************************************************************/

/*****************************************************************************
 * Private functions
 ****************************************************************************/

/* Setup I2C handle and parameters */
void setupI2CMaster()
{
/* Enable I2C clock and reset I2C peripheral - the boot ROM does not
   do this */
Chip_I2CM_Init(LPC_I2C0);

/* Setup I2CM transfer rate */
Chip_I2CM_SetBusSpeed(LPC_I2C0, I2C_BITRATE);
}

/* Function to wait for I2CM transfer completion */
static void WaitForI2cXferComplete(I2CM_XFER_T *xferRecPtr)
{
/* Test for still transferring data */
while (xferRecPtr->status == I2CM_STATUS_BUSY) {
/* Sleep until next interrupt */
//__WFI();
}
}

/* Function to setup and execute I2C transfer request */
static void SetupXferRecAndExecute(uint8_t devAddr,
   uint8_t *txBuffPtr,
   uint16_t txSize,
   uint8_t *rxBuffPtr,
   uint16_t rxSize)
{
/* Setup I2C transfer record */
i2cmXferRec.slaveAddr = devAddr;
i2cmXferRec.options = 0;
i2cmXferRec.status = 0;
i2cmXferRec.txSz = txSize;
i2cmXferRec.rxSz = rxSize;
i2cmXferRec.txBuff = txBuffPtr;
i2cmXferRec.rxBuff = rxBuffPtr;
Chip_I2CM_Xfer(LPC_I2C0, &i2cmXferRec);

/* Wait for transfer completion */
WaitForI2cXferComplete(&i2cmXferRec);
}

/*****************************************************************************
 * Public functions
 ****************************************************************************/

/**
 * @briefHandle I2C0 interrupt by calling I2CM interrupt transfer handler
 * @returnNothing
 */
void I2C0_IRQHandler(void)
{
/* Call I2CM ISR function with the I2C device and transfer rec */
Chip_I2CM_XferHandler(LPC_I2C0, &i2cmXferRec);
}

/* Master I2CM receive in interrupt mode */

float LM92_ReadTemp()
{
uint8_t rx_buffer[3];
uint8_t tx_buffer[3];

/* Read LM92 temerature sensor */
SetupXferRecAndExecute(I2C_ADDR, tx_buffer, 1, rx_buffer, 2);

/* Test for valid operation */
if (i2cmXferRec.status == I2CM_STATUS_OK) {
/* Note results are only valid when there are no errors */
return(0.0625 * (((rx_buffer[0] << 8) + rx_buffer[1]) >>3));
}
return (0);
}


and set a timer to read data every 100mseg:

void TIMER16_1_IRQHandler(void)
{
if (Chip_TIMER_MatchPending(LPC_TIMER16_1, 1))
{
Chip_TIMER_ClearMatch(LPC_TIMER16_1, 1);
temp = LM92_ReadTemp();
}
}


the problem is that not all temp reading have good values. I mean, first reading have good temp value, next reding not, next reading not, next reading have good value and so. Good, wrong value seems to be a litle random.
I think the problem could be on WaitForI2cXferComplete. I have to remove WFI becouse of the rest of my code...

I'm wondering what is the best driver for this kind of aplication. I try to test ROM I2C api, but I can't make it work.

Thank!
Labels (1)
0 Kudos
0 Replies