fw/layer1: Add common TPU win setup/cleanup code
[osmocom-bb.git] / src / target / firmware / layer1 / sync.c
index 0c85c57..c8840de 100644 (file)
@@ -56,7 +56,7 @@
 #include <layer1/tpu_window.h>
 #include <layer1/l23_api.h>
 
-#include <l1a_l23_interface.h>
+#include <l1ctl_proto.h>
 
 struct l1s_state l1s;
 
@@ -176,8 +176,13 @@ void l1s_reset_hw(void)
        tpu_end_scenario();
 }
 
+/* Lost TDMA interrupt detection.  This works by starting a hardware timer
+ * that is clocked by the same master clock source (VCTCXO).  We expect
+ * 1875 timer ticks in the duration of a TDMA frame (5000 qbits / 1250 bits) */
+
 /* Timer for detecting lost IRQ */
 #define TIMER_TICKS_PER_TDMA   1875
+#define TIMER_TICK_JITTER      1
 
 static int last_timestamp;
 
@@ -189,7 +194,10 @@ static inline void check_lost_frame(void)
                last_timestamp += (4*TIMER_TICKS_PER_TDMA);
 
        diff = last_timestamp - timestamp;
-       if (diff != 1875)
+
+       /* allow for a bit of jitter */
+       if (diff < TIMER_TICKS_PER_TDMA - TIMER_TICK_JITTER ||
+           diff > TIMER_TICKS_PER_TDMA + TIMER_TICK_JITTER)
                printf("LOST!\n");
 
        last_timestamp = timestamp;
@@ -209,6 +217,8 @@ void l1s_compl_sched(enum l1_compl c)
  * generated by TPU once every TDMA frame */
 static void l1_sync(void)
 {
+       uint16_t sched_flags;
+
        putchart('+');
 
        check_lost_frame();
@@ -244,7 +254,12 @@ static void l1_sync(void)
        }
 
        /* execute the sched_items that have been scheduled for this
-        * TDMA frame */
+        * TDMA frame (including setup/cleanup steps) */
+       sched_flags = tdma_sched_flag_scan();
+
+       if (sched_flags & TDMA_IFLG_TPU)
+               l1s_win_init();
+
        tdma_sched_execute();
 
        if (dsp_api.r_page_used) {
@@ -259,7 +274,11 @@ static void l1_sync(void)
                dsp_api.r_page ^= 1;
        }
 
-       //dsp_end_scenario();
+       if (sched_flags & TDMA_IFLG_DSP)
+               dsp_end_scenario();
+
+       if (sched_flags & TDMA_IFLG_TPU)
+               tpu_end_scenario();
 
        /* schedule new / upcoming TDMA items */
        mframe_schedule();
@@ -299,7 +318,7 @@ static int l1s_abort_cmd(__unused uint8_t p1, __unused uint8_t p2,
 void l1s_dsp_abort(void)
 {
        /* abort right now */
-       tdma_schedule(0, &l1s_abort_cmd, 0, 0, 0);
+       tdma_schedule(0, &l1s_abort_cmd, 0, 0, 0, 10);
 }
 
 void l1s_tx_apc_helper(void)