aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/libboard/common/source/led.c
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2017-03-06 16:58:00 +0100
committerHarald Welte <laforge@gnumonks.org>2017-03-06 16:58:00 +0100
commitabba8a8d85a0ed3f699ac6fd38178fbc42dcacf9 (patch)
treeaaa09dd914fe92361ecd691ea66a46bc4a4e29b3 /firmware/libboard/common/source/led.c
parent054216d94d5e772c01daee554ce7067580c13a35 (diff)
LED: Introduce LED blinking pattern code
It might be useful to display some different blinking patterns to indicate specific system state (such as DFU mode vs. regular firmware)
Diffstat (limited to 'firmware/libboard/common/source/led.c')
-rw-r--r--firmware/libboard/common/source/led.c426
1 files changed, 258 insertions, 168 deletions
diff --git a/firmware/libboard/common/source/led.c b/firmware/libboard/common/source/led.c
index 1a88e45..e4d2d57 100644
--- a/firmware/libboard/common/source/led.c
+++ b/firmware/libboard/common/source/led.c
@@ -1,168 +1,258 @@
-/* ----------------------------------------------------------------------------
- * ATMEL Microcontroller Software Support
- * ----------------------------------------------------------------------------
- * Copyright (c) 2008, Atmel Corporation
- *
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * - Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the disclaimer below.
- *
- * Atmel's name may not be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
- * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
- * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- * ----------------------------------------------------------------------------
- */
-
-/**
- * \file
- */
-
-/*------------------------------------------------------------------------------
- * Headers
- *------------------------------------------------------------------------------*/
-
-#include "board.h"
-
-/*------------------------------------------------------------------------------
- * Local Variables
- *------------------------------------------------------------------------------*/
-
-#ifdef PINS_LEDS
-static const Pin pinsLeds[] = { PINS_LEDS } ;
-static const uint32_t numLeds = PIO_LISTSIZE( pinsLeds ) ;
-#endif
-
-/*------------------------------------------------------------------------------
- * Global Functions
- *------------------------------------------------------------------------------*/
-
-/**
- * Configures the pin associated with the given LED number. If the LED does
- * not exist on the board, the function does nothing.
- * \param led Number of the LED to configure.
- * \return 1 if the LED exists and has been configured; otherwise 0.
- */
-extern uint32_t LED_Configure( uint32_t dwLed )
-{
-#ifdef PINS_LEDS
- // Check that LED exists
- if ( dwLed >= numLeds)
- {
-
- return 0;
- }
-
- // Configure LED
- return ( PIO_Configure( &pinsLeds[dwLed], 1 ) ) ;
-#else
- return 0 ;
-#endif
-}
-
-/**
- * Turns the given LED on if it exists; otherwise does nothing.
- * \param led Number of the LED to turn on.
- * \return 1 if the LED has been turned on; 0 otherwise.
- */
-extern uint32_t LED_Set( uint32_t dwLed )
-{
-#ifdef PINS_LEDS
- /* Check if LED exists */
- if ( dwLed >= numLeds )
- {
- return 0 ;
- }
-
- /* Turn LED on */
- if ( pinsLeds[dwLed].type == PIO_OUTPUT_0 )
- {
-
- PIO_Set( &pinsLeds[dwLed] ) ;
- }
- else
- {
- PIO_Clear( &pinsLeds[dwLed] ) ;
- }
-
- return 1 ;
-#else
- return 0 ;
-#endif
-}
-
-/**
- * Turns a LED off.
- *
- * \param led Number of the LED to turn off.
- * \return 1 if the LED has been turned off; 0 otherwise.
- */
-extern uint32_t LED_Clear( uint32_t dwLed )
-{
-#ifdef PINS_LEDS
- /* Check if LED exists */
- if ( dwLed >= numLeds )
- {
- return 0 ;
- }
-
- /* Turn LED off */
- if ( pinsLeds[dwLed].type == PIO_OUTPUT_0 )
- {
- PIO_Clear( &pinsLeds[dwLed] ) ;
- }
- else
- {
- PIO_Set( &pinsLeds[dwLed] ) ;
- }
-
- return 1 ;
-#else
- return 0 ;
-#endif
-}
-
-/**
- * Toggles the current state of a LED.
- *
- * \param led Number of the LED to toggle.
- * \return 1 if the LED has been toggled; otherwise 0.
- */
-extern uint32_t LED_Toggle( uint32_t dwLed )
-{
-#ifdef PINS_LEDS
- /* Check if LED exists */
- if ( dwLed >= numLeds )
- {
- return 0 ;
- }
-
- /* Toggle LED */
- if ( PIO_GetOutputDataStatus( &pinsLeds[dwLed] ) )
- {
- PIO_Clear( &pinsLeds[dwLed] ) ;
- }
- else
- {
- PIO_Set( &pinsLeds[dwLed] ) ;
- }
-
- return 1 ;
-#else
- return 0 ;
-#endif
-}
-
+#include <stdint.h>
+#include <string.h>
+#include <assert.h>
+
+#include <osmocom/core/timer.h>
+
+#include "board.h"
+#include "utils.h"
+#include "led.h"
+
+#ifdef PINS_LEDS
+static const Pin pinsLeds[] = { PINS_LEDS } ;
+
+static void led_set(enum led led, int on)
+{
+ ASSERT(led < PIO_LISTSIZE(pinsLeds));
+
+ if (on)
+ PIO_Set(&pinsLeds[led]);
+ else
+ PIO_Clear(&pinsLeds[led]);
+}
+
+/* LED blinking code */
+
+/* a single state in a sequence of blinking */
+struct blink_state {
+ /* duration of the state in ms */
+ uint16_t duration;
+ /* bringhtness of LED during the state */
+ uint8_t on;
+} __attribute__((packed));
+
+static const struct blink_state bs_off[] = {
+ { 0, 0 }
+};
+
+static const struct blink_state bs_on[] = {
+ { 0, 1 }
+};
+
+static const struct blink_state bs_3on_5off[] = {
+ { 300, 1 }, { 500, 0 }
+};
+
+static const struct blink_state bs_3on_30off[] = {
+ { 300, 1 }, { 3000, 0 }
+};
+
+static const struct blink_state bs_3on_1off_3on_30off[] = {
+ { 300, 1 }, { 100, 0 }, { 300, 1 }, { 3000, 0 }
+};
+
+static const struct blink_state bs_3on_1off_3on_1off_3on_30off[] = {
+ { 300, 1 }, { 100, 0 }, { 300, 1 }, { 100, 0 }, { 300, 1 }, { 3000, 0 }
+};
+static const struct blink_state bs_200on_off[] = {
+ { 20000, 1 }, { 0, 0 },
+};
+static const struct blink_state bs_600on_off[] = {
+ { 60000, 1 }, { 0, 0 },
+};
+
+
+/* a blink pattern is an array of blink_states */
+struct blink_pattern {
+ const struct blink_state *states;
+ uint16_t size;
+};
+
+/* compiled-in default blinking patterns */
+static const struct blink_pattern patterns[] = {
+ [BLINK_ALWAYS_OFF] = {
+ .states = bs_off,
+ .size = ARRAY_SIZE(bs_off),
+ },
+ [BLINK_ALWAYS_ON] = {
+ .states = bs_on,
+ .size = ARRAY_SIZE(bs_on),
+ },
+ [BLINK_3O_5F] = {
+ .states = bs_3on_5off,
+ .size = ARRAY_SIZE(bs_3on_5off),
+ },
+ [BLINK_3O_30F] = {
+ .states = bs_3on_30off,
+ .size = ARRAY_SIZE(bs_3on_30off),
+ },
+ [BLINK_3O_1F_3O_30F] = {
+ .states = bs_3on_1off_3on_30off,
+ .size = ARRAY_SIZE(bs_3on_1off_3on_30off),
+ },
+ [BLINK_3O_1F_3O_1F_3O_30F] = {
+ .states = bs_3on_1off_3on_1off_3on_30off,
+ .size = ARRAY_SIZE(bs_3on_1off_3on_1off_3on_30off),
+ },
+ [BLINK_200O_F] = {
+ .states = bs_200on_off,
+ .size = ARRAY_SIZE(bs_200on_off),
+ },
+ [BLINK_600O_F] = {
+ .states = bs_600on_off,
+ .size = ARRAY_SIZE(bs_600on_off),
+ },
+};
+
+struct led_state {
+ /* which led are we handling */
+ enum led led;
+
+ /* timer */
+ struct osmo_timer_list timer;
+
+ /* pointer and size of blink array */
+ const struct blink_pattern *pattern;
+
+ unsigned int cur_state;
+ unsigned int illuminated;
+
+ /* static allocated space for custom blinking pattern */
+ struct blink_pattern pattern_cust;
+ struct blink_state blink_cust[10];
+};
+
+static unsigned int cur_state_inc(struct led_state *ls)
+{
+ ls->cur_state = (ls->cur_state + 1) % ls->pattern->size;
+ return ls->cur_state;
+}
+
+static const struct blink_state *
+next_blink_state(struct led_state *ls)
+{
+ return &ls->pattern->states[cur_state_inc(ls)];
+}
+
+/* apply the next state to the LED */
+static void apply_blinkstate(struct led_state *ls,
+ const struct blink_state *bs)
+{
+ led_set(ls->led, bs->on);
+ ls->illuminated = bs->on;
+
+ /* re-schedule the timer */
+ if (bs->duration) {
+ uint32_t us = bs->duration * 1000;
+ osmo_timer_schedule(&ls->timer, us / 1000000, us % 1000000);
+ }
+}
+
+static void blink_tmr_cb(void *data)
+{
+ struct led_state *ls = data;
+ const struct blink_state *next_bs = next_blink_state(ls);
+
+ /* apply the next state to the LED */
+ apply_blinkstate(ls, next_bs);
+}
+
+static struct led_state led_state[] = {
+ [LED_GREEN] = {
+ .led = LED_GREEN,
+ .timer.cb = blink_tmr_cb,
+ .timer.data = &led_state[LED_GREEN],
+ },
+ [LED_RED] = {
+ .led = LED_RED,
+ .timer.cb = blink_tmr_cb,
+ .timer.data = &led_state[LED_RED],
+ },
+};
+#endif /* PINS_LEDS */
+
+void led_blink(enum led led, enum led_pattern blink)
+{
+#ifdef PINS_LEDS
+ struct led_state *ls;
+
+ if (led >= ARRAY_SIZE(led_state))
+ return;
+ ls = &led_state[led];
+
+ /* stop previous blinking, if any */
+ osmo_timer_del(&ls->timer);
+ led_set(led, 0);
+ ls->illuminated = 0;
+ ls->pattern = NULL;
+ ls->cur_state = 0;
+
+ switch (blink) {
+ case BLINK_CUSTOM:
+ ls->pattern = &ls->pattern_cust;
+ break;
+ default:
+ if (blink >= ARRAY_SIZE(patterns))
+ return;
+ ls->pattern = &patterns[blink];
+ break;
+ }
+
+ if (ls->pattern && ls->pattern->size > 0)
+ apply_blinkstate(ls, &ls->pattern->states[0]);
+#endif
+}
+
+enum led_pattern led_get(enum led led)
+{
+#ifdef PINS_LEDS
+ struct led_state *ls;
+ unsigned int i;
+
+ if (led >= ARRAY_SIZE(led_state))
+ return -1;
+ ls = &led_state[led];
+
+ if (ls->pattern == &ls->pattern_cust)
+ return BLINK_CUSTOM;
+
+ for (i = 0; i < ARRAY_SIZE(patterns); i++) {
+ if (ls->pattern == &patterns[i])
+ return i;
+ }
+#endif
+ /* default case, shouldn't be reached */
+ return -1;
+}
+
+void led_start(void)
+{
+ led_set(LED_GREEN, led_state[LED_GREEN].illuminated);
+ led_set(LED_RED, led_state[LED_RED].illuminated);
+}
+
+void led_stop(void)
+{
+ led_set(LED_GREEN, 0);
+ led_set(LED_RED, 0);
+}
+
+void led_init(void)
+{
+#ifdef PINS_LEDS
+ PIO_Configure(pinsLeds, PIO_LISTSIZE(pinsLeds));
+ led_set(LED_GREEN, 0);
+ led_set(LED_RED, 0);
+#endif
+}
+
+void led_fini(void)
+{
+#ifdef PINS_LEDS
+ /* we don't actually need to do this, but just in case... */
+ osmo_timer_del(&led_state[LED_RED].timer);
+ osmo_timer_del(&led_state[LED_GREEN].timer);
+ led_set(LED_GREEN, 0);
+ led_set(LED_RED, 0);
+#endif
+}