EmbeddedRelated.com
Forums

Linux sam9263: How to read a button?

Started by Thomas December 11, 2009
Hi

this should be trivial - but still too much for me.

Pls, can anyone provide directions what to do?

I want to read any kind of buttons like DIP switch etc.

Linux does not allow to access the ports directly ("segmentation fault").

So it needs to be done with linux drivers.
I am sure something is done in linux 2.6.30-at91-exp.patch

Suggestions welcome!

Thanks indeed.
Thomas

On Friday 11 December 2009 22:14:54 Thomas wrote:
> Hi
>
> this should be trivial - but still too much for me.
>
> Pls, can anyone provide directions what to do?
>
> I want to read any kind of buttons like DIP switch etc.
>
> Linux does not allow to access the ports directly ("segmentation fault").
>
> So it needs to be done with linux drivers.
> I am sure something is done in linux 2.6.30-at91-exp.patch
>
> Suggestions welcome!
>

Most of the GPIO pins can be controlled from user space via sysfs interface.
Let's say we want to use GPIO5 as output.

Configure gpio pin as output:

$echo 5 > /sys/class/gpio/export
$echo out > /sys/class/gpio/gpio5/direction

Change value:

$echo 1 > /sys/class/gpio/gpio5/value
$echo 0 > /sys/class/gpio/gpio5/value

Configure same gpio pin as input:

$echo in > /sys/class/gpio/gpio5/direction

Read its value:

$cat /sys/class/gpio/gpio5/value

These provides low level gpio control functionality. On top of this, GPIO keys
functionality can be used for user space programs. Many AT91 evaluation kits
uses this interface. Following code snippet is taken from board-sam9263.c and
demonstrates how to use gpio_keys. User space programs will be able to use
these buttons via event interface. One pin will be left arrow, other will be
right:

static struct gpio_keys_button ek_buttons[] = {
{ /* BP1, "leftclic" */
.code = BTN_LEFT,
.gpio = AT91_PIN_PC5,
.active_low = 1,
.desc = "left_click",
.wakeup = 1,
},
{ /* BP2, "rightclic" */
.code = BTN_RIGHT,
.gpio = AT91_PIN_PC4,
.active_low = 1,
.desc = "right_click",
.wakeup = 1,
}
};

static struct gpio_keys_platform_data ek_button_data = {
.buttons = ek_buttons,
.nbuttons = ARRAY_SIZE(ek_buttons),
};

static struct platform_device ek_button_device = {
.name = "gpio-keys",
.id = -1,
.num_resources = 0,
.dev = {
.platform_data = &ek_button_data,
}
};

static void __init ek_add_device_buttons(void)
{
at91_set_GPIO_periph(AT91_PIN_PC5, 1); /* left button */
at91_set_deglitch(AT91_PIN_PC5, 1);
at91_set_GPIO_periph(AT91_PIN_PC4, 1); /* right button */
at91_set_deglitch(AT91_PIN_PC4, 1);

platform_device_register(&ek_button_device);
}

Regards,
Caglar

> Thanks indeed.
> Thomas
Hello Caglar,

thanks for the swift reply.
The code below: is it to go into my apps?
What includes are required then?

kind regards
Thomas

> static struct gpio_keys_button ek_buttons[] = {
> { /* BP1, "leftclic" */
> .code = BTN_LEFT,
....

On Friday 11 December 2009 23:26:35 Thomas wrote:
> Hello Caglar,
>

Hello,

> thanks for the swift reply.
> The code below: is it to go into my apps?
> What includes are required then?
>

No. It is a kernel code. If you want to use that code you can do either of
these:

1) Add it to your board setup files.
2) Write a new kernel module which will include module_init and module_exit
definitions in addition to mentioned GPIO keys code.

If your applications are not using any GUI libraries then using sysfs is the
most straightforward method.

If you are using Qt, GTK, DirectFB or any other GUI library then sysfs
interface won't be helpful and you will need event interface, hence the gpio-
keys.

Best regards,
Caglar