summaryrefslogtreecommitdiff
path: root/dali2.c
blob: 57bd72f11b14f8638f5c086e92462c3a516c40fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#define DALI_INVALID	0xa0cc

enum dali_state {
	DALI_IDLE = 0,
	DALI_SPACE,		// 1
	DALI_DEAD_SPACE,	// 2

	DALI_RX_SBIT,		// 3
	DALI_RX_DATA,		// 4

	DALI_TX_SBIT0,		// 5
	DALI_TX_SBIT1,		// 6
	DALI_TX_0,		// 7
	DALI_TX_1,		// 8
	DALI_TX_STOP,		// 9
};
static volatile uint8_t dali_state;

static volatile uint8_t dali_rx;
static volatile bool dali_rx_avail;

static volatile uint16_t dali_tx;
static volatile bool dali_tx_rq;

struct dalistat {
	uint16_t
		rxok,
		falsestart,
		noise,
		manchester;
} dalistat;


#undef DEBUG

#ifdef DEBUG
#define XBUF 36
static uint8_t dbuf[XBUF], sbuf[XBUF], fbuf[XBUF];
static int8_t dbufpos;
#define F_MAJ	0x01
#define F_MCH	0x10
#endif

#define dali_s_listening()	(dali_state <= DALI_SPACE)
#define dali_s_cdr()		(dali_state == DALI_RX_DATA)

ISR(TIMER0_COMPA_vect)
{
	static uint8_t history = 0;
#define hist_size		5
#define hist_mask		((1 << hist_size) - 1)
#define hist_majority_1()	(((history | (history + 1)) & hist_mask) == hist_mask)
#define hist_majority_0()	(((history & (history - 1)) & hist_mask) == 0)

	static uint8_t bitpos = 0;
	static uint8_t rx_prevstate_errs, rx_data;

	static uint16_t tx_data;

	uint8_t bit = 0;
	static uint8_t subsamp = 0;
	uint8_t flags = 0;

	subsamp++;
	subsamp &= 7;

	while (TCNT0 < 52)
		asm volatile ("nop\n" ::: "memory");
	/* CTC mode didn't work, CBA to debug */
	TCNT0 = 0;

	history <<= 1;
	history |= (PIND >> D_DALII) & 1;

	/*
	 * subsamp	1	2	3	4	5	6	7	0	||
	 *									^ state trigger
	 *		     /edge/	    <----------- stable / sample ---------->
	 */
	if (dali_s_cdr()) {
		if (subsamp >= 2 && subsamp <= 6 && ((history & 0x0f) == 0x03 || (history & 0x0f) == 0x0c))
			subsamp = 4;
	}
	if (dali_s_listening() && hist_majority_1()) {
		rx_prevstate_errs = dali_state;
		dali_state = DALI_RX_SBIT;
		subsamp = 0;
		return;
	}

	if (subsamp)
		return;

#ifdef DEBUG
	if (dali_state != DALI_IDLE && dbufpos < XBUF) {
		if (dbufpos >= 0) {
			sbuf[dbufpos] = dali_state;
			dbuf[dbufpos] = history;
		}
		dbufpos++;
	}
#endif

	switch (dali_state) {
	case DALI_SPACE:
	case DALI_DEAD_SPACE:
		if (bitpos > 0) {
			bitpos--;
			return;
		}
		dali_state = DALI_IDLE;
	case DALI_IDLE:
		break;

	case DALI_RX_SBIT:
		if (!hist_majority_0()) {
			if (history == 0xf8) {
				subsamp = 7;
				return;
			}

			dalistat.falsestart++;
			dali_state = rx_prevstate_errs;
			return;
		}
		dali_state = DALI_RX_DATA;
		rx_prevstate_errs = 0;
		rx_data = 0;
		bitpos = 0;
		break;

	case DALI_RX_DATA:
		if (!(hist_majority_1() || hist_majority_0())) {
			rx_prevstate_errs++;
			dalistat.noise++;
			bit = 0;
		} else
			bit = hist_majority_1();

		if (bitpos & 1) {
			if (bit == (rx_data & 1)) {
				rx_prevstate_errs++;
				dalistat.manchester++;
			}
		} else {
			rx_data <<= 1;
			rx_data |= bit;
		}
		bitpos++;
		if (bitpos == 16) {
			bitpos = 20;
			dali_state = DALI_DEAD_SPACE;
			if (!rx_prevstate_errs) {
				dali_rx = rx_data;
				dali_rx_avail = 1;
				dalistat.rxok++;
			}
		}
		break;

	case DALI_TX_SBIT0:
		bit = 1;
		dali_state = DALI_TX_SBIT1;
		break;
	case DALI_TX_SBIT1:
		bit = 0;
		dali_state = DALI_TX_0;
		break;
	case DALI_TX_0:
		if (bitpos == 16) {
			bitpos = 8;
			dali_state = DALI_TX_STOP;
			PORTD |= (1 << D_DALIO);
		} else {
			bit = (tx_data & (1 << (15 - bitpos))) ? 1 : 0;
			dali_state = DALI_TX_1;
		}
		break;
	case DALI_TX_1:
		bit = (tx_data & (1 << (15 - bitpos))) ? 0 : 1;
		bitpos++;
		dali_state = DALI_TX_0;
		break;

	case DALI_TX_STOP:
		if (bitpos == 0) {
			dali_tx_rq = 0;
			bitpos = 25;
			dali_state = DALI_SPACE;
		} else
			bitpos--;
		break;
	}

	if ((dali_state == DALI_TX_SBIT1) || (dali_state == DALI_TX_0) || (dali_state == DALI_TX_1)) {
		if (bit) {
			PORTD &= ~(1 << D_DALIO);
		} else {
			PORTD |= (1 << D_DALIO);
		}
	}

#ifdef DALI_USER_IDLE
	if (dali_state == DALI_IDLE && !dali_tx_rq) {
		uint16_t next = dali_user_idle();
		if (next != DALI_INVALID) {
			tx_data = next;
			bitpos = 0;
			dali_state = DALI_TX_SBIT0;
			subsamp = 7;
		}
	}
#endif

	if (dali_state == DALI_IDLE && dali_tx_rq) {
		tx_data = dali_tx;
		bitpos = 0;
		dali_tx = DALI_INVALID;
		dali_tx_rq = 2;
		dali_state = DALI_TX_SBIT0;
		subsamp = 7;
	}
}

