linuswck 90f18a2a00 pl init: Correct the initialization sequence
- Si5340 should be initialized first before calling any PL register since if
    Si5340 is not preprogrammed, there is not PL system clock driving any PL register.
- Adc Initialization causes the PL MMCM to relock and trigger a global PL reset. Thus,
    CSR registers should only be altered after Adc is initialized successfully.
2025-04-08 12:51:39 +08:00

182 lines
6.0 KiB
Python

# This file is part of Fast Servo Software Package.
#
# Copyright (C) 2023 Jakub Matyas
# Warsaw University of Technology <jakubk.m@gmail.com>
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import time
import spidev
from pyfastservo.common import (
ADC_AFE_CTRL_ADDR,
ADC_BITSLIP_ADDR,
ADC_CH0_HIGH_ADDR,
ADC_CH0_LOW_ADDR,
ADC_CH1_HIGH_ADDR,
ADC_CH1_LOW_ADDR,
ADC_DELAY_ADDR,
ADC_FRAME_ADDR,
AUX_ADC_ADDR,
MAP_MASK,
PAGESIZE,
write_to_memory,
read_from_memory
)
# /dev/spidev1.0 <=> spidev<BUS>.<DEVICE>
MAIN_ADC_BUS = 1
MAIN_ADC_DEVICE = 1
AUX_ADC_BUS = 1
AUX_ADC_PORT_A = 2
AUX_ADC_PORT_B = 3
def spi_write(spi, address, value):
spi.xfer2([address, value])
def spi_read(spi, address):
rx_buffer = spi.xfer2([0x80 | address, 0x00])
return rx_buffer[1]
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) # 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):
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
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)
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 read_frame():
return read_from_memory(ADC_FRAME_ADDR, 1)[0]
def perform_word_alignment():
for i in range(4):
current_frame = read_frame()
if current_frame & 0x0F != 0x0C:
print(f"Performing bitslip (iteration: {i}). Current frame: 0x{current_frame:02x}")
write_to_memory(ADC_BITSLIP_ADDR, 1)
else:
print(f"No bitslip required; Current frame: 0x{current_frame:02x}")
break
prev_frame = read_frame()
for tap_delay in range(32):
write_to_memory(ADC_DELAY_ADDR, tap_delay)
current_frame = read_frame()
print(f"Tap delay: {tap_delay}, Current frame: 0x{current_frame:02x}")
print(f"prev_frame: 0x{prev_frame:02x}")
if current_frame != prev_frame:
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 True
prev_frame = current_frame
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 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"Configure ADC AFE Gain: ch1_10x: {"10x" if ch1_x10 else "1x"} | ch2_x10: {"10x" if ch2_x10 else "1x"}")
return afe_ctrl
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 configure_ltc2195():
# Calling this fn stops the PL input clock momentarily, triggers PL reset.
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:
test_pattern = 0x811F
success &= main_adc_config(spi, test_pattern)
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()