0

Using app timer in sdk13

Tostikoning gravatar image

asked 2017-04-21 09:58:42 +0200

Hi all,

I am using a nRF52832 with SD S132 V4.0.2 ion SDK13.

My application currently uses TWI, SPI and BLE and also 1 wire (which is based on delays). Everything works fine. The 1 wire is used before the SD is enabled and after it is done it will not be used anymore, so it wouldn't interfere with the SD.

On my custom board I have an ADC conected through SPI. I want to take a sample every 100ms based on a app timer timeout. However the timeout interrupt is not fired.

I use the following code in main start:

NRF_LOG_INIT(NULL); // Initialize the logging module

err_code = app_timer_init();
APP_ERROR_CHECK(err_code);


/* Initialize peripherals */
adc_init();
twi_init();
press_init();
batt_init();
ds2431_getprofile();
measurement_init();
NRF_LOG_FLUSH();

/* Initialize bluetooth */
ble_stack_init();
peer_manager_init(erase_bonds);
gap_params_init();
gatt_init();
advertising_init();
ble_services_init();
conn_params_init();

/* Start advertising on start-up */
err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
APP_ERROR_CHECK(err_code); 

measurement_start();

The timer is started in measurement_start and created in measurement_create:

APP_TIMER_DEF(m_pt100_timer_id);

void measurement_init(void)
{
    uint32_t time_base;
    ret_code_t err_code;

    /* Define a timer id used for 10Hz sample rate */
    err_code = app_timer_create(&m_pt100_timer_id, APP_TIMER_MODE_REPEATED, measurement_handler);
    APP_ERROR_CHECK(err_code);
}

void measurement_start(void)
{
    /* Start timer */
    app_timer_start(&m_pt100_timer_id, MEAS_TIMER_INTERVAL, NULL);

    /* Clear previous measurement in flash */

}

void measurement_handler(void * p_context) // this code is not fired
{
    float   temperature = 0, pressure = 0;

    // do measurement, code currently not included
}

I have the following questions:

  1. Does app_timer module enable RTC1 or do I need to enable it?
  2. Do I need to initialize the LF clk?
  3. Should I first enable the softdevice and then the timer, or the other way around?
  4. Do you guys know why my timeout is not fired?
edit retag flag offensive close delete report spam

2 answers

Sort by ยป oldest newest most voted
0
Tostikoning gravatar image

answered 2017-04-21 12:19:03 +0200

updated 2017-04-21 12:19:34 +0200

With help from @Sigurd I managed to find the problem. It was a problem all programmers will experience in their careers and one of my finest "d'oh" moments.

You should create the timer with the following function:

ret_code_t app_timer_create(app_timer_id_t const *      p_timer_id,
                            app_timer_mode_t            mode,
                            app_timer_timeout_handler_t timeout_handler)

And start the timer with the following function:

ret_code_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context)

Notice the difference in the first argument. The init requires an address of your timer id, and when you start a timer it only needs a copy. So when I changed my call from:

err_code = app_timer_start(&m_pt100_timer_id, APP_TIMER_TICKS(1000), NULL);

to

err_code = app_timer_start(m_pt100_timer_id, APP_TIMER_TICKS(1000), NULL);

It worked like a charm! Oh and btw, do not try to debug after the softdevice was enabled, it can get quirky.

edit flag offensive delete publish link more
0
sigurdon gravatar image

answered 2017-04-21 11:13:30 +0200

Hi,

Q1: Does app_timer module enable RTC1 or do I need to enable it?

A1: The app_timer module enable RTC1 in the function app_timer_init()


Q2:Do I need to initialize the LF clk?

A2:If you are using the SoftDevice, the LFCLCK gets initialized and enabled when you enable the SoftDevice.


Q3:Should I first enable the softdevice and then the timer, or the other way around?

A3: You can do both, but you won't get any timeout before the LFCLK is started.


Q4:Do you guys know why my timeout is not fired?

A4: Try to change app_timer_start(&m_pt100_timer_id, MEAS_TIMER_INTERVAL, NULL); with app_timer_start(m_pt100_timer_id, APP_TIMER_TICKS(100), NULL);

