<?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>RNG interrupt handler not called</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/47393/rng-interrupt-handler-not-called</link><description>RNG interrupt handler is not called. 
 
 Using nrfx_rng library to generate random numbers. 
 
 VALRDY event is generated but interrupt handler nrfx_rng_irq_handler in library is not called. 
 Breakpoint not hit. 
 
 Despite EVENTS_VALRDY being set. </description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Sun, 16 May 2021 15:24:59 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/47393/rng-interrupt-handler-not-called" /><item><title>RE: RNG interrupt handler not called</title><link>https://devzone.nordicsemi.com/thread/309905?ContentTypeID=1</link><pubDate>Sun, 16 May 2021 15:24:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:42e8b5c9-39b5-4e68-b0b1-783b8246d711</guid><dc:creator>akofoed</dc:creator><description>&lt;p&gt;Thank you very much - it worked.&lt;/p&gt;
&lt;p&gt;I must have slept during C-class when volatile was the subject.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: RNG interrupt handler not called</title><link>https://devzone.nordicsemi.com/thread/309903?ContentTypeID=1</link><pubDate>Sun, 16 May 2021 14:32:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8df82da3-cd4c-44a7-b15f-3e7eb68a65a4</guid><dc:creator>Dmitry</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;the compiler optimizes (throws away) your busy-wait loop because it has no idea that&amp;nbsp;random_number_index may change outside of this loop. Try to define&amp;nbsp;&lt;span&gt;random_number_index&amp;nbsp;as volatile.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: RNG interrupt handler not called</title><link>https://devzone.nordicsemi.com/thread/309897?ContentTypeID=1</link><pubDate>Sun, 16 May 2021 10:35:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:328a39d7-40b4-445b-b748-75e4e9e9831b</guid><dc:creator>akofoed</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I seem to have a similar issue with the RNG. I am not using the SoftDevice but it seems that the RNG does not run the interrupt function no matter what priotity I give it.&lt;/p&gt;
&lt;p&gt;The issue is when waiting for the RNG to generate the correct number of numbers.&lt;/p&gt;
&lt;p&gt;Using &lt;span style="font-family:courier new, courier;"&gt;while(random_number_index &amp;lt; len) {};&lt;/span&gt; does not work, while &lt;span style="font-family:courier new, courier;"&gt;nrf_delay_ms(1);&lt;/span&gt; does!&lt;/p&gt;
&lt;p&gt;NRFX:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static int random_number_index;
static uint8_t *random_number_buf_ptr;
static int random_number_max_len;

void rng_event_handler(uint8_t rng_data)
{
    if (random_number_index &amp;lt; random_number_max_len)
    {
        random_number_buf_ptr[random_number_index] = rng_data;
        random_number_index++;
    }
}

int mcu_rng_generate(int len, uint8_t *rng_buf)
{
    // Reset random number counter
    random_number_index = 0;

    // Setup safety so that RNG does not fill up buffer beyound len
    random_number_max_len = len;
    random_number_buf_ptr = rng_buf;

    // Start random number generator
    nrfx_rng_start();

    // Wait for required number of random numbers to be generated
    // nrf_delay_ms(1); // &amp;lt;-- THIS WORKS
    while(random_number_index &amp;lt; len) {}; // &amp;lt;-- THIS DOES NOT WORK
    // NRF_LOG_INFO(&amp;quot;%d &amp;lt; %d&amp;quot;, random_number_index, len);

    // Stop random number generator
    nrfx_rng_stop();

    return NRF_SUCCESS;
}

