<?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>Compiler optimization breaks SAADC in SDK 15.3.0</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/52120/compiler-optimization-breaks-saadc-in-sdk-15-3-0</link><description>Hello! 
 It seems to me that GCC compiler optimization (-O3 -g3) somehow break the SAADC. 
 When enabling said optimizations the SAADC no longer gives me valid measurements (e.g. I have an expected ADC value of 1200 but when enabling optimizations I get</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 17 Sep 2019 11:18:39 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/52120/compiler-optimization-breaks-saadc-in-sdk-15-3-0" /><item><title>RE: Compiler optimization breaks SAADC in SDK 15.3.0</title><link>https://devzone.nordicsemi.com/thread/210082?ContentTypeID=1</link><pubDate>Tue, 17 Sep 2019 11:18:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c6e783fb-f5d7-435f-b744-edd8af0a3422</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;I am not 100 % sure that the workaround you have for implementing the calibration routine is water tight.&lt;/p&gt;
&lt;p&gt;Could you try this?&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;NRF_SAADC-&amp;gt;TASKS_STOP = 1;
while(NRF_SAADC-&amp;gt;EVENTS_STOPPED == 0);
NRF_SAADC-&amp;gt;EVENTS_STOPPED = 0;

NRF_SAADC-&amp;gt;TASKS_CALIBRATEOFFSET = 1;
while(NRF_SAADC-&amp;gt;EVENTS_CALIBRATEDONE == 0);
NRF_SAADC-&amp;gt;EVENTS_CALIBRATEDONE = 0;

NRF_SAADC-&amp;gt;TASKS_STOP = 1;
while(NRF_SAADC-&amp;gt;EVENTS_STOPPED == 0);
NRF_SAADC-&amp;gt;EVENTS_STOPPED = 0;

NRF_SAADC-&amp;gt;TASKS_START = 1;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;PS: Note that you do not need to calibrate every time you do a conversion, unless there&amp;#39;s a temperature change for instance or any other external change.&lt;/p&gt;
&lt;p&gt;Normally its OK if you calibrate on boot.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Compiler optimization breaks SAADC in SDK 15.3.0</title><link>https://devzone.nordicsemi.com/thread/210044?ContentTypeID=1</link><pubDate>Tue, 17 Sep 2019 09:12:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:002b2be4-5192-4477-8660-f8e2d99c70a4</guid><dc:creator>mikoay</dc:creator><description>&lt;p&gt;Hi and thanks for your reply!&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;It&amp;#39;s the nrfx call which fails. In the first code snippet above I am calling the `nrfx_saadc_sample_convert` function. That&amp;#39;s where I already get the wrong data.&lt;/p&gt;
&lt;p&gt;When printing after the float conversion I am printing the previously received ADC value, as well as the newly calculated voltage value, if you look closely.&lt;/p&gt;
&lt;p&gt;For example I&amp;#39;d expect an ADC value of 1200, which I get if I have disabled the compiler optimizations. When enabling the optimizations I only get a value of 3.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I am using GCC Version 7-2018-q2. Also, I have LTO disabled.&lt;/p&gt;
&lt;p&gt;Here you can see the part of my Makefile responsible for setting these flags, depending on the given CONFIGURATION&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;# Optimization flags
ifeq ($(CONFIGURATION), debug)
    OPT = -O0 -g3
    CFLAGS += -DDEBUG
    ASMFLAGS += -DDEBUG
else
    OPT = -O3 -g3
endif
# Uncomment the line below to enable link time optimization
#OPT += -flto&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Compiler optimization breaks SAADC in SDK 15.3.0</title><link>https://devzone.nordicsemi.com/thread/209982?ContentTypeID=1</link><pubDate>Tue, 17 Sep 2019 06:46:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d42c8cda-4292-471a-8fbf-545dd4b70051</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;What version of GCC are you using? LTO used?&lt;/p&gt;
[quote user=""]I have been able to disable optimizations for the nrfx_saadc.c file by adding &amp;quot;#pragma GCC optimize(&amp;quot;O0&amp;quot;)&amp;quot; to the start of the file. This makes the SAADC measurement work again but it just doesn&amp;#39;t feel right.[/quote]
&lt;p&gt;You&amp;#39;re right to be worried.&lt;/p&gt;
&lt;p&gt;However, where does the corruption occur? Is it the float operation that fails, or is the nrfx call returning the incorrect value? It seems that you print only after the float conversion from the code snippets that you posted.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>