Update PL init scripts

- Cleanup
- Better debugging message
- Adjust Dac Init sequence so that it always outputs 0V during init
This commit is contained in:
linuswck 2025-02-14 18:02:05 +08:00
parent 4a3fe50427
commit 04c8e7b58e
5 changed files with 154 additions and 131 deletions

View File

@ -54,28 +54,42 @@ def spi_read(spi, address):
def main_adc_config(spi, test_pattern):
high_word = (test_pattern & 0xFF00) >> 8
low_word = test_pattern & 0xFF
register_settings = {
0x01: 0x20, # REGISTER A1: set to Two's complement Data Format
0x02: 0x11, # REGISTER A2: set to LVDS output, set 4 data lanes
0x03: high_word, # REGISTER A3: test pattern high word
0x04: low_word, # REGISTER A4: test pattern low word
}
spi_write(spi, 0x00, 0x80) # reset
spi_write(spi, 0x01, 0x20) # REGISTER A1: set to Two's complement Data Format
spi_write(spi, 0x02, 0x15) # REGISTER A2: set to LVDS output, set 4 data lanes and turn on test mode
spi_write(spi, 0x03, high_word) # REGISTER A3: test pattern high word
spi_write(spi, 0x04, low_word) # REGISTER A4: test pattern low word
spi_write(spi, 0x00, 0x80) # Soft Reset
for addr, val in register_settings.items():
spi_write(spi, addr, val)
return verify_registers_vals(spi, register_settings)
def main_adc_test_mode(spi, enable):
reg_contents = 0x15 if enable else 0x11 # set to LVDS output, set 4 data lanes and turn on or off test mode
spi_write(spi, 0x02, reg_contents)
value = spi_read(spi, 0x02)
# set to LVDS output, set 4 data lanes and turn on or off test mode
if enable:
value |= 1 << 2
else:
value &= 0xfb
def verify_adc_registers(spi, reg_to_check):
spi_write(spi, 0x02, value)
return verify_registers_vals(spi, {0x02: value})
def verify_registers_vals(spi, reg_to_check):
for register, expected_value in reg_to_check.items():
value = spi_read(spi, register)
print(f"Spi readback register 0x{register:02x}: 0x{value:02x}")
if value != expected_value:
print(f"Different value read than sent in reg 0x{register:02x}")
print(f"Different value read than sent in reg 0x{register:02x}. Expected: 0x{expected_value:02x} Got: 0x{value:02x}")
return False
return True
def read_frame():
return read_from_memory(ADC_FRAME_ADDR, 1)[0]
def perform_bitslip():
def perform_word_alignment():
for i in range(4):
current_frame = read_frame()
if current_frame & 0x0F != 0x0C:
@ -83,9 +97,8 @@ def perform_bitslip():
write_to_memory(ADC_BITSLIP_ADDR, 1)
else:
print(f"No bitslip required; Current frame: 0x{current_frame:02x}")
return
break
def find_edge():
prev_frame = read_frame()
for tap_delay in range(32):
write_to_memory(ADC_DELAY_ADDR, tap_delay)
@ -98,40 +111,32 @@ def find_edge():
final_delay = ((tap_delay+1) // 2) + 2
print(f"Edge detected; setting iDelay to: {final_delay}")
write_to_memory(ADC_DELAY_ADDR, final_delay)
return
return True
prev_frame = current_frame
# If no edge detected
final_delay = 11
print(f"No edge detected; setting iDelay to: {final_delay}")
write_to_memory(ADC_DELAY_ADDR, final_delay)
return False
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():
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}")
def is_clk_aligned(spi, test_pattern):
aligned = True
main_adc_test_mode(spi, True)
for i in range(100):
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)
if adc_ch0 != test_pattern or adc_ch1 != test_pattern:
aligned = False
break
main_adc_test_mode(spi, False)
return aligned
def enable_adc_afe(ch1_x10=False, ch2_x10=False):
ctrl_value = (ch2_x10 << 1) | ch1_x10
write_to_memory(ADC_AFE_CTRL_ADDR, ctrl_value)
afe_ctrl = read_from_memory(ADC_AFE_CTRL_ADDR, 1)[0]
print(f"ADC_AFE_CTRL: 0x{afe_ctrl:02X}")
print(f"Configure ADC AFE Gain: ch1_10x: {"10x" if ch1_x10 else "1x"} | ch2_x10: {"10x" if ch2_x10 else "1x"}")
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)
@ -140,62 +145,37 @@ def print_adc_channel(ch):
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():
print()
print("### Initializing LTC2195 Adc")
spi = spidev.SpiDev()
spi.open(MAIN_ADC_BUS, MAIN_ADC_DEVICE)
spi.max_speed_hz = 50000
spi.mode = 0b00 # CPOL = 0 CPHA = 0
spi.cshigh = False
success = True
try:
spi.open(MAIN_ADC_BUS, MAIN_ADC_DEVICE)
spi.max_speed_hz = 50000
spi.mode = 0b00 # CPOL = 0 CPHA = 0
spi.cshigh = False
test_pattern = 0x811F
main_adc_config(spi, test_pattern)
verify_adc_registers(spi, {
0x01: 0x20,
0x02: 0x15,
0x03: (test_pattern & 0xFF00) >> 8,
0x04: test_pattern & 0xFF
})
success &= main_adc_config(spi, test_pattern)
# Performing Word Align
perform_bitslip()
find_edge()
# Printing it once is not enough to check whether the alignment is correct.
for i in range(100):
print_adc_channels()
main_adc_test_mode(spi, False)
verify_adc_registers(spi, {0x02: 0x11}) # Verify test mode is off
# 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)
success &= perform_word_alignment()
if is_clk_aligned(spi, test_pattern):
print("PL Data and Clock Alignment is verified")
else:
success &= False
print("Error: Clocks are not aligned")
enable_adc_afe(ch1_x10=0, ch2_x10=0)
except Exception as e:
print(f"Error configuring LTC2195: {e}")
success = False
finally:
spi.close()
if success:
print("LTC2195 Adc init completed")
else:
print("LTC2195 Adc init failed")
return success
if __name__ == "__main__":
configure_ltc2195()

