I coded a simple timer using app_timer and tested it on my nRF51822-DK:
#ifndef NRF51
#error NRF51 must be defined.
#endif
#include <nrf.h>
#include <app_timer.h>
#include <nrf_delay.h>
#include <nordic_common.h>
#include "nrf_drv_config.h"
#include "leds.h"
uint8_t flag;
APP_TIMER_DEF(timer);
/**
* @brief Function for error handling, which is called when an error has occurred.
*
* @param[in] error_code Error code supplied to the handler.
* @param[in] line_num Line number where the handler is called.
* @param[in] p_file_name Pointer to the file name.
*/
void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name)
{
// On assert, the system can only recover with a reset.
#ifndef DEBUG
// On release mode we simply reset the device.
NVIC_SystemReset();
#else
// This call can be used for debug purposes during application development.
// @note CAUTION: Activating this code will write the stack to flash on an error.
// This function should NOT be used in a final product.
// It is intended STRICTLY for development/debugging purposes.
// The flash write will happen EVEN if the radio is active, thus interrupting
// any communication.
// Use with care. Uncomment the line below to use.
//ble_debug_assert_handler(error_code, line_num, p_file_name);
// The following variable helps Keil keep the call stack visible, in addition, it can be set to
// 0 in the debugger to continue executing code after the error check.
volatile bool loop = true;
UNUSED_VARIABLE(loop);
uint32_t m_error_code = error_code;
uint32_t m_line_num = line_num;
const uint8_t * m_p_file_name = p_file_name;
UNUSED_VARIABLE(m_error_code);
UNUSED_VARIABLE(m_line_num);
UNUSED_VARIABLE(m_p_file_name);
__disable_irq();
led_green_on();
led_red_off();
for (;;)
{
nrf_delay_ms(100);
led_green_toggle();
led_red_toggle();
}
#endif // DEBUG
}
uint32_t event_handler(app_timer_timeout_handler_t timeout_handler,
void * p_context)
{
flag = 0;
return NRF_SUCCESS;
}
void timeout()
{
led_red_on();
}
int main()
{
uint32_t retval = 0;
leds_init();
// Initialize app_timer.
APP_TIMER_INIT(TIMER_PRESCALER, OP_QUEUES_SIZE, event_handler);
retval = app_timer_create(&timer, APP_TIMER_MODE_SINGLE_SHOT, timeout);
app_timer_start(timer, APP_TIMER_TICKS(2000, TIMER_PRESCALER), NULL);
flag = 1;
do
{
nrf_delay_ms(200);
led_green_toggle();
} while(flag);
// This should never be reached. If it is app_error_hanlder will be called.
APP_ERROR_CHECK_BOOL(false);
return retval;
}
Where the "green" led is LED1 and the "red" one is LED2, TIMER_PRESCALER is 0, OP_QUEUES_SIZE is 8.
I would expect the green led to blink for ~2seconds and then the blink will stop, the red led will turn on and after that both will be toggling fast.
But it seems I never get a timeout. What am I missing here?