UART won't run

New to XMOS and XCore? Get started here.
Ganux
Active Member
Posts: 35
Joined: Tue Mar 08, 2011 12:58 pm

UART won't run

Postby Ganux » Tue Apr 19, 2011 10:08 am

Hello,

I have an XC-3 development kit; and I'm trying to get the UART running, but I can't.
This is the code I wrote:

Code: Select all

#include <xs1.h>
#include <print.h>
#include <platform.h>

#define UART_BIT_RATE 115200
#define BIT_LENGTH   (XS1_TIMER_HZ / UART_BIT_RATE)
#define STOP_BIT_LENGTH	(BIT_LENGTH *2)
#define FALSE 	0
#define TRUE	1

void uart_transmit_init(out port txd);
void uart_transmit(out port txd, char bytes[], int numBytes);
void uart_receive(in port txr, char bytes[], int numBytes);

on stdcore[0] : out port txd = PORT_UART_TX;
on stdcore[0] : in port txr = PORT_UART_RX;

int main(void)
{
	char transmit[] = { 11 , 242, 57 };
	char receive[] = { 0, 0, 0 };

	/* Initialize the transmitter */
	uart_transmit_init(txd);

	/* Run the transmitter and receiver in parallel */
	par {
		uart_transmit(txd, transmit, 3);
		uart_receive(txr, receive, 3);
	}

	/* Print out the received values *//*
	for (int i=0;i<3;i++) {
		printIntln(receive[i]);
	}*/
	return 0;
}


void uart_transmit_init(out port txd)
{
	/* Set pin output to 1 initially to indicate no transmission */
	txd <: 1;
}

void uart_transmit(out port txd, char bytes[], int numBytes)
{
	unsigned time;
	for (int i = 0; i < numBytes; i += 1)
	{
		int byte = bytes[i];
		// Start bit
		txd <: 0 @ time;
		// Data bits
		for (int j = 0; j < 8; j += 1)
		{
			time += BIT_LENGTH;
			txd @ time <: >> byte;
		}
		// Stop bit
		time += BIT_LENGTH;
		txd @ time <: 1;
		time += STOP_BIT_LENGTH;
		txd @ time <: 1;
	}
}


void uart_receive(in port txr, char bytes[], int numBytes)
{
	for (int i = 0; i < numBytes; i += 1)
	{
		unsigned time; // Tracks previous port event time
		unsigned startBitTime; // Time at beginning of the start bit
		int input_val = 0; // The current value being inputted
		int got_start_bit = FALSE; // Whether start bit has been received
		// Wait for start bit of the required length
		while (!got_start_bit) {
			txr when pinseq(0) :> int x @ startBitTime;
			// Wait until 0 returned for half of BIT TIME
			// or revert to a 1 on the port
			got_start_bit = TRUE;
			while (got_start_bit && time < (startBitTime + BIT_LENGTH/2)) {
				int x;
				txr :> x @ time;
				if (x==1)
					got_start_bit = FALSE;
			}
		}
		// Data bits
		for (int j = 0; j < 8; j += 1)
		{
			time += BIT_LENGTH;
			txr @ time :> >> input_val;
		}
		// Input will by in the high byte of input val
		// need to shift it down into the low byte
		bytes[i] = (unsigned char) (input_val >> 24);
	}
}
Can anyone help me get this one going? ;-)

Greetz,
Ganux_
Ganux
Active Member
Posts: 35
Joined: Tue Mar 08, 2011 12:58 pm

Postby Ganux » Thu Apr 21, 2011 10:04 am

Anyone?

must i tweak the XTAG (1) controller in UART mode?

Ganux_
Heater
Respected Member
Posts: 296
Joined: Thu Dec 10, 2009 10:33 pm

Postby Heater » Thu Apr 21, 2011 9:14 pm

Ganux,

I tried running your tx code under the simulator.

One thing I noticed in the signal trace is that the line state is set to 1 for a very short time prior to the first start bit. I might suspect any receiver might not synch up on that correctly.

Anyway even after modifying things to give 10 bits of 1 prior to the first char I did not get any characters received by the simulator.

Just now I can't get anything down loaded to my dev board.
Heater
Respected Member
Posts: 296
Joined: Thu Dec 10, 2009 10:33 pm

Postby Heater » Thu Apr 21, 2011 9:29 pm

Ganux,

Got it working!

Go to the "run" menu, select "run configurations".

In the device options select "run on hardware" and find your correct adapter hardware.

Check the boxes "Run I/O server" and importantly "Run UART server".

