xmega and structs

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

Hi all,

I feel like 100 years when reading the Xmega notes. In the old days, the way to set a bit was:

PORTA |= (1<<PA0);
PORTA &= ~(1<<PA0);

But when looking at the Xmega they have all this fancy struct stuff. Anyone care to give examples on how the two lines above would be in Xmega struct style?

Thanks and have a nice weekend!
/Bjorn

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

Wouldn't it just be this?

PORTA.OUT |= (1<<PA0); 
PORTA.OUT &= ~(1<<PA0); 

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
PORTA.OUT |= (1<<PA0);         // behaves like mega PORTA
PORTA.OUT &= ~(1<<PA0);

PORTA.OUTSET = (1<<PA0);
PORTA.OUTCLR = (1<<PA0);

PORTA.OUTSET = (1<<PA0_bp);    // use bit position
PORTA.OUTCLR = (1<<PA0_bp);

PORTA.OUTSET = (PA0_bm);       // use bit mask
PORTA.OUTCLR = (PA0_bm);

There are app notes for this.

HTH David.

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

AVR1313 says:

/*! \brief Getting started example 1.
 *
 *  This function implements the "Basic Digital I/O" example from the
 *  "Getting Started" section in the application note.
 *
 *  This function will continously read input from 8 switches connected to PORTC
 *  and output the pin state to 8 LEDs connected to PORTD.
 */
void Example1( void )
{
	/* Configure data direction for PORTC and PORTD. */
	PORT_SetPinsAsInput( &PORTC, 0xFF );
	PORT_SetPinsAsOutput( &PORTD, 0xFF );

	/* Continously copy the value from PORTC to PORTD. */
	do {
		uint8_t temp = PORT_GetPortValue( &PORTC );
		PORT_SetOutputValue( &PORTD, temp );
	} while (true);
}
/*! \brief This macro sets the data direction for all I/O pins in the port or
 *         the virtual port.
 *
 *  /note This macro can also be used to access virtual ports.
 *
 *  \param _port           Pointer to the PORT_t or VPORT_t instance.
 *  \param _directionMask  The direction setting mask. The direction for pin n in
 *                         the port is controlled by bit n. A 0 means input. A 1
 *                         means output.
 */
#define PORT_SetDirection( _port, _directionMask) ( (_port)->DIR = _directionMask )



/*! \brief Sets the data direction of a set of pins to output
 *
 *  This macro sets the data direction of the selected port pins to output
 *  without altering the data direction of the other pins in that port.
 *
 *  \param _port         Pointer to the PORT_t instance.
 *  \param _outputMask   A bit mask of the pins to set as output. A one in bit
 *                       location n will configure pin n as output.
 */
#define PORT_SetPinsAsOutput( _port, _outputMask ) ( (_port)->DIRSET = _outputMask )



/*! \brief Sets the data direction of a set of pins to input
 *
 *  This macro sets the data direction of the selected port pins to input
 *  without altering the data direction of the other pins in that port.
 *
 *  \param _port         Pointer to the PORT_t instance.
 *  \param _inputMask    A bit mask of the pins to set as input. A one in bit
 *                       location n will configure pin n as input.
 */
#define PORT_SetPinsAsInput( _port, _inputMask) ( (_port)->DIRCLR = _inputMask )
/*! \brief Set the output value of a set of I/O pins to logic high.
 *
 *  This macro sets the output value of a set of I/O pins to logic high.
 *  (Unless inverted I/O has been enabled for the pins) It does not alter the
 *  pins not specified in the bit mask.
 *
 *  \param _port         Pointer to the PORT_t instance.
 *  \param _setMask      The bit mask of pins to set to logic high level.
 */
#define PORT_SetPins( _port, _setMask) ( (_port)->OUTSET = _setMask )



/*! \brief Set the output value of a set of I/O pins to logic low.
 *
 *  This macro sets the output value of a set of I/O pins to logic low.
 *  (Unless inverted I/O has been enabled for the pins) It does not alter the
 *  pins not specified in the bit mask.
 *
 *  \param _port         Pointer to the PORT_t instance.
 *  \param _clearMask    The bit mask of pins to set to logic low level.
 */
