save ports in a struct

Technical questions regarding the XTC tools and programming with XMOS.
themech
Member++
Posts: 17
Joined: Tue Sep 23, 2014 12:17 pm

save ports in a struct

Post by themech »

Hi,

I am trying to save ports in a struct to have them in a handy fashion for a function.

Code: Select all

typedef struct sensors_struct {
    int bitport;
    port channel_a, channel_b, hall_sensor;
    unsigned bit_a, bit_b, bit_hs;
} sensors;
Unfortunately as soon as I am trying to declare/define a struct I get an error:

Code: Select all

portxy undeclared here
Is there a way to store ports in a struct or an array?
User avatar
sethu_jangala
XCore Expert
Posts: 589
Joined: Wed Feb 29, 2012 10:03 am

Post by sethu_jangala »

It is possible to declare ports in structure. Have a look at the following I2C example for more details:
https://github.com/xcore/sc_i2c/blob/ma ... /src/i2c.h

You need to include xccompat.h in your program.
themech
Member++
Posts: 17
Joined: Tue Sep 23, 2014 12:17 pm

Post by themech »

The compiler asks to put the ports on a specific tile, is it possible to link the ports to a tile before hand or in the struct?
User avatar
sethu_jangala
XCore Expert
Posts: 589
Joined: Wed Feb 29, 2012 10:03 am

Post by sethu_jangala »

Once you have declared the structure as shown below:

Code: Select all

typedef struct r_i2c {
    port scl;
    port sda;
    unsigned int clockTicks; 
}  r_i2c;
You can do as shown below:

Code: Select all

struct r_i2c i2cOne = {
   on tile[0] : XS1_PORT_1B,
   on tile[0] : XS1_PORT_1A,
    1008,
};
themech
Member++
Posts: 17
Joined: Tue Sep 23, 2014 12:17 pm

Post by themech »

Thanks this was extremly helpful!