<?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>nRF9160 Timer issue</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/65885/nrf9160-timer-issue</link><description>Dear Sir. 
 
 I am using the Timer of nRF916 . 
 In a simple project the timer is working fine ( see attached code ). 
 A buzzer is connected to PWM output pin 12. 
 I can here the Buzzer beeps as expected . 
 
 
 
 In another larger project , I insert</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 17 Sep 2020 09:24:15 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/65885/nrf9160-timer-issue" /><item><title>RE: nRF9160 Timer issue</title><link>https://devzone.nordicsemi.com/thread/270023?ContentTypeID=1</link><pubDate>Thu, 17 Sep 2020 09:24:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d81f6dd1-f212-4f8b-83fe-2f6939db69a3</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Could you try starting the timer instance after initializing io_init and pwm_init?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160 Timer issue</title><link>https://devzone.nordicsemi.com/thread/269799?ContentTypeID=1</link><pubDate>Wed, 16 Sep 2020 09:19:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:27a706c2-ffe5-40c3-a132-ab13b32f1a93</guid><dc:creator>ephraim</dc:creator><description>&lt;p&gt;Hi.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I stripped all unnecessary code .&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Still the same error ( reset over and over.)&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Please Advise&lt;pre class="ui-code" data-mode="text"&gt;/*
 * Copyright (c) 2018 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
 */

#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;drivers/uart.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;main.h&amp;gt;

#include &amp;lt;drivers/adc.h&amp;gt;
#include &amp;lt;drivers/spi.h&amp;gt;
#include &amp;lt;drivers/i2c.h&amp;gt;
#include &amp;lt;drivers/gpio.h&amp;gt;
#include &amp;lt;drivers/pwm.h&amp;gt;
//#include &amp;lt;drivers/timer.h&amp;gt;

#include &amp;lt;net/mqtt.h&amp;gt;
#include &amp;lt;net/socket.h&amp;gt;
#include &amp;lt;lte_lc.h&amp;gt;
#if defined(CONFIG_LWM2M_CARRIER)
#include &amp;lt;lwm2m_carrier.h&amp;gt;
#endif

#include &amp;lt;kernel.h&amp;gt;
#include &amp;lt;c:/zephyr/v1.2.0/zephyr/include/power/reboot.h&amp;gt;


#include &amp;lt;MT25QL128ABA.h&amp;gt;
#include &amp;lt;afe4900.h&amp;gt;
#include &amp;lt;adp536x.h&amp;gt;
#include &amp;lt;lis2dh12.h&amp;gt;
#include &amp;lt;init.h&amp;gt;

#include &amp;lt;hal/nrf_saadc.h&amp;gt;
#include &amp;lt;stdarg.h&amp;gt;
 

#define TIEMR_INTERVAL_SEC 1
#define TIEMR_INTERVAL_mSEC 10
 
struct k_timer bz_timer;

 
struct device * pwm_dev; 



#define MY_STACK_SIZE 512
#define MY_PRIORITY 5
K_THREAD_STACK_DEFINE(my_stack_area, MY_STACK_SIZE);
struct k_work_q my_work_q;

struct device_info {
    struct k_work work;
    char name[16]
} my_device;

  
  

  

void bz_func(struct k_timer *timer_id) 
{
     k_work_submit(&amp;amp;my_device.work);
}


void activate_buzzer(struct k_work *item)
{
     unsigned char k;
     static unsigned int time = 0;

    struct device_info *the_device =
        CONTAINER_OF(item, struct device_info, work);
   
   
   
     if(time == 0)
     {
       printk(&amp;quot;Fisrt PWM set \n&amp;quot; );
       pwm_pin_set_usec(pwm_dev,PWM_IO, 260, 130,0);
       SetLed(BLUE,ON); 
       SetLed(GREEN,OFF); 
     }
    if(time == 10)
    {
       printk(&amp;quot;Second PWM set \n&amp;quot; );
       pwm_pin_set_usec(pwm_dev,PWM_IO, 400, 200,0);
       SetLed(BLUE,OFF); 
       SetLed(GREEN,ON); 
     }
    if(++time &amp;gt;= 20)
       time = 0;
}



void main(void)
{
      int err;
      static unsigned int i = 0;
 

 
  //    initStructures();
  
  //     _AFE4900_AQUIRE_MODE.aquire_mode =  afe4900_aquire_sample;

      k_timer_init(&amp;amp;bz_timer, bz_func, NULL);
      k_timer_start(&amp;amp;bz_timer, K_SECONDS(1), K_MSEC(100));

      k_work_q_start(&amp;amp;my_work_q, my_stack_area,
              K_THREAD_STACK_SIZEOF(my_stack_area), MY_PRIORITY); 

      strcpy(my_device.name, &amp;quot;FOO_dev&amp;quot;);
      k_work_init(&amp;amp;my_device.work, activate_buzzer);



      io_init();
      pwm_init();
      

 
      while(1) {

          k_sleep(K_MSEC(2000));
    
      };
}

  &lt;/pre&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pwm_5F00_reset.png" /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160 Timer issue</title><link>https://devzone.nordicsemi.com/thread/269791?ContentTypeID=1</link><pubDate>Wed, 16 Sep 2020 08:53:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:59def75d-4e25-42f3-979c-5384624b46c0</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In this case, my_isr() is equivalent to your k_timer handler (bz_func).&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160 Timer issue</title><link>https://devzone.nordicsemi.com/thread/269785?ContentTypeID=1</link><pubDate>Wed, 16 Sep 2020 08:36:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:044f9c58-88ab-4848-bf53-ca5b4fd33533</guid><dc:creator>ephraim</dc:creator><description>&lt;p&gt;Hi.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;One more thing&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;pre&gt;&lt;span class="cm"&gt;/* install my_isr() as interrupt handler for the device (not shown) */&lt;/span&gt;
