> You could also just use the built-in bitfield selection such as: > > // Alarm output > #define ALARMENABLE port2.out.pin5 = 1 > #define ALARMDISABLE port2.out.pin5 = 0 > > A little clearer than having a macro reference another macro to define a port (ie > MakeBit()). >
I agree that it would be nice to dispense with the MakeBit macro. However, an important point for me is that these *Bit macros are highly portable in usage, and fairly portable in definition, across a range of compilers and targets, and also for assemblers, and that the code generated is optimal. A number of the compilers I have used have poor bit-field handling, and might generate lots of shifts and masks instead of a single bit instruction. But by all means, take my code and adapt it to suit yourself and your own code style. > I do like the SetBit, ResetBit, RoggleBit, and GetBit macros tho, thanks! "RoggleBit" has a nice sound to it - I wonder what it should do? Drive the line up and down a few times to losen up the electrons? :-) mvh., David > -Mark > > > -----Original Message----- > From: [email protected] > [mailto:[email protected]] On Behalf Of David Brown > Sent: Tuesday, July 04, 2006 3:18 AM > To: GCC for MSP430 - http://mspgcc.sf.net > Subject: Re: [Mspgcc-users] bitfield code gen questions > > > ----- Original Message ----- > From: "Grant Edwards" <[email protected]> > To: <[email protected]> > Sent: Tuesday, July 04, 2006 3:52 AM > Subject: Re: [Mspgcc-users] bitfield code gen questions > > > > On 2006-07-04, David Smead <[email protected]> wrote: > > > > > You can define all ports and pins in a single file and only need to > > > change that file if you move pins around. I always put defines in > > > io_pins.h and do the following > > > > > > > > > #define ADC_ENABLE_BIT 0x08 > > > #define ADC_PORT P3OUT > > > > > > #define ADC_ENABLE ( ADC_PORT |= ADC_ENABLE_BIT ) > > > #define ADC_DISABLE ( ADC_PORT &= ~(ADC_ENABLE_BIT) ) > > > > True. For some reason I've always found that clumsy. For one > > thing need a third definition for reading the bit: > > > > #define ADC_ENABLE (ADC_PORT & ADC_ENABLE_BIT) > > > > Except that conflicts with with the macro you use to set bit. > > > > > > It always seemed so much cleaner to just do this: > > > > #define ADCEnable P3OUTb.b3 > > > > However you do it, I agree entirely that you want to use a single name for > your pins rather than explicitly using both a port address and bit reference > in the main code. > > Personally, I have used a set of macros for many years. They sometimes need > modification when I start with a new micro or a new compiler, but I have > used the same API for over a decade with something like twenty different > compilers and assemblers. > > > #define ledGreen MakeBit(P3OUT, 3) > #define inputSwitch MakeBit(P3IN, 5) > > > void foo(void) { > SetBit(ledGreen); > OutputEnable(ledGreen); > > while (1) { > if (GetBit(inputSwitch)) { > ResetBit(ledGreen); > }; > }; > } > > > Generated code is optimal (at least, with optimisation enabled), and since > it uses bitmasks at heart, you don't have to worry about the ordering of > bits in bitfields. > > mvh., > > David > > > > > // Put this in a common include file. > > #define pinOffset 0 // Offset from port address for pin inputs > #define portOffset 1 // Offset from port address for port outputs > #define dirOffset 2 // Offset from port address for pin directions > > #define _BitPort(add, bitM) add > #define BitPort(bitAdd) _BitPort(bitAdd) > #define _BitAdd(bitAdd) (unsigned int) &(bitAdd) > #define BitAdd(bitAdd) (unsigned int) &BitPort(bitAdd) > #define _BitMask(add, bitM) bitM > #define BitMask(bitAdd) _BitMask(bitAdd) > > #define _BitPortDir(add, bitM) *(&add + dirOffset - portOffset) > > // NB! Only works for output ports > #define OutputEnable(bitAdd) _BitPortDir(bitAdd) |= _BitMask(bitAdd) > #define OutputDisable(bitAdd) _BitPortDir(bitAdd) &= ~_BitMask(bitAdd) > > #define MakeBit(port, bitNo) port, (1 << bitNo) > > #define SetBit(bitAdd) _BitPort(bitAdd) |= _BitMask(bitAdd) > #define ResetBit(bitAdd) _BitPort(bitAdd) &= ~_BitMask(bitAdd) > #define ToggleBit(bitAdd) _BitPort(bitAdd) ^= _BitMask(bitAdd) > #define GetBit(bitAdd) (_BitPort(bitAdd) & _BitMask(bitAdd)) > > > > Using Tomcat but need to do more? Need to support web services, security? > Get stuff done quickly with pre-integrated technology to make your job easier > Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 > _______________________________________________ > Mspgcc-users mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/mspgcc-users > > > ------------------------------------------------------------------------- > Using Tomcat but need to do more? Need to support web services, security? > Get stuff done quickly with pre-integrated technology to make your job easier > Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 > _______________________________________________ > Mspgcc-users mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/mspgcc-users > > >