static void dali_send(uint16_t word)
{
#if 0
	uart_wait();
	uart_puts("\n\n\ndali_send ");
	uart_puthex(word >> 8);
	uart_puthex(word & 0xff);
	uart_puts("\n");
	uart_wait();
#endif

	dali_rx_avail = 0;
	dali_tx = word;

	do	asm volatile ("" ::: "memory");
	while (dali_tx_rq);

	dali_tx_rq = 1;
	while (dali_tx_rq || dali_state != DALI_IDLE) {
		asm volatile ("" ::: "memory");
	}

#if 0
	uart_wait();
	uart_puts("dali_sent ");
	uart_puthex(word >> 8);
	uart_puthex(word & 0xff);
	uart_puts(" ");
	if (dali_rx_avail)
		uart_puthex(dali_rx);
	else
		uart_puts("norx");
	uart_puts("\n");
	uart_wait();
	for (uint8_t x = 0; x < dbufpos; x++)
		uart_puthex(sbuf[x]);
	uart_puts("\n");
	uart_wait();
	for (uint8_t x = 0; x < dbufpos; x++)
		uart_puthex(dbuf[x]);
	uart_puts("\n");
	uart_wait();
	for (uint8_t x = 0; x < dbufpos; x++)
		uart_puthex(fbuf[x]);
	uart_puts("\n");
	uart_wait();
#endif
}

static void dali_twice(uint16_t word)
{
	dali_send(word);
	_delay_ms(25);
	dali_send(word);
}

static void dali_init(void)
{
	PORTD |= (1 << D_DALII) | (1 << D_DALIO);

	dali_state = DALI_IDLE;
	dali_tx_rq = 0;
	dali_rx_avail = 0;

	dalistat.falsestart = dalistat.noise = dalistat.manchester = dalistat.rxok = 0;

	TIFR0  = (1 << OCF0A);
	TCNT0 = 0;
	OCR0A = 49; /*52*/			// 8 MHz / 8 / 52 = 19230.
	asm volatile ("" ::: "memory");
	TIMSK0 = (1 << OCIE0A);
	TCCR0A = 0;
	asm volatile ("" ::: "memory");
	TCCR0B = (0 << CS02) | (1 << CS01) | (0 << CS00); // 8 MHz / 8 = 1MHz
}

static void dali_buscheck(void)
{
	while ((PIND >> D_DALII) & 1) {
		uart_puttick();
		uart_puts("DALI bus stuck/not powered/active\n");
		_delay_ms(20);
	}
}