Project

General

Profile

Download (2.7 KB) Statistics
| Branch: | Tag: | Revision:
1
/* SAM7DFU blink code support 
2
 * (C) 2006 by Harald Welte <laforge@gnumonks.org>
3
 *
4
 */
5

    
6
#include <os/pit.h>
7
#include <os/dbgu.h>
8
#include <os/led.h>
9
#include <os/blinkcode.h>
10

    
11
#define NUM_LEDS 2
12

    
13
enum blinkcode_state {
14
	BLINKCODE_STATE_NONE,
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 */
19
	BLINKCODE_STATE_DONE,
20
};
21

    
22
#define TIME_SILENT	(1*HZ)
23
#define TIME_INIT	(1*HZ)
24
#define TIME_BLINK	(HZ/4)
25

    
26
struct blinker {
27
	struct timer_list timer;
28
	enum blinkcode_state state;
29
	int num;
30
	int cur;
31
	uint8_t led;
32
};
33

    
34
static struct blinker blink_state[NUM_LEDS];
35

    
36
static void blinkcode_cb(void *data)
37
{
38
	/* we got called back by the timer */
39
	struct blinker *bl = data;
40
	
41
	DEBUGPCRF("(jiffies=%lu, data=%p, state=%u)",
42
		  jiffies, data, bl->state);
43
	switch (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;
48
		bl->cur = bl->num;
49
		break;
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;
55
		break;
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;
61
		break;
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;
66
		bl->cur--;
67
		bl->timer.expires = jiffies + TIME_BLINK;
68
		if (bl->cur <= 0)
69
			bl->state = BLINKCODE_STATE_DONE;
70
		break;
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;
76
		break;
77
	case BLINKCODE_STATE_DONE:
78
		/* we've been on, turn off */
79
		led_switch(bl->led, 0);
80
		return;
81
		break;
82
	}
83
	/* default case: re-add the timer */
84
	timer_add(&bl->timer);
85
}
86

    
87
void blinkcode_set(int led, enum blinkcode_num num)
88
{
89
	DEBUGPCRF("(jiffies=%lu, led=%u, num=%u)", jiffies, led, num);
90

    
91
	if (--led > NUM_LEDS)
92
		return;
93

    
94
	timer_del(&blink_state[led].timer);
95

    
96
	blink_state[led].num = num;
97
	blink_state[led].state = BLINKCODE_STATE_NONE;
98
	blink_state[led].timer.expires = jiffies;
99

    
100
	if (num != BLINKCODE_NONE)
101
		timer_add(&blink_state[led].timer);
102
}
103

    
104
void blinkcode_init(void)
105
{
106
	int i;
107

    
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;
114
	}
115
}
(1-1/40)
Add picture from clipboard (Maximum size: 48.8 MB)