From 3629c9c8317cb7cb2f5d83384bc444c81bc1c153 Mon Sep 17 00:00:00 2001 From: Florian Agbuya Date: Tue, 20 Aug 2024 10:03:30 +0800 Subject: [PATCH] test nixbld --- fast-servo/pyfastservo/loopback.py | 49 ++++++++++++++++++++++++++++++ flake.nix | 2 +- 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 fast-servo/pyfastservo/loopback.py diff --git a/fast-servo/pyfastservo/loopback.py b/fast-servo/pyfastservo/loopback.py new file mode 100644 index 0000000..12f0a8e --- /dev/null +++ b/fast-servo/pyfastservo/loopback.py @@ -0,0 +1,49 @@ +import time +from pyfastservo import adc, dac +from pyfastservo.common import ( + ADC_CH0_HIGH_ADDR, + ADC_CH0_LOW_ADDR, + CH0_HIGH_WORD_ADDR, + CH0_LOW_WORD_ADDR, + read_from_memory, + write_to_memory +) +### + +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) + +def read_dac(): + high_word = read_from_memory(CH0_HIGH_WORD_ADDR, 1)[0] + low_word = read_from_memory(CH0_LOW_WORD_ADDR, 1)[0] + return (high_word << 8) | low_word + +def perform_loopback_test(test_value): + print(f"Setting DAC output to {test_value}...") + write_dac(test_value) + time.sleep(0.1) # Allow time for the signal to stabilize + + print("Reading ADC value...") + adc_value = read_adc() + print(f"ADC readback: {adc_value}") + + print("Reading DAC value...") + dac_value = read_dac() + print(f"DAC readback: {dac_value}") + + if abs(test_value - adc_value) <= 2 and abs(test_value - dac_value) <= 2: + print("Loopback test PASSED!") + else: + print("Loopback test FAILED!") + print(f"Expected: {test_value}, ADC: {adc_value}, DAC: {dac_value}") + +def main(): + test_value = 0x2000 # Mid-range value (8192 in decimal) + perform_loopback_test(test_value) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/flake.nix b/flake.nix index 962210d..80f79f3 100644 --- a/flake.nix +++ b/flake.nix @@ -481,7 +481,7 @@ inherit migen misoc vivado; }; packages.armv7l-linux = { - inherit fast-servo-gateware linien-server; + inherit fast-servo-gateware linien-server pyfastservo; } // (board-package-set { board = "zc706"; }) // (board-package-set { board = "fast-servo"; });