EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

Union/type help..

Started by Micah Stevens January 16, 2005
microbit wrote:
> Hi Micah,
> 
> Weather still bad up there I here.
> 
> 
>>Only I'm not sure how to define the adc/spi types. Is there a way
to define a 
>>data type with an arbitrary number of bits in it? I can't find any
way to do 
>>this in my searching. 
> 
> 
>>I need a 12 bit type, and an 8 bit type. Actually, 8 bit is easy enough,

>>that's a char.. 
> 
> 
> Doing it that way you'd need bitfields, I'd say - some compilers
handle unsigned bitfields
> actually quite well, say, bitfield [6]. 
> The only other way is to shift around of course...
> 
> No need of course to make sure you don't change compiler in a hurry,
or set it
> configurable. I;m no expert, but AFAIK Bitorder of bitfields is not
guaranteed to
> overlay in any specifc consistent way between MCUs ot maybe even different
compile
> settings. Never really tried that enough.
Yepp I agree, you can do it, but it is not portable and platform and
compiler 
depended, so you must be aware and check the compiler output everytime things 
change. I'm still looking for a nice way to do it in legal C.

"6.7.2.1 Structure and union specifiers" from the c standards it says:
10 An implementation may allocate any addressable storage unit large enough to 
hold a bit-field. If enough space remains, a bit-field that immediately follows 
another bit-field in astructure shall be packed into adjacent bits of the same 
unit. If insufficient space remains, whether a bit-field that does not fit is 
put into the next unit or overlaps adjacent units is implementation-defined. The

order of allocation of bit-fields within a unit (high-order to low-order or 
low-order to high-order) is implementation-defined. The alignment of the
addressable storage unit is unspecified.
12 Each non-bit-field member of a structure or union object is aligned in an 
implementationdefined manner appropriate to its type.


There's a -fpack-struct option for the gcc compiler.

Greetz,

	Georg

> 
> B rgds
> Kris
> 
> 
> 
> 
> 
> 
> .
> 
>  
> Yahoo! Groups Links
> 
> 
> 
>  
> 
> 
> 
> 
> 


Beginning Microcontrollers with the MSP430

But you could create a bitfield defined structure and make an array out of 
that, right? or no? 

On Sunday 16 January 2005 01:01 pm, Paul Curtis wrote:
>  You can't create an array of bitfields in C...
>
>  > -----Original Message-----
>  > From: microbit [mailto:microbit@micr...]
>  > Sent: 16 January 2005 20:23
>  > To: msp430@msp4...
>  > Subject: Re: [msp430] Union/type help..
>  >
>  >
>  > Hi Micah,
>  >
>  > Weather still bad up there I here.
>  >
>  > > Only I'm not sure how to define the adc/spi types. Is there
>  >
>  > a way to
>  >
>  > > define a data type with an arbitrary number of bits in it? I
can't
>  > > find any way to do this in my searching.
>  > >
>  > > I need a 12 bit type, and an 8 bit type. Actually, 8 bit is easy
>  > > enough, that's a char..
>  >
>  > Doing it that way you'd need bitfields, I'd say - some
>  > compilers handle unsigned bitfields actually quite well, say,
>  > bitfield [6].
>  > The only other way is to shift around of course...
>  >
>  > No need of course to make sure you don't change compiler in a
>  > hurry, or set it configurable. I;m no expert, but AFAIK
>  > Bitorder of bitfields is not guaranteed to overlay in any
>  > specifc consistent way between MCUs ot maybe even different
>  > compile settings. Never really tried that enough.
>  >
>  > B rgds
>  > Kris
>  >
>  >
>  > 
>  >
>  >
>  >
>  > .
>  >
>  >
>  > .

Hi Micah. It really depends a lot on what you are doing. packing 2 x 12 
bit samples into 3 bytes takes processing time, not a lot if you use 
registers, but enough. The best answer Depends upon the quality of 
audio, and the content required. Is the audio music or speech for 
example? You will need to make sure your input filter is adequate to 
prevent aliasing. If you are doing voice then I'd A or u-law compand the 
12 bit sample to 8 bits, or do something totally different like the 
single bit voice codec I've posted here. Simply by varying the sample 
rate you can vary the quality enormously, and it is inherently a 12:1 
compressor. Sampling voice at 60ksps would give you excellent quality, 
at 50kbaud data rate. in fact I'd venture a guess (based on real world 
tests) that broadcats radio quality music is possible using this 
technique, at less than the uncompressed 8000sps you would normally 
employ for voice. At 12 bits thats 96kbps for voice. Stricly speaking 
music should be sampled around 22ksps minimum, or 262kbps at 12 bit 
sample size, equivalent to 327.5kbaud compacted but uncompressed.

