<?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 Optimisation in nrf51 Keil</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/27043/using-optimisation-in-nrf51-keil</link><description>Hi,
I have the following piece of code. 
 nrf_gpio_cfg_output(PIN_EN_BSTR); 
 for(int i=0;i&amp;lt;300;i++)

{

	nrf_gpio_pin_clear(PIN_EN_BSTR);

	__NOP();

	__NOP();

	__NOP();

	__NOP();

	__NOP();

	__NOP();

	__NOP();

	__NOP();</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 11 Oct 2016 13:02:21 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/27043/using-optimisation-in-nrf51-keil" /><item><title>RE: Using Optimisation in nrf51 Keil</title><link>https://devzone.nordicsemi.com/thread/106193?ContentTypeID=1</link><pubDate>Tue, 11 Oct 2016 13:02:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:deefbe35-ed1e-4804-985c-10bc7adb7c03</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Then use PWM library provided by the SDK instead of creating your own delays. PWM library will create same  delays and will be same of all optimizations and with or without softdevice.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using Optimisation in nrf51 Keil</title><link>https://devzone.nordicsemi.com/thread/106194?ContentTypeID=1</link><pubDate>Mon, 10 Oct 2016 02:54:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9d8980ed-f293-4c89-a44a-e754ee71ee79</guid><dc:creator>nordicdev</dc:creator><description>&lt;p&gt;Thanks Aryan. Bascally I am looking for a code where I can do PWM of a gpio pin with on period (1usec) and off period as 100usec : The main point would be the on and off time of PWM should be the same for all optimisation levels, O0 to O3. Thanks&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using Optimisation in nrf51 Keil</title><link>https://devzone.nordicsemi.com/thread/106192?ContentTypeID=1</link><pubDate>Wed, 21 Sep 2016 07:08:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:165bf1fe-b81a-4207-929e-af19486f71d9</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;I tested it myself on nRF51&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;int main(void)
{
nrf_gpio_cfg_output(11);

    while(1)

    {

        nrf_gpio_pin_clear(11);
        nrf_gpio_pin_set(11);

    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;with -O0 it is 0.36us&lt;br /&gt;
with -O3 it is 0.12us&lt;/p&gt;
&lt;p&gt;considering 1 cpu cycle is 0.062us I think it is doing excellent job with -O3 only two cycles and with -O0 it is taking 6 cpu cycles which is reasonable because of many debug NOPS and unoptimized reads and writes.&lt;/p&gt;
&lt;p&gt;inside the nrf_delay_us you also have C logic to to compare if number_of_us is positive or not, this compare logic will generate different type of code with different optimization level and will run in a loop to the number of us you give in the argument. I am not sure if we can create a software emulated delay accurate to nano seconds at different optimizations.&lt;/p&gt;
&lt;h2&gt;&lt;strong&gt;I Suggest you use hardware timer for it.&lt;/strong&gt;&lt;/h2&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using Optimisation in nrf51 Keil</title><link>https://devzone.nordicsemi.com/thread/106195?ContentTypeID=1</link><pubDate>Wed, 21 Sep 2016 03:28:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3ff45e19-5018-40f9-a18c-cc5f2bfe49a7</guid><dc:creator>nordicdev</dc:creator><description>&lt;p&gt;Hi Aryan, Thanks for your reply. With O0, even without adding any nops(whether inline or not), there is a delay of 1usec between nrf_gpio_pin_clear and nrf_gpio_pin_set. With O3, there is 320nsec between  nrf_gpio_pin_clear and nrf_gpio_pin_set. So, if I add nops(inline or not), this is further going to add delay in the O0. I want the delay to be the same irrespective whether it is O0 or O3. Is it possible? Thanks.
THis is my code:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;nrf_gpio_cfg_output(PIN_EN_BSTR);

for(int i=0;i&amp;lt;300;i++)

{

	nrf_gpio_pin_clear(PIN_EN_BSTR);


	nrf_gpio_pin_set(PIN_EN_BSTR);

	nrf_delay_us(100);

}

nrf_gpio_pin_clear(PIN_EN_BSTR);
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Using Optimisation in nrf51 Keil</title><link>https://devzone.nordicsemi.com/thread/106191?ContentTypeID=1</link><pubDate>Tue, 20 Sep 2016 04:26:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8c3a8acf-3052-43f2-83cc-15b29ab1b6ff</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;__NOP()  can be optimized by compiler,  instead use inline assembly instructions that you can find in nrf_delay.h&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;  __ASM volatile (
       &amp;quot; NOP\n\t&amp;quot;
       &amp;quot; NOP\n\t&amp;quot;
       &amp;quot; NOP\n\t&amp;quot;
       &amp;quot; NOP\n\t&amp;quot;
       &amp;quot; NOP\n\t&amp;quot;
       &amp;quot; NOP\n\t&amp;quot;
       &amp;quot; NOP\n\t&amp;quot;
       &amp;quot; NOP\n\t&amp;quot;
       &amp;quot; NOP\n\t&amp;quot;
       &amp;quot; NOP\n\t&amp;quot;);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Inline assembly is not optimized.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>