<?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>How to implement timer interrupt in nRF9160?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/45200/how-to-implement-timer-interrupt-in-nrf9160</link><description>Timer interrupt does not work well. In the code below, LED doesn&amp;#39;t turn off and &amp;quot;interrupt!&amp;quot; message is not received. I really appreciate it if you give me any advice.</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 26 Mar 2019 14:00:29 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/45200/how-to-implement-timer-interrupt-in-nrf9160" /><item><title>RE: How to implement timer interrupt in nRF9160?</title><link>https://devzone.nordicsemi.com/thread/178399?ContentTypeID=1</link><pubDate>Tue, 26 Mar 2019 14:00:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0cbc7c97-2838-4f99-90a5-fe09f6b980e3</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Glad to hear that it worked!&lt;/p&gt;
&lt;p&gt;The kernel timers are very handy to use, and only requires a few set of lines to get working properly. However, since they are based on the 32.768 kHz RTC, they&amp;#39;re not high speed.&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: How to implement timer interrupt in nRF9160?</title><link>https://devzone.nordicsemi.com/thread/178393?ContentTypeID=1</link><pubDate>Tue, 26 Mar 2019 13:57:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:70f906af-7d81-4d69-b063-522c01e52153</guid><dc:creator>Yusuke</dc:creator><description>&lt;p&gt;Thanks a lot! The code below works!&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;nrf9160.h&amp;gt;
#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;misc/printk.h&amp;gt;
#include &amp;lt;device.h&amp;gt;
#include &amp;lt;gpio.h&amp;gt;

#define TIEMR_INTERVAL_SEC 5

struct device *dev;
struct k_timer my_timer;
uint8_t toggle = 0;

void my_expiry_function(struct k_timer *timer_id){
        ++toggle;
        gpio_pin_write(dev, 16, toggle % 2);
        printk(&amp;quot;interrupt!\r\n&amp;quot;);
}

void main(void)
{
        printk(&amp;quot;------------\r\nHello, World!\r\n&amp;quot;);
        k_timer_init(&amp;amp;my_timer, my_expiry_function, NULL);

	dev = device_get_binding(&amp;quot;GPIO_0&amp;quot;);
        gpio_pin_configure(dev, 16, GPIO_DIR_OUT);
        gpio_pin_write(dev, 16, toggle);
        k_timer_start(&amp;amp;my_timer, K_SECONDS(TIEMR_INTERVAL_SEC), K_SECONDS(TIEMR_INTERVAL_SEC));
        
        while(1){
                 printk(&amp;quot;Start to sleep\r\n&amp;quot;);
                 k_cpu_idle();
                 printk(&amp;quot;timer interrupt for %u sec\r\n&amp;quot;, TIEMR_INTERVAL_SEC);
        }
}
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to implement timer interrupt in nRF9160?</title><link>https://devzone.nordicsemi.com/thread/178274?ContentTypeID=1</link><pubDate>Tue, 26 Mar 2019 09:47:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8933b6ba-c92c-4abd-83f5-f57b48651b9a</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;Since zephyr rtos owns the interrupts, we need to instruct it that we want the interrupt in our application, similar to this:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/support-attachments/beef5d1b77644c448dabff31668f3a47-7c4624ac838d4a759896d39753e44ef0/timer.zip"&gt;devzone.nordicsemi.com/.../timer.zip&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I would instead of doing the above, especially for longer delays, recommend that you look into the kernel timers instead (RTC based):&lt;/p&gt;
&lt;p&gt;&lt;a href="https://docs.zephyrproject.org/1.13.0/kernel/timing/timers.html"&gt;https://docs.zephyrproject.org/1.13.0/kernel/timing/timers.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&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: How to implement timer interrupt in nRF9160?</title><link>https://devzone.nordicsemi.com/thread/178176?ContentTypeID=1</link><pubDate>Mon, 25 Mar 2019 16:03:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a42a0ce7-05e6-4ba8-a3f4-c5038d84273f</guid><dc:creator>Yusuke</dc:creator><description>&lt;p&gt;Hi, Hakon. Thank you for your advice, which is very helpful.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m new to zephyr.&lt;/p&gt;
&lt;p&gt;Do you mean that zephyr has some bug in timer interrupt, or I&amp;#39;m doing a wrong thing?&lt;/p&gt;
&lt;p&gt;I&amp;#39;m referring to the code below as well as nRF9160-SICA-R_Nordic_Semiconductor.pdf.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/NordicPlayground/nrf51-TIMER-examples/blob/master/timer_example_timer_mode/main.c"&gt;https://github.com/NordicPlayground/nrf51-TIMER-examples/blob/master/timer_example_timer_mode/main.c&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to implement timer interrupt in nRF9160?</title><link>https://devzone.nordicsemi.com/thread/178028?ContentTypeID=1</link><pubDate>Mon, 25 Mar 2019 09:23:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2949ace8-51f0-40fe-8546-f69b7a63aea5</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;I suspect that this is caused by the OS itself, as the interrupt vector should be declared through the zephyr rtos API, similar to what is done here for the libbsd port file:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/NordicPlayground/fw-nrfconnect-nrf/blob/master/lib/bsdlib/bsd_os.c#L284"&gt;https://github.com/NordicPlayground/fw-nrfconnect-nrf/blob/master/lib/bsdlib/bsd_os.c#L284&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s the documentation for this:&amp;nbsp;&lt;a href="https://docs.zephyrproject.org/1.13.0/api/kernel_api.html#c.IRQ_DIRECT_CONNECT"&gt;https://docs.zephyrproject.org/1.13.0/api/kernel_api.html#c.IRQ_DIRECT_CONNECT&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&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: How to implement timer interrupt in nRF9160?</title><link>https://devzone.nordicsemi.com/thread/177943?ContentTypeID=1</link><pubDate>Sat, 23 Mar 2019 12:46:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3c72c577-c853-4149-b1b1-24b66f1c92e4</guid><dc:creator>Yusuke</dc:creator><description>&lt;p&gt;Hi, Hakon. Thank you for your reply.&amp;nbsp;I tested the following code, but got fail. Do you know why?&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;&amp;lt;main.c&amp;gt;
#include &amp;lt;nrf9160.h&amp;gt;
#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;misc/printk.h&amp;gt;
#include &amp;lt;device.h&amp;gt;
#include &amp;lt;nrf_timer.h&amp;gt;
#include &amp;lt;gpio.h&amp;gt;

