diff --git a/fast-servo/pyfastservo/adc.py b/fast-servo/pyfastservo/adc.py index 9cbe137..2f7efe1 100644 --- a/fast-servo/pyfastservo/adc.py +++ b/fast-servo/pyfastservo/adc.py @@ -19,6 +19,7 @@ import spidev +import json from pyfastservo.common import ( ADC_AFE_CTRL_ADDR, ADC_BITSLIP_ADDR, @@ -113,11 +114,12 @@ def find_edge(): def read_adc_channel(high_addr, low_addr): return (read_from_memory(high_addr, 1)[0] << 8) | read_from_memory(low_addr, 1)[0] -def print_adc_channels(): +def get_adc_channels(): adc_ch0 = read_adc_channel(ADC_CH0_HIGH_ADDR, ADC_CH0_LOW_ADDR) adc_ch1 = read_adc_channel(ADC_CH1_HIGH_ADDR, ADC_CH1_LOW_ADDR) print(f"Final ADC_CH0: 0x{adc_ch0:04x}") print(f"Final ADC_CH1: 0x{adc_ch1:04x}") + return adc_ch0, adc_ch1 def enable_adc_afe(ch1_x10=False, ch2_x10=False): ctrl_value = (ch2_x10 << 1) | ch1_x10 @@ -147,7 +149,10 @@ def configure_ltc2195(): # Performing Word Align perform_bitslip() find_edge() - print_adc_channels() + adc_ch0, adc_ch1 = get_adc_channels() + + with open('adc_values.json', 'w') as f: + json.dump({'adc_ch0': adc_ch0, 'adc_ch1': adc_ch1}, f) main_adc_test_mode(spi, False) verify_adc_registers(spi, {0x02: 0x11}) # Verify test mode is off diff --git a/fast-servo/pyfastservo/loopback.py b/fast-servo/pyfastservo/loopback.py index 12f0a8e..a45c936 100644 --- a/fast-servo/pyfastservo/loopback.py +++ b/fast-servo/pyfastservo/loopback.py @@ -1,3 +1,4 @@ +import json import time from pyfastservo import adc, dac from pyfastservo.common import ( @@ -8,11 +9,26 @@ from pyfastservo.common import ( read_from_memory, write_to_memory ) -### + +try: + with open('adc_values.json', 'r') as f: + adc_values = json.load(f) + INITIALIZED_ADC_CH0 = int(adc_values['adc_ch0'], 16) # Convert hex string to int + INITIALIZED_ADC_CH1 = int(adc_values['adc_ch1'], 16) # Convert hex string to int +except FileNotFoundError: + print("Warning: ADC initialization values not found. Using default addresses.") + INITIALIZED_ADC_CH0 = None + INITIALIZED_ADC_CH1 = None def read_adc(): - adc_value = adc.read_adc_channel(ADC_CH0_HIGH_ADDR, ADC_CH0_LOW_ADDR) - return adc_value + if INITIALIZED_ADC_CH0 is not None: + return INITIALIZED_ADC_CH0 + else: + return adc.read_adc_channel(ADC_CH0_HIGH_ADDR, ADC_CH0_LOW_ADDR) + +# def read_adc(): +# adc_value = adc.read_adc_channel(ADC_CH0_HIGH_ADDR, ADC_CH0_LOW_ADDR) +# return adc_value def write_dac(value): dac.set_dac_output(value) @@ -42,7 +58,7 @@ def perform_loopback_test(test_value): print(f"Expected: {test_value}, ADC: {adc_value}, DAC: {dac_value}") def main(): - test_value = 0x2000 # Mid-range value (8192 in decimal) + test_value = 0x3F3F # Mid-range value (8192 in decimal) perform_loopback_test(test_value) if __name__ == "__main__":