How to declare Push-button on startKIT?

Non-technical related questions should go here.
Posts: 17
Joined: Mon Dec 16, 2013 11:44 pm

How to declare Push-button on startKIT?

Post by marssystems »

How do I define the push-button on the startKIT?

The documentation isn't clear on this neither are the demos that use the push-button.

Do I need to define the 32bit port first and then define the button?


#define out port p32 = XS1_PORT_32A;

#define in port butt1 = XS1_PORT_32A0;



Posts: 17
Joined: Mon Dec 16, 2013 11:44 pm

Post by marssystems »

I already have port p32 defined like this:-

port p32 = XS1_PORT_32A; //PORT 32A
int leds[10] = {
0b01111111111111111111, //LED A1
0b10111111111111111111, //LED B1
0b11011111111111111111, //LED C1
0b11111110111111111111, //LED A2
0b11111111011111111111, //LED B2
0b11111111101111111111, //LED C2
0b11111111110111111111, //LED A3
0b11111111111011111111, //LED B3
0b11111111111101111111, //LED C3
0b11111111111111111111 //ALL OFF

So I get an 'Error: L00130 Symbol p32 is a duplicate port declaration.' when I try to compile.
User avatar
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

Post by Folknology »

Unfortunately on Startkit the button is on a different pin but the same port as the LEDs (among other things). This means you could just define the port generally (neither 'OUT' or 'IN') and either write to it or read from it depending on your mode. Alternatively use the tools 13 version of new moveable ports this would allow more specific definition of input and output port setups allowing you to switch between them, but doesn't really add anything extra functionality wise. In either case it's only one port direction at any time : Button input or LED output, you will have to dynamically switch between those. Probably best to use the port primarily to drive LEDS and use a timer to trigger regular button checks (32A input to see if port state has changed since last check)

Posts: 17
Joined: Mon Dec 16, 2013 11:44 pm

Post by marssystems »

OK - thanks for that. I'll see if I can't glean any more info from the startkit_gpio module as used in the Demo examples.
User avatar
XCore Expert
Posts: 844
Joined: Sun Jul 11, 2010 1:31 am

Post by segher »

Just leave the port as output, and do

Code: Select all

unsigned button = peek(p32) & 1;
User avatar
XCore Legend
Posts: 1130
Joined: Thu May 27, 2010 10:08 am

Post by infiniteimprobability »

The glowing leds app has a nice example of using both button read and setting LEDs (both of which are on the same 32b port), using the startkit_gpio.h library which shows off interfaces (new feature in tools 13). ... rc/main.xc

The main app is also of the form:

Code: Select all

which is a good way to program event driven XMOS apps - core is paused, ready to respond to an event as soon as it happens.
User avatar
Active Member
Posts: 32
Joined: Thu Sep 25, 2014 1:19 am

Post by tuck1s »

The GPIO example is nice, but it's a lot of code for a simple on/off button presser.

I found that peek() on its own did not give the expected results. It's necessary to flip the port into input mode with a

Code: Select all

before reading. And if you read using p:>, it needs an asm("nop") to allow the lines to settle (perhaps for 1 clock edge?)
peek() seems to work without a nop.

So this can be included within another task (which typically you won't want a delay_milliseconds() in real code - you might have a similar cadence on an existing loop).

Code: Select all

#include <timer.h>
#include <xs1.h>
#include <stdio.h>
 Handling button presses
const int leds_3x3_spin[] = {0xA1F81, 0xC1F81, 0xE1B81, 0xE1F01, 0xE1E81, 0xE1D81, 0xE0F81, 0x71F81};
port p = XS1_PORT_32A;

void task1b(void)
    // The last read value off the port.
    unsigned int current_val = 0, new_val;
    unsigned int led_pos;

    while (1) {
        p :> void;                 // Port into input mode
        new_val = peek(p) & 0x1;   // Button is in bit0
        if(new_val !=current_val) {
           if (new_val) {
               printf("Button up\n");
           else {
               printf("Button down\n");
           current_val = new_val;
        // Display spinning dot on the 3x3 LEDs
        p <: leds_3x3_spin[led_pos &0x7];

int main() {
  printf("Starting ...\n");
  return 0;