avr-chat
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-chat] why does ISR not always seem to run on wake from sleep?


From: Rogier Wolff
Subject: Re: [avr-chat] why does ISR not always seem to run on wake from sleep?
Date: Thu, 10 Nov 2016 13:37:44 +0100
User-agent: Mutt/1.5.21 (2010-09-15)

On Tue, Nov 08, 2016 at 03:55:52PM -0900, Britton Kerin wrote:
> On Mon, Nov 7, 2016 at 10:58 PM, Rolf Pfister <address@hidden> wrote:
> > Am 08.11.2016 um 01:56 schrieb Britton Kerin:
> >>
> >> #define BLINK_PB5_LED_FOREVER(period_ms) \
> >>   do {                                   \
> >>     for ( ; ; ) {                        \
> >>       PORTB |= _BV (PORTB5);             \
> >>       _delay_ms (period_ms / 2.0);       \
> >>       PORTB &= ~(_BV (PORTB5));          \
> >>       _delay_ms (period_ms / 2.0);       \
> >>     }                                    \
> >>   } while ( 0 );
> >>
> >
> > This do while dont make any sense, with while(0) it will
> > run only once. while(1) would do it forever.
> > Anyway the for(;;) will do the loop for ever.
> 
> Yes, it's just a syntactic habit for functionesque macros,
> you're right that the outer do-while is pointless here

The "syntactic habit" as done here is normally a good idea: 
with the do { ... } while (0) you can make a multi-statement
macro behave exactly like you'd expect from a single statement. 

Consider: 

// On <somemachine> we need to call flush after each debug print. 
#define dprint_int(str, i) \
    printf ("%s: %d.\n", str, i); \
    fflush (stdout);

This will have funny effects if you do:

    if (a > 10) // Odd, but not critical yet. 
        dprint_int("odd, a > 10", a);

which will suddenly call the "fflush" all the time, even when a <= 10.
(As the flush does "almost nothing", it won't be that bad in my
"quick" example, but if for example the print happens to be the second
instruction you'd have unexpected results..... )

With the "do { ... } while (0)" around the two statements, the
statements will be executed once as intended, and "wrap" correctly
around the "if" statement.

As far as I know, this trick does not allow your semicolon after the
while (0). Maybe often it IS allowed, but in some corner case, you'd
get a syntax error for the double semicolon.Ah: 
  for (something_that_is_now_a_define(); i < 10;i)) 
     somethingelse ();

That's where an extra semicolon wouldn't work. 

        Roger. 

-- 
** address@hidden ** http://www.BitWizard.nl/ ** +31-15-2600998 **
**    Delftechpark 26 2628 XH  Delft, The Netherlands. KVK: 27239233    **
*-- BitWizard writes Linux device drivers for any device you may have! --*
The plan was simple, like my brother-in-law Phil. But unlike
Phil, this plan just might work.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]