#define PORT_ClearPins( _port, _clearMask) ( (_port)->OUTCLR = _clearMask )

So I'd suggest

	PORT_SetPinsAsOutput( &PORTA, (1<<PA0) );
	PORT_SetPins( &PORTA, (1<<PA0) );
	PORT_ClearPins( &PORTA, (1<<PA0) );

Or if you want to de-macro that:

	(&PORTA)->DIRSET = (1<<PA0);
	(&PORTA)->OUTSET = (1<<PA0);
	(&PORTA)->OUTCLR = (1<<PA0);

But I've never used Xmega and that's after 5 minutes study of AVR1313. It's probably worth studying it (especially the example code) further.

Cliff

EDIT: As David's clearer code shows

(&struct)->xxx

is just

struct.xxx

EDIT2: David, doesn't that make PA0==PA0_bp? Why bother with "_bp" or is this just to differentiate it from a "_bm"?

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

The app notes give you some generic functions. By all means use them, but for simple jobs you might just as well set the register in a simple statement.

The generic routines are brilliant for handling all 8 USARTs in one go. Or for 'complicated' register setting.

David.

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

Quote:

but for simple jobs you might just as well set the register in a simple statement.

[full disclosure: not an Xmega person yet] For "real" jobs where I/O port work is significant enough so that streamlining is desired, I suspect most of us will take advantage of the virtual ports and do the important stuff off them. IN and OUT reg operations should look pretty much like PIN and PORT operations do now.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Thanks for all the great replies guys!

But there some to be a very basic problem. PA0 is not definied for the Xmega. Instead PIN0, PIN0_bm and PIN0_bp should be used. Or am I missing a header?

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

bjornhi wrote:
PA0 is not definied for the Xmega. Instead PIN0, PIN0_bm and PIN0_bp should be used. Or am I missing a header?
No, you are right. These defines are renamed. 'PA0' does not exist anymore. So you don't need any further headers.

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

david.prentice wrote:

PORTA.OUT |= (1<<PA0);         // behaves like mega PORTA
PORTA.OUT &= ~(1<<PA0);

PORTA.OUTSET = (1<<PA0);
PORTA.OUTCLR = (1<<PA0);

PORTA.OUTSET = (1<<PA0_bp);    // use bit position
PORTA.OUTCLR = (1<<PA0_bp);

PORTA.OUTSET = (PA0_bm);       // use bit mask
PORTA.OUTCLR = (PA0_bm);

There are app notes for this.

HTH David.

David - what is the app note number. 1313 seems to mention nothign about these methods

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

Your best bet is to download all the xmega app notes. Once on your harddisk, rename them to something you can understand. e.g. doc1234 -> "AVR1605_xmega_Bootloader" or "AVR1313_xmega_io_pins"

The xmega manual and data sheets describe all the sfr's and what each bit does.

In practice, once you have done a slightly more complicated initialisation, the actual sfr use is much the same as on a mega.

The generic access routines in the app notes are pretty straightforward. You can whittle them down to a direct access and treat the same as a mega. (via macros)

David.

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

AVR1313: Using the XMEGA IO Pins and External Interrupts mentions some files:
The driver package consists of the following files:
• port_driver.c – I/O port driver source file.
• port_driver.h – I/O port driver header file.
• port_example.c – Examples using the I/O port driver.

I'm sorry to admit that I don't know where to find these files. Atmel's search tool didn't turn up anything, neither did my attempt at straight site navigation. Googling port_driver.c and restricting the search to atmel's domain generates only a single hit -- to the application note.

Can someone please help me out?

Thanks.

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

steverino wrote:
I'm sorry to admit that I don't know where to find these files.

Sigh. Found my own answer. The little CD icon next to the pdf icon that represents the app note.

My atmel experience to date has been brief and humbling. I've been working with uP's a long while and I have never had this much trouble getting rolling with a new uP and associated tools.

Very humbling indeed.

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

Quote:

Very humbling indeed.

Blame their website designer. ;-)

(you are not the first person here who didn't spot that the disk icons lead to the .zip files)

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

clawson wrote:
(you are not the first person here who didn't spot that the disk icons lead to the .zip files)

Somehow that doesn't make me feel a whole lot better. But thanks anyway! :-)