<?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>High-frequency GPIO</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/68095/high-frequency-gpio</link><description>Hello, all. 
 
 I am working on a project in which the ultimate goal is to read an 8-bit signal with 10kHz frequency. 
 To test if this would be possible, I tried to generate a digital signal using a timer. The timer is configured as below: 
 
 
 And</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 11 Nov 2020 11:04:59 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/68095/high-frequency-gpio" /><item><title>RE: High-frequency GPIO</title><link>https://devzone.nordicsemi.com/thread/279461?ContentTypeID=1</link><pubDate>Wed, 11 Nov 2020 11:04:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5c263e67-da51-4cea-aa64-1ff7f16819aa</guid><dc:creator>Thiago</dc:creator><description>&lt;p&gt;It works fine now. Thank you very much for your help.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: High-frequency GPIO</title><link>https://devzone.nordicsemi.com/thread/279425?ContentTypeID=1</link><pubDate>Wed, 11 Nov 2020 08:52:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a58bdb70-3516-4409-8606-938385ba34a5</guid><dc:creator>Dmitry</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;you have set&amp;nbsp;TIMER_INSTANCE-&amp;gt;CC[0] = 1, so CPU spends all the time in&amp;nbsp;TIMER2_IRQHandler, not leaving any time for low-priority processes.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: High-frequency GPIO</title><link>https://devzone.nordicsemi.com/thread/279413?ContentTypeID=1</link><pubDate>Wed, 11 Nov 2020 08:19:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f912559c-8db6-40fc-b01c-0128bbcb0b10</guid><dc:creator>Thiago</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;After following the suggestions above:&lt;/p&gt;
[quote userid="5418" url="~/f/nordic-q-a/68095/high-frequency-gpio/278899#278899"] I think&amp;nbsp;sprintf() and&amp;nbsp;SEGGER_RTT_WriteString() eat most of these 57 microseconds, try to comment them out. To achieve maximum frequency, use HAL module functions (nrf_timer.h) instead of nrf_drv_timer[/quote]
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I got an improvement in the frequency of almost 18 times as seen in this image:&lt;/p&gt;
&lt;p&gt;+&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/1040x1200/__key/communityserver-discussions-components-files/4/tek00011.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I used this code to initialize the clock and for the Interrupt service routine:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#define TIMER_INSTANCE NRF_TIMER2
 /*---------------------------------------------------------------------------*/
 void
 timer2_init(void)
 {
    TIMER_INSTANCE-&amp;gt;PRESCALER = 0;

    TIMER_INSTANCE-&amp;gt;TASKS_STOP = 1;	/* Stop timer */
    TIMER_INSTANCE-&amp;gt;MODE = TIMER_MODE_MODE_Timer;  /* taken from Nordic dev zone */
    TIMER_INSTANCE-&amp;gt;BITMODE = TIMER_BITMODE_BITMODE_16Bit;
    TIMER_INSTANCE-&amp;gt;CC[0] = 1;
    TIMER_INSTANCE-&amp;gt;TASKS_CLEAR = 1; /* Clear timer */
    TIMER_INSTANCE-&amp;gt;INTENSET = TIMER_INTENSET_COMPARE0_Enabled &amp;lt;&amp;lt; TIMER_INTENSET_COMPARE0_Pos;
    TIMER_INSTANCE-&amp;gt;SHORTS = (TIMER_SHORTS_COMPARE0_CLEAR_Enabled &amp;lt;&amp;lt; TIMER_SHORTS_COMPARE0_CLEAR_Pos);
    
    NVIC_ClearPendingIRQ(TIMER2_IRQn);
    NVIC_SetPriority(TIMER2_IRQn, 6);
    NVIC_EnableIRQ(TIMER2_IRQn);

    

    TIMER_INSTANCE-&amp;gt;TASKS_START = 1;
 }


 void
 TIMER2_IRQHandler(void)
 {
     if (TIMER_INSTANCE-&amp;gt;EVENTS_COMPARE[0] != 0)
    {
    	TIMER_INSTANCE-&amp;gt;EVENTS_COMPARE[0] = 0;

        if(currentSample &amp;lt; 10){
            GPIO0_OUT_R &amp;amp;= ~(0xFF&amp;lt;&amp;lt;18);   
            GPIO0_OUT_R |= ((SineWave[currentSample])&amp;lt;&amp;lt;18);   
            //sprintf(GPIO_Out_current_Value, &amp;quot;GPIO0_OUT register current value: %d\n&amp;quot;, (GPIO0_OUT_R&amp;gt;&amp;gt;18)&amp;amp;0xFF);
            //SEGGER_RTT_WriteString(0, GPIO_Out_current_Value);
            currentSample++;
        }
        else{
            currentSample = 0;
        }

    	TIMER_INSTANCE-&amp;gt;SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Msk;
    }
 }&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;The frequency improvement is great, &lt;strong&gt;but&amp;nbsp;the module is not advertising anymore.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If I don&amp;#39;t make a call for the timer2_init() function in the main(), it advertises, but not otherwise.&lt;/p&gt;
