<?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>Using simple hardware timer with event</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/92191/using-simple-hardware-timer-with-event</link><description>Hi, 
 
 For my app (using zephyr and nrf52840) I want to use hardware Timer like that, is there any manual or example how to do that? 
 Need able to set period of timer from 1 to about 100 ms. 
 1) Set Capture/Compare reg. 
 2) Set CLK div for the timer</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 22 Sep 2022 19:25:03 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/92191/using-simple-hardware-timer-with-event" /><item><title>RE: Using simple hardware timer with event</title><link>https://devzone.nordicsemi.com/thread/387571?ContentTypeID=1</link><pubDate>Thu, 22 Sep 2022 19:25:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f176110c-e899-4bb8-ac7c-06e80042d055</guid><dc:creator>JamesDougherty</dc:creator><description>&lt;p&gt;Per the documentation:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;After expiration alarm can be set again, disabling is not needed. When alarm expiration handler is called, channel is considered available and can be set again in that context.&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://docs.zephyrproject.org/3.0.0/reference/peripherals/counter.html#c.counter_set_channel_alarm"&gt;https://docs.zephyrproject.org/3.0.0/reference/peripherals/counter.html#c.counter_set_channel_alarm&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I would start a new thread about your other question.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using simple hardware timer with event</title><link>https://devzone.nordicsemi.com/thread/387569?ContentTypeID=1</link><pubDate>Thu, 22 Sep 2022 19:20:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:41d76c09-9497-4725-8c87-970231d273a4</guid><dc:creator>witc</dc:creator><description>&lt;p&gt;not sure if I should establish a new thread... But&lt;/p&gt;
