81 lines
1.5 KiB
C
81 lines
1.5 KiB
C
#include "led.h"
|
|
|
|
#include <zephyr/logging/log.h>
|
|
#include <zephyr/drivers/gpio.h>
|
|
|
|
LOG_MODULE_REGISTER(led, LOG_LEVEL_INF);
|
|
|
|
static const struct gpio_dt_spec led0_gpio = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);
|
|
static const struct gpio_dt_spec led1_gpio = GPIO_DT_SPEC_GET(DT_ALIAS(led1), gpios);
|
|
static int led_states[2] = {0, 0};
|
|
|
|
static int pin_init(const struct gpio_dt_spec *dt) {
|
|
int ret;
|
|
|
|
if (!device_is_ready(dt->port)) {
|
|
LOG_ERR("LED0 GPIO device not ready");
|
|
return -ENODEV;
|
|
}
|
|
|
|
ret = gpio_pin_configure_dt(dt, GPIO_OUTPUT_INACTIVE);
|
|
if (ret != 0) {
|
|
LOG_ERR("Failed to configure LED0 GPIO: %d", ret);
|
|
return ret;
|
|
}
|
|
|
|
// Turn it off
|
|
ret = gpio_pin_set_dt(dt, 0);
|
|
if (ret != 0) {
|
|
LOG_ERR("Failed to initialize LED");
|
|
return ret;
|
|
}
|
|
|
|
LOG_INF("LED driver initialized (GPIO %d)", dt->pin);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int led_init() {
|
|
int ret;
|
|
|
|
ret = pin_init(&led0_gpio);
|
|
if (ret != 0) {
|
|
return ret;
|
|
}
|
|
|
|
ret = pin_init(&led1_gpio);
|
|
if (ret != 0) {
|
|
return ret;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int led_set(int led, int state) {
|
|
int ret;
|
|
int set_led = led ? 1 : 0;
|
|
led_states[set_led] = state ? 1 : 0;
|
|
|
|
if (!set_led) {
|
|
ret = gpio_pin_set_dt(&led0_gpio, led_states[set_led]);
|
|
if (ret != 0) {
|
|
LOG_ERR("Failed to set LED0: %d", ret);
|
|
}
|
|
}
|
|
else {
|
|
ret = gpio_pin_set_dt(&led1_gpio, led_states[set_led]);
|
|
if (ret != 0) {
|
|
LOG_ERR("Failed to set LED1: %d", ret);
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
int led0_toggle() {
|
|
return led_set(0, !led_states[0]);
|
|
}
|
|
|
|
int led1_toggle() {
|
|
return led_set(1, !led_states[1]);
|
|
} |