// Random number generator
int mcu_rng_init()
{
    // Initialise random number generator
    nrfx_rng_config_t config = NRFX_RNG_DEFAULT_CONFIG;
    return nrfx_rng_init(&amp;amp;config, rng_event_handler);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Same thing when using the HW registers directly:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;int mcu_rng_generate(int len, uint8_t *rng_buf)
{
    // Reset random number counter
    random_number_index = 0;

    // Setup safety so that RNG does not fill up buffer beyound len
    random_number_max_len = len;
    random_number_buf_ptr = rng_buf;

    /* Configure for bias correction. */
    NRF_RNG-&amp;gt;CONFIG = RNG_CONFIG_DERCEN_Enabled &amp;lt;&amp;lt; RNG_CONFIG_DERCEN_Pos;

    /* Enable value ready interrupt. */
    NRFX_IRQ_PRIORITY_SET(RNG_IRQn, 6);
    NRFX_IRQ_ENABLE(RNG_IRQn);

    NRF_RNG-&amp;gt;INTENSET = RNG_INTENSET_VALRDY_Enabled &amp;lt;&amp;lt; RNG_INTENSET_VALRDY_Pos;

    /* Start random number generation. */
    NRF_RNG-&amp;gt;TASKS_START = 1U;

    /* Wait for required number of random numbers to be generated. */
    // nrf_delay_ms(1); // &amp;lt;-- THIS WORKS
    while(random_number_index &amp;lt; len); &amp;lt;-- THIS DOES NOT WORK!

    /* Stop random number generator. */
    NRF_RNG-&amp;gt;TASKS_STOP = 1U;

    return NRF_SUCCESS;
}


void RNG_IRQHandler(void)
{
    // Clear event
    NRF_RNG-&amp;gt;EVENTS_VALRDY = 0U;
    if (random_number_index &amp;lt; random_number_max_len)
    {
        random_number_buf_ptr[random_number_index] = NRF_RNG-&amp;gt;VALUE;
        random_number_index++;
    }
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I have tried setting the NRFX_IRQ_PRIORITY_SET(RNG_IRQn, 6); to different things (0 and 12) but no difference.&lt;/p&gt;
&lt;p&gt;Can anybody help explaining what nrf_delay_ms(1); does differently than the busy-waiting version?&lt;/p&gt;
&lt;p&gt;Kind regards&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: RNG interrupt handler not called</title><link>https://devzone.nordicsemi.com/thread/188886?ContentTypeID=1</link><pubDate>Thu, 23 May 2019 14:58:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4937867d-3064-4fe3-92b1-441bae82abd3</guid><dc:creator>David Anthony</dc:creator><description>&lt;p&gt;In the end, as this is a relatively simple peripheral, I decided to access the hardware directly. Works fine.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void generateRandomNumbers(uint8_t * p_random_numbers)
{
   assert(p_random_numbers != NULL);

   random_number_pointer = p_random_numbers;

   /* Reset random number counter. */
   random_number_index = 0U;

   /* Initialise random number generator. */

   /* Configure for bias correction. */
   NRF_RNG-&amp;gt;CONFIG = RNG_CONFIG_DERCEN_Enabled &amp;lt;&amp;lt; RNG_CONFIG_DERCEN_Pos;

   /* Enable value ready interrupt. */
   sd_nvic_EnableIRQ(RNG_IRQn);

   NRF_RNG-&amp;gt;INTENSET = RNG_INTENSET_VALRDY_Enabled &amp;lt;&amp;lt; RNG_INTENSET_VALRDY_Pos;

   /* Start random number generation. */
   NRF_RNG-&amp;gt;TASKS_START = 1U;

   /* Wait for required number of random numbers to be generated. */
   while(random_number_index &amp;lt; CHALLENGE_LEN);

   /* Stop random number generator. */
   NRF_RNG-&amp;gt;TASKS_STOP = 1U;
}


void RNG_IRQHandler(void)
{
   /* Random value ready. */

   /* Clear event. */
   NRF_RNG-&amp;gt;EVENTS_VALRDY = 0U;

   random_number_pointer[random_number_index] = NRF_RNG-&amp;gt;VALUE;

   /* Move on to next location. */
   random_number_index++;
}
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: RNG interrupt handler not called</title><link>https://devzone.nordicsemi.com/thread/188006?ContentTypeID=1</link><pubDate>Mon, 20 May 2019 14:39:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f0f55bf1-8535-4e83-948b-a76ae771ccb0</guid><dc:creator>David Anthony</dc:creator><description>&lt;p&gt;Thanks. OK will look at that.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: RNG interrupt handler not called</title><link>https://devzone.nordicsemi.com/thread/187883?ContentTypeID=1</link><pubDate>Mon, 20 May 2019 11:46:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5879baea-5438-47d5-825e-ddc6b6bbad3f</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;I assume&amp;nbsp;\nRF5_SDK_15.3.0_59ac345\examples\peripheral\rng\ can be used as reference, just make sure that you define&amp;nbsp;SOFTDEVICE_PRESENT if used with the softdevice.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Kenneth&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>