Xmega IO with structs and macro usage - problem with macro

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

Not using macros regularly the xmega io system is forcing my hand a little. I've made my own new macro in an attempt to make testing I/O pins easy but have come stuck with a macro not accepting a #define as its arguements, as shown. Any clues TIA.?

// PORT / PIN DEFINITIONS

// Use following standard macro to turn output on
//
// PORT_SetOutputBit( _port, _bitPosition )

// Use following standard macro to turn output off
//
// PORT_ClearOutputBit( _port, _bitPosition )



// This is MY NEW MACRO to get the port data
#define PORT_GetPortBit( _port, _bitPosition ) ( (_port)->IN & (1 << _bitPosition) )


// These are the port defines - note there is one differnt
// I'd like to define a name as a port AND port pin position in one go - if possible

#define bluetooth_power  	( &PORTB, 0 )       // 0 = Power On
#define sensor_power     	( &PORTB, 1 )       // 0 = Power On
#define red_led           	( &PORTF, 6 )      // 1 = Led On
#define green_led         	( &PORTF, 6 )    // 1 = Led On
#define link_established_bp		1


//This is how it works - and this compiles ok

if (PORT_GetPortBit( &PORTC, link_established_bp ) == 0) {}

//This is how I WANT to use it- bit this fails
//complaining the macro only gets one argument

if (PORT_GetPortBit( sensor_power) == 0) {}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

rowifi wrote:
a macro not accepting a #define as its arguements,
I believe that the problem will be rectified by removing the parentheses.
#define bluetooth_power     &PORTB, 0    // 0 = Power On
#define sensor_power        &PORTB, 1    // 0 = Power On
#define red_led             &PORTF, 6    // 1 = Led On
#define green_led           &PORTF, 6    // 1 = Led On

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

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

If macros make your skin crawl, then why use them?

static inline uint8_t getSensorPower()
{
    return (&PORTB)->IN & (1 << 1);
}

Regards,
Steve A.

The Board helps those that help themselves.

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

If this is an Xmega, why not use the individual port bit operations directly, instead of your own macro stuff? Read section 13.14 in the XMega A manual, and application note AVR1000 and forget about your macros.

PORTB.OUTSET = PIN0_bm;  // Bluetooth power on
PORTB.OUTCLR = PIN0_bm;  // Bluetooth power off

Stealing Proteus doesn't make you an engineer.

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

Don : Removing the brackets doesn't fix it.

What does work is:

if (PORT_GetPortBit sensor_power == 0) {} 

Which is ok so long as no brackets are defined at the point of using the macro. Unfortunately it doesn't look or read nice.

My goal was to define a pinname which encompassed the port AND the pin position in one go, rather that having two defines - one for the Port and another for the Pin position.

My issue with the following code is that it doesn't use a single define - I would still need to substitute two definitions for the PORT and the Bit position:

PORTB.OUTSET = PIN0_bm;  // Bluetooth power on 
PORTB.OUTCLR = PIN0_bm;  // Bluetooth power off 

Maybe I'm trying to do something that isn't the accepted way.

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

Quote:

My issue with the following code is that it doesn't use a single define
Oh, our royal highness has issues. What about wrapping it in a shitty macro? But let me guess, you didn't even read the manual, and you have no real clue about C?

#define first(a, b)   (a)
#define second(a, b)  (b)

#define IShitMyPantsUsingAnAssignmentToSetAPinSoIUseThisMacro(c)  \
    do { first(c).OUTSET = second(c); } while(0)

#define WHATEVER  PORTB, PIN0_bm

IShitMyPantsUsingAnAssignmentToSetAPinSoIUseThisMacro(WHATEVER);

Stealing Proteus doesn't make you an engineer.

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

rowifi wrote:
What does work is:
if (PORT_GetPortBit sensor_power == 0) {} 

Which is ok so long as no brackets are defined at the point of using the macro. Unfortunately it doesn't look or read nice.

This should work

#define bluetooth_power     &PORTB, 0
#define sensor_power        &PORTB, 1
#define red_led              &PORTF, 6
#define green_led            &PORTF, 6

#define PORT_GetPortBitN(name) PORT_GetPortBit(name) 

if (PORT_GetPortBitN( sensor_power) == 0) {} 

But mix of PORT_GetPortBitN(name) and PORT_GetPortBit(port,bit) may be doesn't look nice also.

wbr, ReAl

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

Thanks Arnold -

But I'd sooner not have your help or advice if it comes wrapped with attitude.

Either offer help - make it clear and be decent about it or be the prat that you seem to be and mock everybody else who you believe to be less skilled at C than you.