Channel Array problems.

Technical questions regarding the XTC tools and programming with XMOS.
Matt
Active Member
Posts: 50
Joined: Sat Feb 13, 2010 12:04 pm

Channel Array problems.

Post by Matt »

Hi Guys,

I am having problems with the following code and I have not idea why. If I change the following code to not use a channel array and to pass the channels individually as parameters the problem goes away. The error is "the reasource is not avaliable".

Code does not work.

Code: Select all

void xc2_firmware_config(chanend config_ch[]) 
{
  while (1) {
    select 
      {
      case (int i=0;i<num_config_ch;i++) config_ch[i] :> int cmd:
          processCommand(config_ch[i], cmd);
        break;
      }
  }
  return;
}
Code does work.

Code: Select all

void xc2_firmware_config(chanend config_ch1, chanend config_ch2) 
{
  while (1) {
    select 
      {
      case config_ch1 :> int cmd:
          processCommand(config_ch1, cmd);
        break;
      case config_ch2:> int cmd:
          processCommand(config_ch2, cmd);
        break;
      }
  }
  return;
}
I know this error can occur if you have a type miss match when passing variable down the channels. I check for this and can not see any errors of this type. I just don't know why the two different method of doing the same thing produce different results.

Is there something I am missing.

Cheers

Matt


User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

Post by Folknology »

As far as I know, when you create a channel 'chan c' it doesn't bind its ends. however in a par{} construct you call functions with a channel. But the function expects a channel end, so the compiler binds the ends of the channel and passes the ends into the functions. I guess with an array of channels it doesn't know which channels to bind. I wondered if channels could be used within Structs but feared the same issues may occur due to the 'magic' the compiler performs with channels in par blocks. It would be nice to have an official explanation of using channel's in other situations like arrays and structs, but it may be that they cannot be used in this way outside of C or ASM.
Matt
Active Member
Posts: 50
Joined: Sat Feb 13, 2010 12:04 pm

Post by Matt »

The xc2_firmware_config thread in the xc2 firmware 1v3 uses the same method to iterate through the channels used.

M.
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

Post by Folknology »

I would need to see the calling situation of the functions are they wrapped by a replicator? I would perhaps need to look at the firmware code also to understand better, but I'm just on my way out, will look later, where is the firmware code, link?

regards
Al
richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post by richard »

You code looks fine to me. I tried constructing a program around your snippet of code:

Code: Select all

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

int num_config_ch = 2;

void processCommand(chanend config_ch1, int cmd)
{
  printstr("Received cmd: ");
  printintln(cmd);
}

#define CHAN_ARRAY

#ifdef CHAN_ARRAY
void xc2_firmware_config(chanend config_ch[])
{
  while (1) {
    select
      {
      case (int i=0;i<num_config_ch;i++) config_ch[i] :> int cmd:
          processCommand(config_ch[i], cmd);
        break;
      }
  }
  return;
}
#else
void xc2_firmware_config(chanend config_ch1, chanend config_ch2)
{
  while (1) {
    select
      {
      case config_ch1 :> int cmd:
          processCommand(config_ch1, cmd);
        break;
      case config_ch2:> int cmd:
          processCommand(config_ch2, cmd);
        break;
      }
  }
  return;
}
#endif

void testbench(chanend config_ch[])
{
  timer t;
  unsigned time;
  config_ch[0] <: 0;
  config_ch[1] <: 1;
  config_ch[0] <: 2;
  config_ch[1] <: 3;
  /* Give the other thread time to print. */
  t :> time;
  t when timerafter(time += 10000) :> void;
  exit(1);
}

int main()
{
  chan c[2];
  par {
#ifdef CHAN_ARRAY
    xc2_firmware_config(c);
#else
    xc2_firmware_config(c[0], c[1]);
#endif
    testbench(c);
  }
}
It works for me both with and without CHAN_ARRAY defined in the 9.9.2 and 10.4 tools.
Matt wrote:The error is "the reasource is not avaliable".
Is this is the full error message? What steps are you taking to get this error?
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

Post by Folknology »

Hi Richard this is interesting using channel arrays without a replicator.

So the compiler sees the array of channels and binds all of the ends appropriately depending on how they are passed to different threads, It would be nice to have a little more insight into how it works with arrays.

Out of interest can channels or arrays of channels be part of structs when passed to threads, would the compiler still catch them inside the passed structs and bind ends appropriately?

regards
Al