Using the nrfx_twim.h to develop a simple I2C communicate code

Hi,

I'm working on a project and I need to develop a code to communicate nRF52840 with a GPS module (Ublox MAX-M10S - address 0x42) over I2C. I don't finded anyone example to do this, so I'm trying to develop a I2C librarie (similar the WIRE.H, used for ESP32). However, Wire.h uses the esp32-hal-i2c library for I2C function calls. I need a library compatible with this one for my I2C calls, nrfx_twim.h seems to be good for that.

Could someone help me with an example code base to test I2C communication using the nrfx_twim.h library?
I'm using the nRF Connect SDK (version 3.2.0) and programming through Visual Studio Code.

Parents Reply
  • Hi, thanks for the example!
    I was a little in doubt about the operation and what the TWIM and TWIS libraries are for, that is, what differentiates between them besides the nomenclature. In the example I saw (hal_nordic/nrfx/samples/src/nrfx_twim_twis/txrx/main.c), information is read from a drone. Why is TWIM and TWIS used? Wouldn't it be possible to use only TWIM to read information from the drone? That is, make the TWI communication with the drone using only the nrfx_twim.h library?

    I ask these questions because for my project, I need to communicate with the module using the nrfx libraries, so I would like to know if I will need to use only nrfx_twim.h or will I need to use both, as in the example.

    Another question, I am using nRF Connect SDK, in this example that I need to use the TWIM driver, do I need to define the pins used in the .overlay file? As in the examples that use I2C communication using the zephyr/drivers/i2c library, in which I define the SDA and SCL pins in the .overlay file.
    What parameters do I have to define in the prj.conf file?

Children
  • Hi,

     

    Agnaldo Pagano said:
    I was a little in doubt about the operation and what the TWIM and TWIS libraries are for, that is, what differentiates between them besides the nomenclature. In the example I saw (hal_nordic/nrfx/samples/src/nrfx_twim_twis/txrx/main.c), information is read from a drone. Why is TWIM and TWIS used? Wouldn't it be possible to use only TWIM to read information from the drone? That is, make the TWI communication with the drone using only the nrfx_twim.h library?

    It shows both a master (TWIM) and a slave (TWIS) implementation, so that you can connect both and see that they communicate over each respective bus. You can read more about this in the respective readme.md file.

    Agnaldo Pagano said:
    I ask these questions because for my project, I need to communicate with the module using the nrfx libraries, so I would like to know if I will need to use only nrfx_twim.h or will I need to use both, as in the example.

    Do you need to communicate with a i2c slave? If yes, then you need to implement a i2c master (TWIM).

     

    Agnaldo Pagano said:
    Another question, I am using nRF Connect SDK, in this example that I need to use the TWIM driver, do I need to define the pins used in the .overlay file? As in the examples that use I2C communication using the zephyr/drivers/i2c library, in which I define the SDA and SCL pins in the .overlay file.
    What parameters do I have to define in the prj.conf file?

    That is completely up to you, if you want to use "nrfx" directly or the i2c driver from zephyr.

    Here's a guide from our nordic academy on this exact setup:

    https://academy.nordicsemi.com/lessons/lesson-6-serial-com-i2c/

    I would recommend that you have a look at that as a reference.

     

    Please also note that your targeted external device seems to also support using UART.

     

    Kind regards,

    Håkon

  • Do you need to communicate with a i2c slave? If yes, then you need to implement a i2c master (TWIM).

    Yes, I need to communicate with a GPS module, to read geo data.

    That is completely up to you, if you want to use "nrfx" directly or the i2c driver from zephyr.

    Here's a guide from our nordic academy on this exact setup:

    https://academy.nordicsemi.com/lessons/lesson-6-serial-com-i2c/

    I would recommend that you have a look at that as a reference.

    Thanks, but my idea is to develop a librarie, similar the Wire.h, used for ESP32, for example. Therefore, I need to use nrfx_twim.h to execute the calls, like nrfx_twim_init(), and the functions to write and read information. 

    My question would be if I need to define the pins I'm going to use (SDA and SCL) in the device tree, the same way I do using the 'drivers/i2c.h' library, as in the example. Or if I need to set in another location.
    In my code, I need to define these SDA and SCL pins as GPIO, right?

    Could you explain to me the difference between these two libraries? ('nrfx_twim.h' and 'drivers/i2c.h')

    Please also note that your targeted external device seems to also support using UART.

    Yes, but for my application I need it to be I2C communication.

  • Hi,

    Agnaldo Pagano said:

    Thanks, but my idea is to develop a librarie, similar the Wire.h, used for ESP32, for example. Therefore, I need to use nrfx_twim.h to execute the calls, like nrfx_twim_init(), and the functions to write and read information. 

    My question would be if I need to define the pins I'm going to use (SDA and SCL) in the device tree, the same way I do using the 'drivers/i2c.h' library, as in the example. Or if I need to set in another location.

    You need to port that specific library to match the driver you choose.

    with nrfx, you do not need to define pins directly in DT, as the SDA/SCL are provided as input for the init-function (type "nrfx_twim_config_t").

    Agnaldo Pagano said:
    Could you explain to me the difference between these two libraries? ('nrfx_twim.h' and 'drivers/i2c.h')

    nrfx is the driver HAL, while i2c is the generic zephyr driver, which also uses nrfx under-the-hood for nRF devices.

    By using the zephyr i2c driver, you can also add any sensor that zephyr rtos already has added support for.

     

    Kind regards,

    Håkon

Related