SPI and optimization?

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Greetings freaks -

 

This is really a mixed question about optimization and AVR SPI operation.

 

Environment: AS7 in Windows 10, ATmega328P

 

I use the SPIF bit in the SPSR (SPI Status Register) to detect when a byte transfer is complete. No interrupts are used. The spec sheet does not indicate any method for clearing the SPIF other than reading the SPDR (SPI Data Register) or responding to an interrupt.

 

Now, I have come to the point where i need to read a bunch of bytes to empty them in the peripheral, discarding the results. I set up a local variable to do the read, but it is optimized away (presumably since the variable is never used), leading to no SPDR read and no reset of SPIF. I know that I could set up a global volatile variable, but that seems to be a waste for something that is really a local variable with a temporary life. 

 

So, how can I insure that this read takes place?

 

Thanks

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Sat. Mar 7, 2020 - 06:23 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The most you should have to do is:

SPDR;

This will force a read and discard the results as the definition of SPDR contains 'volatile'.

Letting the smoke out since 1978

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hmm, did not realize you could  do that.

 

Thanks

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Does that not generate a warning like "statement with no effect" or somesuch ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Something isn't quite right. Show minimal test case. SPDR is/should be volatile. Any access, even if not stored due to optimizer, should still be generated.

You gurus with better memories can comment on the GCC reordering possibility.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hmmm, I thought it was being optimized out,  but when I retried, it all seemed OK. 

 

Sorry for the noise!

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Small nitpick - ‘insure’ and ‘ensure’. Insure is your plan B when the worst happens, ensure is to put things in place so it doesn’t happen.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bottom line is there will have been IN Rn, SPSR. Sure Rn may not have been used any further but the IN will have been in the LSS

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman -

 

But, of course!

 

Jim

 

 

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
Does that not generate a warning like "statement with no effect" or somesuch ?

No because it does have an effect.  It forces an IN instruction to be emitted by the compiler.  If anything, one would think it would complain about the result being discarded but the C compiler in GCC doesn't.  I'm not a C standards guru so I don't know if this is expected or not.

Letting the smoke out since 1978

 

 

 

 

Last Edited: Sun. Mar 8, 2020 - 12:55 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:

Small nitpick - ‘insure’ and ‘ensure’. Insure is your plan B when the worst happens, ensure is to put things in place so it doesn’t happen.

Ganssle makes that transposition often.  Makes me crazy ;-)

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Kartman wrote:
Small nitpick - ‘insure’ and ‘ensure’. Insure is ...

... Flo.

Kartman wrote:
, ensure is ...

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.