summaryrefslogtreecommitdiff
path: root/lightctrl.c
blob: 464a0aed3b9d6ddabc29e84428b170ef242094ab (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
#define F_CPU 8000000
#include <stdint.h>
#include <stdbool.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.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"

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("\ninit done\n");

	can_init();
	can_CANSTAT();

	dali_search();

	uint8_t ctr = 0;
	while (1) {
		if (canint) {
			canint = false;
			can_int();
		}
		_delay_ms(5);
		do_tick();

		ctr++;
		if (ctr % 64 == 0) {
			dali_send(0xffa0);
			if (dali_rx_avail) {
				uart_puts("ll ");
				uart_puthex(dali_rx);
				uart_puts("\n");
				can_send(0xaa800000, 1, (uint8_t *)&dali_rx);
			} else
				uart_puts("ll noans\n");
		}

		if (ctr == 255) {
			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");
		}
	}
}

void __do_copy_data(void) __attribute__((naked, section (".init4"), used));
void __do_copy_data(void) { }