This is something that could be a very useful addition.  In fact, one of the
things I've been testing recently is what sort of code mspgcc produces for
bitfields, since many embedded compilers I have used in the past produce
horrible code for bitfields (the answer is mspgcc makes very good code,
except perhaps in the bizare case of switches on bitfields).

Using bitfields instead of the traditional unsigned char and #defines has
two big advantages that I can see.  One is debugging, now that we have gdb
with a choice of guis (or even the command-line version, which I often use).
You can put these sorts of structures directly in a watch window, and find
the bits you want directly without having to do all sorts of mental
gymnastics with binary convertions.  This is perhaps even more useful for
control registers - you can quickly view them without having to look up bit
numbers.

The other big advantage is in tying the bit number and address together.  I
always think this is very important, particularly for external pins.  For
example, if I have a signal "ledRed" on port 2 pin 5, I have a statement
    #define ledRed P2OUT, (1 << 5)
and I use a couple of macros to set it high or low, such as
"SetBit(ledRed)".  This is much more secure than just using a #define for
the bit number (or bit mask), and less repetative than "ledRedPort |=
ledRedMask".  However, with your suggested structures, I could simply use
"#define ledRed port2.out.pin5" and "ledRed = 1" in the main code.

mvh.

David



> Folks,
> I am thinking of changind some io API (not really changing, but
extensions).
> So, will it worth if we declare ports as  structures:
> --------------------------------------------------------------------------
-------------
> typedef union port {
>   volatile unsigned char reg_p;
>   volatile struct {
>     unsigned __p0:1, __p1:1, __p2:1, __p3:1, __p4:1, __p5:1, __p6:1,
__p7:1;
>   } __pin;
> } ioport_t;
>
> #define pin0    __pin.__p0
> #define pin1    __pin.__p1
> #define pin2    __pin.__p2
> #define pin3    __pin.__p3
> #define pin4    __pin.__p4
> #define pin5    __pin.__p5
> #define pin6    __pin.__p6
> #define pin7    __pin.__p7
>
> typedef struct {
>   ioport_t in, out,dir,ifg,ies,ie,sel;
> } xport_t;
>
> xport_t port0 asm("0x10");
> /// and so on...
> ----------------------------------------------------------------------
> So, user can write:
> port0.out.pin1 = 1;
> xxx = port0.in.pin2;
> port0.reg_p = 0x7e;
> or something.
>
> Of course this will not discard existing declarations.
>
> If we're going to add this declaration to header files, we need to choose
a
> name for portX. Will 'port0 .. port6' be suitable for it?
>
> Or just forget about it?
>
> ~d
>
>
> /********************************************************************
>      ("`-''-/").___..--''"`-._     (\   Dimmy the Wild      UA1ACZ
>       `6_ 6  )   `-.  (     ).`-.__.`)  Enterprise Information Sys
>       (_Y_.)'  ._   )  `._ `. ``-..-'   Nevsky prospekt,   20 / 44
>     _..`--'_..-_/  /--'_.' ,'           Saint Petersburg,   Russia
>    (il),-''  (li),'  ((!.-'             +7 (812)  3468202, 5585314
>  ********************************************************************/
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Mspgcc-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/mspgcc-users
>
>



Reply via email to