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

APP_TIMER_DEF for several timers with same timer id. like C++ class member

hello

yes I have problems with my app_timers in SDK14.2 (migration from 12),

I well noticed that the prototypes of the functions create and start changed from sdk12.

// create timer

static app_timer_t timer_data = { {0} };
m_timer_id = &timer_data;
err_code = app_timer_create(&m_timer_id, APP_TIMER_MODE_REPEATED, cbk_measure);
APP_ERROR_CHECK(err_code);
-> this works well
ret_code_t err_code;
err_code = app_timer_start(m_timer_id, APP_TIMER_TICKS(m_probing_interval_ms), (void *) this);
NRF_LOG_DEBUG("FIXME: app_timer_start returns %d", err_code);
// APP_ERROR_CHECK(err_code);
-> but the start returns error code 8 (invalid param if i remember correctly) and I dont understand why. I tried APP_TIMER_DEF, but my timer_ids are C++ class members. I cannot simply use the APP_TIMER_DEF macro because I dont know if it ok if already declared. but it was already the case ion SDK12 and worked AFAIK

thx,

yacine

  • Actually I investigated a bit further and it looks like the problem comes from the fact that I used timer_id as a C++ member of a class. When I instantiate a new object, I want a separate timer_id and the declaration macro give me the same number at each instantiation. This is why app_timer_create retturns error code 8 (invalid state) this second time.

    Code:

    // create timer (in C++ constructor)
    	static app_timer_t timer_data = { { 0 } };  //= APP_TIMER_DEF(m_timer_id);
    	m_timer_id = &timer_data;                   //
    	NRF_LOG_DEBUG("FIXME: m_timer_id (before app_timer_create)= %d", m_timer_id);
    	err_code = app_timer_create(&m_timer_id, APP_TIMER_MODE_REPEATED, Probe::cbk_measure);
    	NRF_LOG_DEBUG("FIXME: app_timer_create returns %d", err_code);
    	NRF_LOG_DEBUG("FIXME: m_timer_id (after app_timer_create) = %d", m_timer_id);
    	//	APP_ERROR_CHECK(err_code);
    
    (...)
    
    //start timer
    	ret_code_t err_code;
    
    	NRF_LOG_DEBUG("FIXME: m_timer_id (before app_timer_start) = %d", m_timer_id);
    	err_code = app_timer_start(m_timer_id, APP_TIMER_TICKS(m_probing_interval_ms), (void *) this);
    	NRF_LOG_DEBUG("FIXME: app_timer_start returns %d", err_code);
    	NRF_LOG_DEBUG("FIXME: m_timer_id (after app_timer_start) = %d", m_timer_id);
    	//	APP_ERROR_CHECK(err_code);
    	
    	

    Logs:

    // first instance of class
    <debug> thc_probe_common: FIXME: m_timer_id (before app_timer_create)= 536884952
    <debug> thc_probe_common: FIXME: app_timer_create returns 0
    <debug> thc_probe_common: FIXME: m_timer_id (after app_timer_create) = 536884952
    (..)
    <debug> thc_probe_common: FIXME: m_timer_id (before app_timer_start) = 536884952
    <debug> thc_probe_common: FIXME: app_timer_start returns 0
    <debug> thc_probe_common: FIXME: m_timer_id (after app_timer_start) = 536884952
    // Second instance of class
    <debug> thc_probe_common: FIXME: m_timer_id (before app_timer_create)= 536884952
    <debug> thc_probe_common: FIXME: app_timer_create returns 8 ---> NOK
    <debug> thc_probe_common: FIXME: m_timer_id (after app_timer_create) = 536884952
    (...)
    <debug> thc_probe_common: FIXME: m_timer_id (before app_timer_start) = 536884952
    <debug> thc_probe_common: FIXME: app_timer_start returns 0
    <debug> thc_probe_common: FIXME: m_timer_id (after app_timer_start) = 536884952

  • Hi Yacine,

      Have you read this migration note for SDK v14.2? 

    Best Regards,

     Martin L.

  • How to define a timer_id as a object class member ? the APP_TIMER_DEF uis not adapted to already declared variable.

  • self fixed:

    in APP_TIMER_DEF macro, "static" allows to exist beyond the functin where it is called, after app_timer_create. hence you can call it later e.g. for the start function, but it also induces that every timer id with the same timer data... this is not good for instantiation/C++ or collections that uses the same timer_id variable name.

    workaround = use malloc instead, which allocate a different memory area (good for several instantiation) and is opersistent (just like static) but you will need to free the memory when no more used.

    // create timer
    	m_timer_id = (app_timer_t *) malloc(sizeof(app_timer_t));
    	memset(m_timer_id, 0, sizeof(app_timer_t));

Related