FRDM-KV11z + MCUXpresso IDE + KSDKv2 + I2C example for external sensor

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

FRDM-KV11z + MCUXpresso IDE + KSDKv2 + I2C example for external sensor

2,270 Views
anoja
Contributor II

Can someone help me with a simple I2C example with no interrupt. I tried the SDK example. But doesnt working for an external sensor (TCS3471).

Labels (1)
7 Replies

1,684 Views
anoja
Contributor II

I noticed that only write to sensor worked. Read from sensor is not working at all.

Hardware: FRDM KV11Z (removed R8,R9,R10) hardwired SCL and SDA to  Color click - TCS3471 color light sensor with RGB LED diode 

This part of the code never exited with success.(I am using cmsis i2c driver): 

device_addr= 0x39, reg_addr = 0x13, rxSize is 1 byte.

   

static bool I2C_ReadAccelRegs(uint8_t device_addr, uint8_t reg_addr, uint8_t *rxBuff, uint32_t rxSize)
{
      /* direction=write : start+device_write;cmdbuff;xBuff; */
      /* direction=recive : start+device_write;cmdbuff;repeatStart+device_read;xBuff; */

     EXAMPLE_I2C_MASTER.MasterTransmit(device_addr, &reg_addr, 1, false);
     while ((!nakFlag) && (!completionFlag))
     {
     }
     nakFlag = false;
     completionFlag = false;
     EXAMPLE_I2C_MASTER.MasterReceive(device_addr, rxBuff, rxSize, false);

     /* wait for transfer completed. */
     while ((!nakFlag) && (!completionFlag))
     {
     }

     nakFlag = false;

     if (completionFlag == true)
     {
     completionFlag = false;
     return true;
     }
     else
     {
     return false;
     }
}

0 Kudos

1,684 Views
philip_drake
NXP Employee
NXP Employee

You could monitor the signals while doing the communication with the accelerometer. You can have more than one I2C slave on the same bus so long as the slave address is unique.

In the pin_mux.c and .h verify that the mux selection is correct to connect the I2C peripheral to the pins. if not you can use the pins config tool to change it.

I assume you mean  you removed R8, R9 and R10 on the freedom board.  You need to keep PTC6 R63 to connect to J2 pin 8 and PTC7 R67 to connect to J1 pin 16. 

I assume you are connection the two I2C signals, VDD and GND to the sensor. You could also connect the interrupt input of the device to eliminate the need to poll the device. Obviously, you will also need to add back in the required external pull up resistors for the two signals as shown in the  frdm board schematic.  The size depends on the speed of the communication you want.  10K values are used on the accelerometer.

 

Regards,

Philip

0 Kudos

1,684 Views
anoja
Contributor II

Hi Philip,

   I have R8,R9 and R10 removed. Added a pull up resistor 10K for both SDA and SCL.

in pin_mux.c I have the following for I2C init:

void I2C0_InitPins(void) {
CLOCK_EnableClock(kCLOCK_PortC); /* Port C Clock Gate Control: Clock enabled */

const port_pin_config_t portc6_pin51_config = {
kPORT_PullUp, /* Internal pull-up resistor is enabled */
kPORT_FastSlewRate, /* Fast slew rate is configured */
kPORT_PassiveFilterDisable, /* Passive filter is disabled */
kPORT_LowDriveStrength, /* Low drive strength is configured */
kPORT_MuxAlt7, /* Pin is configured as I2C0_SCL */
};
PORT_SetPinConfig(PORTC, PIN6_IDX, &portc6_pin51_config); /* PORTC6 (pin 51) is configured as I2C0_SCL */
const port_pin_config_t portc7_pin52_config = {
kPORT_PullUp, /* Internal pull-up resistor is enabled */
kPORT_FastSlewRate, /* Fast slew rate is configured */
kPORT_PassiveFilterDisable, /* Passive filter is disabled */
kPORT_LowDriveStrength, /* Low drive strength is configured */
kPORT_MuxAlt7, /* Pin is configured as I2C0_SDA */
};
PORT_SetPinConfig(PORTC, PIN7_IDX, &portc7_pin52_config); /* PORTC7 (pin 52) is configured as I2C0_SDA */
}

I used the same source code of accelerometer. But changed the address to my device address (0x29).

I cant find even the power on i2c signal on oscilloscope. The sensor I am trying to connect to is TCS3471 which is a light sensor.

uint8_t writedata[3];
writedata[0] = (WRITE | (device_addr << 1)); //Slave address: The address is 0x29 or 0b00101001 for I2c this is shifted left and 0 is added for write
writedata[1] = (reg_addr | CMD); // reg is the TAOS unit register address for the byte of interest, cmd is required by the chip and is always 80
writedata[2] = value;

I2C_LS.MasterTransmit(device_addr, writedata, 3, false);

0 Kudos

1,684 Views
anoja
Contributor II

Hi Philip,

   Changing the pull up to 1k5 solved the issue. this resistor value is for 400kbps speed. Thanks for the help!

Anoja

0 Kudos

1,684 Views
philip_drake
NXP Employee
NXP Employee

Excellent, I'm glad you got it working. 

Philip

1,684 Views
philip_drake
NXP Employee
NXP Employee

I tested the SDK_2.x_FRDM-KV11Z version 2.2.0 and ran the bubble level demo with no problems on my FRDM-KV11 board.  The bubble level demo does not use interrupts with the I2C interaction with the accelerometer on the board.

My suggestion is to verify that you have no pin or I2C slave address conflicts.  

I hope this helps.

Regards,

Philip

1,684 Views
anoja
Contributor II

I agree. I did try the on board accelerometer. But connecting to an external sensor is my issue. I removed the 0 ohm radiates across the pond so that there is no interference with the external sensor input. Using  PTC6 and PTC7 for SDA/ SCL. 

I confirmed that the is no pin conflicts. Slave address has been modified.

0 Kudos