From 4ec7bc619f3b4af419ba4b2cc6115852ad854c30 Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Fri, 9 Jan 2015 05:14:50 +0100 Subject: dali: foobar --- dali2.c | 21 +++++++++++++++++++-- dali_ctl.c | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/dali2.c b/dali2.c index ee00c1b..57bd72f 100644 --- a/dali2.c +++ b/dali2.c @@ -64,6 +64,8 @@ ISR(TIMER0_COMPA_vect) subsamp++; subsamp &= 7; + while (TCNT0 < 52) + asm volatile ("nop\n" ::: "memory"); /* CTC mode didn't work, CBA to debug */ TCNT0 = 0; @@ -198,6 +200,18 @@ ISR(TIMER0_COMPA_vect) } } +#ifdef DALI_USER_IDLE + if (dali_state == DALI_IDLE && !dali_tx_rq) { + uint16_t next = dali_user_idle(); + if (next != DALI_INVALID) { + tx_data = next; + bitpos = 0; + dali_state = DALI_TX_SBIT0; + subsamp = 7; + } + } +#endif + if (dali_state == DALI_IDLE && dali_tx_rq) { tx_data = dali_tx; bitpos = 0; @@ -221,7 +235,10 @@ static void dali_send(uint16_t word) dali_rx_avail = 0; dali_tx = word; - asm volatile ("" ::: "memory"); + + do asm volatile ("" ::: "memory"); + while (dali_tx_rq); + dali_tx_rq = 1; while (dali_tx_rq || dali_state != DALI_IDLE) { asm volatile ("" ::: "memory"); @@ -273,7 +290,7 @@ static void dali_init(void) TIFR0 = (1 << OCF0A); TCNT0 = 0; - OCR0A = 52; // 8 MHz / 8 / 52 = 19230. + OCR0A = 49; /*52*/ // 8 MHz / 8 / 52 = 19230. asm volatile ("" ::: "memory"); TIMSK0 = (1 << OCIE0A); TCCR0A = 0; diff --git a/dali_ctl.c b/dali_ctl.c index 5db3c09..4d8f872 100644 --- a/dali_ctl.c +++ b/dali_ctl.c @@ -1,3 +1,5 @@ +#define DALI_C_QBALLAST 0x0191 + #define DALI_C_TERMINATE 0xa100 #define DALI_C_DTR 0xa300 #define DALI_C_INITIALISE 0xa500 @@ -16,9 +18,6 @@ static uint8_t dali_s_byte, dali_sh, dali_sm, dali_sl; static bool dali_s_notfound; -/* [0] bit 0 => addr 0, [0] bit 1 => addr 1, etc. */ -static uint8_t dali_map[8]; - EEMEM uint8_t dali_nextaddr = 0x02; static uint8_t dali_assign; @@ -52,6 +51,7 @@ static void dali_search_byte(uint16_t cmd) bit >>= 1; } dali_twice(cmd | dali_s_byte); + uart_puthex(dali_s_byte); } static void dali_search_single(void) @@ -81,6 +81,8 @@ static void dali_search_single(void) static void dali_search(void) { + uint8_t errtries = 0; + dali_twice(DALI_C_INITIALISE); /* _some_ EVGs keep the long address in nonvolatile memory, @@ -92,7 +94,12 @@ static void dali_search(void) dali_assign = eeprom_read_byte(&dali_nextaddr); do { + uint8_t ph = dali_sh, pm = dali_sm, pl = dali_sl; + + uart_puttick(); + uart_puts("dali search... "); dali_search_single(); + uart_puts("\n"); if (!dali_s_notfound) { wdt_reset(); @@ -102,8 +109,12 @@ static void dali_search(void) uart_puthex(dali_sm); uart_puthex(dali_sl); dali_send(DALI_C_QURYSHORT); + if (!dali_rx_avail) + dali_send(DALI_C_QURYSHORT); + wdt_reset(); if (dali_rx_avail) { uint8_t addr = 0xff; + errtries = 0; if ((dali_rx & 0x81) == 0x01) { addr = dali_rx >> 1; @@ -112,7 +123,7 @@ static void dali_search(void) uart_puthex(addr); } - if (addr == 0x00 || (dali_map[addr >> 3] & (1 << (addr & 0x07)))) { + if (addr == 0x00 || target_present(addr)) { uart_puts(" reprog"); addr = 0xff; } @@ -132,12 +143,21 @@ static void dali_search(void) uart_puts("\n"); if (addr < 64) - dali_map[addr >> 3] |= 1 << (addr & 0x7); - } else + target_set_present(addr); + + wdt_reset(); + dali_send(DALI_C_WITHDRAW); + } else { uart_puts(" error\n"); + if (errtries < 3) { + dali_sh = ph; + dali_sm = pm; + dali_sl = pl; + errtries++; + } else + errtries = 0; + } - wdt_reset(); - dali_send(DALI_C_WITHDRAW); } } while (!dali_s_notfound); -- cgit v1.2.1