View File

@ -56,6 +56,14 @@ def spi_read(spi, address):
rx_buffer = spi.xfer2([0x80 | address, 0x00])
return rx_buffer[1]
def verify_registers_vals(spi, reg_to_check):
for register, expected_value in reg_to_check.items():
value = spi_read(spi, register)
if value != expected_value:
print(f"Different value read than sent in reg 0x{register:02x}. Expected: 0x{expected_value:02x} Got: 0x{value:02x}")
return False
return True
def soft_reset(spi):
spi_write(spi, 0x00, 0x10) # Software reset
spi_write(spi, 0x00, 0x00) # Release software reset
@ -67,17 +75,22 @@ def check_version(spi):
return version == DAC_VERSION
def configure_dac(spi):
power_down_reg = spi_read(spi, 0x01)
spi_write(spi, 0x01, power_down_reg & ~(1 << 0)) # Clear EXTREF bit for internal reference
spi_write(spi, 0x0D, 0x00) # Set RREF to 10 kΩ for 1.0V reference
spi_write(spi, 0x04, 0xA0) # Enable on-chip IRSET (1.6 kΩ for 20mA output)
spi_write(spi, 0x07, 0xA0) # Enable on-chip QRSET (1.6 kΩ for 20mA output)
spi_write(spi, 0x05, 0x00) # Disable internal IRCML
spi_write(spi, 0x08, 0x00) # Disable internal QRCML
spi_write(spi, 0x02, 0xB4) # Enable 2's complement, IFirst: True, IRising: True, DCI_EN: Enabled
spi_write(spi, 0x14, 0x00)
spi_write(spi, 0x14, 0x08) # Trigger the retimer to reacquire the clock relationship
spi_write(spi, 0x14, 0x00)
register_settings = {
0x01: 0x40, # Clear EXTREF bit for internal reference,
0x02: 0xB4, # Enable 2's complement, IFirst: True, IRising: True, DCI_EN: Enabled
0x0D: 0x00, # Set RREF to 10 kΩ for 1.0V reference
0x04: 0xA0, # Enable on-chip IRSET (1.6 kΩ for 20mA output)
0x07: 0xA0, # Enable on-chip QRSET (1.6 kΩ for 20mA output)
0x05: 0x00, # Disable internal IRCML
0x08: 0x00, # Disable internal QRCML
}
for addr, val in register_settings.items():
spi_write(spi, addr, val)
success = verify_registers_vals(spi, register_settings)
if success:
# Set manual override output default back to 0V in 2's complement
set_dac_input_override_value(0x0000)
return success
def dac_self_calibration(spi):
spi_write(spi, 0x12, 0x00) # Reset calibration status
@ -93,13 +106,14 @@ def dac_self_calibration(spi):
spi_write(spi, 0x12, 0x00) # Clear calibration bits
spi_write(spi, 0x0E, 0x30) # Keep CALSELI and CALSELQ set, clear CALCLK
print("DAC self-calibration completed")
print("DAC self-calibration completes")
return True
def manual_override(enable=True):
def set_dac_input_override(enable=True):
reg_contents = read_from_memory(CTRL_ADDR, 1)[0]
to_write = reg_contents | 0b1 if enable else reg_contents & 0b110
write_to_memory(CTRL_ADDR, to_write)
print(f"Set DAC Output Manual Override: {enable}")
print(f"Enable DAC Input Override: {enable}")
def power_down_afe(channel, power_down=True):
assert channel in (0, 1)
@ -114,7 +128,7 @@ def power_down_afe(channel, power_down=True):
reg_contents = read_from_memory(CTRL_ADDR, 1)[0]
print(f"Power Down DAC AFE Ch{channel}: {power_down}")
def set_dac_output(value):
def set_dac_input_override_value(value):
value = min(value, 0x3FFF)
low_word = value & 0xFF
high_word = (value >> 8) & 0x3F
@ -126,19 +140,28 @@ def set_dac_output(value):
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}")
print(f"Set DAC Input Override Value to: 0x{value:04X}")
def check_clk_relationship(spi):
# Trigger the retimer to reacquire the clock relationship
spi_write(spi, 0x14, 0x00)
spi_write(spi, 0x14, 0x08)
spi_write(spi, 0x14, 0x00)
clkmode_reg = spi_read(spi, 0x14)
print(f"CLKMODE reg: 0x{clkmode_reg:02X}")
print(f"AD9117 CLKMODE reg: 0x{clkmode_reg:02X}")
if clkmode_reg & 0b00010000:
print("Clock relationship is not found")
print("Ad9117 Clock relationship is established")
return False
else:
print("Clock relationship is found")
print("Ad9117 Clock relationship is found")
return True
def configure_ad9117():
print()
print("### Initializing AD9117 Dac")
success = True
spi = spidev.SpiDev()
spi.open(MAIN_DAC_BUS, MAIN_DAC_DEVICE)
spi.max_speed_hz = 5000
@ -151,27 +174,30 @@ def configure_ad9117():
print("Unrecognized DAC version")
return False
power_down_afe(0, True)
power_down_afe(1, True)
# Disable DAC Output during Initialization
spi_write(spi, 0x01, 0x46)
success &= verify_registers_vals(spi, {0x01: 0x46})
configure_dac(spi)
check_clk_relationship(spi)
dac_self_calibration(spi)
# Enable DAC outputs
spi_write(spi, 0x01, spi_read(spi, 0x01) & ~((1 << 4) | (1 << 3)))
success &= configure_dac(spi)
success &= check_clk_relationship(spi)
success &= dac_self_calibration(spi)
power_down_afe(0, False)
power_down_afe(1, False)
manual_override(False)
print("AD9117 configuration completed successfully")
return True
if success:
set_dac_input_override(False)
# Re-Enable DAC Output
spi_write(spi, 0x01, 0x40)
verify_registers_vals(spi, {0x01: 0x40})
print("AD9117 Dac init completed")
else:
print("AD9117 Dac init fails")
return success
except Exception as e:
print(f"Error configuring AD9117: {e}")
print(f"Error configuring AD9117 Dac: {e}")
return False
finally:
spi.close()