edit flag offensive delete publish link more

Comments

Hi Sigurd,

I tried the change you suggested in A4. Unfortunately it did not work. The following snippet is from the sdk_config.h:

    // <e> APP_TIMER_ENABLED - app_timer - Application timer functionality
//==========================================================
#ifndef APP_TIMER_ENABLED
#define APP_TIMER_ENABLED 1
#endif
#if  APP_TIMER_ENABLED
// <o> APP_TIMER_CONFIG_RTC_FREQUENCY  - Configure RTC prescaler.

// <0=> 32768 Hz
// <1=> 16384 Hz
// <3=> 8192 Hz
// <7=> 4096 Hz
// <15=> 2048 Hz
// <31=> 1024 Hz

#ifndef APP_TIMER_CONFIG_RTC_FREQUENCY
#define APP_TIMER_CONFIG_RTC_FREQUENCY 0
#endif

// <o> APP_TIMER_CONFIG_IRQ_PRIORITY  - Interrupt priority


// <i> Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice
// <0=> 0 (highest)
// <1=> 1
// <2=> 2
// <3=> 3
// <4=> 4
// <5=> 5
// <6=> 6
// <7=> 7

#ifndef APP_TIMER_CONFIG_IRQ_PRIORITY
#define APP_TIMER_CONFIG_IRQ_PRIORITY 7
#endif

// <o> APP_TIMER_CONFIG_OP_QUEUE_SIZE - Capacity of timer requests queue.
// <i> Size of the queue depends on how many timers are used
// <i> in the system, how often timers are started and overall
// <i> system latency. If queue size is too small app_timer calls
// <i ...
(more)
Tostikoning ( 2017-04-21 11:20:10 +0200 )editconvert to answer

The rest of the application is working fine? The only problem is that the timeout is not fired?

Have you tried debugging the application? Are you running into the error handler somewhere? See this post on how to debug.

Sigurd ( 2017-04-21 11:25:53 +0200 )editconvert to answer

Yeah, I mean I can connect to my application and read all my data. I use a LED blinking from main, just to detect if no error has occured or if it is stuck. It keeps blinking. A different LED is toggled in the handler, which does not blink, even when I set it to 1s instead of 100ms. In the debugger I do not enter the handler. Should I use the wait for event functions? Are those mandatory for the intterupt to fire? Also I do not use the scheduler.

Tostikoning ( 2017-04-21 11:34:18 +0200 )editconvert to answer

What does app_timer_start(m_pt100_timer_id, APP_TIMER_TICKS(100), NULL); return? Try do send the err_code to the error handler:

void measurement_start(void)
{
    ret_code_t err_code;
    /* Start timer */
    err_code = app_timer_start(&m_pt100_timer_id, MEAS_TIMER_INTERVAL, NULL);
    APP_ERROR_CHECK(err_code);

    /* Clear previous measurement in flash */

}
Sigurd ( 2017-04-21 11:39:03 +0200 )editconvert to answer

Okay, this helped me. Not in an expected way. I missed a reset. i put a breakpoint in the measurement start function. No error code is returned but i can see it resetting. I'll try to investigate this further.

EDIT 1: Strange things are happening. I also placed a breakpoint in the RTC1_IRQHandler in app_timer.c . Another breakpoint is in measurement_start() just after

err_code = app_timer_start(m_pt100_timer_id, APP_TIMER_TICKS(5000), NULL);

btw app_timer_start does not like to get a address of the timer id, but would rather like a copy.

ret_code_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context);

This change allows me to enter the RTC1_IRQHandler() however I still do not enter my own handler and it still resets.

Also it only enters the RTC1_IRQhandler if I wait a a few seconds, if I just continue it resets without entering the RTC1_IRQHandler(). I will continue..

EDIT 2: If I change the ...(more)

Tostikoning ( 2017-04-21 11:42:34 +0200 )editconvert to answer

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer. Do not ask a new question or reply to an answer here.

[hide preview]

User menu

    or sign up

Recent questions

Question Tools

1 follower

Stats

Asked: 2017-04-21 09:58:42 +0200

Seen: 395 times

Last updated: april 21