Multidimensional bitset

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

I am trying to interface to an laser mouse sensor.I would like to be able to set individual bits by their names listed in the data sheet for easy identification.I have the following code.

//Multidimential bitset.cpp

#include 
#include 

int main ()
{

// Create a bitset that is 228 bits long called Regs "registers".
// These 228 bits represent all the bits in the 28 registers being used "Reserved registers were excluded" 

std::bitset<228> Regs;

// Create variables to represent the individual registers and all the bits them.
//----------------------------------------------------
//MOTION register and it's individual bits.

 char MOTION   = bitset[0] : 8  //8 bits for the MOTION register
 bool MOT 	  = MOTION[7] : 1  //bit 7 of the MOTION register
 bool OVF 	  = MOTION[3] : 1  //bit 4 of the MOTION register
 bool LP_VALID = MOTION[4] : 1  //bit 3 of the MOTION register
 bool FAULT    = MOTION[5] : 1  //bit 2 of the MOTION register
 
 //bits 5,4,unused,bits 0,1 reserved

//----------------------------------------------------
//Delta_X register and it's individual bits.

 char Delta_X  = bitset[1] : 8  //8 bits for the MOTION register
 bool _X7 	   = Delta_X[7]: 1  //bit 7 of the MOTION register
 bool _X6	   = Delta_X[6]: 1  //bit 4 of the MOTION register
 bool _X5      = Delta_X[5]: 1  //bit 3 of the MOTION register
 bool _X4      = Delta_X[4]: 1  //bit 2 of the MOTION register
 bool _X3      = Delta_X[3]: 1  //bit 2 of the MOTION register
 bool _X2      = Delta_X[2]: 1  //bit 2 of the MOTION register
 bool _X1      = Delta_X[1]: 1  //bit 2 of the MOTION register
 bool _X0      = Delta_X[0]: 1  //bit 2 of the MOTION register

//----------------------------------------------------
}

Now I would set the 7th bit called MOT in the MOTION register to a 1 with the following.

Regs.MOTION.set(MOT,1)

opinions? any sugestions, maps instead maybe?

Last Edited: Wed. Mar 12, 2008 - 06:51 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am trying to learn, I do not set the bitset lib. Can you point me to it.

Thanks.

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

Bitset is not a standard C++ container "sequence or associative" So it's not technically part of the STL "Standard Template Library". It's a cross between a set and a vector, it's a library in the C++ namespace called std It's location is:
C:/Program Files/Atmel/AVR Tools/AVR32 Toolchain/lib/gcc/avr32/4.2.2/include/c++/bitset
Not a lot of books talk about it and most that do don't show examples only syntax that you can look up online. There's a good example in one of the Ditel series books. You can get it at half.com for about $50

"Pages 1130-1134"
C++ How to Program, 6/e
Harvey M. Deitel and Paul J. Deitel
ISBN-10: 0136152503
ISBN-13: 978-0136152507

http://www.amazon.com/How-Program-Harvey-Paul-Deitel/dp/0136152503/ref=sr_1_29?ie=UTF8&s=books&qid=1232498264&sr=8-29

Here's a couple of good examples:
http://www.kuqin.com/cpplib/stl/bitset/set.html
http://ovir.icp.ac.ru/sni/online/stdlibug/bit_2576.htm

Standard C++ Bible by Al Stevens , Clayton Walnum
# ISBN-10: 0764546546
# ISBN-13: 978-0764546549
http://www.amazon.com/gp/reader/...
Search Inside book for: bitset, links 1-9, Pages 605,607, 645-651

Here's an excellent artice about register access using C++:
http://accu.org/index.php/journals/281

Last Edited: Tue. Jan 27, 2009 - 11:53 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

:arrow:

Last Edited: Sun. Jan 11, 2009 - 06:59 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The AVR32 software framework "drivers, ie. GPIO SPI USART ect." is supplied in C language only not C++. The makefiles do not support C++ either. I emailed Atmel just recently and they said they have no plans to release a C++ version of the framework. Jõhvi posted how to make the existing framework files work with C++ and a makefile example.
https://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=59144&highlight=c+framework
I imagine it would still be difficult. But when using bitfields the ordering of bits in memory varies from compiler to compiler. I'am not shure how the GNU compilier orders them. I really wish Atmel would make it's software framework in C++. It's getting harder and harder to find tutorials on C.

Last Edited: Sun. Jan 11, 2009 - 06:55 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Let me note that gcc-avr32 seems to chew the C++ code just fine. It's the framework that seems to be drifting in incompatible directions with C++.

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

How to program an 8-bit microcontroller using C language by Richard Mann, Imagecraft
Page 2: http://www.atmel.com/dyn/resourc...

