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
  • 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.

Related