trennfix/sw: Single timer single delay solution seems to work
authorPhilipp Hachtmann <hachti@hachti.de>
Sat, 11 Feb 2017 05:04:57 +0000 (06:04 +0100)
committerPhilipp Hachtmann <hachti@hachti.de>
Sun, 12 Feb 2017 17:04:25 +0000 (18:04 +0100)
Signed-off-by: Philipp Hachtmann <hachti@hachti.de>
trennfix/sw/mm/include/mm/mm_switch.h
trennfix/sw/mm/src/mm_switch.c

index 94655a903d1830384a3107e8f9d616b07940f2aa..f7f0d159c463bf1eca9fbe9fb238642032a41b72 100644 (file)
@@ -9,7 +9,9 @@ enum mm_recstate {
        MM_FIRST_SLOW_SAMPLE, /* If clock arrives, we stay on the fast path! */
        MM_FAST_SAMPLE,
        MM_FAST_WAIT_FOR_CLOCK,
+       MM_SLOW_SAMPLE_DELAY,
        MM_SLOW_SAMPLE,
+       MM_SLOW_WAIT_FOR_CLOCK_DELAY,
        MM_SLOW_WAIT_FOR_CLOCK,
 };
 
index 30f43cbaa3ddd13f0391d3e76a54cbec4c464318..ba68a8b92354319fba7eec165bfae9495de962bd 100644 (file)
@@ -222,9 +222,10 @@ void shift(uint8_t value)
 }
 #endif
 
+static volatile uint8_t mm_rec_tolerated_timeouts;
+
 
 ISR(MM_TIMER_INT_VECT) {
-       static uint8_t tolerated_timeouts = 0;
 
        static volatile uint8_t shift_command_first;
        static volatile uint8_t shift_function_first;
@@ -232,6 +233,8 @@ ISR(MM_TIMER_INT_VECT) {
        uint8_t address;
        uint8_t command;
 
+       MM_TSTART_FAST;
+
 #ifdef MM_FILTER_REPEATED
        static uint8_t address_last = 0xff;
        static uint8_t function_last = 0xff;
@@ -239,43 +242,48 @@ ISR(MM_TIMER_INT_VECT) {
 #endif
 
        switch(recstate) {
+       
        case MM_FIRST_FAST_SAMPLE:
-               recstate = MM_FIRST_SLOW_SAMPLE;
-               MM_TSTART_FAST; /* Will not run out in fast! */
+               recstate = MM_FIRST_SLOW_SAMPLE      ;
                break;
 
        case MM_FIRST_SLOW_SAMPLE:
                bitno = 0;
 
        case MM_SLOW_SAMPLE:
-               recstate = MM_SLOW_WAIT_FOR_CLOCK;
-               MM_TSTART_SLOW;
+               recstate = MM_SLOW_WAIT_FOR_CLOCK_DELAY;
                break;
-       
+
        case MM_FAST_SAMPLE:
                recstate = MM_FAST_WAIT_FOR_CLOCK;
-               MM_TSTART_FAST;
                break;
 
        case MM_FAST_WAIT_FOR_CLOCK: /* A timeout! */
-               if (tolerated_timeouts) {
-                       tolerated_timeouts--;
-                       MM_TSTART_FAST;
-                       return;
+               if (mm_rec_tolerated_timeouts) {
+                       mm_rec_tolerated_timeouts--;
+               } else {
+                       recstate = MM_IDLE;
+
                }
-               recstate = MM_IDLE;
-               MM_TSTOP;
+               return;
+
+       case MM_SLOW_SAMPLE_DELAY:
+               recstate = MM_SLOW_SAMPLE;
+               return;
+
+       case MM_SLOW_WAIT_FOR_CLOCK_DELAY:
+               recstate = MM_SLOW_WAIT_FOR_CLOCK;
                return;
 
        case MM_SLOW_WAIT_FOR_CLOCK:
-               if (tolerated_timeouts) {
-                       tolerated_timeouts--;
-                       MM_TSTART_SLOW;
+               if (mm_rec_tolerated_timeouts) {
+                       mm_rec_tolerated_timeouts--;
+                       recstate = MM_SLOW_WAIT_FOR_CLOCK_DELAY;
                        return;
                }
        default:
-               MM_TSTOP;
                recstate = MM_IDLE;
+       case MM_IDLE:
                return;
        }
        
@@ -286,8 +294,7 @@ ISR(MM_TIMER_INT_VECT) {
                shift_address_first  = shift_address;
                shift_function_first = shift_function;
                shift_command_first  = shift_command;
-
-               tolerated_timeouts = 18;
+               mm_rec_tolerated_timeouts = 18;
        } 
 
        if (bitno == 36) {
@@ -301,9 +308,11 @@ ISR(MM_TIMER_INT_VECT) {
 #endif
                                address = lookup_decoder(shift_address);
                                
-                               if (recstate == MM_SLOW_WAIT_FOR_CLOCK) {
+                               if (recstate == MM_SLOW_WAIT_FOR_CLOCK_DELAY) {
+                                       trigger();
                                        mm_switch_drive(address, shift_function, shift_command);
                                } else if (recstate == MM_FAST_WAIT_FOR_CLOCK) {
+                                       trigger();
                                        command = lookup_command(shift_command);
                                        mm_switch_command(address, command);
                                }
@@ -359,29 +368,37 @@ void mm_pinchange_handler(void)
        if (!sense_last)
                return;
 
+       MM_TSTART_FAST;
+
        switch(recstate) {
        case MM_IDLE:
                bitno = 0;
                recstate = MM_FIRST_FAST_SAMPLE;
-               MM_TSTART_FAST;
                break;
+
        case MM_FIRST_SLOW_SAMPLE:
                recstate = MM_FAST_SAMPLE;
-               MM_TSTART_FAST;
                break;
+
        case MM_FAST_WAIT_FOR_CLOCK:
                recstate = MM_FAST_SAMPLE;
-               MM_TSTART_FAST;
+               mm_rec_tolerated_timeouts = 0;
                break;
+
+       case MM_SLOW_WAIT_FOR_CLOCK_DELAY: /* If clock comes early */
+               recstate = MM_SLOW_WAIT_FOR_CLOCK;
+               break;
+
        case MM_SLOW_WAIT_FOR_CLOCK:
-               recstate = MM_SLOW_SAMPLE;
-               MM_TSTART_SLOW;
+               recstate = MM_SLOW_SAMPLE_DELAY;
+               mm_rec_tolerated_timeouts = 0;
                break;
 
                /* Not expected */
        case MM_FIRST_FAST_SAMPLE:
        case MM_FAST_SAMPLE:
        case MM_SLOW_SAMPLE:
+               recstate = MM_IDLE;
        default:
                break;
        }