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, ®, 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, ®, 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?