#define F_CPU 8000000 #include #include #include #include #include #include #include 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 && can_rx_addr.b[3] == 0x7f && (can_rx_dlc & 0x4f) == 1) { uart_puts("-- SET\n"); if (!can_rx_data[0]) { dim_state = 0; dali_send(DALI_ADDR | 0x100); } else { if (!dim_state && can_rx_data[0]) dali_send(DALI_ADDR | 0x108); dim_state = can_rx_data[0]; dali_send(DALI_ADDR | dim_state); } } } 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(); 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]; 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 "); } else uart_puts("-- "); uart_puts("\n"); can_send(0xe6080400 + base, 8, 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) { }