Driving a clock only if data is ready?

Technical questions regarding the XTC tools and programming with XMOS.
User avatar
Interactive_Matter
XCore Addict
Posts: 216
Joined: Wed Feb 10, 2010 10:26 am

Driving a clock only if data is ready?

Post by Interactive_Matter »

Hi,

I am outputting data to a HL1606 string. It has an SPI like interface.
So it has a clock wchih is signaling the data put out.
I managed to set up a clocked port as specified in Programming in XC manual.
But I do not trust the clock - or to be more specific I do not trust my code to output the data fast enugh. So I want to ensure that I have a clock port which is clocking only when there is data to clock on the port - so putting something like 16 bits on the port and driving the clock just for 16 cycles.
I found a completely different question here http://www.xcore.com/forum/viewtopic.php?p=6067#p6067

Code: Select all

clock clk2Mhz = XS1_CLKBLK_1;
buffered out port:32 CLCK;
...
configure_clock_rate(clk2Mhz,50,25);
configure_out_port ( CLCK , clk2Mhz, 0 );
start_clock(clk2Mhz);
// Output clock edges
CLCK <: 0x55555555;
...
output 32 data values here
...
// Output next clock edges
CLCK <: 0x55555555;
...


Is this part of the solution I need?
Can I use the CLCK output port as clock source for the data output port like this:

Code: Select all

configure_clock_src(clkData, CLCK);
and use that as output clock for the data port?

Code: Select all

configure_out_port(data_port, clkData, 0);
Tha would require two clocks?

I just want to output the clock signal only if there is data on the port. Or just stop the clock after I have output some data until new data is available. Just found no easy answer in the documentation

Thanks

Marcus


User avatar
boeserbaer
Active Member
Posts: 51
Joined: Fri Jan 29, 2010 4:36 pm

Post by boeserbaer »

There are two immediate options as I see it:

1. "BIT BANG" everything.
I would consider this the easiest.

example:

Code: Select all

unsigned char rtcRead(unsigned char addr, unsigned status, out port spi_sclk,
		out port spi_mosi, out port rtc_ss, in port spi_miso) {
	timer t;
	unsigned int time;
	unsigned command;
	unsigned x;
	unsigned shifting = 8;

	command = 0;
	command |= ((addr << 0) & 0x0000007f);
	x = bitrev(command) >> 24;

	spi_sclk <: 0;
	spi_mosi <: >> x;
	status &= 0x0e;
	rtc_ss <: status;

	t :> time;
	time += M41T93_BIT_TIME;
	t when timerafter ( time ) :> void;
	while (shifting--) {
		time += M41T93_BIT_TIME / 2;
		t when timerafter ( time ) :> void;
		spi_sclk <: 1;
		time += M41T93_BIT_TIME / 2;
		t when timerafter ( time ) :> void;
		spi_sclk <: 0;
		spi_mosi <: >> x;
	}
	time += M41T93_BIT_TIME / 2;
	shifting = 8;
	x = 0;
	t when timerafter ( time ) :> void;
	spi_mosi <: 0;
	while (shifting--) {
		time += M41T93_BIT_TIME / 2;
		t when timerafter ( time ) :> void;
		spi_miso :> >> x;
		spi_sclk <: 1;
		time += M41T93_BIT_TIME / 2;
		t when timerafter ( time ) :> void;
		spi_sclk <: 0;
	}
	time += M41T93_BIT_TIME / 2;
	t when timerafter ( time ) :> void;
	//	spi0_miso :> >> x;
	status |= 0x01;
	rtc_ss <: status;
	return (unsigned char)(bitrev(x) & 0x000000ff);
}

2. Buffered output, bit bang the clock only, and let the xmos hardware shift out the data for you. Example code in the thread: https://www.xcore.com/forum/viewtopic.php?f=7&t=1117

Regards Mike
Last edited by octal on Tue Mar 22, 2011 8:43 pm, edited 1 time in total.
Reason: Please quote your code with [code][/code] tags
User avatar
Interactive_Matter
XCore Addict
Posts: 216
Joined: Wed Feb 10, 2010 10:26 am

Post by Interactive_Matter »

Yep,

that is how I have done it at the end. I was hoping to get some of the computational load off my code and performed by the port buffer.