1 /* SAM7DFU blink code support
2 * (C) 2006 by Harald Welte <laforge@gnumonks.org>
9 #include <os/blinkcode.h>
13 enum blinkcode_state {
15 BLINKCODE_STATE_SILENT, /* period of silence at start */
16 BLINKCODE_STATE_INIT, /* initial long light */
17 BLINKCODE_STATE_BLINK_OFF, /* blinking out, currently off */
18 BLINKCODE_STATE_BLINK_ON, /* blinking out, currently on */
22 #define TIME_SILENT (1*HZ)
23 #define TIME_INIT (1*HZ)
24 #define TIME_BLINK (HZ/4)
27 struct timer_list timer;
28 enum blinkcode_state state;
34 static struct blinker blink_state[NUM_LEDS];
36 static void blinkcode_cb(void *data)
38 /* we got called back by the timer */
39 struct blinker *bl = data;
41 DEBUGPCRF("(jiffies=%lu, data=%p, state=%u)",
42 jiffies, data, bl->state);
44 case BLINKCODE_STATE_NONE:
45 led_switch(bl->led, 0);
46 bl->state = BLINKCODE_STATE_SILENT;
47 bl->timer.expires = jiffies + TIME_SILENT;
50 case BLINKCODE_STATE_SILENT:
51 /* we've finished the period of silence, turn led on */
52 led_switch(bl->led, 1);
53 bl->state = BLINKCODE_STATE_INIT;
54 bl->timer.expires = jiffies + TIME_INIT;
56 case BLINKCODE_STATE_INIT:
57 /* we've finished the period of init */
58 led_switch(bl->led, 0);
59 bl->state = BLINKCODE_STATE_BLINK_OFF;
60 bl->timer.expires = jiffies + TIME_INIT;
62 case BLINKCODE_STATE_BLINK_OFF:
63 /* we've been off, turn on */
64 led_switch(bl->led, 1);
65 bl->state = BLINKCODE_STATE_BLINK_ON;
67 bl->timer.expires = jiffies + TIME_BLINK;
69 bl->state = BLINKCODE_STATE_DONE;
71 case BLINKCODE_STATE_BLINK_ON:
72 /* we've been on, turn off */
73 led_switch(bl->led, 0);
74 bl->state = BLINKCODE_STATE_BLINK_OFF;
75 bl->timer.expires = jiffies + TIME_BLINK;
77 case BLINKCODE_STATE_DONE:
78 /* we've been on, turn off */
79 led_switch(bl->led, 0);
83 /* default case: re-add the timer */
84 timer_add(&bl->timer);
87 void blinkcode_set(int led, enum blinkcode_num num)
89 DEBUGPCRF("(jiffies=%lu, led=%u, num=%u)", jiffies, led, num);
94 timer_del(&blink_state[led].timer);
96 blink_state[led].num = num;
97 blink_state[led].state = BLINKCODE_STATE_NONE;
98 blink_state[led].timer.expires = jiffies;
100 if (num != BLINKCODE_NONE)
101 timer_add(&blink_state[led].timer);
104 void blinkcode_init(void)
108 for (i = 0; i < NUM_LEDS; i++) {
109 blink_state[i].num = 0;
110 blink_state[i].state = BLINKCODE_STATE_NONE;
111 blink_state[i].led = i+1;
112 blink_state[i].timer.data = &blink_state[i];
113 blink_state[i].timer.function = &blinkcode_cb;