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

Failing to understand app_timer

Hello.

I cannot get my head around app_timers. After reading the docs, examples and answers on the devzone I'm still unable to get it running. The timeout callback isn't called after starting the timer. Any assistance, please?

app_timer_id_t timerFoo;

void foobarHandler(void *context)
{
	log("foobar\r\n");
}

int main()
{	
	uartInit();
	log("begin\r\n");
	
	APP_TIMER_INIT(0, 1, 4, NULL);
	if(app_timer_create(&timerFoo, APP_TIMER_MODE_SINGLE_SHOT, foobarHandler) != NRF_SUCCESS) log("creation failed\r\n");
	if(app_timer_start(timerFoo, APP_TIMER_TICKS(2000, 0), NULL) != NRF_SUCCESS) log("starting failed\r\n");
	
	return 0;
}
Parents Reply Children
  • How do I do that though? Could you please point me to some info on that? By digging around I found this "nrf_drv_clock_init()" function. Is this it? It returns NRF_ERROR_SOFTDEVICE_NOT_ENABLED "sd_softdevice_enable()" seems like the thing I have to use here but I don't know what parameters I'm supposed to give it.

  • You don't have a softdevice - so you need to just start an LF clock

    nRF51_SDK_8.0.0/examples/peripheral/bsp/main.c

    there's an example, there are lots.

    The main function exiting isn't an issue. Just because you fall off the end of main() doesn't mean the CPU stops running, CPUs don't just .. stop. Normally it ends up in an exit loop just branching constantly to the same instruction, which is equivalent to adding a while().

  • Thank you so much! That's really helpful. Now, what if I want delays shorter than a milliecond? Do I simply give APP_TIMER_TICKS a floating point value?

  • definitely not as it takes an unsigned integer. Not much uses floats (which is a good thing)

    There's really loads of example code on this plus lots of comments in the header files about how to set the divider, there are macros to get ticks for different milliseconds, microseconds etc etc. The documentation has a good intro to the whole library too.

    I really would look at the examples, although you aren't going to get much less than 1/10 millisecond with this library.

  • I wouldn't ask if I hadn't already looked at the examples. I couldn't find any that actually used app_timer.. . I couldn't find any that would use fractions of a millisecond for timing either. I guess I have to look through all of the examples available, eh?

    Either way, thank you for your assistance so far

Related