Enabling TWIM I2C on nRF52810 DK

I am currently working on enabling and transmitting/receiving data using TWIM on my nRF52810 development kit. The documentation states that in order to enable TWIM I need to provide a pointer to a structure containing the registers of the peripheral as seen below:

Unfortunately I am unsure how I can initialize this structure. Is there a function that returns an initialized NRF_TWIM_Type or do I need to manually create the structure myself. Thank you in advance and apologies if this is a dumb question

  • Hope this will help you. Mine code for nRF52810:

    #include "nrf_drv_twi.h"
    #include "nrfx_twim.h"
    
    /**
     *Pins
     */
    #define SCL_PIN 14
    #define SDA_PIN 12
    
    /**
     *Create TWI instance.
     */
    #define TWI_INSTANCE_ID 0
    
    static const nrfx_twim_t m_twi = NRFX_TWIM_INSTANCE(TWI_INSTANCE_ID);
    
    /**
     * @brief  Function for i2c bus init.
     * @param  none
     * @return none
     */
    void twi_init()
    {
      ret_code_t err_code;
    
      const nrfx_twim_config_t twi_config = {
        .scl = SCL_PIN,
        .sda = SDA_PIN,
        .frequency = NRF_TWIM_FREQ_400K,
        .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
        .hold_bus_uninit = false
      };
    
      err_code = nrfx_twim_init(&m_twi, &twi_config, NULL, NULL); // initialize I2C
      APP_ERROR_CHECK(err_code);
    
      nrfx_twim_enable(&m_twi); // enable I2C
    }
    
    /**
     * @brief  Function to write one byte into XXXXX register at specified register address
     * @param  16 bit register address + 1 byte of data to be written
     * @return true if transmission was successful, otherwise false
     */
    bool XXXXX_register_write_byte(uint8_t register, uint8_t value)
    {
      ret_code_t err_code;
    
      uint8_t tx_buffer[2]; // buffer to hold registry address + data
    
      tx_buffer[0] = register; // register address byte
      tx_buffer[1] = value;    // value to write
    
      while (nrfx_twim_is_busy(&m_twi)) { }
      err_code = nrfx_twim_tx(&m_twi, your_dev_addr, tx_buffer, sizeof(tx_buffer), false);
    
      if (NRF_SUCCESS != err_code) {
        return false;
      }
      return true;
    }

    and sdk_config:

  • Thank you very much for this example! A couple beginner questions, when you refer to sdk_config do you mean prj.conf or is this a different configuration file that I need to find? In addition how might I use TWIM to transmit data. It seems that in your example you are sending data. Also I can't seem to import nrf_drv_twi is that part of the old sdk or is it a user created file?

  • sdk_config.h is the configuration file for the nRF SDK. If you open example-projects inside SDK you will see it

    (for ex.: nRF5_SDK_17.1.0\examples\peripheral\blinky\pca10040e\blank\ses)

    nrf_drv_twi maybe you don't need this. Try first with nrfx_twim_XXXX

  • Hi,

    NRF_TWIM_Type is defined in chip header file in the MDK (e.g nrf52810.h for nRF52810 chip).

    I'm not sure exactly which API you are referring to in your initial post, but it sounds like you are using nRF Connect SDK from your comment about prj.conf. 

    Zephyr provides an I2C driver API, which you can use if you do not want to write your own TWIM driver. This uses nrfx_twi(m) internally, and should use TWIM/EasyDMA if you set CONFIG_NRFX_TWIMx in your prj.conf file.

    Best regards,
    Jørgen

  • Ah I was using the nRF Connect SDK, so unfortunately this example won't apply. Thank you very much for the help!

Related