-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdali_drv.c
305 lines (282 loc) · 9.41 KB
/
dali_drv.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
/********************************************************************
;
; DALI master
;
; For transmit, this module uses GPIO - P0.28 (DALI send pin)
; DALI forward frame format:
;
; | S | 8 address bits | 8 command bits | stop |
; | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | | |
;
; ---+ +-+ +---+ +-+ +-+ +-+ +-+ +-+ +---+ +-+ +-+ +-+ +---+ +-+ +-+ +------------
; | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
; +-+ +-+ +-+ +-+ +-+ +-+ +---+ +-+ +---+ +-+ +-+ +-+ +-+ +-+ +-+
;
; |2TE|2TE|2TE|2TE|2TE|2TE|2TE|2TE|2TE|2TE|2TE|2TE|2TE|2TE|2TE|2TE|2TE| 4TE |
;
;
; For receive, this module uses T0-CAP0 input (capture and interrupt on both edges)
; CAP0.0 (P0.30) is connected to P0.29 (to check high / low level by software)
; DALI slave backward frame format:
;
; | S | 8 data bits | stop |
; | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | | |
;
; +---------------+ +-+ +---+ +-+ +-+ +-+ +-+ +-+ +-------------
; | | | | | | | | | | | | | | | | |
; -+ +-+ +-+ +-+ +-+ +-+ +-+ +---+ +-+
;
; |4 + 7 to 22 TE |2TE|2TE|2TE|2TE|2TE|2TE|2TE|2TE|2TE| 4TE |
;
; 2TE = 834 usec (1200 bps)
;
*******************************************************************************************/
#include "dali_drv.h"
#include <avr/interrupt.h>
static int low_time; //<! captured puls low time
static int high_time; //<! captured puls high time
static BYTE value; //<! used for dali send bit
BYTE position; //<! keeps track of sending bit position
static BYTE previous; //<! previous received bit
static WORD frame; //<! holds the received slave backward frame
static BYTE f_repeat; //<! flag command shall be repeated
static BYTE f_busy; //<! flag DALI transfer busy
BYTE f_error; //<! flag DALI transfer error
WORD f_dbg;
/*
extern WORD forward; //<! DALI forward frame
extern BYTE answer; //<! DALI slave answer
extern BYTE f_dalitx;
extern BYTE f_dalirx;
*/
static void
DALI_Shift_Bit(BYTE val)
{
if (frame & 0x100) // frame full ?
{
frame = 0; // yes, ERROR
f_error = 1;
//TIMSK &= ~0x20; //stop capture interrupts
}
else
frame = (frame << 1) | val; // shift bit
}
/************************************************************************
; DALI_Decode (we only take action at a rising edge)
;
; Half(prev) Bit Low Time High Time Action New Half Bit
; -------------------------------------------------------------------
; 0 0 0 Shift 0 0
; 0 0 1 -ERROR- *
; 0 1 0 Shift 0,1 1
; 0 1 1 -ERROR- *
; 1 0 0 Shift 1 1
; 1 0 1 Shift 0 0
; 1 1 0 -ERROR- *
; 1 1 1 Shift 0,1 1
;
***********************************************************************/
static void
DALI_Decode(void)
{
BYTE action;
action = previous << 2;
if ((high_time > MIN_2TE) && (high_time < MAX_2TE))
action = action | 1; // high_time = long
else if (!((high_time > MIN_TE) && (high_time < MAX_TE)))
{
frame = 0; // DALI ERROR
f_error = 1;
//TIMSK &= ~0x20; //stop capture interrupts
return;
}
if ((low_time > MIN_2TE) && (low_time < MAX_2TE))
action = action | 2; // low_time = long
else if (!((low_time > MIN_TE) && (low_time < MAX_TE)))
{
frame = 0; // DALI ERROR
f_error = 1;
//TIMSK &= ~0x20; //stop capture interrupts
return;
}
switch (action)
{
case 0:
DALI_Shift_Bit(0); // short low, short high, shift 0
break;
case 1:
frame = 0; // short low, long high, ERROR
f_error = 1;
//TIMSK &= ~0x20; //stop capture interrupts
break;
case 2:
DALI_Shift_Bit(0); // long low, short high, shift 0,1
DALI_Shift_Bit(1);
previous = 1; // new half bit is 1
break;
case 3:
frame = 0; // long low, long high, ERROR
f_error = 1;
//TIMSK &= ~0x20; //stop capture interrupts
break;
case 4:
DALI_Shift_Bit(1); // short low, short high, shift 1
break;
case 5:
DALI_Shift_Bit(0); // short low, long high, shift 0
previous = 0; // new half bit is 0
break;
case 6:
frame = 0; // long low, short high, ERROR
f_error = 1;
//TIMSK &= ~0x20; //stop capture interrupts
break;
case 7:
DALI_Shift_Bit(0); // long low, long high, shift 0,1
DALI_Shift_Bit(1);
break;
default:
break; // invalid
}
}
/*! \brief Timer capture interrupt service routine
*
*/
ISR(TIMER1_CAPT_vect)
{
RESET_DALI_TIMER();
f_dbg++;
if (TCCR1B & _BV(ICES1))
{
if (!DALI_RX())
PORTC &= !(1<<PC1); /* LED on */
}
else
{
if (DALI_RX())
PORTC &= !(1<<PC1); /* LED on */
}
if (DALI_RX()) // check rising or falling edge
{
/* Handle the rising edge*/
if (frame != 0) // not first pulse ?
{
low_time = ICR1; // rising, so capture low time
DALI_Decode(); // decode received bit
}
else
{
previous = 1; // first pulse, so shift 1
DALI_Shift_Bit(1);
}
SET_DALI_FALLING_EDGE(); //wait for the next falling edge
}
else
{
/* Handle the falling edge*/
high_time = ICR1; // falling, so capture high time
SET_DALI_RAISING_EDGE(); //wait for the next rising edge
}
}
/*! \brief Timer compare interrupt service routine
*
* This interrupt is triggered when the compare
* register equals the timer. It increments the
* counter and handles the transmission of each
* bit.
*/
ISR(TIMER1_COMPA_vect)
{
// reset timer
if (value)
SET_DALI_TX(); // DALI output pin high
else
RESET_DALI_TX(); // DALI output pin low
if (position == 0) // 0TE second half of start bit = 1
{
value = 1;
}
else if (position < 33) // 1TE - 32TE, so address + command
{
value = (forward >> ((32 - position) / 2)) & 1;
if (position & 1)
value = !value; // invert if first half of data bit
}
else if (position == 33) // 33TE start of stop bit (4TE)
{ // and start of minimum settling time (7TE)
value = 1;
}
else if (position == 44) // 44TE, end of stopbits and settling time
{
if (!DALI_RX())
f_error = 1;
SET_DALI_TIMER_COMPARE_22TE(); // receive slave answer, timeout of 22TE = 9,174 msec
SET_DALI_FALLING_EDGE(); //wait for the falling edge next capture
CLEAR_DALI_PENDING_CAPTURE(); //reset any pending captures
ENABLE_DALI_CAPT_INTR(); //enable capture interrupts
}
else if (position == 45) // end of transfer
{
/* stop and reset timer */
DISABLE_DALI_INTERRUPT();
DISABLE_DALI_CAPT_INTR();
STOP_DALI_TIMER();
RESET_DALI_TIMER();
if (frame & 0x100) // backward frame (answer) completed ?
{
answer = (BYTE) frame; // OK ! save answer
f_dalirx = 1; // and set flag to signal application
}
frame = 0; // reset receive frame
f_busy = 0; // end of transmission
if (f_repeat) // repeat forward frame ?
f_dalitx = 1; // yes, set flag to signal application
}
position++;
}
void
DALI_Send(void)
{
if (f_repeat) // repeat last command ?
{
f_repeat = 0;
}
else if ((forward & 0xE100) == 0xA100 || (forward & 0xE100) == 0xC100)
{
if ((forward & 0xFF00) == INITIALISE || forward == RANDOMISE)
{
f_repeat = 1; // special command shall be repeated within 100 ms
}
}
else if ((forward & 0x1FF) >= 0x120 && (forward & 0x1FF) <= 0x180)
{
f_repeat = 1; // configuration command shall be repeated within 100 ms
}
while (f_busy)
; // Wait until dali port is idle
frame = 0;
value = 0; // first half of start bit = 0
position = 0;
f_error = 0;
f_busy = 1; // Activate the timer module to transfer
f_dbg = 0;
SET_DALI_TIMER_COMPARE_WAIT_ONE(); // ~2400 Hz
DISABLE_DALI_CAPT_INTR(); // disable capture interrupt
ENABLE_DALI_INTERRUPT(); // Enable interrupt on match, CTC
RESET_DALI_TIMER(); // reset timer
START_DALI_TIMER(); // enable timer. I assume the interrupts are enabled
}
void
DALI_Init(void)
{
CLEAR_DALI_TIMER_ON_COMPARE_MATCH();
RESET_DALI_TIMER();
SET_DALI_TIMER_COMPARE_WAIT_ONE();
f_busy = 0;
f_error = 0;
SETUP_DALI_TX_DIR; /* set DALi TX to output */
SET_DALI_TX();
/*TODO: set the CAPTURE input*/
/* CAPTURE pin is set as input*/
SETUP_DALI_RX_DIR;
}