#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);
}
|