From b0b3b071d3fdad0cff464f17f74e60804e647da1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikolai=20G=C3=BCtschow?= Date: Thu, 11 Jul 2024 14:48:16 +0200 Subject: [PATCH 1/3] drivers/led: add LED_NUMOF and convenience inline functions --- drivers/include/led.h | 99 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 93 insertions(+), 6 deletions(-) diff --git a/drivers/include/led.h b/drivers/include/led.h index 40277f3c30..73caeb741e 100644 --- a/drivers/include/led.h +++ b/drivers/include/led.h @@ -9,7 +9,7 @@ /** * @defgroup drivers_led Control on-board LEDs * @ingroup drivers_actuators - * @brief Access macros to control the on-board LEDs + * @brief Access macros and functions to control the on-board LEDs * * This header contains a set of macros for controlling the on-board LEDs of * a board. The LEDs are enumerated, starting from LED0 to LED7. As most @@ -24,7 +24,7 @@ * @{ * * @file - * @brief Macros for controlling the on-board LEDs + * @brief Macros and inline functions for controlling the on-board LEDs * * @author Hauke Petersen */ @@ -106,15 +106,102 @@ extern "C" { #define LED7_IS_PRESENT /**< indicate that LED7 is present */ #endif +/** + * Number of LEDs available on the current board. + */ +#if defined(LED7_IS_PRESENT) +#define LED_NUMOF 8 +#elif defined(LED6_IS_PRESENT) +#define LED_NUMOF 7 +#elif defined(LED5_IS_PRESENT) +#define LED_NUMOF 6 +#elif defined(LED4_IS_PRESENT) +#define LED_NUMOF 5 +#elif defined(LED3_IS_PRESENT) +#define LED_NUMOF 4 +#elif defined(LED2_IS_PRESENT) +#define LED_NUMOF 3 +#elif defined(LED1_IS_PRESENT) +#define LED_NUMOF 2 +#elif defined(LED0_IS_PRESENT) +#define LED_NUMOF 1 +#else +#define LED_NUMOF 0 +#endif + /** @} */ /** - * @name Convenience LED control macros + * @name Convenience LED control functions and macros * @{ */ -#define LED_ON(x) LED ## x ##_ON /**< Turn on led x */ -#define LED_OFF(x) LED ## x ## _OFF /**< Turn off led x */ -#define LED_TOGGLE(x) LED ## x ##_TOGGLE /**< Toggle led x */ +#define LED_ON(id) LED ## id ##_ON /**< Turn on an LED */ +#define LED_OFF(id) LED ## id ## _OFF /**< Turn off an LED */ +#define LED_TOGGLE(id) LED ## id ##_TOGGLE /**< Toggle an LED */ + +/** + * Turn on an LED. + * + * @note If id is a compile-time constant, consider using @ref LED_ON(id) instead. + * + * @param id id of LED between 0 and 7 + */ +static inline void led_on(unsigned id) +{ + switch (id) { + case 0: LED0_ON; break; + case 1: LED1_ON; break; + case 2: LED2_ON; break; + case 3: LED3_ON; break; + case 4: LED4_ON; break; + case 5: LED5_ON; break; + case 6: LED6_ON; break; + case 7: LED7_ON; break; + } +} + +/** + * Turn off an LED. + * + * @note If id is a compile-time constant, consider using @ref LED_OFF(id) instead. + * + * @param id id of LED between 0 and 7 + */ +static inline void led_off(unsigned id) +{ + switch (id) { + case 0: LED0_OFF; break; + case 1: LED1_OFF; break; + case 2: LED2_OFF; break; + case 3: LED3_OFF; break; + case 4: LED4_OFF; break; + case 5: LED5_OFF; break; + case 6: LED6_OFF; break; + case 7: LED7_OFF; break; + } +} + +/** + * Toggle an LED. + * + * @note If id is a compile-time constant, consider using @ref LED_TOGGLE(id) instead. + * + * @param id id of LED between 0 and 7 + */ +static inline void led_toggle(unsigned id) +{ + switch (id) { + case 0: LED0_TOGGLE; break; + case 1: LED1_TOGGLE; break; + case 2: LED2_TOGGLE; break; + case 3: LED3_TOGGLE; break; + case 4: LED4_TOGGLE; break; + case 5: LED5_TOGGLE; break; + case 6: LED6_TOGGLE; break; + case 7: LED7_TOGGLE; break; + } +} + /** @} */ #ifdef __cplusplus From 751a430f890983649de7525e960a5acd3fa45f15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikolai=20G=C3=BCtschow?= Date: Thu, 11 Jul 2024 14:49:37 +0200 Subject: [PATCH 2/3] tests/leds: adapt test to use led.h provided macros and functions --- tests/leds/main.c | 57 +++++++++++------------------------------------ 1 file changed, 13 insertions(+), 44 deletions(-) diff --git a/tests/leds/main.c b/tests/leds/main.c index 143983495b..e32eb80423 100644 --- a/tests/leds/main.c +++ b/tests/leds/main.c @@ -22,8 +22,7 @@ #include #include "clk.h" -#include "board.h" -#include "periph_conf.h" +#include "led.h" #include "periph/gpio.h" #define DELAY_SHORT (coreclk() / 50) @@ -38,41 +37,11 @@ void dumb_delay(uint32_t delay) int main(void) { - int numof = 0; - /* get the number of available LED's and turn them all off*/ -#ifdef LED0_ON - ++numof; - LED0_OFF; -#endif -#ifdef LED1_ON - ++numof; - LED1_OFF; -#endif -#ifdef LED2_ON - ++numof; - LED2_OFF; -#endif -#ifdef LED3_ON - ++numof; - LED3_OFF; -#endif -#ifdef LED4_ON - ++numof; - LED4_OFF; -#endif -#ifdef LED5_ON - ++numof; - LED5_OFF; -#endif -#ifdef LED6_ON - ++numof; - LED6_OFF; -#endif -#ifdef LED7_ON - ++numof; - LED7_OFF; -#endif + unsigned numof = LED_NUMOF; + for (unsigned i = 0; i < numof; i++) { + led_off(i); + } puts("On-board LED test\n"); /* cppcheck-suppress knownConditionTrueFalse @@ -86,7 +55,7 @@ int main(void) } for (unsigned i = 0; i < 4; ++i) { -#ifdef LED0_ON +#ifdef LED0_IS_PRESENT LED0_ON; dumb_delay(DELAY_LONG); LED0_OFF; @@ -100,7 +69,7 @@ int main(void) LED0_TOGGLE; dumb_delay(DELAY_LONG); #endif -#ifdef LED1_ON +#ifdef LED1_IS_PRESENT LED1_ON; dumb_delay(DELAY_LONG); LED1_OFF; @@ -114,7 +83,7 @@ int main(void) LED1_TOGGLE; dumb_delay(DELAY_LONG); #endif -#ifdef LED2_ON +#ifdef LED2_IS_PRESENT LED2_ON; dumb_delay(DELAY_LONG); LED2_OFF; @@ -128,7 +97,7 @@ int main(void) LED2_TOGGLE; dumb_delay(DELAY_LONG); #endif -#ifdef LED3_ON +#ifdef LED3_IS_PRESENT LED3_ON; dumb_delay(DELAY_LONG); LED3_OFF; @@ -142,7 +111,7 @@ int main(void) LED3_TOGGLE; dumb_delay(DELAY_LONG); #endif -#ifdef LED4_ON +#ifdef LED4_IS_PRESENT LED4_ON; dumb_delay(DELAY_LONG); LED4_OFF; @@ -156,7 +125,7 @@ int main(void) LED4_TOGGLE; dumb_delay(DELAY_LONG); #endif -#ifdef LED5_ON +#ifdef LED5_IS_PRESENT LED5_ON; dumb_delay(DELAY_LONG); LED5_OFF; @@ -170,7 +139,7 @@ int main(void) LED5_TOGGLE; dumb_delay(DELAY_LONG); #endif -#ifdef LED6_ON +#ifdef LED6_IS_PRESENT LED6_ON; dumb_delay(DELAY_LONG); LED6_OFF; @@ -184,7 +153,7 @@ int main(void) LED6_TOGGLE; dumb_delay(DELAY_LONG); #endif -#ifdef LED7_ON +#ifdef LED7_IS_PRESENT LED7_ON; dumb_delay(DELAY_LONG); LED7_OFF; From bb97445b20c5e9d5b51282fb5529a27aa523a4ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikolai=20G=C3=BCtschow?= Date: Thu, 11 Jul 2024 15:11:21 +0200 Subject: [PATCH 3/3] boards/*: include periph/gpio.h in board.h --- boards/adafruit-grand-central-m4-express/include/board.h | 1 + boards/adafruit-itsybitsy-m4/include/board.h | 1 + boards/arduino-mkr1000/include/board.h | 1 + boards/arduino-mkrfox1200/include/board.h | 1 + boards/arduino-mkrwan1300/include/board.h | 1 + boards/arduino-mkrzero/include/board.h | 1 + boards/arduino-nano-33-iot/include/board.h | 1 + boards/bastwan/include/board.h | 1 + boards/common/arduino-due/include/board.h | 1 + boards/common/arduino-zero/include/board.h | 1 + boards/common/saml1x/include/board.h | 1 + boards/feather-m0/include/board.h | 1 + boards/gd32vf103c-start/include/board.h | 1 + boards/hamilton/include/board.h | 1 + boards/samd10-xmini/include/board.h | 1 + boards/samd20-xpro/include/board.h | 1 + boards/samd21-xpro/include/board.h | 1 + boards/same54-xpro/include/board.h | 1 + boards/saml21-xpro/include/board.h | 1 + boards/samr21-xpro/include/board.h | 1 + boards/samr30-xpro/include/board.h | 1 + boards/samr34-xpro/include/board.h | 1 + boards/seeedstudio-gd32/include/board.h | 1 + boards/seeeduino_xiao/include/board.h | 1 + boards/sensebox_samd21/include/board.h | 1 + boards/serpente/include/board.h | 1 + boards/sipeed-longan-nano/include/board.h | 1 + boards/sodaq-autonomo/include/board.h | 1 + boards/sodaq-explorer/include/board.h | 1 + boards/sodaq-one/include/board.h | 1 + boards/sodaq-sara-aff/include/board.h | 1 + boards/sodaq-sara-sff/include/board.h | 1 + 32 files changed, 32 insertions(+) diff --git a/boards/adafruit-grand-central-m4-express/include/board.h b/boards/adafruit-grand-central-m4-express/include/board.h index 811c9d7947..02fb9bb796 100644 --- a/boards/adafruit-grand-central-m4-express/include/board.h +++ b/boards/adafruit-grand-central-m4-express/include/board.h @@ -20,6 +20,7 @@ #define BOARD_H #include "cpu.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/adafruit-itsybitsy-m4/include/board.h b/boards/adafruit-itsybitsy-m4/include/board.h index f0023cd4ab..91dbaef91e 100644 --- a/boards/adafruit-itsybitsy-m4/include/board.h +++ b/boards/adafruit-itsybitsy-m4/include/board.h @@ -20,6 +20,7 @@ #define BOARD_H #include "cpu.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/arduino-mkr1000/include/board.h b/boards/arduino-mkr1000/include/board.h index 2ea9afe1a3..e5a5d75e93 100644 --- a/boards/arduino-mkr1000/include/board.h +++ b/boards/arduino-mkr1000/include/board.h @@ -24,6 +24,7 @@ #include "periph_conf.h" #include "board_common.h" #include "arduino_pinmap.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/arduino-mkrfox1200/include/board.h b/boards/arduino-mkrfox1200/include/board.h index bfa91efa4d..4181ac5cd4 100644 --- a/boards/arduino-mkrfox1200/include/board.h +++ b/boards/arduino-mkrfox1200/include/board.h @@ -24,6 +24,7 @@ #include "periph_conf.h" #include "board_common.h" #include "arduino_pinmap.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/arduino-mkrwan1300/include/board.h b/boards/arduino-mkrwan1300/include/board.h index 0b14032872..e36a8fe611 100644 --- a/boards/arduino-mkrwan1300/include/board.h +++ b/boards/arduino-mkrwan1300/include/board.h @@ -25,6 +25,7 @@ #include "periph_conf.h" #include "board_common.h" #include "arduino_pinmap.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/arduino-mkrzero/include/board.h b/boards/arduino-mkrzero/include/board.h index d35ebfd5d8..5a00cbaaed 100644 --- a/boards/arduino-mkrzero/include/board.h +++ b/boards/arduino-mkrzero/include/board.h @@ -24,6 +24,7 @@ #include "periph_conf.h" #include "board_common.h" #include "arduino_pinmap.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/arduino-nano-33-iot/include/board.h b/boards/arduino-nano-33-iot/include/board.h index 9012c534a8..1209d41602 100644 --- a/boards/arduino-nano-33-iot/include/board.h +++ b/boards/arduino-nano-33-iot/include/board.h @@ -20,6 +20,7 @@ #define BOARD_H #include "cpu.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/bastwan/include/board.h b/boards/bastwan/include/board.h index dc4c5c7d17..d729dca6f3 100644 --- a/boards/bastwan/include/board.h +++ b/boards/bastwan/include/board.h @@ -25,6 +25,7 @@ #define BOARD_H #include "cpu.h" +#include "periph/gpio.h" #include "periph/pm.h" #ifdef __cplusplus diff --git a/boards/common/arduino-due/include/board.h b/boards/common/arduino-due/include/board.h index 566069bef0..76093b9d42 100644 --- a/boards/common/arduino-due/include/board.h +++ b/boards/common/arduino-due/include/board.h @@ -23,6 +23,7 @@ #include "cpu.h" #include "arduino_pinmap.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/common/arduino-zero/include/board.h b/boards/common/arduino-zero/include/board.h index d8ee6399a4..0268e9d2b6 100644 --- a/boards/common/arduino-zero/include/board.h +++ b/boards/common/arduino-zero/include/board.h @@ -25,6 +25,7 @@ #include "periph_conf.h" #include "periph_cpu.h" #include "arduino_pinmap.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/common/saml1x/include/board.h b/boards/common/saml1x/include/board.h index 7402e333c6..b7b3d75dc8 100644 --- a/boards/common/saml1x/include/board.h +++ b/boards/common/saml1x/include/board.h @@ -23,6 +23,7 @@ #define BOARD_H #include "cpu.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/feather-m0/include/board.h b/boards/feather-m0/include/board.h index 723c27f71b..bd410fdfb2 100644 --- a/boards/feather-m0/include/board.h +++ b/boards/feather-m0/include/board.h @@ -23,6 +23,7 @@ #include "cpu.h" #include "periph_conf.h" #include "periph_cpu.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/gd32vf103c-start/include/board.h b/boards/gd32vf103c-start/include/board.h index bf2b7441d7..370a0bf02a 100644 --- a/boards/gd32vf103c-start/include/board.h +++ b/boards/gd32vf103c-start/include/board.h @@ -20,6 +20,7 @@ #define BOARD_H #include "board_common.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/hamilton/include/board.h b/boards/hamilton/include/board.h index 51241d3a06..619a57b540 100644 --- a/boards/hamilton/include/board.h +++ b/boards/hamilton/include/board.h @@ -24,6 +24,7 @@ #include "cpu.h" #include "periph_conf.h" #include "periph_cpu.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/samd10-xmini/include/board.h b/boards/samd10-xmini/include/board.h index 6d040c4678..4e9dabdee0 100644 --- a/boards/samd10-xmini/include/board.h +++ b/boards/samd10-xmini/include/board.h @@ -23,6 +23,7 @@ #include "cpu.h" #include "periph_conf.h" #include "periph_cpu.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/samd20-xpro/include/board.h b/boards/samd20-xpro/include/board.h index 38f2a7e71d..7fa342ed20 100644 --- a/boards/samd20-xpro/include/board.h +++ b/boards/samd20-xpro/include/board.h @@ -23,6 +23,7 @@ #include "cpu.h" #include "periph_conf.h" #include "periph_cpu.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/samd21-xpro/include/board.h b/boards/samd21-xpro/include/board.h index afaa639b63..4f64a4da93 100644 --- a/boards/samd21-xpro/include/board.h +++ b/boards/samd21-xpro/include/board.h @@ -26,6 +26,7 @@ #include "cpu.h" #include "periph_conf.h" #include "periph_cpu.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/same54-xpro/include/board.h b/boards/same54-xpro/include/board.h index a712f88c6e..6f7cb8db3a 100644 --- a/boards/same54-xpro/include/board.h +++ b/boards/same54-xpro/include/board.h @@ -21,6 +21,7 @@ #define BOARD_H #include "cpu.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/saml21-xpro/include/board.h b/boards/saml21-xpro/include/board.h index cc0ae2a34e..65643c8d2b 100644 --- a/boards/saml21-xpro/include/board.h +++ b/boards/saml21-xpro/include/board.h @@ -21,6 +21,7 @@ #define BOARD_H #include "cpu.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/samr21-xpro/include/board.h b/boards/samr21-xpro/include/board.h index 590e5f0965..617441a554 100644 --- a/boards/samr21-xpro/include/board.h +++ b/boards/samr21-xpro/include/board.h @@ -24,6 +24,7 @@ #include "cpu.h" #include "periph_conf.h" #include "periph_cpu.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/samr30-xpro/include/board.h b/boards/samr30-xpro/include/board.h index 5286e32f82..25a62607cf 100644 --- a/boards/samr30-xpro/include/board.h +++ b/boards/samr30-xpro/include/board.h @@ -22,6 +22,7 @@ #define BOARD_H #include "cpu.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/samr34-xpro/include/board.h b/boards/samr34-xpro/include/board.h index 7b2dc4b807..87a6337d41 100644 --- a/boards/samr34-xpro/include/board.h +++ b/boards/samr34-xpro/include/board.h @@ -21,6 +21,7 @@ #define BOARD_H #include "cpu.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/seeedstudio-gd32/include/board.h b/boards/seeedstudio-gd32/include/board.h index 361363f428..ec05c3edf7 100644 --- a/boards/seeedstudio-gd32/include/board.h +++ b/boards/seeedstudio-gd32/include/board.h @@ -22,6 +22,7 @@ #define BOARD_H #include "board_common.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/seeeduino_xiao/include/board.h b/boards/seeeduino_xiao/include/board.h index 3b4410064a..5eaf74540c 100644 --- a/boards/seeeduino_xiao/include/board.h +++ b/boards/seeeduino_xiao/include/board.h @@ -24,6 +24,7 @@ #include "cpu.h" #include "periph_conf.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/sensebox_samd21/include/board.h b/boards/sensebox_samd21/include/board.h index 9d41f1fee3..2d28c0e678 100644 --- a/boards/sensebox_samd21/include/board.h +++ b/boards/sensebox_samd21/include/board.h @@ -25,6 +25,7 @@ #include "cpu.h" #include "periph_conf.h" #include "periph_cpu.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/serpente/include/board.h b/boards/serpente/include/board.h index e615a8bc75..09cd52d730 100644 --- a/boards/serpente/include/board.h +++ b/boards/serpente/include/board.h @@ -23,6 +23,7 @@ #include "cpu.h" #include "periph_conf.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/sipeed-longan-nano/include/board.h b/boards/sipeed-longan-nano/include/board.h index a4b1baa2ec..9fd6341aab 100644 --- a/boards/sipeed-longan-nano/include/board.h +++ b/boards/sipeed-longan-nano/include/board.h @@ -21,6 +21,7 @@ #define BOARD_H #include "board_common.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/sodaq-autonomo/include/board.h b/boards/sodaq-autonomo/include/board.h index fa7a6cdcee..fe1350bedd 100644 --- a/boards/sodaq-autonomo/include/board.h +++ b/boards/sodaq-autonomo/include/board.h @@ -21,6 +21,7 @@ #include "cpu.h" #include "board_common.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/sodaq-explorer/include/board.h b/boards/sodaq-explorer/include/board.h index 7987d044b9..6c98e9d231 100644 --- a/boards/sodaq-explorer/include/board.h +++ b/boards/sodaq-explorer/include/board.h @@ -21,6 +21,7 @@ #include "cpu.h" #include "board_common.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/sodaq-one/include/board.h b/boards/sodaq-one/include/board.h index c4aa4d75d9..2823680382 100644 --- a/boards/sodaq-one/include/board.h +++ b/boards/sodaq-one/include/board.h @@ -21,6 +21,7 @@ #include "cpu.h" #include "board_common.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/sodaq-sara-aff/include/board.h b/boards/sodaq-sara-aff/include/board.h index 44b96a5b51..2008676a53 100644 --- a/boards/sodaq-sara-aff/include/board.h +++ b/boards/sodaq-sara-aff/include/board.h @@ -21,6 +21,7 @@ #include "cpu.h" #include "board_common.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" { diff --git a/boards/sodaq-sara-sff/include/board.h b/boards/sodaq-sara-sff/include/board.h index 9504ec6de4..25503fd61b 100644 --- a/boards/sodaq-sara-sff/include/board.h +++ b/boards/sodaq-sara-sff/include/board.h @@ -21,6 +21,7 @@ #include "cpu.h" #include "board_common.h" +#include "periph/gpio.h" #ifdef __cplusplus extern "C" {