From 0927824c614531f41a009fbc02231d78ab06068f Mon Sep 17 00:00:00 2001 From: Florian Agbuya Date: Thu, 29 Aug 2024 17:16:10 +0800 Subject: [PATCH] test write_sample --- fast-servo/pyfastservo/dac.py | 52 +++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/fast-servo/pyfastservo/dac.py b/fast-servo/pyfastservo/dac.py index cb9d5a7..617047f 100644 --- a/fast-servo/pyfastservo/dac.py +++ b/fast-servo/pyfastservo/dac.py @@ -100,16 +100,30 @@ def power_down(channel, power_down=True): reg_contents = read_from_memory(CTRL_ADDR, 1)[0] print(f"REG contents: 0b{reg_contents:03b}") -def set_dac_output(value): - value = min(value, 0x3FFF) - low_word = value & 0xFF - high_word = (value >> 8) & 0x3F +# def set_dac_output(value): +# value = min(value, 0x3FFF) +# low_word = value & 0xFF +# high_word = (value >> 8) & 0x3F + +# write_to_memory(CH0_HIGH_WORD_ADDR, high_word) +# write_to_memory(CH0_LOW_WORD_ADDR, low_word) +# write_to_memory(CH1_HIGH_WORD_ADDR, high_word) +# write_to_memory(CH1_LOW_WORD_ADDR, low_word) +# print(f"DAC output set to: 0x{value:04X}") + +def write_sample(channel, sample): + assert channel in (0, 1) + if channel == 0: + addresses = [CH0_HIGH_WORD_ADDR, CH0_LOW_WORD_ADDR] + else: + addresses = [CH1_HIGH_WORD_ADDR, CH1_LOW_WORD_ADDR] + + low_word_value = sample & 0xFF + high_word_value = (sample >> 8) & 0x3F + values = [high_word_value, low_word_value] + for addr, value in zip(addresses, values): + write_to_memory(addr, value) - write_to_memory(CH0_HIGH_WORD_ADDR, high_word) - write_to_memory(CH0_LOW_WORD_ADDR, low_word) - write_to_memory(CH1_HIGH_WORD_ADDR, high_word) - write_to_memory(CH1_LOW_WORD_ADDR, low_word) - print(f"DAC output set to: 0x{value:04X}") def configure_ad9117(): spi = spidev.SpiDev() @@ -134,6 +148,26 @@ def configure_ad9117(): power_down(1, False) manual_override(True) + # Write sample value 0x1FFF to both channels + sample_value = 0x1FFF + write_sample(0, sample_value) # Write to channel 0 + write_sample(1, sample_value) # Write to channel 1 + + print(f"Sample value 0x{sample_value:04X} written to both channels") + + # Read back the values from memory + ch0_high = read_from_memory(CH0_HIGH_WORD_ADDR, 1)[0] + ch0_low = read_from_memory(CH0_LOW_WORD_ADDR, 1)[0] + ch1_high = read_from_memory(CH1_HIGH_WORD_ADDR, 1)[0] + ch1_low = read_from_memory(CH1_LOW_WORD_ADDR, 1)[0] + + # Reconstruct the 14-bit values + ch0_value = (ch0_high << 8) | ch0_low + ch1_value = (ch1_high << 8) | ch1_low + + print(f"Read back value for channel 0: 0x{ch0_value:04X}") + print(f"Read back value for channel 1: 0x{ch1_value:04X}") + print("AD9117 configuration completed successfully") return True