select and channels

Technical questions regarding the XTC tools and programming with XMOS.
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

select and channels

Post by Folknology »

Hi folks

I am trying to solve a generic issues with channels and think I'm being stupid here but, I would like to take input from a channel but do not wish to block the thread as it has more important stuff to be getting on with. My first thought is to use a select statement of some sort. The channel is receiving integers in and asynchronous manner, I don't know when they are coming and I need to keep the channel open through the life of the thread. I also need to make decisions based on the value of the int received over the channel which effects how this thread operate. In the XC prog manual I only found one select statement combined with a channel which was based around a master slave but this channel use is not transaction like (not fixed number of transfers) so that is not suitable. Any ideas, any references to channels used in selects or some other method that doesn't block the thread?

Hers the kind of thing I'm talking about (pseudo code):

Code: Select all

int RESET;
int START;
int STOP;

void gatherdata(chanend control,int data[], int size) {
...
while(1){
  select {
   case control :> x when x == RESET :
     reset();
     break;
   case control :> x when x == START :
     start();
     break;
   case control :> x when x == STOP :
     return 0;
   case p pinusneg(0) :> void :
    collect_data(data,size);
    break;
  ... other case statements..
  }
}
}
Last edited by Folknology on Sun May 30, 2010 3:49 pm, edited 1 time in total.


User avatar
f_petrini
Active Member
Posts: 43
Joined: Fri Dec 11, 2009 8:20 am

Post by f_petrini »

If I understand your post correctly you are on the right track.
Mixing channel statements and pin statements in the same select is the way to go.

Here's an example from one of my projects. I've got a single thread acting as a LED controller and button listener. The select will block and wait for any event on either the channels or the pins.

Code: Select all

		select
		{
			case button_A when pinsneq(a) :> a:
				...
				break;
			case button_B when pinsneq(b) :> b:
				...
				break;
			case ledch_bot :> x:
				...
				break;
			case ledch_top :> x:
				...
				break;
		}
You can also use a default case to get non-blocking selects.
The channel case in this example will only be executed if there is data available in the channel, otherwise the default case is directly executed:

Code: Select all

  select
  {
    case ch :> x:
      ...
      break;
    default:
      ...
      break;
  }
Since you can't use a channel in more than one case at a time your example needs a minor modification to work:

Code: Select all

select {
   case control :> x :
      switch(x)
      {
          case RESET :
            reset();
            break;
          case START :
            start();
            break;
          ...
      }
      break;
    case p pinusneg(0) :> void :
      collect_data(data,size);
      break;
}
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

Post by Folknology »

Thanks Fredrik

I figured the channel use might be single case only, like ports, so the extra switch isn't an issue and isn't completely unexpected...

Thank you for the concise suggestions
regards
Al