Cheers]

Al

Micah Stevens wrote:

> 
> Hmm.. True it's easier, but not exactly bandwidth efficient. Currently
I've 
> got quite a bit of overhead, so maybe I can get something working that way,

> but the next revision of product will need to support more data, so less 
> bandwidth..
> 
> It's for an RF link BTW, hence my bandwidth limitation.. I'm
sampling audio.. 
> so there's a lot of data. 
> 
> Speaking of sampling audio, is there an efficient way to oversample with
the 
> ADC? or should I just up the sample rate and just average the samples with 
> the CPU? I think it will be a 4:1 oversample. (60Khz total) 
> 
> I'll need to do that eventually as well.. the noise floor is pretty
high as I 
> have things now. 
> 
> -Micah
> 
> On Sunday 16 January 2005 12:37 pm, onestone wrote:
> 
>> Just up, or not been to bed yet Kris?
>>
>> Given that it's C, unless there is an overwhelming need to keep
your
>> data compressed it would be easier to send the twelve bits as two
>> separate bytes, as they are read from the ADC.
>>
>> Al
>>
>> microbit wrote:
>> > Hi Micah,
>> >
>> > Weather still bad up there I here.
>> >
>> >>Only I'm not sure how to define the adc/spi types. Is
there a way to
>> >> define a data type with an arbitrary number of bits in it? I
can't find
>> >> any way to do this in my searching.
>> >>
>> >>
>> >>I need a 12 bit type, and an 8 bit type. Actually, 8 bit is
easy enough,
>> >>that's a char..
>> >
>> > Doing it that way you'd need bitfields, I'd say - some
compilers handle
>> > unsigned bitfields actually quite well, say, bitfield [6].
>> > The only other way is to shift around of course...
>> >
>> > No need of course to make sure you don't change compiler in a
hurry, or
>> > set it configurable. I;m no expert, but AFAIK Bitorder of
bitfields is
>> > not guaranteed to overlay in any specifc consistent way between
MCUs ot
>> > maybe even different compile settings. Never really tried that
enough.
>> >
>> > B rgds
>> > Kris
>> >
>> >
>> > 
>> >
>> >
>> >
>> > .
>> >
>> > 
>> > .
> 
> 
> 
> .
> 
>  
> Yahoo! Groups Links
> 
> 
> 
>  
> 
> 
> 
> 


Hi, 

> -----Original Message-----
> From: Micah Stevens [mailto:micah@mica...] 
> Sent: 16 January 2005 21:11
> To: msp430@msp4...
> Subject: Re: [msp430] Union/type help..
> 
> But you could create a bitfield defined structure and make an 
> array out of that, right? or no? 

No.  You'll find that you can take sizeof(structure) and it will return
size in units of chars, not in units of bits.

Regards,

-- Paul.

> 
> On Sunday 16 January 2005 01:01 pm, Paul Curtis wrote:
> >  You can't create an array of bitfields in C...
> >
> >  > -----Original Message-----
> >  > From: microbit [mailto:microbit@micr...]
> >  > Sent: 16 January 2005 20:23
> >  > To: msp430@msp4...
> >  > Subject: Re: [msp430] Union/type help..
> >  >
> >  >
> >  > Hi Micah,
> >  >
> >  > Weather still bad up there I here.
> >  >
> >  > > Only I'm not sure how to define the adc/spi types. Is 
> there  >  > 
> > a way to  >  > > define a data type with an arbitrary 
> number of bits 
> > in it? I can't  > > find any way to do this in my
searching.
> >  > >
> >  > > I need a 12 bit type, and an 8 bit type. Actually, 8 
> bit is easy  
> > > > enough, that's a char..
> >  >
> >  > Doing it that way you'd need bitfields, I'd say - some
 
> > compilers 
> > handle unsigned bitfields actually quite well, say,  > bitfield
[6].
> >  > The only other way is to shift around of course...
> >  >
> >  > No need of course to make sure you don't change compiler in
a  > 
> > hurry, or set it configurable. I;m no expert, but AFAIK  > 
> Bitorder of 
> > bitfields is not guaranteed to overlay in any  > specifc consistent

> > way between MCUs ot maybe even different  > compile settings. Never

> > really tried that enough.
> >  >
> >  > B rgds
> >  > Kris
> >  >
> >  >
> >  >   > 
>  >  >  > 
> > .
> >  >
> >  >
> >  > .
> 
> 
> .
> 
>  
> Yahoo! Groups Links
> 
> 
> 
>  
> 
> 
> 
> 
> 

microbit wrote:

