avr-gcc-list
[Top][All Lists]
Advanced

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

Re: [avr-gcc-list] generic queue library for AVR GCC?


From: Bruce D. Lightner
Subject: Re: [avr-gcc-list] generic queue library for AVR GCC?
Date: Wed, 17 Nov 2004 10:29:59 -0800
User-agent: Mozilla Thunderbird 0.5 (Windows/20040207)

Bjarne Laursen wrote:

Bruce D. Lightner wrote:

How about this...

    #include <avr/io.h>
    #include <avr/interrupt.h>
    #include <inttypes.h>
    #define begin_critical_section()  SREG; cli()
    #define end_critical_section(val) SREG = val
    ....
    {
        unsigned char sreg = begin_critical_section();
        sm_qin = sm_qout = 0;   // reset queue pointers
        end_critical_section(sreg);
    }

This gets you the same, optimally efficient AVR code.

but be careful
this will not work:

       unsigned char cr;
       if (bSomeFlag) cr = begin_critical_section();
       sm_qin = sm_qout = 0;   // reset queue pointers
       if (bSomeFlag) end_critical_section(cr);

cli() will get executed even if bSomeFlag is 0.
maybe an inline function will do the job.

OK.  Let's continue to "beat this dead horse" to make sure it's
*really* dead! :-)

You all of course know that "," can be used in place of ";" to
separate C statements.  Therefore we can make a minor change to
the first macro definition...

     #include <avr/io.h>
     #include <avr/interrupt.h>
     #include <inttypes.h>
     #define begin_critical_section()  SREG, cli()
     #define end_critical_section(val) SREG = val
     ....
     {
         unsigned char sreg = begin_critical_section();
         sm_qin = sm_qout = 0;   // reset queue pointers
         end_critical_section(sreg);
     }

Note the "," just before the "cli()".

This makes Bjarne's...

  if (bSomeFlag) cr = begin_critical_section();

...work just fine.

Best regards,

Bruce

--
 Bruce D. Lightner
 Lightner Engineering
 La Jolla, California
 Voice: +1-858-551-4011
 FAX: +1-858-551-0777
 Email: address@hidden
 URL: http://www.lightner.net/lightner/bruce/


reply via email to

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