upiwin/src/gpio.c

90 lines
2.6 KiB
C
Raw Normal View History

2019-11-29 02:18:02 -07:00
#include <stddef.h>
2019-11-29 01:52:56 -07:00
#include <stdint.h>
#include <errno.h>
#include <fcntl.h>
2019-11-29 02:18:02 -07:00
#include <unistd.h>
2019-11-29 01:52:56 -07:00
#include <sys/mman.h>
#include <bcm2835.h>
2019-11-29 01:52:56 -07:00
#include "log.h"
2019-11-29 02:05:30 -07:00
#include "gpio.h"
2019-11-29 01:52:56 -07:00
#define GLINE_BUTTON1 17
#define GLINE_BUTTON2 22
#define GLINE_BUTTON3 23
#define GLINE_BUTTON4 27
#define GLINE_BACKLIGHT 18
2019-11-29 01:52:56 -07:00
2019-12-01 14:34:57 -07:00
#define PWM_BACKLIGHT 0
2019-11-29 01:52:56 -07:00
int Gpio_setup(void)
{
if (!bcm2835_init())
{
Log(LFATAL, "Error initializing BCM2835 library");
2019-11-29 01:52:56 -07:00
return -1;
}
2019-11-29 01:52:56 -07:00
/* 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);
2019-11-29 01:52:56 -07:00
2019-12-01 14:34:57 -07:00
/* configure the PWM for the backlight */
bcm2835_gpio_set_pud(GLINE_BACKLIGHT, BCM2835_GPIO_PUD_OFF);
bcm2835_gpio_fsel(GLINE_BACKLIGHT, BCM2835_GPIO_FSEL_ALT5);
bcm2835_pwm_set_clock(BCM2835_PWM_CLOCK_DIVIDER_2);
bcm2835_pwm_set_mode(PWM_BACKLIGHT, 1, 1);
bcm2835_pwm_set_range(PWM_BACKLIGHT, BACKLIGHT_MAX + 1);
bcm2835_pwm_set_data(PWM_BACKLIGHT, BACKLIGHT_MAX);
2019-11-29 01:52:56 -07:00
return 0;
}
void Gpio_cleanup(void)
{
2019-12-01 14:34:57 -07:00
/* close down the backlight lines */
bcm2835_pwm_set_mode(PWM_BACKLIGHT, 1, 0);
bcm2835_gpio_set_pud(GLINE_BACKLIGHT, BCM2835_GPIO_PUD_OFF);
bcm2835_gpio_fsel(GLINE_BACKLIGHT, BCM2835_GPIO_FSEL_INPT);
/* 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");
2019-11-29 01:52:56 -07:00
}
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;
2019-12-01 01:21:26 -07:00
return rc;
}
2019-12-01 14:34:57 -07:00
void Gpio_set_backlight(unsigned level)
{
if (level > BACKLIGHT_MAX)
level = BACKLIGHT_MAX;
bcm2835_pwm_set_data(PWM_BACKLIGHT, level);
}