> I dunno anymore Al :-}
> Days and nights kinda shift in and out of phase this time of the year.
> Well, you know , big dreamers never sleep.

Hey let me know when you're driving will you, I ONLY dream when I'm 
asleep ;@}

Al


> 
> 
> 
>>Just up, or not been to bed yet Kris?
>>
>>Given that it's C, unless there is an overwhelming need to keep
your 
>>data compressed it would be easier to send the twelve bits as two 
>>separate bytes, as they are read from the ADC.
>>
>>Al
> 
> 
> 
> 
> 
> 
> 
> .
> 
>  
> Yahoo! Groups Links
> 
> 
> 
>  
> 
> 
> 
> 


Oh the joys of asm toys ;@}

Al

Georg Ritter wrote:

> microbit wrote:
> 
>>Hi Micah,
>>
>>Weather still bad up there I here.
>>
>>
>>
>>>Only I'm not sure how to define the adc/spi types. Is there a
way to define a 
>>>data type with an arbitrary number of bits in it? I can't find
any way to do 
>>>this in my searching. 
>>
>>
>>>I need a 12 bit type, and an 8 bit type. Actually, 8 bit is easy
enough, 
>>>that's a char.. 
>>
>>
>>Doing it that way you'd need bitfields, I'd say - some
compilers handle unsigned bitfields
>>actually quite well, say, bitfield [6]. 
>>The only other way is to shift around of course...
>>
>>No need of course to make sure you don't change compiler in a
hurry, or set it
>>configurable. I;m no expert, but AFAIK Bitorder of bitfields is not
guaranteed to
>>overlay in any specifc consistent way between MCUs ot maybe even
different compile
>>settings. Never really tried that enough.
> 
> Yepp I agree, you can do it, but it is not portable and platform and
compiler 
> depended, so you must be aware and check the compiler output everytime
things 
> change. I'm still looking for a nice way to do it in legal C.
> 
> "6.7.2.1 Structure and union specifiers" from the c standards it
says:
> 10 An implementation may allocate any addressable storage unit large enough
to 
> hold a bit-field. If enough space remains, a bit-field that immediately
follows 
> another bit-field in astructure shall be packed into adjacent bits of the
same 
> unit. If insufficient space remains, whether a bit-field that does not fit
is 
> put into the next unit or overlaps adjacent units is
implementation-defined. The 
> order of allocation of bit-fields within a unit (high-order to low-order or

> low-order to high-order) is implementation-defined. The alignment of the
> addressable storage unit is unspecified.
> 12 Each non-bit-field member of a structure or union object is aligned in
an 
> implementationdefined manner appropriate to its type.
> 
> 
> There's a -fpack-struct option for the gcc compiler.
> 
> Greetz,
> 
> 	Georg
> 
> 
>>B rgds
>>Kris
>>
>>
>>
>>
>>
>>
>>.
>>
>> 
>>Yahoo! Groups Links
>>
>>
>>
>> 
>>
>>
>>
>>
>>
> 
> 
> 
> 
> .
> 
>  
> Yahoo! Groups Links
> 
> 
> 
>  
> 
> 
> 
> 


// Send two 12-bit samples x and y packed into 24 bits.
void send_two_samples(unsigned x, unsigned y)
{
  unsigned long data = ((unsigned long)x << 12) + y;
  spi_write(&data, 3);  // assumes little-endian addressing of MSP430
}

Simple.  Forget bitfields.  C isn't that crippled.

--
Paul Curtis, Rowley Associates Ltd  http://www.rowley.co.uk
CrossWorks for MSP430, ARM, and now AVR processors  