View File

@ -20,9 +20,30 @@
from pyfastservo import adc, si5340, dac
def main():
si5340.configure_si5340()
adc.configure_ltc2195()
dac.configure_ad9117()
print()
print("### PL Initialization")
print()
# Set output voltage to be 0V after AD9117-2 soft reset
# DAC starts up in unsigned Data Format. Set DAC to output 0V before Clock is provided to it
dac.set_dac_input_override_value(0x1FFF)
dac.set_dac_input_override(True)
si5340_ok = si5340.configure_si5340()
adc_ok = adc.configure_ltc2195()
dac_ok = dac.configure_ad9117()
if si5340_ok and adc_ok and dac_ok:
print()
print("### PL init success")
print()
else:
print()
print("### Something is wrong with PL init")
print(f"Si5340 Clock Generator Init Status: {"Success" if si5340_ok else "Failed"}")
print(f"LTC2195 Adc Init Status: {"Success" if adc_ok else "Failed"}")
print(f"Ad9117 Dac Init Status: {"Success" if dac_ok else "Failed"}")
print()
if __name__ == "__main__":
main()

View File

@ -93,6 +93,8 @@ def check_los_status(bus):
return not xaxb_los
def configure_si5340():
print()
print("### Initializing Si5340 Clock Generator")
with SMBus(BUS_NO) as bus:
if not wait_device_ready(bus):
print("Device not ready. Aborting.")
@ -271,10 +273,13 @@ def configure_si5340():
if not pll_locked:
print("Error: PLL is not locked")
return False
elif not xaxb_signal_present:
print("Error: XA/XB signal is lost")
return False
else:
print("Si5340 configuration completed successfully")
print("Si5340 init completed")
return True
if __name__ == "__main__":
configure_si5340()

View File

@ -431,17 +431,8 @@
cp ${fast-servo-gateware}/gateware.bin /lib/firmware/
echo gateware.bin > /sys/class/fpga_manager/fpga0/firmware
# Reset the PL and initialize PL, ADC and DAC clock
echo "Configuring Si5340 to generate Clocks"
python3 -m pyfastservo.si5340
# Iniailize ADC
echo "Initialize ADC"
python3 -m pyfastservo.adc
# Initialize DAC
echo "Initialize DAC"
python3 -m pyfastservo.dac
# Initialize All PL Peripherals
python3 -m pyfastservo.initialize
'';
})];
system = "x86_64-linux";