Bitfields in a C Struct
C allows you to declare bitfields within a structure, such as:

typedef struct {

       unsigned 	bit0  :  1,
		             bit1  :  1,
		             bit2  :  1,
		             bit3  :  1,
		             bit4  :  1,
		             bit5  :  1,
		             bit6  :  1,
		             bit7  :  1}
}IOREG;
#define PORTA(* (IOREG *) 0x03B)
…
int i = PORTA.bit0;  // read
…
PORTA.bit = 1;         // write

This is seemingly a nice way to map the I/O register bits to the C language. However, a potential problem exists:
The C standard does not define the bitfield allocation order, and the compiler may allocate bitfields either from right to left or from left to right. If you use this method, you should make sure to consult the compiler manual to ensure that your use of the bit ordering matched the complier’s usage.
It is also possible that some compliers may generate more verbose code for bitfield operations as compared to bitwise operation.
Lastly, according to the C standard, only "unsigned (int)" and "int" are acceptable datatypes for a bitfield member. Some compliers allow "unsigned char", but it is an extension.
Whether or not a compiler allocates only a byte for the above structure depends on the particular compiler. If a compiler uses two bytes for the above structure, then using this method of accessing bits will not work.
Due to these reasons, this approach is not really recommended for bitwise accessing of IO registers.

I found the support pages for GNU GCC complier but what version comes with the latest AVR32 toolchain : 2.1.4 ? http://www.atmel.com/dyn/products/tools_card.asp?tool_id=4118

Last Edited: Sun. Jan 11, 2009 - 06:58 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No, you can not access single bits in the I/O registers the way you do here.

To clear RDRF you should do

volatile avr32_spi_t *spi0 = AVR32_SPI0;
spi0->sr &= ~AVR32_SPI_SR_RDRF_MASK;

That was from the top of my head, not sure it compiles and runs flawless.

Hans-Christian

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

:arrow:

Last Edited: Sun. Jan 11, 2009 - 06:58 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

... and I/O registers are not the same as normal memory, so you will write rubbish to the 32-bit register if you try to write single bits using the bitfield structure given above.

Latest GCC for AVR32 is version 4.2.2.

Hans-Christian

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

It's still nice to know the GCC version so I can look up these points. http://gcc.gnu.org/onlinedocs/gcc-4.2.2/gcc/Structures-unions-enumerations-and-bit_002dfields-implementation.html#Structures-unions-enumerations-and-bit_002dfields-implementation
Is there somewhere in AVR32 studio that shows the GCC version.? I looked in Help-> About AVR32 studio -> Feature details and in plugin details.

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

If you do check environment from the help menu, then it shows he GCC version + Atmel vendor version.

Hans-Christian

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

Yes, I see the version.
Help -> AVR32 studio -> Check Enviroment
It has a button for copying it to your clipboard.

Quote:
AVR32 Studio has checked your system to ensure the proper versions of required utilities are in place.

avr32program: Found version 3.0.4
avr32gdbproxy: Found version 3.0.11
avr32-gdb: Found version 6.7.1.atmel.1.0.3
avr32-g++: Found version 4.2.2-atmel.1.0.8
avr32-gcc: Found version 4.2.2-atmel.1.0.8
avr32-as: Found version 2.17.atmel.1.2.6
avr32-nm: Found version 2.17.atmel.1.2.6

You are using the MinGW version of the AVR32 Utilities and AVR32/GNU Toolchain.

Have fun!


Thank you hce

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

Quote:
To clear RDRF you should do:
volatile avr32_spi_t *spi0 = AVR32_SPI0;
spi0->sr &= ~AVR32_SPI_SR_RDRF_MASK;


That doesn't work I get these errors:
assignment of read-only member 'sr'
incompatible types in initialization

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

SR which is Status Register is read only. To clear the RDRF (receive data register full) bit, you need to read the RDR register, and the bit will clear itself.

If you are trying to disable interrupts on RDRF then you should do

spi0->idr &= ~AVR32_SPI_SR_RDRF_MASK;

Hans-Christian

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

As I said using:

volatile avr32_spi_t *spi0 = AVR32_SPI0;

gives the error: incompatible types in initialization. AVR32_SPI0 is defined in uc3a0512es.h which is automatically included when you build any of the example projects and I have It and spi_192.h in my .c file:

#include 
#include 

So how should spi0 be defined?

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

IIRC it should be like

volatile avr32_spi_t *spi0 = &AVR32_SPI0;

And the only include you need is

#include 

Hans-Christian

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

Ok, yes that works thank you, but what do you mean IIRC?

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

IIRC = "if I remember correctly" :)

Letting the smoke out since 1978