Channel Blocking Confusion

New to XMOS and XCore? Get started here.
Post Reply
wowden
New User
Posts: 2
Joined: Fri Oct 01, 2010 12:59 am

Channel Blocking Confusion

Post by wowden »

Hello XMOS community,

I am new to the XMOS environment and I am trying to get my feet wet. I think there is something I am failing to understand about channels. I am running two function simultaneously; one to measure the frequency of a button being pressed, while the other will request the frequency and print it. However, when I run the code it blocks and a<:1 and never enters the 1st case. What am I doing wrong? Thank you for your help.

Cheers,
Wowden

Code: Select all


/*test code*/

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

void chopFreq( chanend chopI, chanend chopO, in port chop_ind);
void bar(chanend a, chanend b, in port B);

on stdcore [0]: in port But1      = PORT_BUTTON_A;
on stdcore [0]: in port But2      = PORT_BUTTON_B;

int main(void)
{	 chan a,b;
	par{

		chopFreq(a,b, But1);
		bar(a,b, But2);

	}
	return '0';
}

void chopFreq(chanend chopI, chanend chopO, in port chop_ind)
{	timer t;
	int garbage=0;
	unsigned time1;
	unsigned time2;
	unsigned freq=-1;
	int state;
	int count=0;
	t:>time1;
	while(1){
		select{
			case chopI :> garbage :
				chopO <: freq;
				break;
			default:
				chop_ind :> state ;
				if(state==14){
					count++;
					chop_ind when pinseq(15):>void;
				}
				break;
		}
		if(count ==10){
			t:>time2;
			count=0;
			freq=(time2-time1)/1000000;
			t:>time1;
		}
	}

}


void bar(chanend b,chanend a, in port B){
	int temp=1;
	while(1){
		B when pinseq(14) :> void;
		a <: 1;
		b :> temp;
		printint(temp);
	};
}





User avatar
bsmithyman
Experienced Member
Posts: 126
Joined: Fri Feb 12, 2010 10:31 pm
Contact:

Post by bsmithyman »

There are a couple of things you might want to look at: for one thing, channels are bidirectional, so you should only need a or b, not both. That shouldn't really cause problems though, it's just not very efficient.

I think the issue might be due to your default case in the select statement. If you give "chop_ind when pinsneq(0xf)" as the guard it will check both cases without getting blocked by the input on But1. I'm not sure if that's exactly what's causing the issue, but it's worth a try. Also as a matter of style, you might want to consider using something like a pinsneq() on the previous value of the button; I'm assuming you have an XC-2 card, but it took me a while to figure out what was going on with the state==14 and pinseq(15) parts. I think it might be easier for others to debug if it didn't depend on knowing how the ports on the XC-2 work.
wowden
New User
Posts: 2
Joined: Fri Oct 01, 2010 12:59 am

Post by wowden »

Hello,

Thank you for your help. Once I replaced the two channels with a single channel, the program started to work. I am puzzled to why, though.
User avatar
bsmithyman
Experienced Member
Posts: 126
Joined: Fri Feb 12, 2010 10:31 pm
Contact:

Post by bsmithyman »

No worries :)

It took me a second look to realize it, but in your original bar function, you specify chanends a and b in the wrong order; so you were trying to do a read on two ends of the same channel, and a write on two ends of the same channel. Looks like that was the problem :)

If everything works now, your default statement should let things sleep whereas my suggestion wouldn't; I'd go with the method you had.
Post Reply