Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Running TWI from within app_timer callback.

I am trying to make TWI calls from an APP_TIMER event.  However the TWI calls are failing.  I can see that the twi message is sent on an oscilloscope, but the function never returns (handler function doesnt get called), and it holds the clock low after the last message.

I have tried increasing the TWI priority to highest (0) but no change.  I am not running a soft device or any other timers.

Is there something prohibiting use of TWI functions from a timer callback function?  Am I doing anything wrong, or is there a better way to go about this?

My main function is as follows, and does not work (twi as shown in image above):

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
int main(void)
{
lfclk_config();
utils_setup();
twi_init_all();
twi_read_init();
while (true)
{
nrf_pwr_mgmt_run();
NRF_LOG_FLUSH();
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

If I modify it to call the twi_timer_handler function in the main loop, then it does work as expected.

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
int main(void)
{
lfclk_config();
utils_setup();
twi_init_all();
while (true)
{
nrf_pwr_mgmt_run();
NRF_LOG_FLUSH();
nrf_delay_ms(100);
twi_timer_handler(NULL);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Here is full code listing:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include "nrf.h"
#include "bsp.h"
#include "hardfault.h"
#include "app_error.h"
#include "app_timer.h"
#include "nrf_pwr_mgmt.h"
#include "nrf_drv_clock.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "nrf_drv_twi.h"
#include "nrf_delay.h"
APP_TIMER_DEF(twi_timer);
static void utils_setup(void)
{
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Many thanks!