#define TIEMR_INTERVAL_SEC 5

struct device *dev;

// make 5 sec timer
// f_timer = 16*1000*1000 / (2^PRESCALER)
// sec_interval = (2^PRESCALER) / 16*1000*1000 x NRF_TIMER0_S-&amp;gt;CC[0]
void config_timer()
{		
        NRF_TIMER1-&amp;gt;TASKS_STOP = 1;
        NRF_TIMER1-&amp;gt;MODE = TIMER_MODE_MODE_Timer;
        NRF_TIMER1-&amp;gt;TASKS_CLEAR = 1;                             // clear time
        NRF_TIMER1-&amp;gt;PRESCALER = 9;                 // value = 0..9
	NRF_TIMER1-&amp;gt;BITMODE = TIMER_BITMODE_BITMODE_32Bit;		   //Set counter to 32 bit resolution. MAX count = 2^32-1
	NRF_TIMER1-&amp;gt;CC[0] = 156250;  //Set value for TIMER0 156250
        NRF_TIMER1-&amp;gt;SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Enabled &amp;lt;&amp;lt; TIMER_SHORTS_COMPARE0_CLEAR_Pos;
        NRF_TIMER1-&amp;gt;INTENSET = TIMER_INTENSET_COMPARE0_Enabled &amp;lt;&amp;lt; TIMER_INTENSET_COMPARE0_Pos; 
        NVIC_SetPriority(TIMER1_IRQn, 1);
        NVIC_EnableIRQ(TIMER1_IRQn);
        NRF_TIMER1-&amp;gt;TASKS_START = 1;               // Start TIMER0
}

void TIMER1_IRQHandler(void)
{
        gpio_pin_write(dev, 16, 0);
        printk(&amp;quot;interrupt!\r\n&amp;quot;);
	if ((NRF_TIMER1-&amp;gt;EVENTS_COMPARE[0] != 0) &amp;amp;&amp;amp; ((NRF_TIMER1-&amp;gt;INTENSET &amp;amp; TIMER_INTENSET_COMPARE0_Msk) != 0))
        {
                  NRF_TIMER1-&amp;gt;EVENTS_COMPARE[0] = 0;           //Clear compare register 0 event	
                  gpio_pin_write(dev, 16, 0);
                  printk(&amp;quot;interrupt!\r\n&amp;quot;);
        }
}

void main(void)
{
        printk(&amp;quot;------------\r\nHello, World!\r\n&amp;quot;);
	dev = device_get_binding(&amp;quot;GPIO_0&amp;quot;);
        gpio_pin_configure(dev, 16, GPIO_DIR_OUT);

        config_timer();
        gpio_pin_write(dev, 16, 1);

        while(1){
                 printk(&amp;quot;Start to sleep\r\n&amp;quot;);
                 k_cpu_idle();
                 printk(&amp;quot;timer interrupt for %u sec\r\n&amp;quot;, TIEMR_INTERVAL_SEC);
        }
}

&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;***** Booting Zephyr OS v1.13.99-ncs2 *****
------------
Hello, World!
Start to sleep
timer interrupt for 5 sec
Start to sleep
***** Hardware exception *****
Current thread ID = 0x2000009c
Faulting instruction address = 0xf4240
Fatal fault in ISR! Spinning...&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to implement timer interrupt in nRF9160?</title><link>https://devzone.nordicsemi.com/thread/177807?ContentTypeID=1</link><pubDate>Fri, 22 Mar 2019 11:32:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:181e9127-9a71-4a92-9751-27fc9c0c1f43</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;Try setting the NRF_TIMER1-&amp;gt;SHORTS, field COMPARE_CLEAR for CC[0]:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://www.nordicsemi.com/DocLib/Content/Product_Spec/nRF9160/latest/timer?673#register.SHORTS"&gt;https://www.nordicsemi.com/DocLib/Content/Product_Spec/nRF9160/latest/timer?673#register.SHORTS&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;If this is not set, the timer will have to roll over again to provide a compare event.&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></channel></rss>