<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Creating application timer with context pointer</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/35512/creating-application-timer-with-context-pointer</link><description>I am trying to use an application timer along with some variables. I am using ble_app_template as base. To start of I&amp;#39;ve got the timer without any parameters being passed, which works just fine. 
 
 But I&amp;#39;m not managing to pass any variables. I have tried</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 19 Jun 2018 08:14:56 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/35512/creating-application-timer-with-context-pointer" /><item><title>RE: Creating application timer with context pointer</title><link>https://devzone.nordicsemi.com/thread/136671?ContentTypeID=1</link><pubDate>Tue, 19 Jun 2018 08:14:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:810e3699-6676-47d5-8380-689346562608</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;I think it&amp;#39;s good practice to use &amp;#39;static&amp;#39; for global variables for the reason you mentioned, but it&amp;#39;s not required. A local static variable is also&amp;nbsp;accessible during the complete execution.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Creating application timer with context pointer</title><link>https://devzone.nordicsemi.com/thread/136655?ContentTypeID=1</link><pubDate>Tue, 19 Jun 2018 07:19:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3a107aba-3b71-46c8-b2a7-8cd9f411fd06</guid><dc:creator>KevinL</dc:creator><description>&lt;p&gt;I understand that applying to local variables. But globals are accessible during the complete execution anyway. Should they still be made static so another file cannot declare a global variable with the same name?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Creating application timer with context pointer</title><link>https://devzone.nordicsemi.com/thread/136649?ContentTypeID=1</link><pubDate>Tue, 19 Jun 2018 06:46:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3e67b16c-b86b-4dba-aebf-c438e711835f</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Glad to hear that you got it to work.&amp;nbsp;As for the remaining question, it&amp;#39;s unsafe to use an auto variable because&amp;nbsp;it will be pushed to the call stack&amp;nbsp;thus only&amp;nbsp;valid in the scope of which the variable was defined (will likely be overwritten at some point).&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Creating application timer with context pointer</title><link>https://devzone.nordicsemi.com/thread/136603?ContentTypeID=1</link><pubDate>Mon, 18 Jun 2018 15:31:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2ee2018d-6c0e-419f-a12f-2d1470244a88</guid><dc:creator>KevinL</dc:creator><description>&lt;p&gt;Thank you for the fast reply. My implementation of the timer was correct all along, the extraction of the rValue in the timeout handler was erroneous.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;One question remains:&lt;/strong&gt; Why should the context pointer be in static memory? I have tried automatic memory allocation, which works just fine.&lt;/p&gt;
&lt;p&gt;Below my work progress for future visitors with the same problem.&lt;/p&gt;
&lt;p&gt;------------------&lt;/p&gt;
&lt;p&gt;What I want to achieve is to do a periodical check and change a variable accordingly, the incrementation is for testing of the timeout handler call only. I want to avoid globals for good coding practice.&lt;/p&gt;
&lt;p&gt;I have changed my context variable to be static. I noticed that I forgot to include my implementation starting the timer where I had been passing the context pointer already.&lt;/p&gt;
&lt;p&gt;Changes made:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;making the counter a static uint32_t&lt;/li&gt;
&lt;li&gt;casting to uint32_t *&lt;/li&gt;
&lt;li&gt;%d to %u&lt;/li&gt;
&lt;li&gt;implementation of valueIncrementer()&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;APP_TIMER_DEF(valueIncrementer_timer_id);       //Had  not included this before
static uint32_t counter = 0;

void valueIncrementer(void *countValue)
{
  uint32_t *pCountValue = countValue;
  uint32_t newValue = *pCountValue;
  newValue++;
  counter = (int)newValue;
  NRF_LOG_INFO(&amp;quot;counter = %u, %d&amp;quot;, newValue, counter);
}
void (*valueIncrementerFPtr)(void *);

static void timers_init(void)
{
    // Initialize timer module.

       err_code = app_timer_create(&amp;amp;valueIncrementer_timer_id, 
                                   APP_TIMER_MODE_REPEATED,
                                   //valueIncrementer);
                                   *valueIncrementerFPtr);
       APP_ERROR_CHECK(err_code);
       /*^MY_JOB^*/
}

static void application_timers_start(void)
{
       ret_code_t err_code;
       int* pointer = &amp;amp;counter;
       err_code = app_timer_start(valueIncrementer_timer_id, TIMER_VALUE_INCREMENTER, &amp;amp;counter);    //Breakpoint: &amp;amp;context is correct
       APP_ERROR_CHECK(err_code); 
}

int main(void)
{
    valueIncrementerFPtr = &amp;amp;valueIncrementer;
    ...
    }&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The context pointer passed to app_timer_start is correct, as well as the address received in the timeout handler (I&amp;#39;ve checked this with *pointer = &amp;amp;counter). Knowing that the received pointer address is correct, it had to be something about retrieving the rValue.&lt;/p&gt;
&lt;p&gt;At first I did not succeed in easily retrieving the value and incrementing the global counter (implementation of valueIncrementer()). I tried incrementing and casting in the same statement. To solve it, I extracted every step into the simplest possible form.&lt;/p&gt;
&lt;p&gt;I am going to reduce this to proper code tomorrow, and calling it a day for now.&lt;/p&gt;
&lt;p&gt;------&lt;/p&gt;
&lt;p&gt;Edit:&lt;/p&gt;
&lt;p&gt;So I have not managed to reduce valueIncrementer() any while keeping the functionality. Underneeth I have only removed the redundant code/checks.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void valueIncrementer(void *countValue)
{
  uint32_t *pCountValue = countValue;
  uint32_t value = *pCountValue;
  value++;
  counter = value;
  NRF_LOG_INFO(&amp;quot;counter = %u&amp;quot;, counter);
}

static void application_timers_start(void)
{
       ret_code_t err_code;
       err_code = app_timer_start(valueIncrementer_timer_id, TIMER_VALUE_INCREMENTER, &amp;amp;counter);
       APP_ERROR_CHECK(err_code); 
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Creating application timer with context pointer</title><link>https://devzone.nordicsemi.com/thread/136550?ContentTypeID=1</link><pubDate>Mon, 18 Jun 2018 12:38:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:08739b94-8196-4c54-bd4d-924b1c2a3484</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The context pointer should be stored in static memory. Eg.,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;static uint32_t context;&amp;nbsp; // Can be defined globally&amp;nbsp;&lt;/p&gt;
&lt;p&gt;// Start application timers.&lt;br /&gt; err_code = app_timer_start(m_battery_timer_id, BATTERY_LEVEL_MEAS_INTERVAL, &amp;amp;context);&lt;br /&gt; APP_ERROR_CHECK(err_code);&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not completely sure I understand what you want to achieve, but in your case, would it make sense to just increment the &amp;quot;context&amp;quot; variable from the timeout handler?&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>