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
|
#define F_CPU 8000000
#include <stdint.h>
#include <stdbool.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <util/delay.h>
const uint8_t __signature[3] __attribute__((section (".signature"), used)) =
{ SIGNATURE_2, SIGNATURE_1, SIGNATURE_0 };
#define B_SCK 5
#define B_MISO 4
#define B_MOSI 3
#define B_SS 2
#define D_TXD 1
#define D_DALII 3
#define D_DALIO 4
#define D_LED1 5
#define D_LED2 6
#define D_TAST 7
#include "uart.c"
#include "dali2.c"
#include "dali_ctl.c"
#include "dim.c"
#include "can.c"
static void can_rx_exec(void)
{
if (can_rx_addr.b[0] == 0xcc
&& (can_rx_addr.b[1] & 0xfb) == 0x08
&& can_rx_addr.b[2] == 0x04) {
uint8_t base = can_rx_addr.b[3], len = can_rx_dlc & 0x4f, pos;
for (pos = 0; pos < len; pos++) {
uint8_t dst = base + pos, val;
if (dst < 0x40 || dst >= 0x80)
continue;
dst -= 0x40;
val = can_rx_data[pos];
if (val == 0xff)
val = 0xfe;
uart_puts("-- SET ");
uart_puthex(dst);
uart_puts(" = ");
uart_puthex(val);
uart_puts("\n");
if (!val) {
// dim_state = 0;
dali_send((dst << 9) | 0x100);
} else {
// if (!dim_state && can_rx_data[0])
dali_send((dst << 9) | 0x108);
dali_send((dst << 9) | val);
}
}
}
}
int main(void)
{
DDRD |= (1 << D_LED1) | (1 << D_LED2 ) | (1 << D_TXD) | (1 << D_DALIO);
PORTD |= (1 << D_LED1) | (1 << D_TAST);
PORTD &= ~(1 << D_LED1);
uart_init();
can_preinit();
dali_init();
dim_init();
sei();
uart_puts("\ndelay...\n");
_delay_ms(1000);
uart_puts("\ninit done\n");
can_init();
can_CANSTAT();
dali_search();
uint16_t ctr = 0;
while (1) {
if (canint) {
canint = false;
can_int();
if (can_rx_addr.b[0]) {
can_rx_exec();
can_rx_addr.u = 0;
}
}
_delay_ms(5);
do_tick();
ctr++;
switch (ctr) {
case 2048:
ctr = 0;
break;
case 512:
case 516:
case 520:
case 524:
case 528:
case 532:
case 536:
case 540:
if (dali_map[(ctr >> 2) & 7]) {
uint8_t buffer[8] = {0, 0, 0, 0, 0, 0, 0, 0};
uint8_t base = (ctr << 1) & 070, map = dali_map[(ctr >> 2) & 7], dlc = 0;
uart_puts("ll");
uart_puthex(base);
uart_puts("> ");
for (int i = 0; i < 8; i++)
if (map & (1 << i)) {
dali_send(0x1a0 | ((base + i) << 9));
if (dali_rx_avail) {
buffer[i] = dali_rx;
uart_puthex(dali_rx);
uart_puts(" ");
} else
uart_puts("EE ");
dlc = i + 1;
} else
uart_puts("-- ");
uart_puts("\n");
can_send(0xe6080440 + base, dlc, buffer);
}
break;
case 1024:
can_send(0xe7000000, 8, (uint8_t *)&dalistat);
uart_puts("dali stats: ");
uart_puthex16(dalistat.rxok);
uart_puts(" ok ");
uart_puthex16(dalistat.falsestart);
uart_puts(" f-start ");
uart_puthex16(dalistat.noise);
uart_puts(" noise ");
uart_puthex16(dalistat.manchester);
uart_puts(" mch-err\n");
break;
case 1536:
can_send(0xcc08047f, 1, (uint8_t *)&dim_state);
break;
}
}
}
void __do_copy_data(void) __attribute__((naked, section (".init4"), used));
void __do_copy_data(void) { }
|