How to carve out 3 1-bit ports from a 4-bit port?

Technical questions regarding the XTC tools and programming with XMOS.
User avatar
landon
Experienced Member
Posts: 71
Joined: Mon Sep 06, 2010 4:05 pm

How to carve out 3 1-bit ports from a 4-bit port?

Post by landon »

I think I'm missing something fundamental in the XC description language. Most examples I've seen of port manipulation for things like SPI are using 1-bit ports. There aren't many 1-bit ports available so I need to use wider ports.

Example: I have a 3-wire device (select, clock, data). I'd like to use 3 of the 4 pins on XS1_PORT_4A as input from this device.

I read in section C.3 of "Programing XC on XMOS Devices", "Specifying Port-to-Pin Mappings" section that "individual bits of the port are identified by means of a superscripted digit 0-31". No examples were given of how this indexing is declared. It's not making sense to me how to declare 3 port variables using specific pins of port 4A and I haven't come across any examples like what I'm looking for.

When I read the description I think of array indexes. I know this doesn't compile, but this is basically what I'm trying to do:

Code: Select all

on stdcore[IO_CORE] : port MY3WIRE_PORT = XS1_PORT_4A;

#define SEL  MY3WIRE_PORT[0]
#define BCLK  MY3WIRE_PORT[1]
#define DATA  MY3WIRE_PORT[3]
for the select, bit clock and data lines.

My question is, how can I make 3 variables, even if they're not true variables but #defines, to emulate 3 1-bit ports from a 4-bit port?

I think I'm missing something in the XC description language that would let me index a specific bit on the port and associate it with a variable or #define. I know this is likely to be a "doh!" when I see the answer, but right now, I'd settle for a 'doh!' moment.

Landon


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

Post by Bianco »

The section "Specifying Port-to-Pin Mappings" talks about the relation between ports and pins. It does not talk about how to use a multiple-bits port as if it were single-bit ports. You can use a multiple-bits ports as single-bit ports by using bitwise operations. These are done in the same way in XC as in C/C++. There are many resources on the internet about that subject, for example: http://www.cprogramming.com/tutorial/bi ... ators.html
User avatar
landon
Experienced Member
Posts: 71
Joined: Mon Sep 06, 2010 4:05 pm

Post by landon »

Fair enough - I thought there might be a specific XC language construct I was missing, but apparently not.

Thanks for your response,

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

Post by Bianco »

XC indeed does not support it.
User avatar
landon
Experienced Member
Posts: 71
Joined: Mon Sep 06, 2010 4:05 pm

Post by landon »

I have one followup question on this topic.

In the case where a multi-bit port is neither an input or output port as declared in a port declaration like:

Code: Select all

on stdcore[IO_CORE] : port MY4WIRE_PORT = XS1_PORT_4A;
Then assuming that this will be a typical 3-wire or 4-wire protocol, say a master SPI with XMOS being master, some of those pins are always input, some are always output.

Lets say I assign one pin to MISO, so I expect it to be a clocked input pin, what does it mean when I do output to the port or input from the 4bit port?

For the sake of illustration, lets say I assign these pins to my SPI device
  • XS1_PORT_4A0 is MISO
    XS1_PORT_4A1 is MOSI
    XS1_PORT_4A2 is CLK
    XS1_PORT_4A3 is CS
How is it possible to setup an XMOS clock source on pin on 4A2, for example? And then what happens to it when I output something using the 4-bit port output (bitwise operations.)

It just seems like I can't quite address a pin on a port like this in the same way I would a normal 1-bit pin like P1A0. It feels like I can't do with a 4-bit port what I want to do in the normal way I would with 4 x 1-bit pins.

Landon
User avatar
landon
Experienced Member
Posts: 71
Joined: Mon Sep 06, 2010 4:05 pm

Post by landon »

One other aspect to this is I'm using an XK-1 dev board. I can see from the XS-1 port spec that there are at least 8 more 1-bit ports that aren't broken out on the expansion connectors. The expansion headers go up to XD23 (P1H0). I'd be golden if I could get some more 1-bit ports.

There are 8 more 1-bit ports above XD23 - are they used for XJTAG? Or are they just not broken out at all to save space?

Landon
User avatar
rp181
Respected Member
Posts: 395
Joined: Tue May 18, 2010 12:25 am

Post by rp181 »

Take a look at this: http://www.xmos.com/published/xk-1-sche ... v0-dev-kit

I didn't look at it myself, but i believe that some are not broken out.
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

Post by Folknology »

I don't understand why but the other ports are not exposed on the XK1, silly decision IMHO but there you go. The only other ports exposed are part of the 32 bit port channel links C and D, these could also be used as part of the 32 bit port but thats even worse for you.

AS to getting SPI out of a 4 bit port I haven't seen an answer but I supect you basically have to bit bang everything using masking/bit wise operations, you fundamentally can't make a 4 bit port act like a single bit port.

I actually need to work out how to bit bang an I2C port from a 4 bit port for something I am working on so I feel you pain!

If there was just one port change I could have from Xmos it would be make all ports capable of being 1 bit ports!!

Anyone got any good examples of using 4bit ports to do things like I2C and SPI, I am sure others have been here before?

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

Post by segher »

When you output to a 4-bit port, all 4 pins become outputs.
When you input from a 4-bit port, all 4 pins become inputs.

You cannot mix input/output pins on the same port; use
only inputs or only outputs.
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

Post by Folknology »

Yes but you can switch between Input and output, albeit all bits at once, making some protocols possible, like perhaps slave I2C?

You might also be able to handle an SPI master which only wrote to output peripherals like a DAC or a display where MISO could safely be ignored. Likewise the Xmos could also be an SPI input only slave from and ADC or other input device.

regards
Al