From f6e5d02481e3fb801b862fc5f91ba1a1bb6399d4 Mon Sep 17 00:00:00 2001
From: jgromes <jan.gromes@gmail.com>
Date: Sun, 2 Oct 2022 12:19:33 +0200
Subject: [PATCH] [Pager] Reworked logic to workaround linker error on MegaCore
 (CI_BUILD_ALL)

---
 src/protocols/Pager/Pager.cpp | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/src/protocols/Pager/Pager.cpp b/src/protocols/Pager/Pager.cpp
index 85a6e76e..477fb5e8 100644
--- a/src/protocols/Pager/Pager.cpp
+++ b/src/protocols/Pager/Pager.cpp
@@ -422,16 +422,21 @@ void PagerClient::write(uint32_t codeWord) {
   Module* mod = _phy->getMod();
   for(int8_t i = 31; i >= 0; i--) {
     uint32_t mask = (uint32_t)0x01 << i;
+    uint32_t start = mod->micros();
     if(codeWord & mask) {
       // send 1
-      uint32_t start = mod->micros();
       _phy->transmitDirect(_baseRaw + _shift);
-      while(mod->micros() - start < _bitDuration);
     } else {
       // send 0
-      uint32_t start = mod->micros();
       _phy->transmitDirect(_baseRaw - _shift);
-      while(mod->micros() - start < _bitDuration);
+    }
+
+    // this is pretty silly, while(mod->micros() ... ) would be enough
+    // but for some reason, MegaCore throws a linker error on it
+    // "relocation truncated to fit: R_AVR_7_PCREL against `no symbol'"
+    uint32_t now = mod->micros();
+    while(now - start < _bitDuration) {
+      now = mod->micros();
     }
   }
 }