how does WinAVR handle bool data type?

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

Hi All,

Some compilers e.g. CodeVision allow bit data types. CodeVision is smart in that you can also specify a set of registers that the bit variables will be placed in so it can use bit set and bit clear instructions while at the same time packing 8 bit vaiables into each byte wide register.

WinAVR has a bool data type, but will it:

a) pack these into bytes, and if it does will it

b) guarantee atomic operation on each bit (i.e. do you need to explictly disable and re-enable interrupts to prevent other bits in the same from being mangled if an ISR happens to fire while a bit is being modified)?

Or does it always use a full byte for each bool bit?

Also can you use the register modifier to force these (packed?) bits into registers?

Thanks!

Mark.

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

Quote:
pack these into bytes
No. A bool in WinAVR is a char.
Quote:
Also can you use the register modifier to force these (packed?) bits into registers?
The register keyword is only a suggestion to the compiler, so it is not forced. You can, however, use specific I/O registers and select/use the bits yourself.

Regards,
Steve A.

The Board helps those that help themselves.

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

Koshchi wrote:
Quote:
pack these into bytes
No. A bool in WinAVR is a char.
Quote:
Also can you use the register modifier to force these (packed?) bits into registers?
The register keyword is only a suggestion to the compiler, so it is not forced. You can, however, use specific I/O registers and select/use the bits yourself.

OK thanks. I found a post that used unions to pack bits into a byte and put them in a specific register (see below), but wondered whether the compiler would be smart enough to use bit set and clear instructions, i.e. be atomic, or whether it would use read modify write. The original post
https://www.avrfreaks.net/index.php?name=PNphpBB2&file=printview&t=64942&start=0 was actually having problems with a shared bit variable between an ISR and foreground code, so maybe it isn't atomic (and probably should be volatile):

register union {
      unsigned char byte;
      struct{
            char b0:1;
            char b1:1;
            char b2:1;
            char b3:1;
            char b4:1;
            char b5:1;
            char b6:1;
            char b7:1;
      }bit;
   }irflag asm("r3");

irflag.bit.b2 = 1;
irflag.bit.b3 = 0; // etc
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You'll have to check the disassembly output to see if it uses CBR/SBR. But for R3, it certainly won't since those only work with registers R16-R31. If you instead bind it to one of the lower I/O registers, then the compiler can use CBI/SBI.

Regards,
Steve A.

The Board helps those that help themselves.

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

Koshchi wrote:
If you instead bind it to one of the lower I/O registers, then the compiler can use CBI/SBI.
Search your AVR's datasheet for the GPIOR register - at least one (usually?) resides in the lower registers and so is accessible to the CBI/SBI instructions.

In general, the compiler is bright enough to use the most efficient instruction for bit setting/clearing, given the restrictions on address space and so on.

Stu

PS: A more format tutorial on this is at [TUT] [C] [GCC] How to define a pin as a variable

Engineering seems to boil down to: Cheap. Fast. Good. Choose two. Sometimes choose only one.

Newbie? Be sure to read the thread Newbie? Start here!

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

Thanks for the replies guys! I think I have a handle on it now, and good to see a tutorial on the subject.

Mark.