From cb4d4ea1d94d078412f547959df52bebc1c55d71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Fri, 6 Nov 2020 13:37:43 +0100 Subject: [PATCH] LEDs: Use GPIO flags from dts properly The flags were always assumed to be ACTIVE_HIGH, which is now fixed. --- src/leds.cpp | 29 +++++++++++++++++++++++++---- test/devicetree_unfixed.h | 1 + test/zephyr.h | 8 ++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/leds.cpp b/src/leds.cpp index 9266bc0..f909599 100644 --- a/src/leds.cpp +++ b/src/leds.cpp @@ -67,7 +67,7 @@ static const int led_pin_states[NUM_LEDS][NUM_LED_PINS] = { /* * GPIO pin numbers (for up to 5 LED pins) */ -static const int led_pins[] = { +static const gpio_pin_t led_pins[] = { DT_PHA_BY_IDX(DT_PATH(leds), gpios, 0, pin), #if NUM_LED_PINS >= 2 DT_PHA_BY_IDX(DT_PATH(leds), gpios, 1, pin), @@ -102,6 +102,25 @@ static const char *const led_ports[] = { #endif }; +/* + * GPIO pin flags (for up to 5 LED pins) + */ +static const gpio_flags_t led_flags[] = { + DT_PHA_BY_IDX(DT_PATH(leds), gpios, 0, flags), +#if NUM_LED_PINS >= 2 + DT_PHA_BY_IDX(DT_PATH(leds), gpios, 1, flags), +#endif +#if NUM_LED_PINS >= 3 + DT_PHA_BY_IDX(DT_PATH(leds), gpios, 2, flags), +#endif +#if NUM_LED_PINS >= 4 + DT_PHA_BY_IDX(DT_PATH(leds), gpios, 3, flags), +#endif +#if NUM_LED_PINS >= 5 + DT_PHA_BY_IDX(DT_PATH(leds), gpios, 4, flags), +#endif +}; + static int led_states[NUM_LEDS]; // must be one of enum LedState static int trigger_timeout[NUM_LEDS]; // seconds until trigger should end @@ -148,11 +167,13 @@ void leds_update_thread() for (unsigned int pin_number = 0; pin_number < NUM_LED_PINS; pin_number++) { switch (led_pin_states[led_count][pin_number]) { case PIN_HIGH: - gpio_pin_configure(led_devs[pin_number], led_pins[pin_number], GPIO_OUTPUT); + gpio_pin_configure(led_devs[pin_number], led_pins[pin_number], + led_flags[pin_number] | GPIO_OUTPUT); gpio_pin_set(led_devs[pin_number], led_pins[pin_number], 1); break; case PIN_LOW: - gpio_pin_configure(led_devs[pin_number], led_pins[pin_number], GPIO_OUTPUT); + gpio_pin_configure(led_devs[pin_number], led_pins[pin_number], + led_flags[pin_number] | GPIO_OUTPUT); gpio_pin_set(led_devs[pin_number], led_pins[pin_number], 0); break; case PIN_FLOAT: @@ -181,7 +202,7 @@ void leds_init(bool enabled) for (unsigned int led = 0; led < NUM_LEDS; led++) { if (enabled) { led_states[led] = LED_STATE_ON; - trigger_timeout[led] = 0; // switched off the first time the main loop is called + trigger_timeout[led] = 0; // switched off the first time the main loop is called } else { led_states[led] = LED_STATE_OFF; diff --git a/test/devicetree_unfixed.h b/test/devicetree_unfixed.h index 34ae1f4..2ff7b31 100644 --- a/test/devicetree_unfixed.h +++ b/test/devicetree_unfixed.h @@ -89,6 +89,7 @@ #define DT_N_S_leds_P_gpios_IDX_0_PH DT_N_S_soc_S_pin_controller_48000000_S_gpio_48000800 #define DT_N_S_leds_P_gpios_IDX_0_VAL_pin 15 +#define DT_N_S_leds_P_gpios_IDX_0_VAL_flags 0 #define DT_N_S_leds_P_gpios_LEN 1 #define DT_N_S_soc_S_pin_controller_48000000_S_gpio_48000800_P_label "GPIOC" diff --git a/test/zephyr.h b/test/zephyr.h index c1393d6..b40c4d7 100644 --- a/test/zephyr.h +++ b/test/zephyr.h @@ -14,6 +14,7 @@ #include "devicetree.h" #include "devicetree_unfixed.h" #include "autoconf.h" +#include "stdint.h" #ifndef __deprecated #define __deprecated __attribute__((deprecated)) @@ -42,6 +43,13 @@ #define LOG_MODULE_REGISTER(a, b) +/* from gpio.h */ + +typedef uint8_t gpio_pin_t; + +typedef uint32_t gpio_flags_t; + + /* crazy functions from Zephyr's util.h */ /**