Hi Dan,
first I want to "warn" you: AFAIK neither early K&R nor later ANSI standard define how the compiler aligns structures (also bit fields) in memory. It is even correct (standard conform) for the compiler to create a memory layout like
|x|x|P1OUT_3|x|x|P1OUT_4|x|x|x|P1OUT_7|P1OUT_2|x|P1OUT_0|P1OUT_1|x|x|x|x|x|P1OUT_5|x|P1OUT_6|x|x|
as a 24 bit aligned structure. So using this version always results in non-portable, extremely tool dependent code. At least you always have to change the way you tell the compiler to pack it (and hope it is packed in a similar way).

So back to your question itself:

typedef struct
{
  unsigned char P1OUT_0        : 1;
  unsigned char P1OUT_1        : 1;
  unsigned char P1OUT_2        : 1;
  unsigned char P1OUT_3        : 1;
  unsigned char P1OUT_4        : 1;
  unsigned char P1OUT_5        : 1;
  unsigned char P1OUT_6        : 1;
  unsigned char P1OUT_7        : 1;
} __attribute__ ((packed)) P1OUT_TYPE;

#define P1OUT ( ( P1OUT_TYPE* ) 0x0021 )

...

#define RED_LED P1OUT->P1OUT_0

...

   RED_LED = 1;

...

should do the job.
Just remark that char is a non-standard type for bit fields. The standard type is int which does not correspond with the size of your register.
Regards

Arnd-Hendrik


Dan Miner wrote:
I know I was a bit terse in my last email (below) but I was rushing
out to an appointment.  Anyway, IAR defines registers like this:

/************************************/
__no_init volatile union
{
  unsigned char P1OUT;  /* Port 1 Output */

  struct
  {
    unsigned char P1OUT_0        : 1;
    unsigned char P1OUT_1        : 1;
    unsigned char P1OUT_2        : 1;
    unsigned char P1OUT_3        : 1;
    unsigned char P1OUT_4        : 1;
    unsigned char P1OUT_5        : 1;
    unsigned char P1OUT_6        : 1;
    unsigned char P1OUT_7        : 1;
  } P1OUT_bit;
} @ 0x0021;          /* Non-standard syntax (pragma) here... */

__no_init volatile union
{
  unsigned short TBCCTL3;  /* Timer B Capture/Compare Control 3 */

  struct
  {
    unsigned short CCIFG         : 1;  /* Capture/compare interrupt flag */
    unsigned short COV           : 1;  /* Capture/compare overflow flag */
    unsigned short OUT           : 1;  /* PWM Output signal if output mode 0 */
    unsigned short CCI           : 1;  /* Capture input signal (read) */
    unsigned short CCIE          : 1;  /* Capture/compare interrupt enable */
    unsigned short OUTMOD        : 3;  /* Output mode 0 */
    unsigned short CAP           : 1;  /* Capture mode: 1 /Compare mode : 0 */
    unsigned short CLLD          : 2;  /* Compare latch load source */
    unsigned short SCS           : 1;  /* Capture sychronize */
    unsigned short CCIS          : 2;  /* Capture input select */
    unsigned short CM            : 2;  /* Capture mode */
  } TBCCTL3_bit;
} @ 0x0188;          /* Non-standard syntax (pragma) here... */
/************************************/

So that I can write code like this:

#define RED_LED    P1OUT_bit.P1OUT_7
#define GREEN_LED  P1OUT_bit.P1OUT_6

void main (void)
{
    RED_LED = 1;
    GREEN_LED = 0;
}

The advantage here is that it's impossible to use a bitfield definition
from one register on a different, incorrect register.
So given the mspgcc structure definition below, does anyone know how
to use the structure in a manner similar to IAR?  I know how structures
work but I don't see how this relates to the way the register address
is defined:

#define TBCCTL3_            0x0188  /* Timer B Capture/Compare Control 3 */
sfrw(TBCCTL3,TBCCTL3_);


                                - Dan Miner



-----Original Message-----
From: Dan Miner Sent: Tuesday, May 03, 2005 2:54 PM

In many of the register definition include files, there are useful bitfield definitions. For example, from timerb.h:

typedef struct {
 volatile unsigned
   ccifg:1,
   cov:1,
   out:1,
   cci:1,
   ccie:1,
   outmod:3,
   cap:1,
   clld:2,
   scs:1,
   ccis:2,
   cm:2;
} __attribute__ ((packed)) tbcctl_t;

Can someone please point me to documentation or an example of
how to use these?  I'm trying to port some IAR code to mspgcc
which uses their version of register bitfields.

I'm using the Win32 package from 20050422.

                                - Dan Miner



-------------------------------------------------------
This SF.Net email is sponsored by: NEC IT Guy Games.
Get your fingers limbered up and give it your best shot. 4 great events, 4
opportunities to win big! Highest score wins.NEC IT Guy Games. Play to
win an NEC 61 plasma display. Visit http://www.necitguy.com/?r=20
_______________________________________________
Mspgcc-users mailing list
Mspgcc-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mspgcc-users



Reply via email to