Need to receive string from uart on xmega

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

I'm using atmel's appnote uart code here:
http://atmel.com/dyn/resources/prod_documents/AVR1307.zip

I'd like for the communications to be interrupt driven, so I'm using the UART_data_t buffer to hold the data.

I'm expecting commands to be entered, and they will be finished by something like a CR or LF so the uC will recognize the end of the string, but I am having a hard time getting the receive buffer into a plain old string that I can strncmp with something.

Here is what I have so far:

#include 
#include 
#include 
#include 
#include "menu_driver.h"
#include "menu.h"


#define F_CPU 		2000000UL
#define USB 		USARTC0
#define BAUDRATE 	9600
#define BAUDSET 	0x0C

volatile uint8_t cmd_buff[40];

USART_data_t USART_data;

volatile uint8_t bufCnt = 0;
char U = 0;
char buffer[10];
bool bFlag = false;


void uartSetup()
{
	PORTE.DIRSET = PIN0_bm;
	PORTC.DIRSET = PIN3_bm;
	PORTC.DIRCLR = PIN2_bm;

	USART_Format_Set(&USB, USART_CHSIZE_8BIT_gc, USART_PMODE_DISABLED_gc, false);

	USARTC0.BAUDCTRLA = 0x0C;

	USARTC0.CTRLA = USART_RXCINTLVL_MED_gc | USART_TXCINTLVL_OFF_gc | USART_DREINTLVL_OFF_gc;

	USARTC0.CTRLB = USART_RXEN_bm | USART_TXEN_bm;
}


int main(void)
{	

	uartSetup();
	sendMenu(&mainmenu, &USART_data);

	asm("sei");

	while(1)
	{
		while (bFlag == false)
		{
		}

		while (bFlag == true)
		{
			char str[bufCnt];
			strncpy(str, buffer, bufCnt);
			USART_PutChar(&USB, buffer[0]);
			USART_PutChar(&USB, buffer[1]);
			bFlag = false;
			bufCnt=0;
		}
	}
}
/*
ISR(USARTC0_DRE_vect)
{
	USART_DataRegEmpty(&USART_data);
}
*/
ISR(USARTC0_RXC_vect)
{
   U = USB.DATA;			
   if (U == 0x0D)			
	{  
		bFlag = true;           
		buffer[bufCnt] = U;
	}    

	else
	{   
		bFlag = false;
		buffer[bufCnt] = U;			
		bufCnt++;							if (bufCnt >= 9) {bufCnt=0;}
	} 
}

I get nothing back from the device (other than the sendMenu text) when I send 0x2D 0x0D (dash then CR) to it, where I am expecting to get a dash then a CR from it.

I do not have jtag, just plain AVRISPMKii :(

---
EDIT

Looks like http://www.gabotronics.com/vmchk/development-boards/xmega-xmultikit.htm's source has good uart code... Might try to work with that.

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

If your xmega is running at 2MHz, the app note USART functions take a significant time to initialise. So you can get a false start bit if you do not specifically set the TX line high.

Compiling AVR1307 with CodeVision gave me garbage due to this false start bit. Avr-gcc will have the same effect if you use -O0.

                        PORTC.OUTSET = (1<<3); 
                        PORTC.DIRSET = (1<<3); 

Otherwise the xmega just goes like any other USART. You putchar() and getchar() to and from the buffers in the usual way.

David.

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

Thank you, I might run it faster in the future but I'll set it high just in case, but I think a large part of my problem is that the program never enters the ISR

I put PORTE.OUTSET = PIN0_bm;
as the first line in the RX ISR and it is connected to a LED (through a working buffer), I send data to the board and the rx isr never fires.

With other programs though, I can receive data just fine

Methinks it is a PMIC issue... testing now

---
EDIT

PMIC.STATUS |= 0x07;
//SEI IS NOT ENOUGH ANYMORE! :arrow: :roll:

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

AVR1307 compiles straight out of the box with avr-gcc. Set your PORTE LED in the Receive ISR() and if you never get anything you know that the hardware is wrong.

But if you look at the code, the program dies as soon as it has sent three bytes.

So you are better off writing a more sensible main() function in AVR1307.

David.