Skip to content

Commit

Permalink
Merge pull request #20833 from chrysn-pull-requests/leds-provided-on-…
Browse files Browse the repository at this point in the history
…demand

drivers/led: Allow LEDn_ON to be disabled by other modules
  • Loading branch information
chrysn authored Aug 26, 2024
2 parents d7669fa + 739e269 commit 48f156f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
15 changes: 15 additions & 0 deletions boards/common/particle-mesh/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ extern "C" {
#define LED2_MASK (1 << 15)
#define LED_MASK (LED0_MASK | LED1_MASK | LED2_MASK)

/* The typical SAUL setup for this board uses PWM to make the LEDs (really a
* single RGB LED) into a PWM controlled RGB LED entry. As a consequence of the
* PWM configuration, toggling the GPIO has no effect any more, and thus we do
* not define the macros so that no LEDs get picked up for LEDn_IS_PROVIDED.
* (The LEDn_ON etc macros will still be present and no-op as usual, but those
* explicitly checking for IS_PROVIDED will get an accurate picture).
*
* Both conditions are typically true when saul_default is on, but strictly, it
* is those two that in combination make LEDs effectively unavailable to users.
* */
#if !(IS_USED(MODULE_AUTO_INIT_SAUL) && IS_USED(MODULE_SAUL_PWM))

#define LED0_ON (LED_PORT->OUTCLR = LED0_MASK)
#define LED0_OFF (LED_PORT->OUTSET = LED0_MASK)
#define LED0_TOGGLE (LED_PORT->OUT ^= LED0_MASK)
Expand All @@ -106,6 +118,9 @@ extern "C" {
#define LED2_ON (LED_PORT->OUTCLR = LED2_MASK)
#define LED2_OFF (LED_PORT->OUTSET = LED2_MASK)
#define LED2_TOGGLE (LED_PORT->OUT ^= LED2_MASK)

#endif /* !(IS_USED(MODULE_AUTO_INIT_SAUL) && IS_USED(MODULE_SAUL_PWM)) */

/** @} */

/**
Expand Down
22 changes: 13 additions & 9 deletions drivers/periph_common/init_leds.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @}
*/

#include "board.h"
#include "led.h"
#include "periph/gpio.h"
#include "kernel_defines.h"

Expand All @@ -35,28 +35,32 @@ void led_init(void)
return;
}

#ifdef LED0_PIN
/* The condition is dual: We don't init if the LED is absent (eg. when a
* LEDn_PIN is defined, but there is a higher level driver such as SAUL PWM that makes the
* direct use impossible), but we also don't init if there is no pin (eg.
* on native where there is a different mechanism for LEDs). */
#if defined(LED0_IS_PRESENT) && defined(LED0_PIN)
LED_INIT(0);
#endif
#ifdef LED1_PIN
#if defined(LED1_IS_PRESENT) && defined(LED1_PIN)
LED_INIT(1);
#endif
#ifdef LED2_PIN
#if defined(LED2_IS_PRESENT) && defined(LED2_PIN)
LED_INIT(2);
#endif
#ifdef LED3_PIN
#if defined(LED3_IS_PRESENT) && defined(LED3_PIN)
LED_INIT(3);
#endif
#ifdef LED4_PIN
#if defined(LED4_IS_PRESENT) && defined(LED4_PIN)
LED_INIT(4);
#endif
#ifdef LED5_PIN
#if defined(LED5_IS_PRESENT) && defined(LED5_PIN)
LED_INIT(5);
#endif
#ifdef LED6_PIN
#if defined(LED6_IS_PRESENT) && defined(LED6_PIN)
LED_INIT(6);
#endif
#ifdef LED7_PIN
#if defined(LED7_IS_PRESENT) && defined(LED7_PIN)
LED_INIT(7);
#endif
}

0 comments on commit 48f156f

Please sign in to comment.