Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs

Porting the library code of the sensor

Hello all,

The sensor (VCNL4035) is connected and found by the nrf52dk. Indeed, the twi_sensor/scanner have been quickly recoded and it shows some progress...

The manufacturer provides the so-called code library where demos and access to the registers is quick via the functions ... can be found on the manufacturers webpage...

Indeed, the i2c_functions are given, but of course not for the nRF52 dk , so , I am trying to workout the i2c/twi by myself, but got stuck...

In the manual of the library this is the order that should be followed with regard to the other uC:

  1. Create a new project in your selected IDE  (DONE)
  2. Configure the required pinouts, the I2C components as well as the USB CDC components (DONE) btw what is meant by the USB CDC components?
  3. Open the “C” folder / “C++” folder (DONE)
  4. Add the following files into the IDE:  (DONE)
    1.  I2C_Functions.c/cpp
    2. “Part Name”_Application _Library.c/cpp, “Part Name”_PS.c/cpp, “Part Name”_ALS.c/cpp, “Part Name”_RGB.c/cpp
    3. typedefinition.h
    4. I2C_Functions.h 
    5. “Part Name”_Application_Library.h, “Part Name”.h, “Part Name”_Prototypes.h
  5. Step 5: Add the I2C API code of your MCU into I2C_Functions.c/cpp or I2C_Functions.h within the #ifdef #endif identifier statement of your MCU. Please ensure that the restart condition is implemented correctly for the I2C read command
  6. Step 6: Activate the MCU-specific code in the files I2C_Functions.c/cpp, I2C_Functions.h, and “Part Name”_Application_Library.c/cpp by defining #define (write your MCU name) in typedefinition.h 

I got to the step 5, in addition, I have setup the twi_inti, twi_handlers... however, I am stuck at the step 5!

As you can see from the above it is required to add the TWI / I2C API ... here I am not sure what / how to use ... do you have any ideas ? Indeed, the step 6 will as well be of importance.

Any help , ideas how to handle this is very much appreciated!

Best.

Parents
  • Thank you for your input Jared, 

    You should pass the handler to the nrf_drv_twi_init() function as the third parameter.

    I guess this is how it should be done:

    // Initialize the TWI as Master device
    void twi_master_init(void) {
      ret_code_t err_code;
      // Configure the settings for twi communication
      const nrf_drv_twi_config_t twi_config = {
          .scl = TWI_SCL_M,                            // SCL Pin
          .sda = TWI_SDA_M,                            // SDA Pin
          .frequency = NRF_DRV_TWI_FREQ_100K,          // comm speed
          .interrupt_priority = APP_IRQ_PRIORITY_HIGH, // Interrupt priority, note if using bluetooth than select prioty carefully
          .clear_bus_init = false                      // Automatically clear bus
      };
    
      // A function to initialize the twi communication
      err_code = nrf_drv_twi_init(&m_twi, &twi_config, twi_handler, NULL);
      APP_ERROR_CHECK(err_code);
      // Enable the TWI communication
      nrf_drv_twi_enable(&m_twi);
    }

    One thing I am not sure here is whether the TWI init ( initialization ) is placed, used in the main.c or is this part of the nrf twi function that I am building for the sensor library, I believe it is part of the main.c, however...

    For example, I am using this code in the new twi function and got an error that err_code = nrf_drv_twi_tx (...) is found undeclared ?

    #include <I2C_Functions.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    
    #include "nrf_drv_twi.h"
    #include "nrf_twim.h"
    
    #include "VCNL4035X01.h"
    
    
    // nRF52DK
    #ifdef nRF52DK
    
    // please ensure that the restart conditition is implemented corretly for the I2C read command
    
    /* Indicates if operation on TWI has ended. */
    static volatile bool m_xfer_done = false;
    
    /* Buffer for samples read from the sensor. */
    static uint8_t m_sample;
    
    // TWI read/write
    int8_t twi_write(uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint16_t length) {
      uint32_t err_code;
    
      uint8_t buffer[255] = {0};
      buffer[0] = reg_addr;
      memcpy(&buffer[1], data, length);
      m_xfer_done = false;
      err_code = nrf_drv_twi_tx(&m_twi, dev_addr, buffer, sizeof(length), false);
      APP_ERROR_CHECK(err_code);
      while (m_xfer_done == false)
        ;
    
      return err_code;
    }
    
    /*
     * dev_addr -> Device address
     * reg_adddr -> Register address
     * data -> Buffer where data read from TWI will be stored
     * length -> Length of data to be read
     */
    int8_t twi_read(uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint16_t length) {
      uint32_t err_code;
      m_xfer_done = false;
      err_code = nrf_drv_twi_tx(&m_twi, dev_addr, &reg_addr, sizeof(reg_addr), true);
      APP_ERROR_CHECK(err_code);
      while (m_xfer_done == false)
        ;
    
      m_xfer_done = false;
      err_code = nrf_drv_twi_rx(&m_twi, dev_addr, data, length);
      APP_ERROR_CHECK(err_code);
      while (m_xfer_done == false)
        ;
    
      return err_code;
    }
    #endif

    Any ideas what I am missing in the include, or is something else ?

    Best.

