Basic clock question

Technical questions regarding the XTC tools and programming with XMOS.
Post Reply
jmtaylor
Member
Posts: 15
Joined: Tue Aug 20, 2013 10:01 am

Basic clock question

Post by jmtaylor »

I have been trying out some basic timed I/O with the XK-1A development board, but it appears I don't understand how to use clocked ports correctly. I have two alternative bits of code, which I would expect to flash LEDs with an equal mark-to-space ratio. One uses timers, the other a clocked port. The former works, but the latter doesn't (the mark-to-space is not equal, and the durations seem unpredictable compared to the values I specify).

I've posted some code below that illustrates my problem - is anybody able to glance at this and see what I have misunderstood?

Many thanks
Jonny.

Code: Select all

out port p_led = on tile[0]:XS1_PORT_4F;
clock clk	= XS1_CLKBLK_1;

int main()
{
	  int count, i;
#if 1
	  // Experimenting with clocked port
	  printf("clocked port\n");
	  configure_clock_ref(clk, 0);
	  configure_out_port(p_led, clk, 0);
	  start_clock(clk);

	  p_led <: 0x1 @ count;
	  while(1)
	  {
		  // I would expect this to give an equal mark-to-space ratio, but it doesn't
		  for (i = 0; i < 5000; i++)
		  {
			  count += 1000000;
			  p_led @ count <: 0x2;
		  }
		  for (i = 0; i < 500; i++)
		  {
			  count += 10000000;
			  p_led @ count <: 0xC;
		  }
	  }
#else
	  // Experimenting with timer, instead
	  timer t;
	  printf("timer\n");
	  t :> count;

	  while(1)
	  {
		  // This gives an equal mark-to-space ratio as I would expect
		  for (i = 0; i < 50; i++)
		  {
			  count += 1000000;
			  t when timerafter(count) :> void;
			  p_led <: 0x2;
		  }
		  for (i = 0; i < 10; i++)
		  {
			  count += 5000000;
			  t when timerafter(count) :> void;
			  p_led <: 0xC;
		  }
	  }
#endif
	return 0;
}


jmtaylor
Member
Posts: 15
Joined: Tue Aug 20, 2013 10:01 am

Post by jmtaylor »

Behaviour under the simulator in the clocked case is also bizarre. It seems to work for "smallish" clock intervals, but at larger clock intervals it behaves unpredictably.

The only thing I wonder is whether the chip might be going to sleep, and stopping the clock - that could I suppose explain the unexpected behaviour when running "live". Is there any chance that might be what is going on? Perhaps this is not what clocked ports are intended to be used for...
User avatar
Bianco
XCore Expert
Posts: 754
Joined: Thu Dec 10, 2009 6:56 pm
Contact:

Post by Bianco »

The timer of a port is 16 bit. Try an increment value below 65k and save the counter value in a unsigned short.
jmtaylor
Member
Posts: 15
Joined: Tue Aug 20, 2013 10:01 am

Post by jmtaylor »

Ah thankyou! I knew I would have missed something basic. As you can see, I'd assumed it was 32 bit.
Post Reply