Waiting for a value on a port

Technical questions regarding the XTC tools and programming with XMOS.
Dennuzzz
Newbie
Posts: 1
Joined: Mon Mar 18, 2013 3:32 pm

Waiting for a value on a port

Post by Dennuzzz »

Hello,
I have a question about waiting on a value on a port. However there is only an example about a 1 bit port while I use a 4 bit port.

I have the code

Code: Select all

void waitForInputPortToBe(in port portToCheck, int pin, int value){
      int portValue;
      portToCheck :> portValue;
      while(portValue & (1 << pin) == value){
            portToCheck :> portValue;
      }
}

But in the XMOS manual there is a very nice way to wait on a one bit port, but how to do this for a 4 bit port?
I thought it should be something like this, but it is not:

Code: Select all

void waitForInputPortToBe(in port portToCheck, int pin, int value){
      portToCheck when pinseq ((value & (1 << pin))) :> value;
}
So what’s the shortest, fastest and best way to program this. I only want to check the value of the pin called pin?


User avatar
segher
XCore Expert
Posts: 844
Joined: Sun Jul 11, 2010 1:31 am

Post by segher »

Hi!

"pinseq" tests whether all four bits on your port are some specific
value; there is no way to set a condition for just one bit.

If all the four signals on the port are slow-changing, it is useful to
do something like

Code: Select all

void waitForInputPortToBe(in port portToCheck, int pin, int value)
{
        int portValue;

        portToCheck :> portValue;
        for (;;) {
                if (portValue & (1 << pin) != value)
                        break;
                portToCheck when pinsneq(portValue) :> portValue;
        }
}
It looks like you've got the condition the wrong way around btw; and
if pin is e.g. 2, value has to be 0 or 4, not 0 or 1; i.e., you might want

Code: Select all

if (((portValue >> pin) & 1) == value)