do{ } while(0)???!!!

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

I found it in avr/wdt.h:

#define wdt_disable() \
do { \
uint8_t temp; \
__asm__ __volatile__ (  \
	"wdr"								"\n\t" \
	"out %[ccp_reg], %[ioreg_cen_mask]" "\n\t" \
	"lds %[tmp], %[wdt_reg]"			"\n\t" \
	"cbr %[tmp], %[timeout_mask]"       "\n\t" \
	"sts %[wdt_reg], %[tmp]"			"\n\t" \
    : [tmp]            "=r" (temp) \
    : [ccp_reg]        "I" (_SFR_IO_ADDR(CCP)),       \
      [ioreg_cen_mask] "r" ((uint8_t)CCP_IOREG_gc),   \
      [wdt_reg]        "n" (_SFR_MEM_ADDR(WDT_CTRLA)),\
      [timeout_mask]   "I" (WDT_PERIOD_gm) \
); \
} while(0)

Why did they use "do{ } while(0)"? frown

This topic has a solution.

Slow and Steady!

Last Edited: Tue. Oct 20, 2020 - 09:20 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

You're not the first person to ask this...

 

https://stackoverflow.com/questi...

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

As clawson says, it's a common 'C' idiom to give a macro that can always be used as a single 'statement'

 

It is nothing specific or special to AVR or Atmel Studio - it is a standard 'C' thing.

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

Interesting, I've never run across this.  I assume the compiler will optimize out any test and branch at the bottom.

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

kk6gm wrote:
 I've never run across this

When I say it's "common", it is usually buried in macros - as the OP showed - so you probably wouldn't actually see it unless you're into delving into the inner workings of these macros ...

 

I assume the compiler will optimize out any test and branch at the bottom

Undoubtedly.

 

 

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

kk6gm wrote:
I've never run across this.
Just as example:

C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr>grep while(0) *.h
sleep.h:    } while(0)
sleep.h:    } while(0)
sleep.h:  } while(0)
sleep.h:    } while(0)
sleep.h:    } while(0)
sleep.h:    } while(0)
sleep.h:} while(0)
sleep.h:} while(0)
sleep.h:} while(0)
wdt.h:} while(0)
wdt.h:} while(0)
wdt.h:} while(0)
wdt.h:}while(0)

So in AVR-LibC it seems to mainly be used in sleep.h and wdt.h

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

I can't recall coming across this either. Late to the party maybe?? I recently came across x - macros. Always something new to learn!

 

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

My personal take on it is that you shouldn't be putting the complexity that might require it into macros anyway. (IMAO).

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

clawson wrote:
that you shouldn't be putting the complexity that might require it into macros anyway.

+1

 

But Atmel did love their Macro Magic - ASF is absolutely full of it.

 

frown

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

awneil wrote:
When I say it's "common", it is usually buried in macros

eg, see: http://c-faq.com/cpp/multistmt.html

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

awneil wrote:
But Atmel did love their Macro Magic - ASF is absolutely full of it.

 

do not forget about the functions that only call a next function that again only calls a next function.....

I bet the compiler and linker will remove all those "just function calls" jumps, but when tracing a certain example it drives one mad as you lose track of were you are coming from, or even worst wile diving get interrupted by a colleague and completely forget what you were actually looking for.

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

Curious. I have never noticed this construct either. The stackoverflow discussion is very informative.

Besides the 'do {...} while(0)' and 'if {...} else' constructs, I came up with '{...}, 0' and similar. But this one generates a compiler warning stating that this does nothing. Yeah, duh.

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

meslomp wrote:
when tracing a certain example it drives one mad as you lose track

Indeed.

 

frown

 

That's why I find it easier to track by stepping in the debugger.

 

However, even that can get caught-out with some of their macro tricks - because the macro magic means the debugger can't find the appropriate source.

 

angry

 

 

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

Beginnning message posters to AVRfreaks should keep in mind that if they ask a relatively simple question here, then they will receive many, even several dozen, replies throughout which none of these replies will actually do something like, say, answer the question.

 

For example, asking "what does do..while(0)" mean?  This creates a loop that will execute exactly one time.  Simple question: simple answer, and I'm only the tenth or so responder to the message.

 

I suggest to all beginners that simple questions on any topic be directly typed into the URL prompt of their browser program, which is usually linked directly to a major search engine like Google.

Last Edited: Wed. Oct 21, 2020 - 01:44 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Simonetta wrote:
asking "what does do..while(0)" mean? 

But that's not what the OP asked - is it?

 

What the OP asked was:

pajuhesh80 wrote:
Why did they use "do{ } while(0)"? frown

Which was answered in the 1st two replies.

 

simple questions on any topic be directly typed into the URL prompt of their browser

Now that is very good advice.

 

It is amazing that people who have grown up in the age of internet & google don't seem to think of that ...

 

frown

 

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: 1

Simonetta wrote:
one of these replies will actually do something like, say, answer the question.
Except for the very first reply which fully answered the question?!?

 

The reason for do{}while(0) is EXACTLY what I pointed to in #2. It is a "grouping" technique to put the whole of an expanded macro into place as if it were a single statement. It has nothing to do with looping when used in this context. (indeed how could it be a "loop" when the termination condition is deliberately being met?)

 

Can I suggest that if you don't understand something in future you don't bother to respond?

Last Edited: Wed. Oct 21, 2020 - 02:22 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

My personal take on it is that you shouldn't be putting the complexity that might require it into macros anyway. (IMAO).

The worst, most screwed up embedded software I have ever had to deal with was loaded with complex macros.  It was nearly impossible to understand, nearly impossible to edit, nearly impossible to debug.

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

kk6gm wrote:
 It was nearly impossible to understand, nearly impossible to edit, nearly impossible to debug.
And not MISRA compliant ;-)

 

(there's actually a lot of sense in the rules that MISRA mandate)

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

clawson wrote:

kk6gm wrote:
 It was nearly impossible to understand, nearly impossible to edit, nearly impossible to debug.
And not MISRA compliant ;-)

 

(there's actually a lot of sense in the rules that MISRA mandate)

Good point.  This was an automated welding machine, so it was controlling high currents, extremely high temperatures, and mechanical motion.  Not just your harmless student Cylon Eye.