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?

  • This catches me out sometimes. Try "i2c-0" instead. Devicetree -> macro translation does weird things with underscores and dashes. 

  • nRF Connect in VS Code is a fine way to build Zephyr examples. You could also use west, Zephyr's build utility, since this is basically what the extension does under the hood. nRF Connect is a superset of Zephyr features so there's no issue building anything in Zephyr w/ nRF Connect. 

  • Thanks, Helmut. Yeah, I like building with west--I often use one of my Windows laptops and building under nRF Connect for VS Code seems to take a bigger performance hit than if I use a Mac or Linux computer.

    Burt

  • when I replace "i2c_0" with "i2c-0" I can build and flash but things still fail. I am running the V2.5.0/zephyr/test/drivers/i2c_api example. when it looks for 

    #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
    it finds i2c_0. I do not know why it now finds the underscore. when I go to run it I get the error shown below. I added test messages. so it says it initialized the I2C peripheral, wrote a config setting but failed when it went to read the config back. any idea what is going on now? I am very comfortable with the I2C protocol. I have been writing I2C drivers for all kinds of devices for a very long time. If I can get SCL and SDA to external pins and have them execute the I2C protocol I will be able to connect my devices to the I2C bus. Is there a simpler I2C example like blinky that just sends reads or writes to the I2C bus?
    *** Booting nRF Connect SDK v2.5.0 ***
    Running TESTSUITE i2c_gy271
    ===================================================================
    START - test_i2c_burst_gy271
    I2C device is ready
    I2C config passed
    I2C get_config failed
  • any idea how I can get I2C interface to work? I cannot proceed until the I2C interface is working.

Reply Children
  • Hi,

    Can you provide your devicetree overlay? Just in general the troubleshooting devicetree section here is a good read. It also covers your question regarding underscore 

    "

    • In C/C++, devicetree names must be lowercased and special characters must be converted to underscores. Zephyr’s generated devicetree header has DTS names converted in this way into the C tokens used by the preprocessor-based <devicetree.h> API.

    • In overlays, use devicetree node and property names the same way they would appear in any DTS file. Zephyr overlays are just DTS fragments.

    A former support member made this example a while back, an simple I2C scanner, I would have a look at it just to compare the overlays. 

    Regards

    Runar

  • if I compile i2c_api application with overlay as follows

    / {
    aliases {
    i2c-0 = &i2c0;
    };
    };

    I can build and run but I get the following errors. I2C does not work. This is the provided standard test in V2.5.0/zephyr/tests/devices. can someone tell me what I have to do to get it running on the nrf52 DK board. the errors are the following:

    *** Booting nRF Connect SDK v2.5.0 ***
    Running TESTSUITE i2c_gy271
    ===================================================================
    START - test_i2c_burst_gy271
    I2C get_config failed

    Assertion failed at ../src/test_i2c.c:159: i2c_gy271_test_i2c_burst_gy271: test_burst_gy271() == TC_PASS is false

    FAIL - test_i2c_burst_gy271 in 0.013 seconds
    ===================================================================
    START - test_i2c_gy271
    I2C get_config failed

    Assertion failed at ../src/test_i2c.c:154: i2c_gy271_test_i2c_gy271: test_gy271() == TC_PASS is false

    FAIL - test_i2c_gy271 in 0.012 seconds
    ===================================================================
    TESTSUITE i2c_gy271 failed.

    ------ TESTSUITE SUMMARY START ------

    SUITE FAIL - 0.00% [i2c_gy271]: pass = 0, fail = 2, skip = 0, total = 2 duration = 0.025 seconds
    - FAIL - [i2c_gy271.test_i2c_burst_gy271] duration = 0.013 seconds
    - FAIL - [i2c_gy271.test_i2c_gy271] duration = 0.012 seconds

    ------ TESTSUITE SUMMARY END ------

    ===================================================================
    PROJECT EXECUTION FAILED

  • Hi Tim,

    Do you have pullup resistors on the device you have connected to the bus? The hardware manual for the DK board shows that pullup resistors on the DK board are only switched into the circuit when an Arduino shield is plugged in: When a shield is connected, there are two analog switches connecting the pull-up resistors to the I2C bus lines (SDA and SCL). This function is using one ground pin on the Arduino shield to control the switch.

    I don't think you have explained enough details above. You are not testing with a gy271 device; correct? You told me offline something about testing with no device on the bus, but I am afraid of misquoting you. I originally thought you were modifying the code for a different device, but now I'm not sure and I assume that DevZone is in the same boat I am in. Spell out the details is my suggestion. And see if I am correct about my concern about pullup resistors not being switched in.

    Burt

  • I'm not familiar with Ztest, so I will need to look into it. However where have you defined gy271 if that is your overlay, nor can I see that that the overlay is enabled. 

    Regards

    Runar

  • Runar: there are no overlays by default in the I2c_api test directory. I created and overlay file and added the i2c-0 alias as you suggested above.

Related