> -----Original Message-----
> From: onestone [mailto:onestone@ones...] 
> Sent: 16 January 2005 21:15
> To: msp430@msp4...
> Subject: Re: [msp430] Union/type help..
> 
> 
> Oh the joys of asm toys ;@}
> 
> Al
> 
> Georg Ritter wrote:
> 
> > microbit wrote:
> > 
> >>Hi Micah,
> >>
> >>Weather still bad up there I here.
> >>
> >>
> >>
> >>>Only I'm not sure how to define the adc/spi types. Is 
> there a way to 
> >>>define a data type with an arbitrary number of bits in it? I
can't 
> >>>find any way to do this in my searching.
> >>
> >>
> >>>I need a 12 bit type, and an 8 bit type. Actually, 8 bit is
easy 
> >>>enough, that's a char..
> >>
> >>
> >>Doing it that way you'd need bitfields, I'd say - some
compilers 
> >>handle unsigned bitfields actually quite well, say, bitfield [6].
> >>The only other way is to shift around of course...
> >>
> >>No need of course to make sure you don't change compiler in 
> a hurry, 
> >>or set it configurable. I;m no expert, but AFAIK Bitorder 
> of bitfields 
> >>is not guaranteed to overlay in any specifc consistent way between 
> >>MCUs ot maybe even different compile settings. Never really 
> tried that enough.
> > 
> > Yepp I agree, you can do it, but it is not portable and 
> platform and 
> > compiler depended, so you must be aware and check the 
> compiler output 
> > everytime things change. I'm still looking for a nice way 
> to do it in legal C.
> > 
> > "6.7.2.1 Structure and union specifiers" from the c 
> standards it says:
> > 10 An implementation may allocate any addressable storage 
> unit large 
> > enough to hold a bit-field. If enough space remains, a 
> bit-field that 
> > immediately follows another bit-field in astructure shall be packed 
> > into adjacent bits of the same unit. If insufficient space remains, 
> > whether a bit-field that does not fit is put into the next unit or 
> > overlaps adjacent units is implementation-defined. The order of 
> > allocation of bit-fields within a unit (high-order to low-order or 
> > low-order to high-order) is implementation-defined. The 
> alignment of the addressable storage unit is unspecified.
> > 12 Each non-bit-field member of a structure or union object 
> is aligned 
> > in an implementationdefined manner appropriate to its type.
> > 
> > 
> > There's a -fpack-struct option for the gcc compiler.
> > 
> > Greetz,
> > 
> > 	Georg
> > 
> > 
> >>B rgds
> >>Kris
> >>
> >>
> >>
> >>
> >>
> >>
> >>.
> >>
> >> 
> >>Yahoo! Groups Links
> >>
> >>
> >>
> >> 
> >>
> >>
> >>
> >>
> >>
> > 
> > 
> > 
> > 
> > .
> > 
> >  
> > Yahoo! Groups Links
> > 
> > 
> > 
> >  
> > 
> > 
> > 
> > 
> 
> 
> 
> .
> 
>  
> Yahoo! Groups Links
> 
> 
> 
>  
> 
> 
> 
> 
> 

25% wasted storage for 1 variable though ;@}

Al

Paul Curtis wrote:

> 
> // Send two 12-bit samples x and y packed into 24 bits.
> void send_two_samples(unsigned x, unsigned y)
> {
>   unsigned long data = ((unsigned long)x << 12) + y;
>   spi_write(&data, 3);  // assumes little-endian addressing of MSP430
> }
> 
> Simple.  Forget bitfields.  C isn't that crippled.
> 
> --
> Paul Curtis, Rowley Associates Ltd  http://www.rowley.co.uk
> CrossWorks for MSP430, ARM, and now AVR processors  
> 
> 
>>-----Original Message-----
>>From: onestone [mailto:onestone@ones...] 
>>Sent: 16 January 2005 21:15
>>To: msp430@msp4...
>>Subject: Re: [msp430] Union/type help..
>>
>>
>>Oh the joys of asm toys ;@}
>>
>>Al
>>
>>Georg Ritter wrote:
>>
>>
>>>microbit wrote:
>>>
>>>
>>>>Hi Micah,
>>>>
>>>>Weather still bad up there I here.
>>>>
>>>>
>>>>
>>>>
>>>>>Only I'm not sure how to define the adc/spi types. Is 
>>
>>there a way to 
>>
>>>>>define a data type with an arbitrary number of bits in it? I
can't 
>>>>>find any way to do this in my searching.
>>>>
>>>>
>>>>>I need a 12 bit type, and an 8 bit type. Actually, 8 bit is
easy 
>>>>>enough, that's a char..
>>>>
>>>>
>>>>Doing it that way you'd need bitfields, I'd say - some
compilers 
>>>>handle unsigned bitfields actually quite well, say, bitfield
[6].
>>>>The only other way is to shift around of course...
>>>>
>>>>No need of course to make sure you don't change compiler in

