[Top][All Lists]

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

[avr-libc-dev] Re: [avr-gcc-list] Re: What Happened to the sbi() and cbi

From: E. Weddington
Subject: [avr-libc-dev] Re: [avr-gcc-list] Re: What Happened to the sbi() and cbi() Macros????
Date: Mon, 31 Jan 2005 11:09:50 -0700
User-agent: Mozilla Thunderbird 0.7.3 (Windows/20040803)

Kevin Neff wrote:

On Sun, 30 Jan 2005, E. Weddington wrote:
I have trouble remembering things that I don't use regularly, so I don't
find this argument particularly convincing.

I am against intentionally making tools hard to use for beginners but I
think that the "improvements" should have technical merit beyond making
things easier for newbies.

- The typecasts are in there to workaround the C language Standard of promoting the operands of bit operaters to ints, which are 16 bits for the AVR (see the avr-libc FAQ for more discussion of this). If these macros are used, then users don't have to remember to do the typecasts, which should, in theory, help with the size of their code.

A good point... but isn't there a way to do this during the build process?

Not that I know of. The problem stems from the fact that the C Language Standard specifies that the operands of bit operations are promoted to an int. GCC is usually very good about adhering to standards. So the extra typecasts are in there to help the compiler understand that this operation is done on 8-bit values. This *should* help it to optimize the assembly better. Admittedly, I can understand where some people would look at this and kind of "glaze over":

PORTA &= (uint8_t)~bit(0);

But this is eaiser to see at a glance:

bit_clear_8(PORTA, bit(0));

I would also like to propose two other macros for this file:

#define BIT(x)   (1 << (x))
#define LONGBIT(x)   ((uint32_t)1 << (x))

Yes, the BIT() macro is the same as _BV(). The _BV() macro is confusing in two ways: one has to remember that "BV" stands for Bit Value. The name BIT is slightly more descriptive. And _BV() has a leading underscore, which technically is supposed to be reserved for the "implementation" according to the C Standard (in this case the library). Having to type out the underscore is annoying. I feel that it's easier to just write out "BIT".

As an alternative to this suggestion, I would advocate adding some simple
bit value macros.  They are fewer characters to type and do not require
special treatement of larger words.
 #define B0 0x01
 #defind B1 0x02

I slightly disagree.

The individual IO header files (and through the <avr/portpins.h> header which is automatically included) already define *bit numbers* for all the registers in the device. I would rather not have to define *bit masks* too. You should always be able to easily construct an expression such as:

bit_set_8(PORTB, bit(PB0) | bit(PB6));


When it comes down to it, if you want to operate on individual bits, you're going to have to learn the C language bit operators to a certain extent. That's what they're there for.


reply via email to

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