&lt;p&gt;I wrote the code based on the &lt;strong&gt;ble_app_uart&lt;/strong&gt; example. I changed nothing in the BLE configuration.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;The main function is looking like this now:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;/**@brief Application main function.
 */
int main(void)
 {
    bool erase_bonds;   

    //NRF_CLOCK-&amp;gt;TASKS_HFCLKSTART = 1;

    // Initialize.
    uart_init();
    log_init();
    timers_init();    
    leds_init(&amp;amp;erase_bonds);
    gpio_init();
    power_management_init();
    ble_stack_init();
    conn_evt_ext_enable();
    data_len_set(DATA_LENGTH_MAX);
    gap_params_init();
    gatt_init();
    services_init();
    timer2_init();
    //digital_signal_event_init();

    //saadc_init();
    //saadc_sampling_event_init();
    

    advertising_init();
    conn_params_init();

    // Start execution.
    printf(&amp;quot;\r\nUART started.\r\n&amp;quot;);
    NRF_LOG_INFO(&amp;quot;Debug logging for UART over RTT started.&amp;quot;);
    SEGGER_RTT_WriteString(0, &amp;quot;Debug logging for UART over RTT started.\n&amp;quot;);


    advertising_start();

    //saadc_sampling_event_enable();



    // Enter main loop.
    for (;;)
    {
        idle_state_handle();
              
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Would anyone have a clue why the advertising stops or what could I do to find out how to solve it?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: High-frequency GPIO</title><link>https://devzone.nordicsemi.com/thread/278930?ContentTypeID=1</link><pubDate>Mon, 09 Nov 2020 08:28:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1ed8b168-0bdc-4f66-98ba-1bfa875a35bb</guid><dc:creator>Thiago</dc:creator><description>&lt;p&gt;Thank you.&lt;/p&gt;
&lt;p&gt;I&amp;#39;ll check the reference you provided.&lt;/p&gt;
[quote userid="5418" url="~/f/nordic-q-a/68095/high-frequency-gpio/278927#278927"]If you&amp;#39;re using BLE stack, you&amp;#39;ll need to configure other timer instance because TIMER0 is occupied by SoftDevice.[/quote]
&lt;p&gt;Oh! I noticed that before but I didn&amp;#39;t know why it happened. Thank you!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: High-frequency GPIO</title><link>https://devzone.nordicsemi.com/thread/278927?ContentTypeID=1</link><pubDate>Mon, 09 Nov 2020 07:54:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4c7dab9d-c918-48c2-8191-06e3f06b9868</guid><dc:creator>Dmitry</dc:creator><description>&lt;p&gt;You can look at source code of&amp;nbsp;&lt;a href="https://contiki-ng.readthedocs.io/en/develop/_api/cpu_2nrf52840_2rtimer-arch_8c_source.html"&gt;Contiki-NG timer module&lt;/a&gt;.&amp;nbsp;If you&amp;#39;re using BLE stack, you&amp;#39;ll need to configure other timer instance because TIMER0 is occupied by SoftDevice.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: High-frequency GPIO</title><link>https://devzone.nordicsemi.com/thread/278918?ContentTypeID=1</link><pubDate>Mon, 09 Nov 2020 05:27:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:837059bb-1d0a-4691-9ed0-49166e66e4ef</guid><dc:creator>Thiago</dc:creator><description>&lt;p&gt;Hello.&lt;/p&gt;
&lt;p&gt;Thank you for your suggestions.&lt;/p&gt;
&lt;p&gt;Just by removing the debugging functions I already noticed a 5x improvement in the frequency. I wrote the code based on the examples found in the nRF5_SDK_16. There, they use the functions in the nrf_drv_timer for timer initialization. Would you know where I could find examples of timer initialization using the functions in the nrf_timer.h?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: High-frequency GPIO</title><link>https://devzone.nordicsemi.com/thread/278899?ContentTypeID=1</link><pubDate>Sun, 08 Nov 2020 20:37:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6c7a91e2-a050-4718-8a3b-f0224376adec</guid><dc:creator>Dmitry</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;as I understood from oscilloscope screenshot, you have a 57-us interval between GPIO switching. I think&amp;nbsp;sprintf() and&amp;nbsp;SEGGER_RTT_WriteString() eat most of these 57 microseconds, try to comment them out. To achieve maximum frequency, use HAL module functions (nrf_timer.h) instead of nrf_drv_timer, and write an irq handler (as short as possible) by yourself.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>