channel in structures

Technical questions regarding the XTC tools and programming with XMOS.
Post Reply
User avatar
Julien
Member++
Posts: 16
Joined: Mon Feb 22, 2010 4:43 pm
Location: France
Contact:

channel in structures

Post by Julien »

Hello,

I am currently working on XMOS products, discovering XC programming language and having fun with channels and ports. So far so good, I can program my board and communicate over UART using ports.

In my project, I wanted to create a C structure (struct) containing some configuration variables (int, char, ...) and a channel, to pass to my threads on start-up. Here is a sample code to make my point clear:

Code: Select all

out port tx = PORT_UART_TX;
void uartTX(chanend c1, chanend c2, out port tx);
void producer(chanend data, chanend ctrl);

struct ctl {
    chan ch;
// chanend ch; ??
    char name;
    int start;
};

int main() {
    struct ctl a1, a2;

    a1.name = 'A';
    a1.start = 0x40FE12;
    a2.name = 'B';
    a2.start = 0xFF2842;

    par {
        on stdcore[0] : producer(a1.ch);
        on stdcore[0] : producer(a2.ch);
        on stdcore[0] : uartTX(a1.ch, a2.ch, tx);
    }
    return 0;
}
The code is not complete, but it shows what I want to do: make a data structure containing a channel and some associated data (e.g: name, start_value, ...)

Unfortunately, XMOS compiler runs into an error on the struct declaration:

Code: Select all

../main.xc:25: error: field `ch' has resource type
It is coherent with XMOS book "Programming XC on XMOS devices" which states, §A.7.4 page 89/139:
A structure or union may not contain a member of incomplete or resource type, except that
a structure may contain a member of type port or timer. If a structure is declared to have
a member with one of these types then variables of the structure may be declared only as
external declarations
So I wonder why it is not possible to have a channel in a C-structure? Is it a tool limitation or a real misconception from my part? I know chan must be associated with at most 2 threads, with chanend, and must be declared in the main body. But my struct is declared in the main body, and the a1.ch is connected to 2 threads.

Sorry if my question is stupid, but I'd better ask than stay stupid myself :-)

Cheers,
Julien


User avatar
paul
XCore Addict
Posts: 169
Joined: Fri Jan 08, 2010 12:13 am
Contact:

Post by paul »

XC is different to C in that it has some restrictions. Creating that structure type in C is fine, just make sure you include the xccompat.h.

For example (.c file):

Code: Select all

#include <xccompat.h>
#include "chan_lib.h"

typedef struct c_struct {
	chanend c;
	unsigned i;
} t_chanStruct;

void func1(t_chanStruct s)
{
	int i = 0;
	chan_out(s.c, i); // in chan_lib.h
}
Where chan_lib.h contains headers for some custom XC functions that handle channel comms.

Hope that helps,
Paul

On two occasions I have been asked, 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
User avatar
Julien
Member++
Posts: 16
Joined: Mon Feb 22, 2010 4:43 pm
Location: France
Contact:

Post by Julien »

Thanks Paul for this tip! It might be the best solution indeed. Did not know about xccompat.h. I'll try that soon and report here if I get into trouble.

Cheers,
Julien
Heater
Respected Member
Posts: 296
Joined: Thu Dec 10, 2009 10:33 pm

Post by Heater »

Yep, xccompat.h is a saviour.

I've used it to pass channels as function parameters into a pile of C code that eventually passes them to some input/output functions in XC. Works a treat.
Post Reply