Bizarre uart error

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

I am getting the strangest problem with my code. I consider myself pretty good at troubleshooting but this has me really stumped.

I'm programming on xmega128a1 but I don't think it's directly related to the chip so I'm posting it here.

My project is for a GPS controlled RC car, and here is my main and System_Init functions.

int main(void)
{		
	System_Init();
	
	for(;;)
	{		
		/* Pass control to the autonomous library */	
		Start_Auto_Control();
	}
}

void System_Init(void)
{
	uint8_t Msg_Start[] = "Program is starting...\n\r";

	Set_Clock_To_32MHz_Ext();
	USART_Init();
	Motors_Init();
	//ADC_Init();
	
	/* Enable high level interrupt. */
	PMIC.CTRL = PMIC_HILVLEX_bm;	
	
	/* Initialize the ring buffers */
	RingBuffer_InitBuffer(&GPS_Buffer);
	RingBuffer_InitBuffer(&Control_Buffer);
	
	/* Iniitialize system vars */
	System.CTRLA = (PIN3_bm) | (PIN2_bm) | (PIN1_bm);
	System.Drive_Range = 75;
	System.Steer_Range = 74;
	System.Drive_Center = 375;
	System.Steer_Center = 375;
	
	PC_NewPage();
	PC_String(Msg_Start);
	
	/* Enable global interrupts */
	sei();
}

This works completely fine when like this. The Start_Auto_Control does a bunch of GPS processing and calculations which has been verified to work correctly. I know my uart functions work because it normally prints 'Program is starting...' to the terminal as shown in my Init functions

The problem starts when I want to start printing debug information through the uart, but only at specific points. If I add a print line above the for() loop as shown below, it still works fine. A 5 prints out on the screen one time.

int main(void)
{		
	System_Init();
	
	PC_Print(0x35);

	for(;;)
	{		
		/* Pass control to the autonomous library */	
		Start_Auto_Control();
	}
}

But if I add it inside the loop like this...

int main(void)
{		
	System_Init();
	
	for(;;)
	{
		PC_Print(0x35);
		
		/* Pass control to the autonomous library */	
		Start_Auto_Control();
	}
}

Nothing prints out at all in my entire project. It's like all the uarts get disabled somehow. I have other print statements in different places in my project and they all just stop working. I know the code is running because the motors will start responding as soon as I program it it. I've tried disconnected the mkii, cycling power, pressing reset.

If I start debugging, it will still not print, but here's where it gets weird. If I pause the execution it will start to print for about half a second and then stop. I can click run, stop, run stop and each time it will print for half a second and then stop. But when I run normally nothing shows up.

I've also determined I can place my print functions deeper inside the code and there will be no problems. It seems to only freeze when it's placed in the highest points of the loop.

I also have two ISRs that transfer incoming uart data to ring buffers. I thought it might be something with them so I got rid of all interrupts and that did nothing. I'm using a jtagice mkii.

Any suggestions?

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

Ok so one more thing I found out is that it only seems to break the printing if I print to USARTC0 which is what my computer is hooked up to. If I print to any other uart like C1, E0, E1 then everything else prints fine.

It's weird because the two uarts are initialized exactly the same way...

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

Does it print in the loop if you comment out the Start_Auto_Control()?

Smiley

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

smileymicros wrote:
Does it print in the loop if you comment out the Start_Auto_Control()?

Smiley

Hi Smiley, no it does not. I tried commenting that out so print was the only item and it didn't work that way either.

I got around the problem by using a timed ISR to print stuff, but I'm still puzzled by it.

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

Did you write Start_Auto_Control()? With a name like that, you'd think it would only need to be called once, not every few dozen microseconds like you're doing.

For a simple trial I'd either pull it out of the for loop, or put a pause of a couple of seconds inside the for loop. Just for grins.

Chuck Baird

"I wish I were dumber so I could be more certain about my opinions. It looks fun." -- Scott Adams

http://www.cbaird.org

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

Without seeing your PC_print function I can only speculate, but I'd guess that you should put it in the loop with a 100ms delay and see if it can print okay. I's speculate that your code is stepping on itself, probably something in the way you are loading the buffers or the way the ISR is written, but we'd have to see that code to know.

Smiley