How to fill a struct with values over an interface

Technical questions regarding the XTC tools and programming with XMOS.
DemoniacMilk
XCore Addict
Posts: 191
Joined: Tue Jul 05, 2016 2:19 pm

How to fill a struct with values over an interface

Post by DemoniacMilk »

let us say I have a struct that contains
  • a large array (1500 bytes) holding data (might be holding 40..1500 bytes of valid data)
  • some variables holding information (timestamps, data length, source, destination, ..)
On receiving data over a hardware interface, a server thread puts the data and additional information into the struct and notifies a client. When the client wants to grab the packet it creates a local packet variable. My goal is to have the server copy the data to the memory location of the clients local packet.

Question is: how do I do that?

My first idea was:
  • pass a pointer to the clients local packet over the interface
  • fill in information and do a memcpy of the data
When using this approach, the project compiles. However, the data is not copied to the clients packet.

I know that passing an array and then doing a memcpy works. Arrays are passed by reference in xC (at least to functions, not sure about interfaces) so I tried:
  • pass a reference to the local packet over the interface
  • fill in information and do a memcpy of the data
but this ends in a compiler error due to an incompatible argument type.

I could pass the data array only to copy the data and receive additional information as 'return' values, but that is not very clean, as changing the struct requires to change the interface function as well. Alternatively, i could return a packet.

What do you think would be a good approach? I'd like to just pass a pointer or reference to the whole struct but im not sure if that is possible.
Gothmag
XCore Addict
Posts: 129
Joined: Wed May 11, 2016 3:50 pm

Post by Gothmag »

If they're on the same tile you can pass by reference or pointer, anything else needs to be passed by value since there is no global memory store. If they're on the same tile you should be ok doing just a memory pointer(pointer/reference). Perhaps you could have interfaces for each specific data set and call for what you need. Might help to see a code sample though I'm not sure if I understand completely.
DemoniacMilk
XCore Addict
Posts: 191
Joined: Tue Jul 05, 2016 2:19 pm

Post by DemoniacMilk »

Ye they are on the same tile. I am not sure what I had changed, but I got it working at some point, thank you.
User avatar
larry
Respected Member
Posts: 275
Joined: Fri Mar 12, 2010 6:03 pm

Post by larry »

Yes, remote reference using an interface is the preferred way.

For tasks one one tile, the reference will be represented as a pointer in the compiled program, with reads and writes that happen without any copy.

For tasks on two different tiles, the array is sent over a channel for reading and only elements that are written are sent back. Note that this works inside the receiving (server) select function. Once passed further, you need to make an explicit copy of the remote reference.

See also "2.2.4 Passing data via interface function calls" in XC programming guide
DemoniacMilk
XCore Addict
Posts: 191
Joined: Tue Jul 05, 2016 2:19 pm

Post by DemoniacMilk »

Ah that is a good hitn as well! Thank you