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
  • Hello Jered 

    I have just realized that I need to write the code from scratch :) so here is the start...

    The problem I am having is now to discern between the master and slave, is there a particular #include for the master i2c or it is part of the regular #include? For example, the code below stops exactly at the ' m_twi ', see below:

    // nRF52DK
    #ifdef nRF52DK
      #include "VCNL4035X01.h"
      #include "nrf_drv_twi.h"
      #include <hal/nrf_twim.h>
    // please ensure that the restart conditition is implemented corretly for the I2C read command, what does that mean...
    int WriteI2C_Bus(struct TransferData *Data) {
      // A flag to indicate the transfer state
      static volatile bool m_xfer_done_1 = false;
      // initialization of initial error = 0
      int Error = 0;
      // Define an array of 3 Bytes to be sent as I2C Write Command as shown in Fig. 10 in the Datasheet Page 7
      uint8_t WData[3] = {Data->RegisterAddress, Data->WData[0], Data->WData[1]};
      // Send I2C Write Command as shown in Fig. 10 in the Datasheet Page 7 with Error checking.
      //Error = ...
    
      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_1 = false;
        err_code = nrf_drv_twi_tx(&m_twi, dev_addr, buffer, sizeof(length), false);
        APP_ERROR_CHECK(err_code);
        while (m_xfer_done_1 == false);
    
        return err_code;
      }
    }
    
    //the read function...
    
    #endif

    Any help is very much appreciated.

    PS indeed, I am following the example of the STM32.

  • I have sorted the above problem... the problem was in the actual declaration of variables. drivers... For example, once the drivers, variables were added to the #ifdef ... #endif clause, then it compiles ok. 

    However, I am not happy with that answer , would somebody please explain why is this not possible to do in the main.c ? For example, at early beginning, just after the include, etc. What is the way to sort this out globally?

    I guess the basic introduction of how the whole program is organized would not hurt to read :)

    Any ideas where to get that info?

    Best.

Reply
  • I have sorted the above problem... the problem was in the actual declaration of variables. drivers... For example, once the drivers, variables were added to the #ifdef ... #endif clause, then it compiles ok. 

    However, I am not happy with that answer , would somebody please explain why is this not possible to do in the main.c ? For example, at early beginning, just after the include, etc. What is the way to sort this out globally?

    I guess the basic introduction of how the whole program is organized would not hurt to read :)

    Any ideas where to get that info?

    Best.

Children
No Data
Related