How to mask a port?

Technical discussions around xCORE processors (e.g. xcore-200 & xcore.ai).
Post Reply
jagspaul
Experienced Member
Posts: 117
Joined: Tue Oct 18, 2011 3:28 pm

How to mask a port?

Post by jagspaul »

let P1 is a 8 bit
for masking we can use following instruction in PIC controller

P1 = P1 & 0xf0;

In XMOS (xc) how to write it?

jags


User avatar
Bianco
XCore Expert
Posts: 754
Joined: Thu Dec 10, 2009 6:56 pm
Contact:

Post by Bianco »

You will need a variable to buffer the port state.

i.e.

Code: Select all

unsigned p_state; // port state buffer

// set bit 0
p :> p_state;
p_state |= 0x01;
p <: p_state;

// clear bit 0
p :> p_state;
p_state &= ~0x01;
p <: p_state;

For better performance you want to skip the reads from the port and just always write the changes to p_state and then write p_state to the port.
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm
Contact:

Post by Folknology »

Also if you are waiting for a specific bit pattern inputs (or just single bit of a multibit port) in XC you can use a select with a pins equal guard:

Code: Select all

select 
{
  case p8a when pinseq(Ox01) :> pval :
    //Detected LS bit high, do stuff
    break;
   default:
     // What todo otherwise
}
Just remember you can't put the same port (p8a in this case) in any of the other case's of the select. If you need to detect inputs on multiple pins then just do a select in any port change (using pins not equal to guard) with a switch to do the masking.

Code: Select all

select
{
  case when pinsneq(last) :> current :
     // Whenever port value changes (any bit change)
    switch(current ^ last)
    {
       case Ox01 : 
         // do bit 0 stuff
         break;
       case Ox02 : 
         // do bit 1 stuff
         break;
......
       default : //Otherwise stuff
    }
    last = current;
    break;
}
regards
Al
User avatar
waluigi
Member++
Posts: 22
Joined: Sun Nov 07, 2010 6:33 pm

Post by waluigi »

Bianco wrote:

Code: Select all

unsigned p_state; // port state buffer

// set bit 0
p :> p_state;
p_state |= 0x01;
p <: p_state;

// clear bit 0
p :> p_state;
p_state &= ~0x01;
p <: p_state;

Unfortunately this isn't quite right - an input on a bidirectional port will reverse the direction of the port, tristate the pins and potentially input the wrong value. What you need in this situation is a PEEK:

Code: Select all

// set bit 0
p <: peek(p) | 0x01;

// clear bit 0
p <: peek(p) & (~0x01);
User avatar
Bianco
XCore Expert
Posts: 754
Joined: Thu Dec 10, 2009 6:56 pm
Contact:

Post by Bianco »

waluigi wrote:
Bianco wrote:

Code: Select all

unsigned p_state; // port state buffer

// set bit 0
p :> p_state;
p_state |= 0x01;
p <: p_state;

// clear bit 0
p :> p_state;
p_state &= ~0x01;
p <: p_state;

Unfortunately this isn't quite right - an input on a bidirectional port will reverse the direction of the port, tristate the pins and potentially input the wrong value. What you need in this situation is a PEEK:

Code: Select all

// set bit 0
p <: peek(p) | 0x01;

// clear bit 0
p <: peek(p) & (~0x01);
You are right sir!
monday mornings...
jagspaul
Experienced Member
Posts: 117
Joined: Tue Oct 18, 2011 3:28 pm

Post by jagspaul »

Is peek(p) is valid of a out port?

jags
User avatar
Ross
XCore Expert
Posts: 962
Joined: Thu Dec 10, 2009 9:20 pm
Location: Bristol, UK

Post by Ross »

jagspaul wrote:Is peek(p) is valid of a out port?

jags
Yes, any port
Post Reply