samples: canopennode: update led and sw to use gpio_dt_spec
Switch the LED and button code to the gpio_dt_spec APIs. This also gets rid of the code conditionals, so that the entire sample code is always compiled. Signed-off-by: Fabio Baltieri <fabio.baltieri@gmail.com>
This commit is contained in:
parent
fa87410cd6
commit
911a483f7a
1 changed files with 56 additions and 57 deletions
|
@ -17,32 +17,20 @@ LOG_MODULE_REGISTER(app);
|
|||
#define CAN_INTERFACE DEVICE_DT_GET(DT_CHOSEN(zephyr_canbus))
|
||||
#define CAN_BITRATE (DT_PROP(DT_CHOSEN(zephyr_canbus), bus_speed) / 1000)
|
||||
|
||||
#if DT_NODE_HAS_PROP(DT_ALIAS(green_led), gpios)
|
||||
#define LED_GREEN_PORT DT_GPIO_LABEL(DT_ALIAS(green_led), gpios)
|
||||
#define LED_GREEN_PIN DT_GPIO_PIN(DT_ALIAS(green_led), gpios)
|
||||
#define LED_GREEN_FLAGS DT_GPIO_FLAGS(DT_ALIAS(green_led), gpios)
|
||||
#endif
|
||||
static struct gpio_dt_spec led_green_gpio = GPIO_DT_SPEC_GET_OR(
|
||||
DT_ALIAS(green_led), gpios, {0});
|
||||
static struct gpio_dt_spec led_red_gpio = GPIO_DT_SPEC_GET_OR(
|
||||
DT_ALIAS(red_led), gpios, {0});
|
||||
|
||||
#if DT_NODE_HAS_PROP(DT_ALIAS(red_led), gpios)
|
||||
#define LED_RED_PORT DT_GPIO_LABEL(DT_ALIAS(red_led), gpios)
|
||||
#define LED_RED_PIN DT_GPIO_PIN(DT_ALIAS(red_led), gpios)
|
||||
#define LED_RED_FLAGS DT_GPIO_FLAGS(DT_ALIAS(red_led), gpios)
|
||||
#endif
|
||||
|
||||
#if DT_NODE_HAS_PROP(DT_ALIAS(sw0), gpios)
|
||||
#define BUTTON_PORT DT_GPIO_LABEL(DT_ALIAS(sw0), gpios)
|
||||
#define BUTTON_PIN DT_GPIO_PIN(DT_ALIAS(sw0), gpios)
|
||||
#define BUTTON_FLAGS DT_GPIO_FLAGS(DT_ALIAS(sw0), gpios)
|
||||
static struct gpio_dt_spec button_gpio = GPIO_DT_SPEC_GET_OR(
|
||||
DT_ALIAS(sw0), gpios, {0});
|
||||
static struct gpio_callback button_callback;
|
||||
#endif
|
||||
|
||||
struct led_indicator {
|
||||
const struct device *dev;
|
||||
gpio_pin_t pin;
|
||||
};
|
||||
|
||||
static struct led_indicator led_green;
|
||||
static struct led_indicator led_red;
|
||||
static uint32_t counter;
|
||||
|
||||
/**
|
||||
|
@ -53,14 +41,13 @@ static uint32_t counter;
|
|||
*/
|
||||
static void led_callback(bool value, void *arg)
|
||||
{
|
||||
struct led_indicator *led = arg;
|
||||
bool drive = value;
|
||||
struct gpio_dt_spec *led_gpio = arg;
|
||||
|
||||
if (!led || !led->dev) {
|
||||
if (!led_gpio || !led_gpio->port) {
|
||||
return;
|
||||
}
|
||||
|
||||
gpio_pin_set(led->dev, led->pin, drive);
|
||||
gpio_pin_set_dt(led_gpio, value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -73,28 +60,39 @@ static void led_callback(bool value, void *arg)
|
|||
*/
|
||||
static void config_leds(CO_NMT_t *nmt)
|
||||
{
|
||||
#ifdef LED_GREEN_PORT
|
||||
led_green.dev = device_get_binding(LED_GREEN_PORT);
|
||||
led_green.pin = LED_GREEN_PIN;
|
||||
if (led_green.dev) {
|
||||
gpio_pin_configure(led_green.dev, LED_GREEN_PIN,
|
||||
GPIO_OUTPUT_INACTIVE
|
||||
| LED_GREEN_FLAGS);
|
||||
int err;
|
||||
|
||||
if (!led_green_gpio.port) {
|
||||
LOG_INF("Green LED not available");
|
||||
} else if (!device_is_ready(led_green_gpio.port)) {
|
||||
LOG_ERR("Green LED device not ready");
|
||||
led_green_gpio.port = NULL;
|
||||
} else {
|
||||
err = gpio_pin_configure_dt(&led_green_gpio,
|
||||
GPIO_OUTPUT_INACTIVE);
|
||||
if (err) {
|
||||
LOG_ERR("failed to configure Green LED gpio: %d", err);
|
||||
led_green_gpio.port = NULL;
|
||||
}
|
||||
}
|
||||
#endif /* LED_GREEN_PORT */
|
||||
#ifdef LED_RED_PORT
|
||||
led_red.dev = device_get_binding(LED_RED_PORT);
|
||||
led_red.pin = LED_RED_PIN;
|
||||
if (led_red.dev) {
|
||||
gpio_pin_configure(led_red.dev, LED_RED_PIN,
|
||||
GPIO_OUTPUT_INACTIVE
|
||||
| LED_RED_FLAGS);
|
||||
|
||||
if (!led_red_gpio.port) {
|
||||
LOG_INF("Red LED not available");
|
||||
} else if (!device_is_ready(led_red_gpio.port)) {
|
||||
LOG_ERR("Red LED device not ready");
|
||||
led_red_gpio.port = NULL;
|
||||
} else {
|
||||
err = gpio_pin_configure_dt(&led_red_gpio,
|
||||
GPIO_OUTPUT_INACTIVE);
|
||||
if (err) {
|
||||
LOG_ERR("failed to configure Red LED gpio: %d", err);
|
||||
led_green_gpio.port = NULL;
|
||||
}
|
||||
}
|
||||
#endif /* LED_RED_PORT */
|
||||
|
||||
canopen_leds_init(nmt,
|
||||
led_green.dev ? led_callback : NULL, &led_green,
|
||||
led_red.dev ? led_callback : NULL, &led_red);
|
||||
led_callback, &led_green_gpio,
|
||||
led_callback, &led_red_gpio);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -140,14 +138,12 @@ static CO_SDO_abortCode_t odf_2102(CO_ODF_arg_t *odf_arg)
|
|||
* @param cb GPIO callback struct.
|
||||
* @param pins GPIO pin mask that triggered the interrupt.
|
||||
*/
|
||||
#ifdef BUTTON_PORT
|
||||
static void button_isr_callback(const struct device *port,
|
||||
struct gpio_callback *cb,
|
||||
uint32_t pins)
|
||||
{
|
||||
counter++;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Configure button GPIO pin and callback.
|
||||
|
@ -156,36 +152,39 @@ static void button_isr_callback(const struct device *port,
|
|||
*/
|
||||
static void config_button(void)
|
||||
{
|
||||
#ifdef BUTTON_PORT
|
||||
const struct device *dev;
|
||||
int err;
|
||||
|
||||
dev = device_get_binding(BUTTON_PORT);
|
||||
if (!dev) {
|
||||
LOG_ERR("failed to get button device");
|
||||
if (button_gpio.port == NULL) {
|
||||
LOG_INF("Button not available");
|
||||
return;
|
||||
}
|
||||
|
||||
err = gpio_pin_configure(dev, BUTTON_PIN,
|
||||
GPIO_INPUT | BUTTON_FLAGS);
|
||||
if (!device_is_ready(button_gpio.port)) {
|
||||
LOG_ERR("Button device not ready");
|
||||
return;
|
||||
}
|
||||
|
||||
err = gpio_pin_configure_dt(&button_gpio, GPIO_INPUT);
|
||||
if (err) {
|
||||
LOG_ERR("failed to configure button gpio: %d", err);
|
||||
return;
|
||||
}
|
||||
|
||||
gpio_init_callback(&button_callback, button_isr_callback,
|
||||
BIT(BUTTON_PIN));
|
||||
BIT(button_gpio.pin));
|
||||
|
||||
err = gpio_add_callback(dev, &button_callback);
|
||||
err = gpio_add_callback(button_gpio.port, &button_callback);
|
||||
if (err) {
|
||||
LOG_ERR("failed to add button callback");
|
||||
LOG_ERR("failed to add button callback: %d", err);
|
||||
return;
|
||||
}
|
||||
|
||||
err = gpio_pin_interrupt_configure(dev, BUTTON_PIN,
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
err = gpio_pin_interrupt_configure_dt(&button_gpio,
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
if (err) {
|
||||
LOG_ERR("failed to enable button callback");
|
||||
LOG_ERR("failed to enable button callback: %d", err);
|
||||
return;
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue