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.

  • Hi,

    First, I would like to point out that we never recommend starting from a totally new project. Instead, we recommend that you start with one of our example and then build your project from that. For instance, since you're using the TWI peripheral I would recommend starting out with the TWI scanner example. The example has everything included that is needed for using the TWI peripheral. 

    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.

    This is shown in the example.

    regards

    Jared 

  • Hello Jared,

    thank you for your input it really helps, indeed, I had a look at the "scanner example" before, and I could read the address of the sensor correctly, in addition, I had a look at the "sensor example", which I have adopted by adding the library and the following lines to the I2C_Functions.h , for example in this file are also added the examples of other uCs.

    In this file below I have added the "nrf_drv_twi.h" , I guess this is what is used in the "scanner example" to get going? 

    See more below:

    #pragma once
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "typedefinition.h"
    #include <stdint.h>
    
    //Include I2C Library for nRF52
    #ifdef nRF52DK
    	#include "nrf_drv_twi.h" // ok?
    #endif
    
    //Include I2C Library for PSoC Cypress MCU
    #ifdef Cypress
    	#include "project.h"
    #endif
    
    //Include I2C Library for STM32F
    #ifdef STM32F
    	#include "stm32f4xx_hal.h"
    #endif
    
    //Include I2C Library for Arduino supported MCU
    #ifdef Arduino
    	//Arduino platforms use Wire.h library while Teensy platforms use i2c_t3.h library.
    	#ifdef i2ct3
    		#include <i2c_t3.h>
    	#endif
    
    	#ifdef wirelib
    		#include <Arduino.h>
    		#include <Wire.h>
    	#endif
    #endif
    
    
    //Struct TransferData Member Definition
    struct TransferData
    {
    	uint8_t RegisterAddress;
    	uint8_t WData[2];
    	uint8_t RData[2];
    	uint8_t length;
    	uint8_t Slave_Address;
    	uint8_t Select_I2C_Bus;
    }; //Struct variables will be declared separately in Sensor API and I2C_Functions.cpp/c
    
    //Struct TransferData Member Definition
    struct GestureTransferData
    {
    	uint8_t RegisterAddress;
    	uint8_t WData[2];
    	uint8_t RData[6];
    	uint8_t length;
    	uint8_t Slave_Address;
    	uint8_t Select_I2C_Bus;
    }; //Struct variables will be declared separately in Sensor API and I2C_Functions.cpp/c
    
    //Function Prototypes For I2C_Functions.cpp/c
    int ReadI2C_Bus(struct TransferData *Data);
    int WriteI2C_Bus(struct TransferData *Data);
    int ReadI2C_Bus_Gesture_Mode(struct GestureTransferData *Data);

    I guess this is needed , however, I cannot compile this:

    int main(void)
    {
        //the sensor triggering should be enabled!
        VCNL4035X01_SET_PS_TRIG(VCNL4035X01_PS_TRIG_EN);
         
        counting = 0;
        
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    
        NRF_LOG_INFO("\r\n TWI sensor example started. ++++++++++++++++++ RESETING +++++++++++++++ ");
        NRF_LOG_INFO("\r\n counting = 0 ");
        NRF_LOG_FLUSH();
    
        twi_init();
    
        VCNL4035_set_mode();
    
        while (true) {
          nrf_delay_ms(500);
          counting = counting + 1;
          NRF_LOG_INFO("\r\n counting =  %d", counting);
          NRF_LOG_INFO("\r\n Waiting 500ms");
          if (counting > 9) {
            counting = 0;
            NRF_LOG_INFO("\r\n counting = 0   *************** ");
          }
          do {
            __WFE();
          } while (m_xfer_done == false);
    
          
          read_sensor_data();
    
          NRF_LOG_FLUSH();
        }
    }
    
    /** @} */

    Compile error:

    Without call the first line in the int main(void) ... the project compiles ok!

    Any comments are very much appreciated.

    Best.

  • Hi.

    Undefined reference means that the compiler isn't able to find the function declaration which usually implies that the correct source file and path isn't included. See this video on how to correctly include source files to a project.

    regards

    Jared 

  • Thanks, apparently the first problem has been resolved, however, it seems I have to dig in and write, port the existing examples of the i2c with the nrf sdk, have a look at the pic. Hope this is the right path?

    For example, the i2c examples for other parts are available, however, I would like to use the nrf as master part ... with the sensor at hand as slave. 

    Hope this is the right path?

    Best. 

  • Hi,

    Other parts as in other Nordic MCUs or other slave devices? But yes, in essence you will have to modify the example so that it fits your project. There isn't any function called "WriteI2C_BUS" in the nRF5 SDK, instead you would have to call the function in the TWI driver which writes to the bus namely nrf_drv_twi_tx().

    regards

    Jared 

Related