Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Community Manager
1,553 Views

How to configure Button in quark D2000 kit?

Jump to solution

Hi

I Have this cod example but in the new software version 1.1. doesn't work, I have This error: Too few arguments to function 'qm_gpio_read_pin'

I see in the documentation that now its needs more arguments : int qm_gpio_read_pin ( const qm_gpio_t gpio, const uint8_t pin, qm_gpio_state_t const state )

I'm having trouble in figure this out, can same one help me on this.

Thanks for the help

MFF

/*

* D2000_Button

* Read the state of the on board user defined switch and print

* the result to a serial terminal

*****************************************************

** This software is offered strictly as is with no **

** warranties whatsoever. Use it at your own risk. **

*****************************************************

*/

# include

/* User defined switch is Digital IO 5 [F2] */

# define BUTTON 2

/* GPIO configuration structure (see qm_gpio.h for details) */

static qm_gpio_port_config_t DDRcfg;

bool state,laststate; /* these will hold the states of the pin */

int main(void)

{

/* send message to terminal */

QM_PRINTF("D2000_Button is running \n\n");

/* set GPIO to input by resetting the associated bit in the DDR*/

DDRcfg.direction=0<<2;

qm_gpio_set_config(QM_GPIO_0,&DDRcfg);

laststate=qm_gpio_read_pin(QM_GPIO_0,BUTTON);

/* get the initial state of the button */

if(laststate)

{

QM_PRINTF("Button is not pressed \n");

}

else

{

QM_PRINTF("Button is pressed \n");

}

while(1) /* loop forever */

{

state=qm_gpio_read_pin(QM_GPIO_0,BUTTON);

if (state!=laststate)

{

laststate=state;

if(laststate)

{

QM_PRINTF("Button is not pressed \n");

}

else

{

QM_PRINTF("Button is pressed \n");

}

}

}

return 0; /* never gets here */

}

0 Kudos

Accepted Solutions
Highlighted
7 Views

Hi ,

The additional argument needed is the GPIO Pin 'State'.. qm_gpio_state_t

/**

* GPIO pin states.

*/

typedef enum {

QM_GPIO_LOW = 0, /**< GPIO low state. */

QM_GPIO_HIGH, /**< GPIO high state. */

QM_GPIO_STATE_NUM /**< Number of GPIO states. */

} qm_gpio_state_t;

Below is an example of how to use it -- I think this should accomplish what you were aiming to do in your code ...

# include

# include

# define BUTTON 2

int main(void)

{

qm_gpio_state_t state;

qm_gpio_port_config_t cfg;

QM_PRINTF("D2000_Button is running \n\n");

/*Set GPIO Mode*/

qm_pmux_select(QM_PIN_ID_2,QM_PMUX_FN_0);

/* set GPIO to input */

qm_pmux_pullup_en(QM_PIN_ID_2, true);

qm_pmux_input_en(QM_PIN_ID_2, true);

cfg.direction=BIT(0);

qm_gpio_set_config(QM_GPIO_0,&cfg);

while(1)

{

qm_gpio_read_pin(QM_GPIO_0,BUTTON, &state);

if (state == 1)

QM_PRINTF("Button is pressed %d\n" , state);

else

QM_PRINTF("Button is not pressed %d\n" , state) ;

}

return 0;

}

--Michelle.

View solution in original post

0 Kudos
3 Replies
Highlighted
8 Views

Hi ,

The additional argument needed is the GPIO Pin 'State'.. qm_gpio_state_t

/**

* GPIO pin states.

*/

typedef enum {

QM_GPIO_LOW = 0, /**< GPIO low state. */

QM_GPIO_HIGH, /**< GPIO high state. */

QM_GPIO_STATE_NUM /**< Number of GPIO states. */

} qm_gpio_state_t;

Below is an example of how to use it -- I think this should accomplish what you were aiming to do in your code ...

# include

# include

# define BUTTON 2

int main(void)

{

qm_gpio_state_t state;

qm_gpio_port_config_t cfg;

QM_PRINTF("D2000_Button is running \n\n");

/*Set GPIO Mode*/

qm_pmux_select(QM_PIN_ID_2,QM_PMUX_FN_0);

/* set GPIO to input */

qm_pmux_pullup_en(QM_PIN_ID_2, true);

qm_pmux_input_en(QM_PIN_ID_2, true);

cfg.direction=BIT(0);

qm_gpio_set_config(QM_GPIO_0,&cfg);

while(1)

{

qm_gpio_read_pin(QM_GPIO_0,BUTTON, &state);

if (state == 1)

QM_PRINTF("Button is pressed %d\n" , state);

else

QM_PRINTF("Button is not pressed %d\n" , state) ;

}

return 0;

}

--Michelle.

View solution in original post

0 Kudos
Highlighted
Community Manager
7 Views

What changes should I make to have the led light up when I press the button.

Thanks for the help

MFF

0 Kudos
Highlighted
7 Views

Hi ,

You could define an output GPIO pin (PIN_OUT) and then just replace the QM_PRINTF's with qm_gpio_set_pin/qm_gpio_clear_pin

qm_gpio_set_pin(QM_GPIO_0, PIN_OUT);

qm_gpio_clear_pin(QM_GPIO_0, PIN_OUT);

However that will only light the LED while the button is pressed... If you want to toggle the LED on/off based on a button press then you need to configure an interrupt on the button... example below should work with QMSI 1.1.

/* QMSI GPIO app example

*

* PIN_OUT will be configured as an output pin and PIN_INTR will be

* configured as an input pin with interrupts

* enabled.

*

* On the Intel(R) Quark(TM) Microcontroller D2000 Development Platform PIN_OUT

* and PIN_INTR are marked "SSO 10" and "A0".

*/

# include "qm_gpio.h"

# include "qm_interrupt.h"

# include "qm_isr.h"

# include "qm_pinmux.h"

/* Connect LED to PIN_OUT */

# define PIN_OUT 0

/*Connect Button to PIN_INTR */

# define PIN_INTR 3

/* Example callback function */

static void gpio_example_callback(void *, uint32_t);

int main(void)

{

qm_gpio_port_config_t cfg;

/* Request IRQ and write GPIO port config */

cfg.direction = BIT(PIN_OUT);

cfg.int_en = BIT(PIN_INTR); /* Interrupt enabled */

cfg.int_type = BIT(PIN_INTR); /* Edge sensitive interrupt */

cfg.int_polarity = BIT(PIN_INTR); /* Rising edge */

cfg.int_debounce = BIT(PIN_INTR); /* Debounce enabled */

cfg.int_bothedge = 0x0; /* Both edge disabled */

cfg.callback = gpio_example_callback;

qm_irq_request(QM_IRQ_GPIO_0, qm_gpio_isr_0);

qm_gpio_set_config(QM_GPIO_0, &cfg);

/* Set PIN_OUT to trigger PIN_INTR interrupt*/

qm_gpio_clear_pin(QM_GPIO_0, PIN_OUT);

qm_gpio_set_pin(QM_GPIO_0, PIN_OUT);

while(1)

{

/* Your code goes here */

}

return 0;

}

void gpio_example_callback(void *data, uint32_t status)

{

/* toggle LED state - ON/OFF */

qm_gpio_state_t state;

qm_gpio_read_pin(QM_GPIO_0,PIN_OUT,&state);

if(state == 0)

{

qm_gpio_set_pin(QM_GPIO_0, PIN_OUT);

}

else{

qm_gpio_clear_pin(QM_GPIO_0, PIN_OUT);

}

}

0 Kudos