>>
>>a hurry, 
>>
>>>>or set it configurable. I;m no expert, but AFAIK Bitorder 
>>
>>of bitfields 
>>
>>>>is not guaranteed to overlay in any specifc consistent way
between 
>>>>MCUs ot maybe even different compile settings. Never really 
>>
>>tried that enough.
>>
>>>Yepp I agree, you can do it, but it is not portable and 
>>
>>platform and 
>>
>>>compiler depended, so you must be aware and check the 
>>
>>compiler output 
>>
>>>everytime things change. I'm still looking for a nice way 
>>
>>to do it in legal C.
>>
>>>"6.7.2.1 Structure and union specifiers" from the c 
>>
>>standards it says:
>>
>>>10 An implementation may allocate any addressable storage 
>>
>>unit large 
>>
>>>enough to hold a bit-field. If enough space remains, a 
>>
>>bit-field that 
>>
>>>immediately follows another bit-field in astructure shall be packed 
>>>into adjacent bits of the same unit. If insufficient space remains, 
>>>whether a bit-field that does not fit is put into the next unit or 
>>>overlaps adjacent units is implementation-defined. The order of 
>>>allocation of bit-fields within a unit (high-order to low-order or 
>>>low-order to high-order) is implementation-defined. The 
>>
>>alignment of the addressable storage unit is unspecified.
>>
>>>12 Each non-bit-field member of a structure or union object 
>>
>>is aligned 
>>
>>>in an implementationdefined manner appropriate to its type.
>>>
>>>
>>>There's a -fpack-struct option for the gcc compiler.
>>>
>>>Greetz,
>>>
>>>	Georg
>>>
>>>
>>>
>>>>B rgds
>>>>Kris
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>.
>>>>
>>>>
>>>>Yahoo! Groups Links
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>>
>>>
>>>.
>>>
>>> 
>>>Yahoo! Groups Links
>>>
>>>
>>>
>>> 
>>>
>>>
>>>
>>>
>>
>>
>>
>>.
>>
>> 
>>Yahoo! Groups Links
>>
>>
>>
>> 
>>
>>
>>
>>
>>
> 
> 
> 
> .
> 
>  
> Yahoo! Groups Links
> 
> 
> 
>  
> 
> 
> 
> 


Hi Paul,

> // Send two 12-bit samples x and y packed into 24
bits.
> void send_two_samples(unsigned x, unsigned y)
> {
>   unsigned long data = ((unsigned long)x << 12) + y;
>   spi_write(&data, 3);  // assumes little-endian addressing of MSP430
> }
> 
> Simple.  Forget bitfields.  C isn't that crippled.

Yes, but bit shifting-wise the MSP430 is IMO.
But yes, I've always avoided bitfields like the plague.
Well, you know what I mean, but I've seen recently that there's just
that
much more again that can be done with C than I thought :-)

Cheers,
Kris


I see.. Thanks for the heads up on that. 


On Sunday 16 January 2005 01:13 pm, Paul Curtis wrote:
>  Hi,
>
>  > -----Original Message-----
>  > From: Micah Stevens [mailto:micah@mica...]
>  > Sent: 16 January 2005 21:11
>  > To: msp430@msp4...
>  > Subject: Re: [msp430] Union/type help..
>  >
>  > But you could create a bitfield defined structure and make an
>  > array out of that, right? or no?
>
>  No. You'll find that you can take sizeof(structure) and it will
return
>  size in units of chars, not in units of bits.
>
>  Regards,
>
>  -- Paul.
>
>  > On Sunday 16 January 2005 01:01 pm, Paul Curtis wrote:
>  > > You can't create an array of bitfields in C...
>  > >
>  > > > -----Original Message-----
>  > > > From: microbit [mailto:microbit@micr...]
>  > > > Sent: 16 January 2005 20:23
>  > > > To: msp430@msp4...
>  > > > Subject: Re: [msp430] Union/type help..
>  > > >
>  > > >
>  > > > Hi Micah,
>  > > >
>  > > > Weather still bad up there I here.
>  > > >
>  > > > > Only I'm not sure how to define the adc/spi
types. Is
>  >
>  > there > >
>  >
>  > > a way to > > > define a data type with an arbitrary
>  >
>  > number of bits
>  >
>  > > in it? I can't > > find any way to do this in my
searching.
>  > > > >
>  > > > > I need a 12 bit type, and an 8 bit type. Actually, 8
>  >
>  > bit is easy
>  >
>  > > > > enough, that's a char..
>  > >
>  > > >
>  > > > Doing it that way you'd need bitfields, I'd say
- some
>  > > compilers
>  > > handle unsigned bitfields actually quite well, say, >
bitfield [6].
>  > > > The only other way is to shift around of course...
>  > > >
>  > > > No need of course to make sure you don't change
compiler in a >
>  > > hurry, or set it configurable. I;m no expert, but AFAIK >
>  >
>  > Bitorder of
>  >
>  > > bitfields is not guaranteed to overlay in any > specifc
consistent
>  > > way between MCUs ot maybe even different > compile settings.
Never
>  > > really tried that enough.
>  > > >
>  > > > B rgds
>  > > > Kris
>  > > >
>  > > >
>  > > >  >
>  >
>  > > > >
>  >
>  > > .
>  > > >
>  > > >
>  > > > .
>  >
>  > .
>  >
>  >
>  > .


The 2024 Embedded Online Conference