&lt;span class="p"&gt;...&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;How do I set thid handler ?.&lt;br /&gt;&lt;br /&gt;Thanks&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160 Timer issue</title><link>https://devzone.nordicsemi.com/thread/269764?ContentTypeID=1</link><pubDate>Wed, 16 Sep 2020 06:49:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4eb9bdc0-8438-4642-8622-c913f137c082</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;You should not call functions that take long time or rely on other interrupts from a k_timer handler, as its also in interrupt context.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you use a work queue, you schedule the function to another context to avoid this scenario, just as described in the work queues documentation:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://docs.zephyrproject.org/1.9.0/kernel/threads/workqueues.html"&gt;https://docs.zephyrproject.org/1.9.0/kernel/threads/workqueues.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160 Timer issue</title><link>https://devzone.nordicsemi.com/thread/269761?ContentTypeID=1</link><pubDate>Wed, 16 Sep 2020 06:42:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e5d9b5da-e2ae-4b9b-9a75-b0847ba7dd73</guid><dc:creator>ephraim</dc:creator><description>&lt;p&gt;Hi.&lt;/p&gt;
&lt;p&gt;I am sorry , I am not following you.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Weather I use the&amp;nbsp;&lt;span&gt;workqueue&amp;nbsp;, I will need to use the Timer.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Is it possible to produce interrupts without the timer ?.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt; if(time == 10)&lt;br /&gt; {&lt;br /&gt;// if(pwm_pin_set_usec(pwm_dev,PWM_IO, 400, 200,0) == 0)&lt;br /&gt; {&lt;br /&gt; SetLed(BLUE,OFF); &lt;br /&gt; SetLed(GREEN,ON); &lt;br /&gt; }&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;It is the&amp;nbsp;pwm_pin_set_usec(pwm_dev,PWM_IO, 400, 200,0) that make the reset .&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160 Timer issue</title><link>https://devzone.nordicsemi.com/thread/269538?ContentTypeID=1</link><pubDate>Tue, 15 Sep 2020 07:32:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8040cb61-b16d-4d63-941b-d67961463848</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The pwm zephyr wrapper for nRF devices is found in pwm_nrfx.c.&lt;/p&gt;
&lt;p&gt;I cannot say what is wrong in your case, but the k_timer is inherited from the RTC interrupt handler, and the PWM module also relies on interrupts/events when setting the pin.&lt;/p&gt;
&lt;p&gt;Can you try to use a workqueue instead of handling the PWM calls in the k_timer event handler?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You can read about workqueues here:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://docs.zephyrproject.org/1.9.0/kernel/threads/workqueues.html"&gt;https://docs.zephyrproject.org/1.9.0/kernel/threads/workqueues.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160 Timer issue</title><link>https://devzone.nordicsemi.com/thread/269348?ContentTypeID=1</link><pubDate>Mon, 14 Sep 2020 10:54:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:40ff9608-7f57-4017-913f-5062f9d33f60</guid><dc:creator>ephraim</dc:creator><description>&lt;p&gt;Hi.&lt;/p&gt;
&lt;p&gt;I found the culprit&amp;nbsp; ,&amp;nbsp;&lt;pre class="ui-code" data-mode="text"&gt;void bz_func(struct k_timer *timer_id) 
{
   // k_work_submit(&amp;amp;work_bz);       
    unsigned char k;
     static unsigned int time = 0;
  //    SetLed(BLUE,ON);  
     k = _BZ_SM.bz_state;
     printk(&amp;quot;Buzzer \n&amp;quot;);
     // Siren
     if(time == 0)
     {
//      if(pwm_pin_set_usec(pwm_dev,PWM_IO, 260, 130,0) == 0)
      {
        SetLed(BLUE,ON); 
        SetLed(GREEN,OFF);
      }
//      else
//      {
//        SetLed(RED,ON);
//        printk(&amp;quot;Buzzer Error\n&amp;quot;);
//      }
     }


     if(time == 10)
     {
//      if(pwm_pin_set_usec(pwm_dev,PWM_IO, 400, 200,0) == 0)
      {
        SetLed(BLUE,OFF); 
       SetLed(GREEN,ON); 
     }
//      else
//      {
//        SetLed(RED,ON);
//        printk(&amp;quot;Buzzer Error\n&amp;quot;);
//      }
     }
     if(++time &amp;gt;= 20)
       time = 0;

}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;The Timer works fine without the update of the PWM .&lt;/p&gt;
&lt;p&gt;When the PWM is re commented , the program reset itself ( see pic ).&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;The same code works fine in another small project .&lt;/p&gt;
&lt;p&gt;Please Advise&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pw_2B00_timer_3D00_reset.png" /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160 Timer issue</title><link>https://devzone.nordicsemi.com/thread/269333?ContentTypeID=1</link><pubDate>Mon, 14 Sep 2020 10:04:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a2d86239-a8ad-4c4e-8af6-dcab3253ad0c</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
[quote user=""]In another larger project , I insert the Timer code , and it behaves erratically .[/quote]
&lt;p&gt;What do you mean by this? Does it stop working after a while? Or does it not update the pwm value?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I see that you&amp;#39;re not checking the return of the call to&amp;nbsp;pwm_pin_set_usec. Please implement a check on the return code to ensure that the value has been set properly.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>