-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdali.c
432 lines (360 loc) · 12.1 KB
/
dali.c
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/stm32/exti.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/timer.h>
#include <math.h>
#include "cdcacm.h"
#include "dali.h"
#include "platform.h"
/*
* TE = half period of DALI 1200 Hz
*
* - Forward frame: 38TE (34 without stop condition)
* - 2 TE start bit (0, 1)
* - 16 * 2TE data (0 --> (1, 0), 1 --> (0, 1))
* - 4 TE stop condition (1, 1, 1, 1)
*
* - 24bit forward frame: 54TE (50 without stop condition)
* - 2 TE start bit (0, 1)
* - 24 * 2TE data (0 --> (1, 0), 1 --> (0, 1))
* - 4 TE stop condition (1, 1, 1, 1)
*
* - Backward frame: 22TE (18 without stop condition)
* - 2 TE start bit (0, 1)
* - 8 * 2TE data (0 --> (1, 0), 1 --> (0, 1))
* - 4 TE stop condition (1, 1, 1, 1)
*
* - Settling time (includes stop condition)
* - Forward to backward: 5,5 - 10,5ms --> aim for 14TE
* - Before Forward:
* - Prio high: 13,5 - 14,7 ms --> aim for 33TE
* - Prio middle high: 14,9 - 16,1 ms --> aim for 36TE
* - Prio middle: 16,3 - 17,7 ms --> aim for 40TE
* - Prio middle low: 17,9 - 19,3 ms --> aim for 43TE
* - Prio low: 19,5 - 21,2 ms --> aim for 47TE
*
*/
#define DALI_RX_QUEUE_LEN 64
#define DALI_TX_QUEUE_LEN 64
#define DALI_OVERSAMPLE 8
#define DALI_STOP_COND 4 * DALI_OVERSAMPLE
#define DALI_RX_BUF_LEN 54 * DALI_OVERSAMPLE
#define DALI_TX_BUF_LEN 51
#define DALI_FREQ (2 * 1200)
#define DALI_TICK (SYSTICK_FREQ / DALI_FREQ)
static volatile struct dali_msg __rx_queue[DALI_RX_QUEUE_LEN];
static volatile int __rx_queue_at;
static volatile int __rx_queue_len;
static volatile struct dali_msg __tx_queue[DALI_TX_QUEUE_LEN];
static volatile int __tx_queue_at;
static volatile int __tx_queue_len;
static volatile uint8_t __rx_buf[DALI_RX_BUF_LEN];
static volatile int __rx_at; /* -1 = not started */
static volatile uint8_t __rx_halfbits[DALI_RX_BUF_LEN / DALI_OVERSAMPLE];
static volatile uint8_t __rx_n_halfbits;
static volatile char __tx_enc_buf[DALI_TX_BUF_LEN];
static volatile uint8_t __tx_enc_len;
static volatile int __tx_enc_at; /* -1 = not started */
static volatile uint32_t __settling_start_tick;
/* Pins */
#define DALI_RX_PORT GPIOA
#define DALI_RX_PORT_RCC RCC_GPIOA
#define DALI_RX_PIN GPIO0
#define DALI_RX_EXTI EXTI0
#define DALI_RX_EXTI_ISR exti0_isr
#define DALI_RX_EXTI_NVIC NVIC_EXTI0_IRQ
#define DALI_TX_PORT GPIOA
#define DALI_TX_PORT_RCC RCC_GPIOA
#define DALI_TX_PIN GPIO1
/* Handlers */
void DALI_RX_EXTI_ISR(void) {
if (__rx_at < 0) {
/* Start RX */
__rx_at = 0;
bool val = gpio_get(DALI_RX_PORT, DALI_RX_PIN);
#ifdef DALI_RX_INV
val = !val;
#endif
__rx_buf[0] = val;
__rx_at++;
timer_enable_counter(TIM2);
}
/* re-enable interrupt */
exti_reset_request(DALI_RX_EXTI);
exti_set_trigger(DALI_RX_EXTI, EXTI_TRIGGER_BOTH);
}
static bool rx_contains_stop(void) {
/* Check if stop condition is at end of buffer */
if (__rx_at <= DALI_STOP_COND)
return false;
int c = 0;
for (int i = __rx_at - DALI_STOP_COND; i < __rx_at; i++) {
if (!__rx_buf[i])
c++;
}
if (c > 1)
return false;
/* clean possible twitches (at most one sample) */
for (int i = 1; i < __rx_at - 1; i++) {
if (__rx_buf[i] != __rx_buf[i - 1] && __rx_buf[i] != __rx_buf[i + 1])
__rx_buf[i] = __rx_buf[i - 1];
}
/* parse halfbits */
__rx_n_halfbits = 0;
int b = 0;
for (int i = 1; i < __rx_at; i++) {
if (__rx_buf[i] != __rx_buf[b]) {
int elapsed =
(int)((double)(i - b) / (double)DALI_OVERSAMPLE + 0.5);
if (elapsed > 2)
/* error, more than two identical halfbits */
break;
for (int k = 0; k < elapsed; k++) {
__rx_halfbits[__rx_n_halfbits] = __rx_buf[b];
__rx_n_halfbits++;
if (__rx_n_halfbits >= DALI_RX_BUF_LEN / DALI_OVERSAMPLE)
__rx_n_halfbits = 0;
}
b = i;
}
}
/* append halfbit 1, which may be recognized as part of stop cond */
if (__rx_n_halfbits % 2 == 1) {
__rx_halfbits[__rx_n_halfbits] = 1;
__rx_n_halfbits++;
}
return true;
}
static int rx_parse_bit(int idx) {
if (idx * 2 + 1 >= __rx_n_halfbits)
return -1;
int b1 = __rx_halfbits[idx * 2];
int b2 = __rx_halfbits[idx * 2 + 1];
if (b1 < 0 || b2 < 0)
return -1;
if (b1 == 0 && b2 == 1)
return 1;
if (b1 == 1 && b2 == 0)
return 0;
return -1;
}
void tim2_isr(void) {
/* Missing part: Collision detection */
if (timer_get_flag(TIM2, TIM_SR_CC1IF)) {
bool rx_done = false;
bool tx_done = false;
uint32_t tick = get_systick();
/* RX */
if (__rx_at > -1) {
bool val = gpio_get(DALI_RX_PORT, DALI_RX_PIN);
#ifdef DALI_RX_INV
val = !val;
#endif
if (__rx_at >= DALI_RX_BUF_LEN) {
rx_done = true;
} else {
__rx_buf[__rx_at] = val;
}
__rx_at++;
if (rx_contains_stop()) {
rx_done |= true;
if (__rx_queue_len < DALI_RX_QUEUE_LEN)
__rx_queue[__rx_queue_len].error = true;
if (rx_parse_bit(0) != 1)
goto done;
uint32_t res = 0;
int pos;
for (pos = 0; pos < 24; pos++) {
int b = rx_parse_bit(pos + 1);
if (b < 0) {
if (pos % 8 != 0)
/* error */
goto done;
else
/* stop bit */
break;
}
res |= b << (23 - pos);
}
if (__rx_queue_len < DALI_RX_QUEUE_LEN) {
if (pos == 8) {
__rx_queue[__rx_queue_len].data[0] = res >> 16;
__rx_queue[__rx_queue_len].len = 1;
} else if (pos == 16) {
__rx_queue[__rx_queue_len].data[0] = res >> 16;
__rx_queue[__rx_queue_len].data[1] = (res >> 8) & 0xFF;
__rx_queue[__rx_queue_len].len = 2;
} else if (pos == 24) {
__rx_queue[__rx_queue_len].data[0] = res >> 16;
__rx_queue[__rx_queue_len].data[1] = (res >> 8) & 0xFF;
__rx_queue[__rx_queue_len].data[2] = res & 0xFF;
__rx_queue[__rx_queue_len].len = 3;
}
__rx_queue[__rx_queue_len].error = false;
}
done:
__rx_queue_len++;
__rx_at = -1;
}
if (!val && tick > __settling_start_tick)
__settling_start_tick = tick;
} else {
rx_done = true;
}
/* TX */
if (__tx_enc_len > 0) {
if (__tx_enc_buf[__tx_enc_at / DALI_OVERSAMPLE]) {
#ifdef DALI_TX_INV
gpio_clear(DALI_TX_PORT, DALI_TX_PIN);
#else
gpio_set(DALI_TX_PORT, DALI_TX_PIN);
#endif
} else {
#ifdef DALI_TX_INV
gpio_set(DALI_TX_PORT, DALI_TX_PIN);
#else
gpio_clear(DALI_TX_PORT, DALI_TX_PIN);
#endif
}
__tx_enc_at++;
if (__tx_enc_at >= __tx_enc_len * DALI_OVERSAMPLE) {
__tx_enc_at = -1;
__tx_enc_len = 0;
tx_done = true;
}
if (tick > __settling_start_tick)
__settling_start_tick = tick;
} else {
tx_done = true;
}
timer_clear_flag(TIM2, TIM_SR_CC1IF);
timer_set_counter(TIM2, 0);
if (rx_done && tx_done) {
timer_disable_counter(TIM2);
}
}
}
/* API */
int dali_init(void) {
__tx_queue_at = 0;
__tx_queue_len = 0;
__rx_queue_at = 0;
__rx_queue_len = 0;
__rx_at = -1;
__tx_enc_len = 0;
__tx_enc_at = -1;
__settling_start_tick = 0;
rcc_periph_clock_enable(DALI_RX_PORT_RCC);
rcc_periph_clock_enable(DALI_TX_PORT_RCC);
/* timer setup */
rcc_periph_clock_enable(RCC_TIM2);
nvic_enable_irq(NVIC_TIM2_IRQ);
rcc_periph_reset_pulse(RST_TIM2);
timer_set_mode(TIM2, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
timer_disable_preload(TIM2);
timer_continuous_mode(TIM2);
timer_set_prescaler(TIM2,
2 * rcc_apb1_frequency / DALI_FREQ / DALI_OVERSAMPLE);
timer_set_period(TIM2, 1);
timer_set_oc_value(TIM2, TIM_OC1, 1);
timer_enable_irq(TIM2, TIM_DIER_CC1IE);
/* tx setup */
gpio_set_mode(DALI_TX_PORT, GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL, DALI_TX_PIN);
#ifdef DALI_TX_INV
gpio_clear(DALI_TX_PORT, DALI_TX_PIN);
#else
gpio_set(DALI_TX_PORT, DALI_TX_PIN);
#endif
/* rx setup */
rcc_periph_clock_enable(RCC_AFIO);
gpio_set_mode(DALI_RX_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN,
DALI_RX_PIN);
gpio_set(DALI_RX_PORT, DALI_RX_PIN);
exti_set_trigger(DALI_RX_EXTI, EXTI_TRIGGER_BOTH);
nvic_enable_irq(DALI_RX_EXTI_NVIC);
exti_select_source(DALI_RX_EXTI, DALI_RX_PORT);
exti_enable_request(DALI_RX_EXTI);
return 0;
}
int dali_read(char *buf) {
if (__rx_queue_at < __rx_queue_len) {
int q = __rx_queue_at;
__rx_queue_at++;
if (__rx_queue_at == __rx_queue_len)
__rx_queue_at = __rx_queue_len = 0;
if (__rx_queue[q].error)
return -1;
for (int i = 0; i < __rx_queue[q].len; i++)
buf[i] = __rx_queue[q].data[i];
return __rx_queue[q].len;
}
return 0;
}
int dali_write(char *buf, int len, dali_prio prio) {
if (__tx_queue_len >= DALI_TX_QUEUE_LEN)
return -1;
__tx_queue[__tx_queue_len].len = len;
for (int i = 0; i < 3; i++)
__tx_queue[__tx_queue_len].data[i] = buf[i];
__tx_queue[__tx_queue_len].prio = prio;
__tx_queue_len++;
return 0;
}
int dali_main(void) {
if (__tx_queue_len > 0 && __tx_queue_at < __tx_queue_len &&
__tx_enc_at == -1) {
uint32_t settling_time = 0;
switch (__tx_queue[__tx_queue_at].prio) {
case DALI_BACKWARD:
settling_time = 14 * DALI_TICK;
break;
case DALI_HIGH:
settling_time = 33 * DALI_TICK;
break;
case DALI_MIDDLE_HIGH:
settling_time = 36 * DALI_TICK;
break;
case DALI_MIDDLE:
settling_time = 40 * DALI_TICK;
break;
case DALI_MIDDLE_LOW:
settling_time = 43 * DALI_TICK;
break;
case DALI_LOW:
settling_time = 47 * DALI_TICK;
break;
}
if (get_systick() - __settling_start_tick > settling_time) {
__tx_enc_len = 0;
__tx_enc_buf[__tx_enc_len++] = 0;
__tx_enc_buf[__tx_enc_len++] = 1;
for (int b = 0; b < __tx_queue[__tx_queue_at].len; b++) {
for (int i = 0; i < 8; i++) {
if (__tx_queue[__tx_queue_at].data[b] >> (7 - i) & 1) {
__tx_enc_buf[__tx_enc_len++] = 0;
__tx_enc_buf[__tx_enc_len++] = 1;
} else {
__tx_enc_buf[__tx_enc_len++] = 1;
__tx_enc_buf[__tx_enc_len++] = 0;
}
}
}
__tx_enc_buf[__tx_enc_len++] = 1;
__tx_enc_at = 0;
timer_enable_counter(TIM2);
__tx_queue_at++;
if (__tx_queue_at == __tx_queue_len)
__tx_queue_at = __tx_queue_len = 0;
}
}
return 0;
}
/*
* dma3fe,dm012a
* dm0198
*
* https://electronics.stackexchange.com/questions/663269/dali-protocol-d4i-how-to-access-to-memory-bank-dtr0-dtr1-at-byte-level-using
*
*/