reconfigure port

Technical questions regarding the XTC tools and programming with XMOS.
psebastiani
Member++
Posts: 29
Joined: Wed Oct 02, 2013 4:20 pm

reconfigure port

Post by psebastiani »

Hi
I have necessity to reconfigure a SPI mosi port. This is a 1 bit buffered output port and i want to remove the "buffered" and make it simply "out port".
I have a single thread that first use the SPI (with module_spi_master), and after I not longer need the SPI and i want reconfigure the MOSI pin.
The SPI struct is defined in "master.h" as:

Code: Select all

typedef struct spi_master_interface
{
    clock blk1;
    clock blk2;
    out buffered port:8 mosi;
    out buffered port:8 sclk;
    in buffered port:8 miso;
} spi_master_interface;
and in my program i have defined the SPI interface as:

Code: Select all

on stdcore[0]: spi_master_interface spi_if =
{
    XS1_CLKBLK_1,
    XS1_CLKBLK_2,
    PORT_SPI_MOSI,
    PORT_SPI_CLK,
    PORT_SPI_MISO
};
The main program is composed like this:

Code: Select all

int main(void) {
 par {
      on stdcore[1] : {
  			...
  		  }
  		on stdcore[0] : {
  			...
  		  }
  	   on stdcore[0] : {
  			 Use_SPI_Function(spi_if);
  			
  			 Reuse_Pin_Function(spi_if);
      }
	}
 }
First the Reuse_Pin_Function() I must reconfigure the SPI mosi pin.
I want create movable pointers and i have add in Reuse_Pin_Function() this instruction:

Code: Select all

pDataValid = reconfigure_port(move(buf_mosi), out port);
.
The problem is the place where the movable pointer must be declared:

Code: Select all

buffered out port:8 * movable buf_mosi=&spi_if.mosi;
out port * movable pDataValid;
If I declare global, the compiler don't find (obviously) spi_if structure because it is declare in stdcore[0]. If I add:

Code: Select all

on stdcore[0]:buffered out port:8 * movable buf_mosi=&spi_if.mosi;
on stdcore[0]:out port * movable pDataValid;
receive this error:
... error: a variable declaration prefixed with on must declare an object of type port or clock
Any suggestion?
Regards


User avatar
davelacey
Experienced Member
Posts: 104
Joined: Fri Dec 11, 2009 8:29 pm

Post by davelacey »

You are best not to declare the pointer as a global, its scope is going to be that of the Reuse_Pin_Function. The following code should work:

Code: Select all

void Reuse_Pin_Function(spi_master_interface &spi_if)
{
  in buffered port:32 * movable p_mosi = &spi_if.mosi;
  out port * movable pDataValid;
  pDataValid = reconfigure_port(move(p_mosi), out port);

  // use pDataValid here

  p_mosi = reconfigure_port(move(pDataValid), in buffered port:32);
}
psebastiani
Member++
Posts: 29
Joined: Wed Oct 02, 2013 4:20 pm

Post by psebastiani »

Tanks for your suggestion, but it dont' work.
If I declare in this manner (I want only remove "buffered" on the mosi pin):

Code: Select all

void Reuse_Pin_Function(spi_master_interface &spi_if)
{
  out buffered port:8 * movable buf_mosi= &spi_if.mosi;
  out port * movable pDataValid;
  pDataValid = reconfigure_port(move(buf_mosi), out port); 

  // use pDataValid here

  p_mosi = reconfigure_port(move(pDataValid), out buffered port:8);
}
the compiler give me this error:
....error: reference to `spi_if' which is not visible in the current scope
....error: object hidden from scope here

on each line where I use spi_if.
This is a strange error, because if I remove the first 3 line in the Reuse_Pin_Function(), the compiler run with no error, and I can use (and it work fine) spi_if internally to this function.
Any suggestion?
psebastiani
Member++
Posts: 29
Joined: Wed Oct 02, 2013 4:20 pm

Post by psebastiani »

The problem is the reconfiguration of a port inside a struct.
I resolved passed directly the port to function instead of the entire structure:

Code: Select all

void Reuse_Pin_Function(out buffered port:8 Pmosi)
{
  out buffered port:8 * movable buf_mosi= &Pmosi;
  out port * movable pDataValid;
  pDataValid = reconfigure_port(move(buf_mosi), out port);

  // use pDataValid here remember to use *pDataValid 

  buf_mosi = reconfigure_port(move(pDataValid), out buffered port:8);
}
and call function in the main with:

Code: Select all

...
Reuse_Pin_Function(spi_if.mosi);
...
This work fine.
Don't forget the second reconfigure port "buf_mosi = reconfigure_port(move(pDataValid), out buffered port:8);" before the end of the function Reuse_Pin_Function(...); otherwise, at the end of this function, the program crash.