I2C gets stuck at wait while complete.

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

I2C gets stuck at wait while complete.

150 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by jose_peeterson on Fri Oct 12 02:52:56 MST 2012
Dear all,

             can someone help me find out why the following code stops at wait while complete.

I am using the vcnl4000 i2c infrared distance finder. any ideas? why?


/*
 * I2C_Infrared_sensor.c
 *
 *  Created on: Oct 9, 2012
 *      Author: Aspire 4736G
 */

#include "stdio.h"
#include "math.h"

#include "lpc17xx_pinsel.h"
#include "lpc17xx_i2c.h"
//#include "lpc17xx_timer.h"
//#include "lpc17xx_gpio.h"

#define TRUE 1
#define FALSE 0

#define I2CDEV LPC_I2C2

#define vcnl4000_i2c_addr 0x13
#define vcnl4000_1r0 0x80
#define vcnl4000_1r1 0x81
#define vcnl4000_1r3 0x83
#define vcnl4000_1r4 0x84
#define vcnl4000_1r5 0x85
#define vcnl4000_1r6 0x86
#define vcnl4000_1r7 0x87
#define vcnl4000_1r8 0x88
#define vcnl4000_1r9 0x89
#define vcnl4000_1r10 0x8A

static void vcnl4000_init();
static void vcnl4000_read();

static int I2CWrite(uint8_t addr, uint8_t* buf, uint32_t len)
{
I2C_M_SETUP_Type txsetup;

txsetup.sl_addr7bit = addr;
txsetup.tx_data = buf;
txsetup.tx_length = len;
txsetup.rx_data = NULL;
txsetup.rx_length = 0;
txsetup.retransmissions_max = 3;

if (I2C_MasterTransferData(I2CDEV, &txsetup, I2C_TRANSFER_POLLING) == SUCCESS){
return (0);
} else {
return (-1);
}
}

static int I2CRead(uint8_t addr, uint8_t* buf, uint32_t len)
{
I2C_M_SETUP_Type rxsetup;

rxsetup.sl_addr7bit = addr;
rxsetup.tx_data = NULL;// Get address to read at writing address
rxsetup.tx_length = 0;
rxsetup.rx_data = buf;
rxsetup.rx_length = len;
rxsetup.retransmissions_max = 3;

if (I2C_MasterTransferData(I2CDEV, &rxsetup, I2C_TRANSFER_POLLING) == SUCCESS){
return (0);
} else {
return (-1);
}
}

static void init_i2c(void)
{
PINSEL_CFG_Type PinCfg;

/* Initialize I2C2 pin connect */
PinCfg.Funcnum = 2;
PinCfg.Pinnum = 10;
PinCfg.Portnum = 0;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 11;
PINSEL_ConfigPin(&PinCfg);

// Initialize I2C peripheral
I2C_Init(LPC_I2C2, 100000);

/* Enable I2C1 operation */
I2C_Cmd(LPC_I2C2, ENABLE);
}





int main(void)
{
init_i2c();

    vcnl4000_init();

    while(1)
       vcnl4000_read();

return 0;
}





static void vcnl4000_init()
{
uint8_t buf_w[1],buf_r[1],buf_set[2];
buf_r[0] = 0x00;

buf_w[0] = vcnl4000_1r0;                // reading register 0
I2CWrite(vcnl4000_i2c_addr,buf_w,1);
I2CRead(vcnl4000_i2c_addr,buf_r,1);

buf_set[0] = vcnl4000_1r0;
buf_set[1] = buf_r[0] | 0x18;
I2CWrite(vcnl4000_i2c_addr,buf_set,2);     // set bits 3 and 4 to 1 to start reading


buf_w[0] = vcnl4000_1r3; // reading register 3
buf_r[0] = 0x00;                         // clear
I2CWrite(vcnl4000_i2c_addr,buf_w,1);
I2CRead(vcnl4000_i2c_addr,buf_r,1);

buf_set[0] = vcnl4000_1r3;
buf_set[1] = buf_r[0] | 0x0A;
I2CWrite(vcnl4000_i2c_addr,buf_set,2);    // set bits 0 t0 5 for led current


buf_w[0] = vcnl4000_1r4; // reading register 4
buf_r[0] = 0x00;                         // clear
I2CWrite(vcnl4000_i2c_addr,buf_w,1);
I2CRead(vcnl4000_i2c_addr,buf_r,1);

buf_set[0] = vcnl4000_1r4;
buf_set[1] = buf_r[0] | 0x0F;
I2CWrite(vcnl4000_i2c_addr,buf_set,2);    // set bits 0 t0 3


buf_w[0] = vcnl4000_1r9; // reading register 9
buf_r[0] = 0x00;                         // clear
I2CWrite(vcnl4000_i2c_addr,buf_w,1);
I2CRead(vcnl4000_i2c_addr,buf_r,1);

buf_set[0] = vcnl4000_1r9;
buf_set[1] = buf_r[0] | 0x00;
I2CWrite(vcnl4000_i2c_addr,buf_set,2);    //


buf_w[0] = vcnl4000_1r10; // reading register 10
buf_r[0] = 0x00;                         // clear
I2CWrite(vcnl4000_i2c_addr,buf_w,1);
I2CRead(vcnl4000_i2c_addr,buf_r,1);

buf_set[0] = vcnl4000_1r10;
buf_set[1] = buf_r[0] | 0x81;
I2CWrite(vcnl4000_i2c_addr,buf_set,2);    //


return;
}


static void vcnl4000_read()
{
uint8_t buf_w[1],buf_r[1],temp_r[1],read_cond[1],buf_light_w[1],buf_light[2];
uint8_t buf_infrared_w[1],buf_infrared[2];
uint16_t light,infrared;
temp_r[0] = 0x00;

buf_w[0] = vcnl4000_1r0;

I2CWrite(vcnl4000_i2c_addr,buf_w,1);
I2CRead(vcnl4000_i2c_addr,buf_r,1);

temp_r[0] = temp_r[0] | buf_r[0];  // copy over to temp

read_cond[0] = temp_r[0] & 0xFF;

if(read_cond[0] == 0xFF)    // read only if bits are 5 and 6 are 1
{

     buf_light_w[0] = vcnl4000_1r5;               // reading light
 I2CWrite(vcnl4000_i2c_addr,buf_light_w,1);
 I2CRead(vcnl4000_i2c_addr,buf_light,2);
 light = (buf_light[0] << 8) | buf_light[1];

 buf_infrared_w[0] =  vcnl4000_1r7;           // reading infrared
 I2CWrite(vcnl4000_i2c_addr,buf_infrared_w,1);
 I2CRead(vcnl4000_i2c_addr,buf_infrared,2);
 infrared = (buf_infrared[0] << 8) | buf_infrared[1];

 printf("light = %d\n",light);
 printf("Infrared = %d\n",infrared);

}
else                        // data unavailable
{
 printf("value not ready yet\n");
 return;
}

return;
}


0 Kudos
0 Replies