Hit "apply" and "run". It should work now printing your transmitted message on the console.

Seems my comment about putting the line high longer at the start is unnecessary it works for me as you have it.

I had to have a look at your problem as I was about to start with a UART myself.
Ganux
Active Member
Posts: 35
Joined: Tue Mar 08, 2011 12:58 pm

Postby Ganux » Thu Apr 21, 2011 10:56 pm

Hi,

Thx for the response!!
now i got this following problem.. ( yes, I'm new to XMOS =D )
I did these things you said,
but i think i can't get it programming..
when i press run it seems to get compiled..
Then it enters UART_TEST Release (1) [XCore Application] xrun (21/04/11 23:54).
but then it directly gets <terminated>..
When i program with "Flash Programmer" i have this output in red from the memory that gets written,
But now i don't get any of these..

Greets,
Ganux_
Heater
Respected Member
Posts: 296
Joined: Thu Dec 10, 2009 10:33 pm

Postby Heater » Fri Apr 22, 2011 8:10 am

Ganux,

I'm pretty new to all this as well, only started playing with my XC1-A recently.
Anyway if you are having trouble running your UART it is probably time to step back and work through the initial tutorial examples again just to be sure your tools and hardware are working correctly.

Here is my version of the xc1a-uart tutorial example that works here.

Code: Select all

#include <platform.h>
#define BIT_RATE 115200
#define BIT_TIME XS1_TIMER_HZ / BIT_RATE

out port uart_tx = PORT_UART_TX;

// Forward declaration
void txByte (out port TXD, int byte);

int main()
{
	char msg[] = "Hello World!\n";
	int i;

	while (1)
	{
		for (i = 0; i < sizeof(msg) - 1; i++)
		{
			txByte(uart_tx, msg[i]);
		}
	}
	return 0;
}

void txByte (out port TXD, int byte)
{
	unsigned time;
	timer t;

	// Input initial time
	t :> time;

	// Output start bit
	TXD <: 0;
	time += BIT_TIME;
	t when timerafter (time) :> void;

	// Output data bits
    for (int i=0; i <8; i++)
    {
    	TXD <: >> byte;
    	time += BIT_TIME;
    	t when timerafter (time) :> void;
    }

    // Output stop bit
    TXD <: 1;
    time += BIT_TIME;
    t when timerafter (time) :> void;
}
Ganux
Active Member
Posts: 35
Joined: Tue Mar 08, 2011 12:58 pm

Postby Ganux » Fri Apr 22, 2011 10:31 am

Hi,

can i get the output of the UART directly in the XDE?
untill now, I used PUTTY to see the output of my XC-3, but still nothing..

Ganux_
Ganux
Active Member
Posts: 35
Joined: Tue Mar 08, 2011 12:58 pm

Postby Ganux » Fri Apr 22, 2011 11:55 am

maybe it might be because i first downloaded it in the flash (xflash) instead as een application (xrun)..
is there a 'standard' that should be programmed in the flash memory first?

Ganux_
Heater
Respected Member
Posts: 296
Joined: Thu Dec 10, 2009 10:33 pm

Postby Heater » Fri Apr 22, 2011 1:31 pm

Ganux,
can i get the output of the UART directly in the XDE?
Yes. I have been running the last "Hello World" UART example as an Xcore application with output displayed in the XDE console window.

Strangely selecting "Run->Run As" from the top menu bar did not show me any options to select.
But right clicking the mouse on the uart project in the project explorer brings up a menu with a "Run As" option and from there you can select between "Write to Flash Memory" or Xcore Application"
On selecting "Xcore Application" it runs, the XDE console tab opens up and there I see the output coming in red.

Now, if I write it to flash instead I never see any output. Go back to xcore application and it works as above again.

Having written it to flash I can get the output on the command line with xrun:

Code: Select all

$ xrun --id 0 --uart --noreset uart_Debug.xe
Power cycle the thing and its still there !

Now a question for you: How did you get to use PUTTY to see your UART output?
I'm using Linux so I'd like to be able to connect with GtkTerm or minicom or such. I have no idea what the serial device is.
Ganux
Active Member
Posts: 35
Joined: Tue Mar 08, 2011 12:58 pm

Postby Ganux » Sat Apr 23, 2011 11:19 am

UART.jpg
Hi,

I think i got ip (partially) working.
now I do get some output, but it are al just squares (check image..)
Anyone got this problem before?
is there kind of a language pack i do need to install?
You do not have the required permissions to view the files attached to this post.

Who is online

Users browsing this forum: No registered users and 3 guests