<?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>RTC usage on Zephyr Issues</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/96102/rtc-usage-on-zephyr-issues</link><description>Hello, 
 I&amp;#39;m trying to run a simple example setting the RTC1 in a nRF52840-DK, but I&amp;#39;m having some issues. 
 Here is my prj,conf and code: 
 
 
 The idea above is set the prescaler to 1 second and fire at every 2 seconds. 
 The RTC handler is simply not</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 27 Jan 2023 15:45:09 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/96102/rtc-usage-on-zephyr-issues" /><item><title>RE: RTC usage on Zephyr Issues</title><link>https://devzone.nordicsemi.com/thread/406908?ContentTypeID=1</link><pubDate>Fri, 27 Jan 2023 15:45:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:71ee3cc4-dfe9-4e4e-8b4b-512ef2d34991</guid><dc:creator>Joao Dullius</dc:creator><description>&lt;p&gt;Awesome, thanks!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: RTC usage on Zephyr Issues</title><link>https://devzone.nordicsemi.com/thread/406907?ContentTypeID=1</link><pubDate>Fri, 27 Jan 2023 11:15:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d1e1d29d-6247-4651-9b53-4b32dd384282</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;The RTC prescaler can take a value between 0 and 4095, this means that the minimum frequency of the RTC is 32768/4096=8. If you set&amp;nbsp;RTC_PRESCALER_MS to 500, your input to&amp;nbsp;RTC_FREQ_TO_PRESCALER() will be 2, which is below the minimum frequency. The prescaler will then likely be set to the maximum value of 4095, which map well to the 4x ratio you are seeing on the interrupts.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: RTC usage on Zephyr Issues</title><link>https://devzone.nordicsemi.com/thread/406906?ContentTypeID=1</link><pubDate>Fri, 27 Jan 2023 11:03:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:98cdb4b8-b701-45f9-9b4b-cf8665e01c5d</guid><dc:creator>Joao Dullius</dc:creator><description>&lt;p&gt;I was able to get it running with the following code:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;zephyr/kernel.h&amp;gt;
#include &amp;lt;zephyr/drivers/gpio.h&amp;gt;
#include &amp;lt;nrfx_rtc.h&amp;gt;
#include &amp;lt;nrfx_timer.h&amp;gt;

#define RTC_INSTANCE 2
#define CC_CHANNEL 0
#define RTC_TIMEOUT_MS 5000
#define RTC_PRESCALER_MS 125

static nrfx_rtc_t rtc = NRFX_RTC_INSTANCE(RTC_INSTANCE);
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(DT_ALIAS(led1), gpios);

void rtc_event_handler(nrfx_rtc_int_type_t int_type)
{
	switch(int_type) {
		case NRFX_RTC_INT_COMPARE0:
			printk(&amp;quot;RTC compare event occurred\n&amp;quot;);
			gpio_pin_toggle_dt(&amp;amp;led);
			nrfx_rtc_counter_clear(&amp;amp;rtc);
        	nrfx_rtc_cc_set(&amp;amp;rtc, CC_CHANNEL, RTC_TIMEOUT_MS / RTC_PRESCALER_MS, true);
			break;
		default:
			break;
	}
}

void main(void)
{
    printk(&amp;quot;Hello World!\n&amp;quot;);

    nrfx_rtc_config_t config = NRFX_RTC_DEFAULT_CONFIG;
	config.prescaler = RTC_FREQ_TO_PRESCALER(1000 / RTC_PRESCALER_MS);
	
    nrfx_rtc_init(&amp;amp;rtc, &amp;amp;config, rtc_event_handler);

	IRQ_CONNECT(DT_IRQN(DT_NODELABEL(rtc2)),
			DT_IRQ(DT_NODELABEL(rtc2), priority),
			nrfx_isr, nrfx_rtc_2_irq_handler, 0);
	irq_enable(DT_IRQN(DT_NODELABEL(rtc2)));			

	if (!device_is_ready(led.port)) {
        return;
    }

    if (gpio_pin_configure_dt(&amp;amp;led, GPIO_OUTPUT_ACTIVE) &amp;lt; 0) {
        return;
    }

 	nrfx_rtc_tick_disable(&amp;amp;rtc);
    nrfx_rtc_counter_clear(&amp;amp;rtc);
    nrfx_rtc_enable(&amp;amp;rtc);
    nrfx_rtc_cc_set(&amp;amp;rtc, CC_CHANNEL, RTC_TIMEOUT_MS / RTC_PRESCALER_MS, true);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;My followup question here is the following:&lt;/p&gt;
&lt;p&gt;I&amp;#39;m toggling a led based on the RTC alarm.&lt;/p&gt;
&lt;p&gt;If I set my defines as:&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#define RTC_TIMEOUT_MS 5000
#define RTC_PRESCALER_MS 125&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;And set my prescaler and cc timer as:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;config.prescaler = RTC_FREQ_TO_PRESCALER(1000 / RTC_PRESCALER_MS);

nrfx_rtc_cc_set(&amp;amp;rtc, CC_CHANNEL, RTC_TIMEOUT_MS / RTC_PRESCALER_MS, true);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I get the expected toggle ratio defined at RTC_TIMEOUT_MS (5 seconds).&lt;/p&gt;
&lt;p&gt;If I change the&amp;nbsp;RTC_PRESCALER_MS to a larger value, like 500 ms, I get my toogle ratio 4x faster (1.25 seconds), and not the same as I expected.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: RTC usage on Zephyr Issues</title><link>https://devzone.nordicsemi.com/thread/406905?ContentTypeID=1</link><pubDate>Fri, 27 Jan 2023 10:06:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9bd131d7-82f6-4af7-bc87-b7bc877ef887</guid><dc:creator>Joao Dullius</dc:creator><description>&lt;p&gt;Hi Jorgen,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thanks for the quick reply.&lt;/p&gt;
&lt;p&gt;With your MACRO I was able to get interrupts, but only for&amp;nbsp;&lt;span&gt;&lt;span&gt;NRFX_RTC_INT_TICK and not&amp;nbsp;&lt;/span&gt;&lt;/span&gt;NRFX_RTC_INT_COMPARE0.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: RTC usage on Zephyr Issues</title><link>https://devzone.nordicsemi.com/thread/406904?ContentTypeID=1</link><pubDate>Fri, 27 Jan 2023 09:09:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7ad5a11e-e747-452c-8fa3-c296c4290b23</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;You need to connect the interrupt from &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.2.0/zephyr/kernel/services/interrupts.html#c.IRQ_CONNECT"&gt;Zephyr&amp;#39;s interrupt handling&lt;/a&gt; to the nrfx_rtc driver in your application for the interrupts to work correctly. Eg.:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;IRQ_CONNECT(DT_IRQN(DT_NODELABEL(rtc1)),
DT_IRQ(DT_NODELABEL(rtc1), priority),
nrfx_isr, nrfx_rtc_1_irq_handler, 0);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Jørgen&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>