upiwin/src/gpio.c

77 lines
2.1 KiB
C

#include <stddef.h>
#include <stdint.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <bcm2835.h>
#include "log.h"
#include "gpio.h"
#define GLINE_BUTTON1 17
#define GLINE_BUTTON2 22
#define GLINE_BUTTON3 23
#define GLINE_BUTTON4 27
#define GLINE_BACKLIGHT 18
#define STATE_BUTTON1 (1 << 0)
#define STATE_BUTTON2 (1 << 1)
#define STATE_BUTTON3 (1 << 2)
#define STATE_BUTTON4 (1 << 3)
int Gpio_setup(void)
{
if (!bcm2835_init())
{
Log(LFATAL, "Error initializing BCM2835 library");
return -1;
}
/* configure the buttons */
bcm2835_gpio_set_pud(GLINE_BUTTON1, BCM2835_GPIO_PUD_UP);
bcm2835_gpio_fsel(GLINE_BUTTON1, BCM2835_GPIO_FSEL_INPT);
bcm2835_gpio_set_pud(GLINE_BUTTON2, BCM2835_GPIO_PUD_UP);
bcm2835_gpio_fsel(GLINE_BUTTON2, BCM2835_GPIO_FSEL_INPT);
bcm2835_gpio_set_pud(GLINE_BUTTON3, BCM2835_GPIO_PUD_UP);
bcm2835_gpio_fsel(GLINE_BUTTON3, BCM2835_GPIO_FSEL_INPT);
bcm2835_gpio_set_pud(GLINE_BUTTON4, BCM2835_GPIO_PUD_UP);
bcm2835_gpio_fsel(GLINE_BUTTON4, BCM2835_GPIO_FSEL_INPT);
/* TODO: other setup */
return 0;
}
void Gpio_cleanup(void)
{
/* TODO: additional cleanup may be required */
/* close down the button lines */
bcm2835_gpio_set_pud(GLINE_BUTTON1, BCM2835_GPIO_PUD_OFF);
bcm2835_gpio_fsel(GLINE_BUTTON1, BCM2835_GPIO_FSEL_INPT);
bcm2835_gpio_set_pud(GLINE_BUTTON2, BCM2835_GPIO_PUD_OFF);
bcm2835_gpio_fsel(GLINE_BUTTON2, BCM2835_GPIO_FSEL_INPT);
bcm2835_gpio_set_pud(GLINE_BUTTON3, BCM2835_GPIO_PUD_OFF);
bcm2835_gpio_fsel(GLINE_BUTTON3, BCM2835_GPIO_FSEL_INPT);
bcm2835_gpio_set_pud(GLINE_BUTTON4, BCM2835_GPIO_PUD_OFF);
bcm2835_gpio_fsel(GLINE_BUTTON4, BCM2835_GPIO_FSEL_INPT);
if (!bcm2835_close())
Log(LWARN, "Closing BCM2835 library failed");
}
int Gpio_read_buttons(void)
{
int rc = 0;
if (bcm2835_gpio_lev(GLINE_BUTTON1) == LOW)
rc |= STATE_BUTTON1;
if (bcm2835_gpio_lev(GLINE_BUTTON2) == LOW)
rc |= STATE_BUTTON2;
if (bcm2835_gpio_lev(GLINE_BUTTON3) == LOW)
rc |= STATE_BUTTON3;
if (bcm2835_gpio_lev(GLINE_BUTTON4) == LOW)
rc |= STATE_BUTTON4;
return rc;
}