This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to use SDK in your custom library in SES?

I have the following code in my main.c file:

/* Indicates if operation on TWI has ended. */
static volatile bool m_xfer_done = false;

/* TWI instance. */
static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);

void my_twi_function() {

  err_code = nrf_drv_twi_tx(&m_twi, mysensor_ADDR, &reg, 1, false);
  APP_ERROR_CHECK(err_code);

  nrf_delay_ms(100);

  err_code = nrf_drv_twi_rx(&m_twi, mysensor_ADDR, msample2, sizeof(msample2));
  APP_ERROR_CHECK(err_code);

  printf("sample = %i %i\n",msample2[0],msample2[1]);
}

I also have mylib.c and mylib.h file. I want to move my function there. I tried the following.

mylib.c

#include "mylib.h"

extern static volatile bool m_xfer_done;
extern static const nrf_drv_twi_t m_twi;

extern ret_code_t nrf_drv_twi_tx(nrf_drv_twi_t const * p_instance,
                          uint8_t               address,
                          uint8_t const *       p_data,
                          uint8_t               length,
                          bool                  no_stop);
                          
extern ret_code_t nrf_drv_twi_rx(nrf_drv_twi_t const * p_instance,
                          uint8_t               address,
                          uint8_t *             p_data,
                          uint8_t               length);

void my_twi_function() {

  err_code = nrf_drv_twi_tx(&m_twi, mysensor_ADDR, &reg, 1, false);
  APP_ERROR_CHECK(err_code);

  nrf_delay_ms(100);

  err_code = nrf_drv_twi_rx(&m_twi, mysensor_ADDR, msample2, sizeof(msample2));
  APP_ERROR_CHECK(err_code);

  printf("sample = %i %i\n",msample2[0],msample2[1]);
}

mylib.h

#include "nrf_drv_twi.h"
#include "nrf_delay.h"

#define mysensor_ADDR      0x21

void my_twi_function();

I also tried to pass m_xfer_done and m_twi as a pointer, but that didn't work either.

Can you give me an example how it should be done?

Parents
  • "static" in this file context means "I am invisible to all other source files". Simply remove "static" in both files:

    In main.c change:
    /* Indicates if operation on TWI has ended. */
    static volatile bool m_xfer_done = false;
    /* TWI instance. */
    static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);
     to:
     /* Indicates if operation on TWI has ended. */
    volatile bool m_xfer_done = false;
    /* TWI instance. */
    const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);
    
    In mylib.c change:
    extern static volatile bool m_xfer_done;
    extern static const nrf_drv_twi_t m_twi;
    to:
    extern volatile bool m_xfer_done;
    extern const nrf_drv_twi_t m_twi;

Reply
  • "static" in this file context means "I am invisible to all other source files". Simply remove "static" in both files:

    In main.c change:
    /* Indicates if operation on TWI has ended. */
    static volatile bool m_xfer_done = false;
    /* TWI instance. */
    static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);
     to:
     /* Indicates if operation on TWI has ended. */
    volatile bool m_xfer_done = false;
    /* TWI instance. */
    const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);
    
    In mylib.c change:
    extern static volatile bool m_xfer_done;
    extern static const nrf_drv_twi_t m_twi;
    to:
    extern volatile bool m_xfer_done;
    extern const nrf_drv_twi_t m_twi;

Children
Related