<?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 to read SAADC with PPI but never SLEEP OFF</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/66799/rtc-to-read-saadc-with-ppi-but-never-sleep-off</link><description>I am using NRF52832 for BLE application with softdevice 132 under SDK15.3. My application is supposed to sleep off after certain amount of time when there is no user activity and using ppi to read SAADC (pressure sensor) with 20 ms interval. So if SAADC</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 18 Nov 2020 22:39:04 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/66799/rtc-to-read-saadc-with-ppi-but-never-sleep-off" /><item><title>RE: RTC to read SAADC with PPI but never SLEEP OFF</title><link>https://devzone.nordicsemi.com/thread/280715?ContentTypeID=1</link><pubDate>Wed, 18 Nov 2020 22:39:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dbe175c0-b6f1-45e1-8c38-9f408d8e3a5a</guid><dc:creator>jalan</dc:creator><description>&lt;p&gt;Yes. Absolutely.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: RTC to read SAADC with PPI but never SLEEP OFF</title><link>https://devzone.nordicsemi.com/thread/273651?ContentTypeID=1</link><pubDate>Thu, 08 Oct 2020 08:09:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:74959e0a-bf58-47d5-b9d7-ad2b38b41ac3</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Another option then is to create the sleep timer using app_timer.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: RTC to read SAADC with PPI but never SLEEP OFF</title><link>https://devzone.nordicsemi.com/thread/273596?ContentTypeID=1</link><pubDate>Wed, 07 Oct 2020 22:54:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a590e015-eee0-40a6-bef7-21d85841437b</guid><dc:creator>jalan</dc:creator><description>&lt;p&gt;Thanks for your reply. As I am using rtc0 for softdevice and rtc 1 for app timer I have no choice to use the counter way. It works well.&lt;/p&gt;
&lt;p&gt;Here is the final code!&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#define POWER_OF_PRESCALER                4                       //0..12
#define PRESCALER                         (1&amp;lt;&amp;lt;POWER_OF_PRESCALER)-1
#define RTC_FREQ                          32768/PRESCALER   

#define NRFX_RTC_MS_TO_TICKS(ms, rtc_freq_hz)    (((ms) * (rtc_freq_hz)) / 1000U) 
#define NRFX_RTC_SEC_TO_TICKS(sec, rtc_freq_hz)  ((sec) * (rtc_freq_hz)) 
#define NRFX_RTC_MIN_TO_TICKS(min, rtc_freq_hz)  ((min) * (rtc_freq_hz) * 60) 

#define SLEEP_OFF_TIMER_MINUTES           60 //minutes to sleep off when no user activity
#define SLEEP_OFF_TICKS                   NRFX_RTC_MIN_TO_TICKS(SLEEP_OFF_TIMER_MINUTES, RTC_FREQ)

#define PS_CAPTURE_INTERVAL_MS            20  //ms for pressure seonsor capture interval
#define PS_TICKS                          NRFX_RTC_MS_TO_TICKS(PS_CAPTURE_INTERVAL_MS, RTC_FREQ)

static uint32_t sleep_cnt = 0;

/** @brief: Function for handling the RTC0 interrupts.
 * Triggered on TICK and COMPARE0 match.
 */
static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
{
    uint32_t err_code;

    if (int_type == NRFX_RTC_INT_COMPARE0) //for test ps interval only
    {
        //workaround for recurring COMPARE[x] interrupt is diabled in nrf_drv_rtc_init_handler
        err_code = nrf_drv_rtc_cc_set(&amp;amp;rtc, 0, PS_TICKS, true);
        APP_ERROR_CHECK(err_code); 

        nrf_gpio_pin_toggle(LED_3); //Test only
        sleep_cnt++;
        //printf(&amp;quot;sleep count: %d\r\n&amp;quot;, sleep_cnt);
        
    }

    if (sleep_cnt &amp;gt;= SLEEP_OFF_TICKS/PS_TICKS) 
    {
        nrf_gpio_pin_set(LED_3); //Test only
        sleep_mode_enter();
    }

}


/** @brief Function initialization and configuration of RTC driver instance.
 */
static void rtc_config(void)
{
    uint32_t err_code;

    //Initialize RTC instance
    nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
    config.prescaler = PRESCALER;
    err_code = nrf_drv_rtc_init(&amp;amp;rtc, &amp;amp;config, rtc_handler);
    APP_ERROR_CHECK(err_code);

    //Set compare channel 0 to trigger interrupt for pressure sensor interval for ppi
    err_code = nrf_drv_rtc_cc_set(&amp;amp;rtc, 0, PS_TICKS, true);
    APP_ERROR_CHECK(err_code); 

}

void timer_with_ppi_init(void)
{
    ret_code_t err_code; 

    // Initialize the PPI 
    err_code = nrf_drv_ppi_init();
    APP_ERROR_CHECK(err_code); 

    rtc_config();


    //Enable on RTC instance
    nrf_drv_rtc_enable(&amp;amp;rtc);

    // Save the address of compare event so that it can be connected to ppi module
    uint32_t rtc_compare_event_addr = nrf_drv_rtc_event_address_get(&amp;amp;rtc, NRF_RTC_EVENT_COMPARE_0);
    uint32_t rtc_clear_task_addr = nrf_drv_rtc_task_address_get(&amp;amp;rtc, NRF_RTC_TASK_CLEAR);

    // Save the task address to a variable so that it can be connected to ppi module for automatic triggering
    uint32_t saadc_sample_task_addr = nrf_drv_saadc_sample_task_get();

    // Allocate the ppi channel by passing it the struct created at the start
    err_code = nrf_drv_ppi_channel_alloc(&amp;amp;m_ppi_channel);
    APP_ERROR_CHECK(err_code); // check for errors
	
	
    // attach the addresses to the allocated ppi channel so that its ready to trigger tasks on events
    err_code = nrf_drv_ppi_channel_assign(m_ppi_channel, rtc_compare_event_addr, saadc_sample_task_addr);
    APP_ERROR_CHECK(err_code); // check for errors

    err_code = nrf_drv_ppi_channel_fork_assign(m_ppi_channel, rtc_clear_task_addr);
    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: RTC to read SAADC with PPI but never SLEEP OFF</title><link>https://devzone.nordicsemi.com/thread/273485?ContentTypeID=1</link><pubDate>Wed, 07 Oct 2020 12:17:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:33d806c2-f8f3-4b9f-8488-1fa6262629d8</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;You are clearing the RTC by connecting the CLEAR task to the COMPARE_1 event in your ppi_init() function. If you remove the following lines, the timer&amp;nbsp;will not be cleared and you should receive the COMPARE_0 event after 60 minutes:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;err_code = nrf_drv_ppi_channel_fork_assign(m_ppi_channel, rtc_clear_task_addr);
APP_ERROR_CHECK(err_code);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Note that if you do not clear the timer, the SAADC sample task will not be triggered a second time until the RTC has overflowed, which may take a long time depending your&amp;nbsp;PRESCALER value. You may have to use a second RTC for entering sleep, or use some other method to determine when to go to sleep. One options could be to count the SAADC sample compare events and enter system OFF when you have counted events corresponding to 60 minutes.&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>