&lt;p&gt;1) How can I set if the timer/counter will work in periodic or single mode?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;2) When debugging in VS code, is there any options for stoping the timer when I am currently on the breakpoint? (I do not want my timer to be running when I am on the breakpoint).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using simple hardware timer with event</title><link>https://devzone.nordicsemi.com/thread/387561?ContentTypeID=1</link><pubDate>Thu, 22 Sep 2022 17:33:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3e1d9051-f1e1-476b-8af3-6632055529cf</guid><dc:creator>JamesDougherty</dc:creator><description>&lt;p&gt;That&amp;#39;s interesting, VS Code actually showed red squiggly line under it for me and when I hover it gives the same error you were seeing.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/0044.error.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Either way, you&amp;#39;re more than welcome and I&amp;#39;m glad I was able to help!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using simple hardware timer with event</title><link>https://devzone.nordicsemi.com/thread/387560?ContentTypeID=1</link><pubDate>Thu, 22 Sep 2022 17:28:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:756fbcbd-77aa-4576-9520-9408cf5292da</guid><dc:creator>witc</dc:creator><description>&lt;p&gt;Great thanks! Now I can blink with my led by using timer&amp;nbsp;&lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f600.svg" title="Grinning"&gt;&amp;#x1f600;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Perhaps a bit of a shame that compiler did not point me to this line of code.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Now I am going to elaborate more with peripherals...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using simple hardware timer with event</title><link>https://devzone.nordicsemi.com/thread/387551?ContentTypeID=1</link><pubDate>Thu, 22 Sep 2022 16:06:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:60408cd1-7ad4-4a28-b41e-f412e2b54bb1</guid><dc:creator>JamesDougherty</dc:creator><description>&lt;p&gt;In main.c change this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/* Timer for LED toogling */
const struct device *counter_dev;
//counter_dev = device_get_binding(TIMER); // &amp;lt;-- Remove this
counter_dev = DEVICE_DT_GET(TIMER);        // &amp;lt;-- Add this
if (counter_dev == NULL) {
	printk(&amp;quot;Device not found\n&amp;quot;);
	return;
}
counter_start(counter_dev);&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using simple hardware timer with event</title><link>https://devzone.nordicsemi.com/thread/387549?ContentTypeID=1</link><pubDate>Thu, 22 Sep 2022 15:57:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f7e4227c-034d-4f64-b67b-32415794a8f1</guid><dc:creator>witc</dc:creator><description>&lt;p&gt;Here it is,&amp;nbsp;&lt;a id="" href="http://leteckaposta.cz/683986250"&gt;http://leteckaposta.cz/683986250&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I really appreciate the help!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using simple hardware timer with event</title><link>https://devzone.nordicsemi.com/thread/387548?ContentTypeID=1</link><pubDate>Thu, 22 Sep 2022 15:51:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:23b2e161-a8dc-46e5-b554-401cd267400b</guid><dc:creator>JamesDougherty</dc:creator><description>&lt;p&gt;Are you able to upload it somewhere? I don&amp;#39;t mind taking a look at it and trying it on the nRF52840-DK I&amp;#39;m using.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using simple hardware timer with event</title><link>https://devzone.nordicsemi.com/thread/387547?ContentTypeID=1</link><pubDate>Thu, 22 Sep 2022 15:48:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9c0bcf7d-de65-47ef-ba01-8096ae76cfbb</guid><dc:creator>witc</dc:creator><description>&lt;p&gt;Just tried - it is the same &lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f61e.svg" title="Disappointed"&gt;&amp;#x1f61e;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using simple hardware timer with event</title><link>https://devzone.nordicsemi.com/thread/387545?ContentTypeID=1</link><pubDate>Thu, 22 Sep 2022 15:44:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:eaadffab-2f2e-4c2e-9404-8efbad34b0bd</guid><dc:creator>JamesDougherty</dc:creator><description>&lt;p&gt;You&amp;#39;re more than welcome. Did you do try doing a pristine build (CTRL+Shift+P &amp;gt; nRF Connect: Pristine Build)?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using simple hardware timer with event</title><link>https://devzone.nordicsemi.com/thread/387544?ContentTypeID=1</link><pubDate>Thu, 22 Sep 2022 15:19:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dc23d272-0313-43db-9e05-ce589b950c6e</guid><dc:creator>witc</dc:creator><description>&lt;p&gt;HI,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thank you really much for your work!! I tried to progress like you mentioned. I have little diferent step 3 - the error is this:&amp;nbsp;&lt;em&gt;lude\generated\devicetree_unfixed.h:6162:38: note: each undeclared identifier is reported only once for each function it appears in&lt;/em&gt;&lt;br /&gt;&lt;em&gt; 6162 | #define DT_N_NODELABEL_timer0 DT_N_S_soc_S_timer_40008000&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;So I tried also to add&amp;nbsp;&lt;em&gt;&amp;nbsp;CONFIG_COUNTER_TIMER0=y but it could not help me. Then I tried to find line 6162, and this is there - have no idea what is the problem:&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1663859353301v1.png" alt=" " /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Now I am stucked and have no idea what is wrong, also I found Kconfig and there when I look for timer it shows me something about timer - what is that? Could it help?&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1663859837347v2.png" alt=" " /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Thank you very much. I am used to for STM32 this is really something new to me.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using simple hardware timer with event</title><link>https://devzone.nordicsemi.com/thread/387508?ContentTypeID=1</link><pubDate>Thu, 22 Sep 2022 13:02:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3061e0cb-2d15-42f3-8be7-fa2d84adb2cc</guid><dc:creator>JamesDougherty</dc:creator><description>&lt;p&gt;Hello - I am too when it comes to firmware projects. I&amp;#39;m on my first firmware project and about 6 months in... It&amp;#39;s been a journey to say the least. DevZone, different samples and unit tests are great to learn from. There is still a lot you have to figure out on your own, which can make it aggravating (especially if you have never done firmware and are not familiar with everything like a seasoned pro would be).&lt;/p&gt;
&lt;p&gt;Anyway, are you using the nRF52840-DK? That&amp;#39;s what I started on so I can actually help you. I looked at the alarm example you mentioned and got it working doing the following:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;1) We need to find the node in the device tree to the timer we want to use. The easiest way is to see the compiled output of the device tree. You can do this by clicking the following icon in VS code:&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1663850268558v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;Then just search (CTRL+F) for &amp;quot;timer&amp;quot; and find the group of timers. In this case we can use the first one &amp;quot;timer0&amp;quot;.&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/compiled_2D00_timer.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;2) We have to reference this so we can use it. So in main.c change the following:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;
#define TIMER DT_NODELABEL(timer0) // nRF52840-DK Timer

