Perfectly expected.
With volatile, the compiler are not allowed to optimize away the memory accesses for updating the loop variable. So you are suddenly getting a lot of memory read/write cycles that probably didn't happen before. I would even have expected that prior to the volatile, that loop would be totally optimized away.

  Johnny

On 2021-03-24 20:47, Grr wrote:
Weird behavior:

Simply changing loop counter variable from uint16_t to volatile uint16_t
causes initial delay (with variable delay = 0) going from ~500 ns to ~120 us

The code is

uint16_t delay;

select_function();
for(delay = 0; delay < transfer->dev->afterstart; delay++);

Any ideas?

El mié, 24 mar 2021 a las 12:21, Gregory Nutt (<spudan...@gmail.com>)
escribió:


What I'm trying to do is to generate hold and disable times for SPI CS,
which should be about 50 ns
That resolution is too high for any system timer.
I started by an empty for loop but it seems optimization gets rid of it
(I
haven't researched the issue properly). Then I thought a proper function
would be better but got stuck in that expression "sleep resolution"

Add volatile to the loop counter variable and the optimizer will not
remove it.





--
Johnny Billquist                  || "I'm on a bus
                                  ||  on a psychedelic trip
email: b...@softjar.se             ||  Reading murder books
pdp is alive!                     ||  tryin' to stay hip" - B. Idol

Reply via email to