<?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>Optimized code stops working</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/6526/optimized-code-stops-working</link><description>On the following code I have a packet of 6 bytes that I write to a buffer. I pass the buffer as read_packet function argument. When optimized, the code doesn&amp;#39;t work. I only got noise on the UART and even with the debugger I coudn&amp;#39;t see the correct values</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 08 Jun 2015 12:54:32 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/6526/optimized-code-stops-working" /><item><title>RE: Optimized code stops working</title><link>https://devzone.nordicsemi.com/thread/22789?ContentTypeID=1</link><pubDate>Mon, 08 Jun 2015 12:54:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a9763cb8-d8ed-4a24-b63d-ac271ec28e94</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;please update your post Jose.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Optimized code stops working</title><link>https://devzone.nordicsemi.com/thread/22797?ContentTypeID=1</link><pubDate>Thu, 16 Apr 2015 14:21:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a8ffc3eb-cf55-4b6f-90ce-1942df217563</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Agree, it does not look like the optimization problem is from this code.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Optimized code stops working</title><link>https://devzone.nordicsemi.com/thread/22796?ContentTypeID=1</link><pubDate>Thu, 16 Apr 2015 12:26:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3da5bc27-f18e-4fb6-bacd-0c26eb3dd6ae</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;I can&amp;#39;t see &lt;em&gt;anything&lt;/em&gt; with that code which would be affected under optimisation, at all. Which makes me wonder if that&amp;#39;s really where the problem is or if it&amp;#39;s elsewhere entirely and what we&amp;#39;re seeing is the aftermath of a debugging session&amp;#39;s worth of edits.&lt;/p&gt;
&lt;p&gt;I agree all the registers are volatile, all the loops should remain, all the values should be read and not optimized out. Without seeing the assembler it&amp;#39;s hard to know what&amp;#39;s going on.&lt;/p&gt;
&lt;p&gt;The only &amp;#39;clue&amp;#39; was in the second message from the OP who said packet has data but buffer doesn&amp;#39;t, so I thought CRCSTATUS&lt;/p&gt;
&lt;p&gt;sending received[1] to app_uart_put is fine, a byte is expected so the compiler should mask off the top and send it in r0 or assume the routine will mask it somewhere itself. It&amp;#39;s a one-argument function so it would get passed in the full 32bit r0 either way&lt;/p&gt;
&lt;p&gt;I agree odd to use uint32_t[] but not incorrect that I can tell.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Optimized code stops working</title><link>https://devzone.nordicsemi.com/thread/22795?ContentTypeID=1</link><pubDate>Thu, 16 Apr 2015 12:10:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:67675421-f8a0-4003-b01e-cea0a5cb5ea7</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;given the code he showed here, there is nothing else optimizable apart from those. So if it works with no optimizations it has to be either variables or arguments. Rest of all the code are volatile device registers. Agree that it is legitimate to copy uint8_t into uint32_t.
What happens if you put uint32_t in uint8_t in app_uart_put(received[1]), i am guessing this should be ok too as only LSB is copied and the rest ignored?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Optimized code stops working</title><link>https://devzone.nordicsemi.com/thread/22794?ContentTypeID=1</link><pubDate>Thu, 16 Apr 2015 11:19:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a54127fa-cac5-4a11-b179-15bdb13c56a1</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;No there&amp;#39;s nothing wrong with that code. It&amp;#39;s odd, admittedly, taking the uint8_t and putting them into a uint32_t array, but it&amp;#39;s quite legitimate code. I assumed that was why he has the 6 individual array assignments instead of a memcpy().&lt;/p&gt;
&lt;p&gt;the buffer pointer &amp;#39;packet&amp;#39; itself is a uint8_t and that&amp;#39;s the one handed off to the RADIO module so it&amp;#39;s fine. That&amp;#39;s the one which needs to be a byte pointer.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Optimized code stops working</title><link>https://devzone.nordicsemi.com/thread/22793?ContentTypeID=1</link><pubDate>Thu, 16 Apr 2015 10:47:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b0007675-2670-42be-b109-a754f19f8260</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;pre&gt;&lt;code&gt;uint32_t received[6] 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;change this to&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;uint8_t received[6]; 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;bool read_packet(uint32_t *buffer)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;change to&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;bool read_packet(uint8_t *buffer)
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Optimized code stops working</title><link>https://devzone.nordicsemi.com/thread/22788?ContentTypeID=1</link><pubDate>Thu, 16 Apr 2015 10:38:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:479c0398-dd0c-4d6e-a954-4c13f318b8fc</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;sorry editing my previous reply as I posted that without seeing into the rest of code, ill answer this soon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Optimized code stops working</title><link>https://devzone.nordicsemi.com/thread/22792?ContentTypeID=1</link><pubDate>Thu, 16 Apr 2015 07:58:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ad713a63-720a-4a48-930c-bce5e0592b1d</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;ok you have to reset the packetptr every time before you call start, but if you&amp;#39;re only doing 1, not a problem. For code readability perhaps setting it right before the TASKS_START is good.&lt;/p&gt;
&lt;p&gt;Your second comment &amp;#39;before the optimisation flag I could see the data on the packet pointer but not on the buffer&amp;#39;. Do you mean before you turned OFF optimisation? If you can see data in the packet pointer but not the buffer that would seem to indicate CRCSTATUS is zero so it didn&amp;#39;t get copied into your buffer.&lt;/p&gt;
&lt;p&gt;I still don&amp;#39;t know what this has to do with optimization - trying to rule out the obvious things first, then I suspect it&amp;#39;s time to get into the assembler.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Optimized code stops working</title><link>https://devzone.nordicsemi.com/thread/22791?ContentTypeID=1</link><pubDate>Thu, 16 Apr 2015 07:39:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fd3f6484-a208-4aa4-8325-f962f67cf8ab</guid><dc:creator>Jose Xavier</dc:creator><description>&lt;p&gt;static uint8_t packet[6];&lt;/p&gt;
&lt;p&gt;and I set the pointer after radio_configure();&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;radio_configure();
NRF_RADIO-&amp;gt;PACKETPTR = (uint32_t)packet;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I&amp;#39;ll set a bool to know if the CRCSTATUS is 1 or not.&lt;/p&gt;
&lt;p&gt;Only after change the optimization flag I got the same exact code working on the same exact condictions. Before the optimization flag I could see the data on the packet pointer but not on the buffer.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Optimized code stops working</title><link>https://devzone.nordicsemi.com/thread/22790?ContentTypeID=1</link><pubDate>Thu, 16 Apr 2015 02:26:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a5931aa2-1aab-4c23-a6a8-ed48b238cb77</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;What&amp;#39;s result and what&amp;#39;s packet? You define result but don&amp;#39;t use it, you use packet but it&amp;#39;s not defined in the code you posted. If you&amp;#39;re using easyDMA where&amp;#39;s the pointer reset before you send the START task?&lt;/p&gt;
&lt;p&gt;Do you know if CRCSTATUS is 1 or not? If it&amp;#39;s not then buffer isn&amp;#39;t re-filled and has whatever garbage was in it when you called the function. Perhaps a failure return from the read function would help in that case.&lt;/p&gt;
&lt;p&gt;None of that has anything to do with optimization, but it&amp;#39;s hard to figure out what could even be happening without a bit of clarification.&lt;/p&gt;
&lt;p&gt;Have you looked at the compiled assembler to see what it&amp;#39;s doing? You would hope the compiler is smart enough not to compile out the while loops as the fields are marked volatile but it&amp;#39;s worth checking.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>