Reply
  • Thank you for your input Jared, 

    You should pass the handler to the nrf_drv_twi_init() function as the third parameter.

    I guess this is how it should be done:

    // Initialize the TWI as Master device
    void twi_master_init(void) {
      ret_code_t err_code;
      // Configure the settings for twi communication
      const nrf_drv_twi_config_t twi_config = {
          .scl = TWI_SCL_M,                            // SCL Pin
          .sda = TWI_SDA_M,                            // SDA Pin
          .frequency = NRF_DRV_TWI_FREQ_100K,          // comm speed
          .interrupt_priority = APP_IRQ_PRIORITY_HIGH, // Interrupt priority, note if using bluetooth than select prioty carefully
          .clear_bus_init = false                      // Automatically clear bus
      };
    
      // A function to initialize the twi communication
      err_code = nrf_drv_twi_init(&m_twi, &twi_config, twi_handler, NULL);
      APP_ERROR_CHECK(err_code);
      // Enable the TWI communication
      nrf_drv_twi_enable(&m_twi);
    }

    One thing I am not sure here is whether the TWI init ( initialization ) is placed, used in the main.c or is this part of the nrf twi function that I am building for the sensor library, I believe it is part of the main.c, however...

    For example, I am using this code in the new twi function and got an error that err_code = nrf_drv_twi_tx (...) is found undeclared ?

    #include <I2C_Functions.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    
    #include "nrf_drv_twi.h"
    #include "nrf_twim.h"
    
    #include "VCNL4035X01.h"
    
    
    // nRF52DK
    #ifdef nRF52DK
    
    // please ensure that the restart conditition is implemented corretly for the I2C read command
    
    /* Indicates if operation on TWI has ended. */
    static volatile bool m_xfer_done = false;
    
    /* Buffer for samples read from the sensor. */
    static uint8_t m_sample;
    
    // TWI read/write
    int8_t twi_write(uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint16_t length) {
      uint32_t err_code;
    
      uint8_t buffer[255] = {0};
      buffer[0] = reg_addr;
      memcpy(&buffer[1], data, length);
      m_xfer_done = false;
      err_code = nrf_drv_twi_tx(&m_twi, dev_addr, buffer, sizeof(length), false);
      APP_ERROR_CHECK(err_code);
      while (m_xfer_done == false)
        ;
    
      return err_code;
    }
    
    /*
     * dev_addr -> Device address
     * reg_adddr -> Register address
     * data -> Buffer where data read from TWI will be stored
     * length -> Length of data to be read
     */
    int8_t twi_read(uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint16_t length) {
      uint32_t err_code;
      m_xfer_done = false;
      err_code = nrf_drv_twi_tx(&m_twi, dev_addr, &reg_addr, sizeof(reg_addr), true);
      APP_ERROR_CHECK(err_code);
      while (m_xfer_done == false)
        ;
    
      m_xfer_done = false;
      err_code = nrf_drv_twi_rx(&m_twi, dev_addr, data, length);
      APP_ERROR_CHECK(err_code);
      while (m_xfer_done == false)
        ;
    
      return err_code;
    }
    #endif

    Any ideas what I am missing in the include, or is something else ?

    Best.

Children
No Data
Related