xmega usart configuration

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

hi, i'm working with an xmega256A3 on a development board using USART F0 through a MAX232R to talk to my usb port. I'm using zterm to watch port traffic, AVRfuses (avrdude gui) with an AVRISPMKII for programming the chip, and xcode with avr-gcc to actually write the

I think the problem is initializing the usart properly. Following the datasheet you need to set TX high, set TX as output, set the baud rate and frame format, set mode, and then enable RX/TX in that order. Which i thought i was doing.

My main function is:

int main()
{
	uart_init();
	uart_sendchar("c");
	return 1;
}

Where's the problem?

Thanks :)

#include 
#include "driver_uart.h"

#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL)))-1)

void uart_init()
{
	// disable interrupts
	cli();
	
	// set directions
	PORTF.OUTSET = PIN3_bm;
	PORTF.DIRSET = PIN3_bm;
	PORTF.DIRCLR = PIN2_bm;
	
	// set baud rate, frame format
	USARTF0.BAUDCTRLA = 0;
	USARTF0.BAUDCTRLB = 103;
	
	// set mode (asynchronous), 8-bit character, 1 stop bit, no parity
	USARTF0.CTRLC = 0x03;
	
	// configure interrupts
	USARTF0.CTRLA |= USART_RXCINTLVL_LO_gc | USART_DREINTLVL_LO_gc;
	
	// enable rx and tx
	USARTF0.CTRLB |= USART_TXEN_bm | USART_RXEN_bm;
	
	// re-enable interrupts
	sei();
}

void uart_sendchar(char c)
{
	//while (!(USARTF0.STATUS & USART_DREIF_bm)) {};
	USARTF0.DATA = c;
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

scubabuddha wrote:
Where's the problem?
What's the symptom?

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

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

dkinzer wrote:
scubabuddha wrote:
Where's the problem?
What's the symptom?
oh yeah, that :D

um, it doesn't work. I can't get the uC to spit anything at me. I don't have a hardware debugger either... lemme hop on over to windows and try the simulator (i hate avrstudio's editor, a lot)

edit: so it appears that USARTF0.DATA = some value doesn't do anything. How then do you actually put data out on the usart?

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

It seems to me that you are enabling DRE interrupts but not providing an ISR??

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

js wrote:
It seems to me that you are enabling DRE interrupts but not providing an ISR??
You seem to be right...

here's what i've come up with. the ISRs are pretty simplistic (i think) and i implemented a FIFO queue to handle input and output, which I attached to this post. I get literally no output to the console... :?

Here's the main function and usart driver:
main:

#include "my_usart_driver.c"

int main (void)
{
	//	initialize();
	uart_init();
	queue_push(outbuf, "t");
	queue_push(outbuf, "e");
	queue_push(outbuf, "s");
	queue_push(outbuf, "t");
	for(;;)
	 {
		// trying to get the processor to echo what i send it
		while (queue_length(inbuf) > 0){		// if inbuf isn't empty
			queue_push(outbuf,queue_pop(inbuf));// empty it to outbuf
		}
	 }
	return 0;
}

usart driver:

#include  
#include "queue.c" // implements FIFO 

#define USART_BAUDRATE 9600
#define USART_BSCALE 0
#define USART_BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL)))-1)
#define USART_BSEL (F_CPU/(16*(USART_BSEL+1)))

queue * inbuf, * outbuf;

void uart_init() 
{ 
	cli(); 
	// set directions 
	PORTF.OUTSET = PIN3_bm; 
	PORTF.DIRSET = PIN3_bm; 
	PORTF.DIRCLR = PIN2_bm; 
	// set baud rate, frame format 
	USARTF0.BAUDCTRLA = 0; 
	USARTF0.BAUDCTRLB = USART_BSEL; 
	// set mode (asynchronous), 8-bit character, 1 stop bit, no parity 
	USARTF0.CTRLC = 0x03; 
	// configure interrupts 
	USARTF0.CTRLA |= USART_RXCINTLVL_LO_gc | USART_DREINTLVL_LO_gc; 
	// enable rx and tx 
	USARTF0.CTRLB |= USART_TXEN_bm | USART_RXEN_bm; 
	sei(); 
}

ISR(USARTF0_RXCINT_vect)
{
	queue_push(inbuf, USARTF0.DATA);
}

ISR(USARTF0_DREINT_vect)
{
	USARTF0.DATA = queue_pop(outbuf);
}

[/code]

Attachment(s): 

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

Your first posting should work as a polled implementation. i.e. remove the xxIE_bm lines, and remove comment at the STATUS poll.

This will verify your hardware.

The last posting looks ok to me. However why not us / adapt the drivers in AVR1307 ?

David.

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

I had it working polled so I know the hardware's right.

I couldn't get the USART drivers given in the app note to compile and they seemed sort of convoluted to me. Tracking down all the macros and functions and keeping track of what they were all doing got to be too complicated... I feel like it should be simpler than that...

I guess I'll try and adapt the app note drivers...

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

I very much doubt that you are short of FLASH space. I agree that the drivers look a little complicated. This pays off if you are using several USARTs.

Otherwise just take the driver and strip it down. You will end up with exactly the same code as you would have had with a mega. Just different register names. You already have the initialisation which is the harder part.

David.

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

i'm thinking my queue implementation might be to blame. It should work just fine but why reinvent the wheel? I'm gonna try using the stdio library

http://www.nongnu.org/avr-libc/u...

between that and the provided drivers i should be able to get it...