From 8d9f527081d3a5fc8a0286e47022a70df8eb9111 Mon Sep 17 00:00:00 2001
From: Joakim Tufvegren <jocke@barbanet.com>
Date: Sun, 20 Jun 2021 04:18:00 +0200
Subject: [PATCH] Add sync_timer support over serial_link (i.e. Ergodox
 Infinity) (#12845)

---
 quantum/serial_link/system/serial_link.c | 22 ++++++++++++++++++++++
 tmk_core/common/sync_timer.c             |  2 +-
 tmk_core/common/sync_timer.h             |  2 +-
 3 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/quantum/serial_link/system/serial_link.c b/quantum/serial_link/system/serial_link.c
index f77483ad8c..6363f8ff3b 100644
--- a/quantum/serial_link/system/serial_link.c
+++ b/quantum/serial_link/system/serial_link.c
@@ -29,10 +29,13 @@ SOFTWARE.
 #include "serial_link/protocol/transport.h"
 #include "serial_link/protocol/frame_router.h"
 #include "matrix.h"
+#include "sync_timer.h"
 #include <stdbool.h>
 #include "print.h"
 #include "config.h"
 
+#define SYNC_TIMER_OFFSET 2
+
 static event_source_t new_data_event;
 static bool           serial_link_connected;
 static bool           is_master = false;
@@ -159,10 +162,16 @@ static matrix_object_t last_matrix = {};
 
 SLAVE_TO_MASTER_OBJECT(keyboard_matrix, matrix_object_t);
 MASTER_TO_ALL_SLAVES_OBJECT(serial_link_connected, bool);
+#ifndef DISABLE_SYNC_TIMER
+MASTER_TO_ALL_SLAVES_OBJECT(sync_timer, uint32_t);
+#endif
 
 static remote_object_t* remote_objects[] = {
     REMOTE_OBJECT(serial_link_connected),
     REMOTE_OBJECT(keyboard_matrix),
+#ifndef DISABLE_SYNC_TIMER
+    REMOTE_OBJECT(sync_timer),
+#endif
 };
 
 void init_serial_link(void) {
@@ -200,14 +209,27 @@ void serial_link_update(void) {
             m->rows[i] = matrix.rows[i];
         }
         end_write_keyboard_matrix();
+
         *begin_write_serial_link_connected() = true;
         end_write_serial_link_connected();
+
+#ifndef DISABLE_SYNC_TIMER
+        *begin_write_sync_timer() = sync_timer_read32() + SYNC_TIMER_OFFSET;
+        end_write_sync_timer();
+#endif
     }
 
     matrix_object_t* m = read_keyboard_matrix(0);
     if (m) {
         matrix_set_remote(m->rows, 0);
     }
+
+#ifndef DISABLE_SYNC_TIMER
+    uint32_t* t = read_sync_timer();
+    if (t) {
+        sync_timer_update(*t);
+    }
+#endif
 }
 
 void signal_data_written(void) { chEvtBroadcast(&new_data_event); }
diff --git a/tmk_core/common/sync_timer.c b/tmk_core/common/sync_timer.c
index de24b463b6..68b92d8b43 100644
--- a/tmk_core/common/sync_timer.c
+++ b/tmk_core/common/sync_timer.c
@@ -26,7 +26,7 @@ SOFTWARE.
 #include "sync_timer.h"
 #include "keyboard.h"
 
-#if defined(SPLIT_KEYBOARD) && !defined(DISABLE_SYNC_TIMER)
+#if (defined(SPLIT_KEYBOARD) || defined(SERIAL_LINK_ENABLE)) && !defined(DISABLE_SYNC_TIMER)
 volatile int32_t sync_timer_ms;
 
 void sync_timer_init(void) { sync_timer_ms = 0; }
diff --git a/tmk_core/common/sync_timer.h b/tmk_core/common/sync_timer.h
index 9ddef45bb2..744e2b50d5 100644
--- a/tmk_core/common/sync_timer.h
+++ b/tmk_core/common/sync_timer.h
@@ -32,7 +32,7 @@ SOFTWARE.
 extern "C" {
 #endif
 
-#if defined(SPLIT_KEYBOARD) && !defined(DISABLE_SYNC_TIMER)
+#if (defined(SPLIT_KEYBOARD) || defined(SERIAL_LINK_ENABLE)) && !defined(DISABLE_SYNC_TIMER)
 void     sync_timer_init(void);
 void     sync_timer_update(uint32_t time);
 uint16_t sync_timer_read(void);