I2C example for nrf52 DK board

I have a nRF52 DK board and all is working fine. I can load the UART peripheral example and talk to my iPhone. I want to add I2C capability to connect to a temperature/Humidity sensor. I tried to run the i2c_api application and I get an error when it tries to build. It tells me to set the correct I2C device. How do I set the correct I2C device and how to I see the SCL and SDA I2C control pins on the external connector. Should they be on P0.26 and P0.27 pins. I was trying to get the i2c_api example working so I could understand how to use the I2c functions. I am very familiar with the I2C protocol. If you can tell me how to select the I2C device, initialize it, and do reads and writes so that SDA/SCL is active on the external connector pins I would be very appreciative. Thanks for you help. I am sure it is operator and error on my part. I am new to the nRF52 DK board.

#if DT_NODE_HAS_STATUS(DT_ALIAS(i2c_0), okay)
#define I2C_DEV_NODE    DT_ALIAS(i2c_0)
#elif DT_NODE_HAS_STATUS(DT_ALIAS(i2c_1), okay)
#define I2C_DEV_NODE    DT_ALIAS(i2c_1)
#elif DT_NODE_HAS_STATUS(DT_ALIAS(i2c_2), okay)
#define I2C_DEV_NODE    DT_ALIAS(i2c_2)
#else
#error "Please set the correct I2C device"
#endif
Parents
  • Hi Timothy! 

    If you are new to our environment I would recommending having a look at DevAcademy to get an overview over NCS. In the NCS fundamentals course we have a lesson covering I2C which you can find here

    Regards

    Runar

  • Runar

    I am not new to NCS. I have been using V1.7.0 with the nRF9160 for several years. I have I2C working fine in all of my examples. 

    I am new to nrf52 and V2.5.0. I am using the i2c_api example. I have not changes anything. it has CONFIG_I2C=y set so I would expect that I2C would work as is. It says I have not set the correct device. what am I doing wrong? How do I set the correct device?

  • Hello Timothy,

    This is a blog post from one of our partners, Golioth, on using generic SPI devices. The process is similar for I2C devices.

    https://blog.golioth.io/how-to-use-generic-spi-devices-with-zephyr/

    The important thing is that each device on the I2C (or SPI as in the example above) bus has its own device tree node. You can define a "generic" node that can then be recognized by the build system and accessed by your application. This node will include parameters like its slave address and maximum frequency.

    I would encourage you to look at a device tree snippet example of an I2C device, like this sensor:

    https://github.com/zephyrproject-rtos/zephyr/blob/a2ad2d71f64b785a10e43f4feb81bf5f45503577/boards/arm/thingy53_nrf5340/thingy53_nrf5340_common.dtsi#L186

    After everything has been defined in the devicetree, you can then get the node using "I2C_DT_SPEC_GET(DT_NODELABEL(<your_nodelabel>))" and use i2c_<operation>_dt function calls with that node.

    https://docs.zephyrproject.org/latest/doxygen/html/group__i2c__interface.html

    I am unable to find the i2c_api example--could you share a link so I could take a look?

    Additionally, the "DT_ALIAS" macros are looking for a node in this style within your devicetree:

    Fullscreen
    1
    2
    3
    4
    5
    / {
    aliases {
    i2c_0 = &i2c0;
    };
    };
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Let me know if this helps.

    Thanks,

    Helmut Lord

  • I think Tim is referring to the Zephyr SDK example zephyr/tests/drivers/i2c/i2c_api. Just my guess. My own question: is nRF Connect for VS Code the recommended way to build and run Zephyr SDK examples?

    Burt

  • I get the following  error if I set up the alias as you suggested. I created an overlay  that is exactly the lines above you said were needed. The error indicates that '_' is not allowed. what am I missing or doing wrong.

    devicetree error: /aliases: alias property name 'i2c_0' should include only characters from [0-9a-z-]

Reply
  • I get the following  error if I set up the alias as you suggested. I created an overlay  that is exactly the lines above you said were needed. The error indicates that '_' is not allowed. what am I missing or doing wrong.

    devicetree error: /aliases: alias property name 'i2c_0' should include only characters from [0-9a-z-]

Children