// #if defined(CONFIG_BOARD_ATSAMD20_XPRO)
// #define TIMER DT_NODELABEL(tc4)
// #elif defined(CONFIG_SOC_FAMILY_SAM)
// #define TIMER DT_NODELABEL(tc0)
// #elif defined(CONFIG_COUNTER_MICROCHIP_MCP7940N)
// #define TIMER DT_NODELABEL(extrtc0)
// #elif defined(CONFIG_COUNTER_RTC0)
// #define TIMER DT_NODELABEL(rtc0)
// #elif defined(CONFIG_COUNTER_RTC_STM32)
// #define TIMER DT_INST(0, st_stm32_rtc)
// #elif defined(CONFIG_COUNTER_NATIVE_POSIX)
// #define TIMER DT_NODELABEL(counter0)
// #elif defined(CONFIG_COUNTER_XLNX_AXI_TIMER)
// #define TIMER DT_INST(0, xlnx_xps_timer_1_00_a)
// #elif defined(CONFIG_COUNTER_ESP32)
// #define TIMER DT_NODELABEL(timer0)
// #elif defined(CONFIG_COUNTER_MCUX_CTIMER)
// #define TIMER DT_NODELABEL(ctimer0)
// #endif&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;3) If you try to compile this you will get an error, along the lines of &amp;quot;main.c:109: undefined reference to `__device_dts_ord_109&amp;#39;&amp;quot;. These error have always confused me and I didn&amp;#39;t know where to look for the answers. One trick I learned is to search the entire project for the dts ord. So hit CTRL+Shift+F and search for &amp;quot;dts_ord_109&amp;quot;. If you click on the first result it will take you to the following:&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/dts109.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;If we look back at our compiled dts and search for that address (40008000) we can see that&amp;#39;s it&amp;#39;s not seeing our timer0 that we defined...&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/dts_2D00_timer.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;Why is this? Well whenever we see an error like this it always means we have forgot to configure something. The solution is to add the following (line 3) to the proj.conf file:&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/timer_2D00_config.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Once you make these few changes it will compile and run just fine.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;To answer your other question... The timer is already initialized since it&amp;#39;s a hardware timer. Calling the counter_start simply calls&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;nrf_timer_task_trigger(config-&amp;gt;timer, NRF_TIMER_TASK_START);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;on the backend.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;The original code has it set for 2 seconds so the alarm had plenty of time to be configured before the timer would fire. I see you changed it to 2000 us, which is 2 ms. I would suspect that the callback would never be fired in this case since the alarm wasn&amp;#39;t configured in time. The counter_start is also a one shot as well, which means it&amp;#39;s only going to happen one time. That&amp;#39;s the reason it gets reset in the callback. You can move the alarm configuration before you start the timer or increase the delay.&lt;/p&gt;
&lt;p&gt;So you can change it to this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;alarm_cfg.flags = 0;
alarm_cfg.ticks = counter_us_to_ticks(counter_dev, DELAY);
alarm_cfg.callback = test_counter_interrupt_fn;
alarm_cfg.user_data = &amp;amp;alarm_cfg;

err = counter_set_channel_alarm(counter_dev, ALARM_CHANNEL_ID,
				&amp;amp;alarm_cfg);

