Re: Zephyr - read I2C sample, nrf52840

Lawrence King
 

Hi Edyta:

 

There are two ways to read sensors. If your sensor has a driver and you have set it up in the device tree correctly you can simply use the sensor examples.

 

If your sensor is really simple, you can cheat and just send i2c packets directly to the sensor from a user program. Here is an example of driving a really simple I/O expander:

 

/* Zephyr specific includes */

#include <zephyr.h>

#include <zephyr/types.h>

#include <device.h>

#include <drivers/i2c.h>

 

#define PCA9536_RED_LED_PIN             0

#define PCA9536_GREEN_LED_PIN           1

#define PCA9536_BLUE_LED_PIN            2

#define PCA9536_I2C_ADDR                (0x82 >> 1)

uint8_t pca9536_state;

 

/* The PCA9536 chip has four 8-bit registers

*      0 - input port register - read only

*      1 - output port register - read/write

*      2 - polarity inversion register - read/write

*              0=normal, 1=invert (only affects the input register)

*      3 - configuration register - read/write

*              0=output, 1=input

* each register has 4-bits corresponding to the 4 GPIOs on the chip

*

* writing a register is reasonably easy (one 3-byte transaction)

*      1) send the device address and the register read/write bit set to 0 (write)

*      2) send the command byte, the command byte contains the address of the register above that you want to write

*      3) write the the contents of the register. the upper 4 bits should always be 1.

*

* reading a register is a multi step process (two 2-byte transactions)

*      1) send the device address and the register read/write bit set to 0 (write)

*      2) send the command byte, the command byte contains the address of the register above that you want to read

*      this completes the first transaction

*      3) send the device address and the register read/write bit set to 1 (read)

*      4) read the response byte, which is the contents of the register. the upper 4 bits should always be 1.

*/

void    pca9536_init(void)

{

        int i;

        struct  i2c_msg msgs[2];

        u8_t data[2][2];

 

        /* printf("pca9536_init().\n"); */

        i2c_dev = device_get_binding("I2C_1");

        if (!i2c_dev) {

                printf("pca9536_init(): I2C Device driver not found.\n");

                return;

        }

        /* set the pins to outputs, and drive high */

        data[0][0] = 3;    /* config register */

        data[0][1] = 0xFF ^ ((1 << PCA9536_RED_LED_PIN) | (1 << PCA9536_GREEN_LED_PIN) | (1 << PCA9536_BLUE_LED_PIN));  /* 3 outputs */

        msgs[0].buf = data[0];

        msgs[0].len = 2U;

        msgs[0].flags = I2C_MSG_WRITE | I2C_MSG_STOP;

 

        data[1][0] = 1;              /* output register */

        data[1][1] = ((1 << PCA9536_RED_LED_PIN) | (1 << PCA9536_GREEN_LED_PIN) | (1 << PCA9536_BLUE_LED_PIN)); /* 3 highs */

        msgs[1].buf = data[1];

        msgs[1].len = 2U;

        msgs[1].flags = I2C_MSG_WRITE | I2C_MSG_STOP;

        i = i2c_transfer(i2c_dev, &msgs[0], 2, (u8_t)PCA9536_I2C_ADDR);

        if (i != 0) {

                printf("pca9536_init(): transfer failed.\n");

                return;

        }

}

 

void    pca9536_set(void)

{

        int i;

        struct  i2c_msg msgs[2];

        u8_t data[2][2];

 

        data[0][0] = 1; /* output register */

        data[0][1] = pca9536_state;

        msgs[0].buf = data[0];

        msgs[0].len = 2U;

        msgs[0].flags = I2C_MSG_WRITE | I2C_MSG_STOP;

        i = i2c_transfer(i2c_dev, &msgs[0], 1, (u8_t)PCA9536_I2C_ADDR);

        if (i != 0) {

                printf("pca9536_set(): transfer failed.\n");

                return;

        }

}

 

Lawrence King

Principal Developer

+1(416)627-7302

 

From: users@... <users@...> On Behalf Of Edyta Bosacka
Sent: Wednesday, July 22, 2020 8:44 AM
To: users@...
Subject: [Zephyr-users] Zephyr - read I2C sample, nrf52840

 

Is there any example how to read data from sensor by I2C?

Join users@lists.zephyrproject.org to automatically receive all group messages.