summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Lamparter <equinox@diac24.net>2012-05-12 19:36:58 +0200
committerDavid Lamparter <equinox@diac24.net>2012-05-12 19:36:58 +0200
commit3ae67269dc61d0ebb2b0a8c78af8fc4348755afb (patch)
treed4a1a9073ec2279ac234448e6e56618b66a55b88
parentabd5066a4950da393417ce520788c018d618488e (diff)
dali: more improvements
-rw-r--r--can.c35
-rw-r--r--dali.c35
2 files changed, 45 insertions, 25 deletions
diff --git a/can.c b/can.c
index c2307a9..b8cb21f 100644
--- a/can.c
+++ b/can.c
@@ -221,16 +221,10 @@ int main(void)
can_init();
can_CANSTAT();
- cli();
- dali_word = 0xff08; /* ON */
- dali_state = 1;
- sei();
- _delay_ms(25);
-
- cli();
- dali_word = 0xff08; /* OFF */
- dali_state = 1;
- sei();
+ dali_send(0xff08);
+ while (dali_queue_busy())
+ ;
+ dali_send(0xff08);
for (int i = 0; i < 40; i++)
_delay_ms(25);
@@ -243,21 +237,14 @@ int main(void)
}
_delay_ms(40);
ctr++;
- if (ctr == 40) {
- dali_word = 0xff91;
- dali_state = 1;
- }
- if (ctr == 80) {
- dali_word = 0xff99;
- dali_state = 1;
- }
- if (ctr == 120) {
- dali_word = 0xffa0;
- dali_state = 1;
- }
+ if (ctr == 40)
+ dali_send(0xff91);
+ if (ctr == 80)
+ dali_send(0xff99);
+ if (ctr == 120)
+ dali_send(0xffa0);
if (ctr == 160) {
- dali_word = 0xff90;
- dali_state = 1;
+ dali_send(0xff90);
ctr = 0;
}
}
diff --git a/dali.c b/dali.c
index 5b9d25d..83007a3 100644
--- a/dali.c
+++ b/dali.c
@@ -1,9 +1,17 @@
+/* invalid command, used as default/do-nothing value
+ * 101CCCCX == special commands, X must be 1.
+ * X is 0 below => invalid
+ */
+#define DALI_INVALID 0xa000
+
static uint16_t dali_word, dali_sendq;
static uint8_t dali_subctr, dali_state, dali_bit;
static uint8_t dali_prevpin;
enum dali_state {
DALI_IDLE = 0,
+ DALI_TX_SPACE,
+
DALI_TX_STARTBIT1,
DALI_TX_STARTBIT2,
@@ -22,7 +30,7 @@ ISR(TIMER0_COMPA_vect)
TCNT0 = 0; /* CTC mode didn't work, CBA to debug */
data = (PIND >> D_DALII) & 1;
- if (dali_state == DALI_IDLE && data) {
+ if (dali_state <= DALI_TX_SPACE && data) {
dali_state = DALI_RX_LEFT;
dali_subctr = 4;
dali_word = 1;
@@ -48,6 +56,18 @@ ISR(TIMER0_COMPA_vect)
switch (dali_state) {
/* idle */
case DALI_IDLE:
+ if (dali_sendq != DALI_INVALID) {
+ dali_word = dali_sendq;
+ dali_sendq = DALI_INVALID;
+ dali_state = DALI_TX_STARTBIT1;
+ }
+ return;
+
+ case DALI_TX_SPACE:
+ /* 12 ms inter-command spacing */
+ if (dali_bit > 28)
+ dali_state = DALI_IDLE;
+ dali_bit++;
return;
/* write */
@@ -109,6 +129,19 @@ ISR(TIMER0_COMPA_vect)
}
}
+static void dali_send(uint16_t word)
+{
+ cli();
+ dali_sendq = word;
+ sei();
+}
+
+static bool dali_queue_busy(void)
+{
+ asm volatile ("" : : : "memory");
+ return dali_sendq != DALI_INVALID;
+}
+
static void dali_init(void)
{
PORTD |= (1 << D_DALII);