counter_start(counter_dev); // Move this here to ensure the alarm is setup&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using simple hardware timer with event</title><link>https://devzone.nordicsemi.com/thread/387389?ContentTypeID=1</link><pubDate>Thu, 22 Sep 2022 06:30:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0aa64953-0d07-4053-89ad-025fdae6d205</guid><dc:creator>witc</dc:creator><description>&lt;p&gt;Thank you,&amp;nbsp;&lt;/p&gt;
&lt;div&gt;&lt;span&gt;I am a novice in the Nordic world. I have gone throught NRF Dev academy, but there is still a lot of unknow. E.G. now I am struggling how to use a peripheral like Timer. I want to call IRQ function every 1 seconds with the hardware timer (in datasheet is written it has 5 Timers). How should I do that? I have found example Alarm, but it can not help me much.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;I tried to modify the example Alarm to find out how to use counter API.&lt;/div&gt;
&lt;p&gt;1) In the example is Timer defined according to MCU like that: Unfortunatelly my nrf52840 is not in that list - how can I bind it with my mcu?&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;pre class="ui-code" data-mode="text"&gt;#if defined(CONFIG_BOARD_ATSAMD20_XPRO)
#define TIMER DT_LABEL(DT_NODELABEL(tc4))
#elif defined(CONFIG_SOC_FAMILY_SAM)
#define TIMER DT_LABEL(DT_NODELABEL(tc0))
#elif defined(CONFIG_COUNTER_MICROCHIP_MCP7940N)
#define TIMER DT_LABEL(DT_NODELABEL(extrtc0))
#elif defined(CONFIG_COUNTER_RTC0)
#define TIMER DT_LABEL(DT_NODELABEL(rtc0))
#elif defined(CONFIG_COUNTER_RTC_STM32)
#define TIMER DT_LABEL(DT_INST(0, st_stm32_rtc))&lt;/pre&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;2) In example is called&amp;nbsp;&lt;pre class="ui-code" data-mode="text"&gt;counter_start&lt;/pre&gt;&amp;nbsp;before it has been initialized - why that?&lt;/p&gt;
&lt;p&gt;Finally there is some timer settings&amp;nbsp; and running the channel:&amp;nbsp;&lt;pre class="ui-code" data-mode="text"&gt;alarm_cfg.flags = 0;
alarm_cfg.ticks = counter_us_to_ticks(counter_dev, 2000);
alarm_cfg.callback = timerCallback;
alarm_cfg.user_data = &amp;amp;alarm_cfg;

err = counter_set_channel_alarm(counter_dev, 0,	&amp;amp;alarm_cfg);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;Thank you fo&amp;nbsp;the clarification. Jan&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div id="gtx-anchor" style="height:13.3565px;left:84.6678px;position:absolute;top:521.009px;width:15.0035px;"&gt;&lt;/div&gt;
&lt;div class="jfk-bubble gtx-bubble" style="left:77px;top:343px;"&gt;
&lt;div id="bubble-8" class="jfk-bubble-content-id"&gt;
&lt;div id="gtx-host" style="max-width:400px;"&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="jfk-bubble-closebtn-id jfk-bubble-closebtn"&gt;&lt;/div&gt;
&lt;div class="jfk-bubble-arrow-id jfk-bubble-arrow jfk-bubble-arrowdown" style="left:84.1748px;"&gt;
&lt;div class="jfk-bubble-arrowimplbefore"&gt;&lt;/div&gt;
&lt;div class="jfk-bubble-arrowimplafter"&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using simple hardware timer with event</title><link>https://devzone.nordicsemi.com/thread/387360?ContentTypeID=1</link><pubDate>Wed, 21 Sep 2022 18:11:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5d930823-695d-4333-915d-40ae9d65ecd6</guid><dc:creator>JamesDougherty</dc:creator><description>&lt;p&gt;Hello witc - See if this helps&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/91996/counter-timer-or-rtc-sample-using-nrf52"&gt;https://devzone.nordicsemi.com/f/nordic-q-a/91996/counter-timer-or-rtc-sample-using-nrf52&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>