From 352a06ba4574b1211ef4df63d626e7041f6c5894 Mon Sep 17 00:00:00 2001
From: Gijs Schoonderbeek <schoonderbeek@astron.nl>
Date: Tue, 10 Jan 2023 11:25:30 +0100
Subject: [PATCH] Added PPS check

---
 apsct_lib.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/apsct_lib.py b/apsct_lib.py
index e306a62..8bb7a19 100644
--- a/apsct_lib.py
+++ b/apsct_lib.py
@@ -17,6 +17,7 @@ Set APSCT_CLK
 import sys
 import APSCT_I2C
 import time
+import RPi.GPIO as gpio
 sys.path.insert(0, '.')
 import os
 if os.name == "posix":
@@ -39,6 +40,7 @@ class ApsctClass:
         self.pll_200 = PllClass("200MHz")
         self.pll_160 = PllClass("160MHz")
         self.sensors = ApsctSensors()
+        self.pps = PpsClass()
 
     def read_IO_expanderis(self):
         #
@@ -100,6 +102,7 @@ class ApsctClass:
         # Check voltages, temp and lock on APSCT 
         #
         result = self.sensors.check_values()
+        result = result & self.pps.check_timing()
         if self.frequency == "200MHz":
             self.pll_200.read_lock()
             lock = self.pll_200.lock
@@ -437,3 +440,48 @@ class ApsctSensors:
         else:
             self.temperature = 9999
         return self.temperature
+
+class PpsClass:
+    #
+    # Class to check the PPS signal 
+    #
+    def __init__(self):
+        #
+        # Whats needed to measure the toggle on GPIO24
+        #
+        gpio.setmode(gpio.BCM)
+        gpio.setup(24, gpio.IN)
+        self.pps_time = 999
+
+    def time_pps(self):
+        gpio.wait_for_edge(24, gpio.RISING)
+        a = time()
+        gpio.wait_for_edge(24, gpio.RISING)
+        b = time()
+        self.pps_time = (b-a)/2
+        return self.pps_time
+
+    def check_pps(self):
+        gpio.add_event_detect(24, gpio.RISING)
+        sleep(5)
+        pps_ok = gpio.event_detected(24)
+        gpio.remove_event_detect(24)
+        return pps_ok
+
+    def print_pps(self):
+        if self.check_pps():
+            print(f"Time between pps is {self.time_pps():4.2f} s")
+        else:
+            print("No PPS found")
+
+    def check_timing(self):
+        print("Check pps timing", end = "" )
+        self.timing = False
+        if not self.check_pps():
+            print(" no pps")
+            return False
+        timepps = self.time_pps()
+        if (0.9 < timepps < 1.1):
+            print(f", timing is OK: {timepps:4.2f} s")
+            self.timing = True    
+        return self.timing
-- 
GitLab