From bbe09de52c552a8c53aa852a694efb977d444457 Mon Sep 17 00:00:00 2001
From: linuswck <linuswck@m-labs.hk>
Date: Fri, 8 Nov 2024 16:29:49 +0800
Subject: [PATCH] pyfastservo adc: Add helper fn for phase shifting the dac ddr
 clock

---
 fast-servo/pyfastservo/adc.py | 53 ++++++++++++++++++++++++++++++++++-
 1 file changed, 52 insertions(+), 1 deletion(-)

diff --git a/fast-servo/pyfastservo/adc.py b/fast-servo/pyfastservo/adc.py
index 8d5e9e6..7072cad 100644
--- a/fast-servo/pyfastservo/adc.py
+++ b/fast-servo/pyfastservo/adc.py
@@ -92,7 +92,19 @@ def mmcm_rst():
     while not(read_frame() & 0x10):
         print(f"Waiting for MMCM to lock")
         time.sleep(0.001)
-    
+
+def inc_ddr_clk_phase():
+    curr_cfg = read_from_memory(ADC_AFE_CTRL_ADDR, 1)[0] & 0x1F
+    write_to_memory(ADC_AFE_CTRL_ADDR, 0x40 | curr_cfg) # Set MMCM Phase Shift to be INC
+    write_to_memory(ADC_AFE_CTRL_ADDR, 0x60 | curr_cfg) # Assert MMCM Phase Shift EN High
+    write_to_memory(ADC_AFE_CTRL_ADDR, curr_cfg)        # Deassert MMCM Phase Shift EN High
+
+def dec_ddr_clk_phase():
+    curr_cfg = read_from_memory(ADC_AFE_CTRL_ADDR, 1)[0] & 0x1F
+    write_to_memory(ADC_AFE_CTRL_ADDR, 0x00 | curr_cfg) # Set MMCM Phase Shift to be DEC
+    write_to_memory(ADC_AFE_CTRL_ADDR, 0x20 | curr_cfg) # Assert MMCM Phase Shift EN High
+    write_to_memory(ADC_AFE_CTRL_ADDR, curr_cfg)        # Deassert MMCM Phase Shift EN High
+
 def find_edge():
     prev_frame = read_frame()
     for tap_delay in range(32):
@@ -131,6 +143,36 @@ def enable_adc_afe(ch1_x10=False, ch2_x10=False):
     print(f"ADC_AFE_CTRL: 0x{afe_ctrl:02X}")
     return afe_ctrl
 
+def search_edge():
+    for tap_delay in range(32):
+        print(f"iDelay to: {tap_delay}")
+        write_to_memory(ADC_DELAY_ADDR, tap_delay)
+        time.sleep(1)
+        current_frame = read_frame()
+        print(f"Tap delay: {tap_delay}, Current frame: 0x{current_frame:02x}")
+        print_adc_channels()
+
+def print_adc_channel(ch):
+    if ch == 0:
+        adc_ch0 = read_adc_channel(ADC_CH0_HIGH_ADDR, ADC_CH0_LOW_ADDR)
+        print(f"Final ADC_CH0: 0x{adc_ch0:04x}")
+    if ch == 1:
+        adc_ch1 = read_adc_channel(ADC_CH1_HIGH_ADDR, ADC_CH1_LOW_ADDR)
+        print(f"Final ADC_CH1: 0x{adc_ch1:04x}")
+
+def find_min_max_ch(ch):
+    test = []
+    for i in range(100):
+        if ch == 0:
+            test.append(read_adc_channel(ADC_CH0_HIGH_ADDR, ADC_CH0_LOW_ADDR))
+        else:
+            test.append(read_adc_channel(ADC_CH1_HIGH_ADDR, ADC_CH1_LOW_ADDR)) 
+        print("ch", ch, hex(test[-1]))
+    print("Min:", hex(min(test)))
+    print("Max:", hex(max(test)))
+    print("Diff:", hex(max(test)-min(test)))
+
+
 def configure_ltc2195():
     spi = spidev.SpiDev()
     try:
@@ -166,6 +208,15 @@ def configure_ltc2195():
         # FIXME: AFE Gain 1x is not functional on that batch of fast servo under development
         enable_adc_afe(ch1_x10=1, ch2_x10=1)
 
+        #find_min_max_ch(0)
+        #find_min_max_ch(1)
+
+        #for i in range(10):
+        #    print_adc_channel(0)
+
+        #for i in range(10):
+        #    print_adc_channel(1)
+
     finally:
         spi.close()