avr-libc-dev
[Top][All Lists]
Advanced

[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: Matt.VanDeWerken
Subject: [avr-libc-dev] RE: [avr-gcc-list] Re: What Happened to the sbi() and cbi() Macros????
Date: Mon, 31 Jan 2005 17:28:09 +1000

Unfortunately, I don't get to "live" in AVR-space 100% of the time (I do
mostly hardware & FPGA development), so I often get confused with the
cbi & sbi macros, specifically I am never sure what I should be using
instead; at least this discussion has clarified that I shouldn't be
using them, although it hasn't really discussed what is the alternative.
I always thought "sbi" was clearer than "_BV", but maybe that's just me.

I really like the proposal put forward by Eric regarding the bit
operations below; I hope something like that makes its way into avr-libc
in the near future.

My vote would be to use the lower-case operator (as in bit vs BIT), I
don't know why, maybe I just have nightmares about old FORTRAN
classes...

Cheers,
Matthew van de Werken - Electronics Engineer
CSIRO E&M - Mining Geoscience - 1 Technology Court - Pullenvale - 4069
p: (07) 3327 4142 * f: (07) 3327 4455 * e: address@hidden
"Pressure, I'll tell you what pressure is. Pressure is a Messerschmitt 
up your arse, playing cricket is not" 
- Cricketer and WWII Fighter Pilot Keith Miller (1919-2004)

-----Original Message-----
From: address@hidden
[mailto:address@hidden On Behalf Of E. Weddington
Sent: Monday, 31 January 2005 4:25 PM
To: Bruce D. Lightner
Cc: address@hidden; Joerg Wunsch; avr-libc-dev
Subject: [avr-gcc-list] Re: What Happened to the sbi() and cbi()
Macros????


<snip>

Now, on moving forward...

Bruce, for "legacy" code that must require cbi() and sbi(), would it be 
reasonable to patch your own version of avr-libc to provide these 
macros? The implemenations are not that hard.

As to the idea that these were very useful macros, then I have a 
counter-proposal:

I would like to create an API that can be used to operate on bits. I 
propose that this would be a new header, <avr/bit.h>. In this header, 
would be macros that would set, clear, toggle, read, and test bits in a 
variable. These macros would also be defined to work on variables of 
size 8 bit, 16 bit and 32 bit. For example:
----------------------------------------------------------------
#include <inttypes.h>
#define bit_set_8(var, mask)   ((var) |= (uint8_t)(mask))
#define bit_clear_8(var, mask)   ((var) &= (uint8_t)~(mask))
#define bit_toggle_8(var, mask)   ((var) ^= (uint8_t)(mask))
#define bit_read_8(var, mask)   ((var) & (uint8_t)(mask))

#define bit_set_16(var, mask)   ((var) |= (uint16_t)(mask))
#define bit_clear_16(var, mask)   ((var) &= (uint16_t)~(mask))
#define bit_toggle_16(var, mask)   ((var) ^= (uint16_t)(mask))
#define bit_read_16(var, mask)   ((var) & (uint16_t)(mask))

// 32 bit versions here

// Shorter named versions for the common operation.
#define bit_set(var, mask)   bit_set_8(var, mask)
#define bit_clear(var, mask) bit_clear_8(var, mask)
#define bit_toggle(var, mask) bit_toggle_8(var, mask)
#define bit_read(var, mask) bit_read_8(var, mask)
----------------------------------------------------------------

There are several reasons why I think this would be useful:
- Doing bit operations with the C language bit operators is confusing to

most newbies. This usually has to be explained again and again. These 
macros make it easier for newbies.
- It's makes it easier to read what operation is happening rather than 
trying to remember the bit operations themselves.
- 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.

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".

The LONGBIT macro is needed to provide a conversion from a bit number to

a 32-bit mask.

Personally, I don't care whether the macro names are upper or lower 
case. I'm fine with bit() and longbit() too.

I feel that it is important that the bit macros supply a bit "mask" and 
not just a bit number because a bit mask is more flexible in defining 
non-contiguous bits to operate on. This means that one can do this:

bit_set(PORTD, BIT(0) | BIT(2) | BIT(4) | BIT(7)); bit_clear(PORTG,
BIT(1) | BIT(6)); bit_toggle(PORTE, BIT(5) | BIT(3)); bit_read(PORTF,
BIT(7));

Would this proposal be useful?

Comments welcome.

Thanks
Eric





_______________________________________________
avr-gcc-list mailing list
address@hidden http://www.avr1.org/mailman/listinfo/avr-gcc-list




reply via email to

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