2024-03-06 17:41:21 +08:00
|
|
|
# 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 spidev
|
2024-04-05 15:30:42 +08:00
|
|
|
from pyfastservo.common import (
|
2024-03-06 17:41:21 +08:00
|
|
|
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,
|
2024-07-10 14:22:18 +08:00
|
|
|
write_to_memory,
|
|
|
|
read_from_memory
|
2024-03-06 17:41:21 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# /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
|
|
|
|
|
|
|
|
|
2024-07-10 14:22:18 +08:00
|
|
|
def spi_write(spi, address, value):
|
|
|
|
spi.xfer2([address, value])
|
2024-03-06 17:41:21 +08:00
|
|
|
|
2024-07-10 14:22:18 +08:00
|
|
|
def spi_read(spi, address):
|
|
|
|
rx_buffer = spi.xfer2([0x80 | address, 0x00])
|
|
|
|
return rx_buffer[1]
|
2024-03-06 17:41:21 +08:00
|
|
|
|
2024-07-10 14:22:18 +08:00
|
|
|
def main_adc_config(spi, test_pattern):
|
|
|
|
high_word = (test_pattern & 0xFF00) >> 8
|
|
|
|
low_word = test_pattern & 0xFF
|
2024-03-06 17:41:21 +08:00
|
|
|
|
2024-07-10 14:22:18 +08:00
|
|
|
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
|
2024-03-06 17:41:21 +08:00
|
|
|
|
2024-07-10 14:22:18 +08:00
|
|
|
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)
|
2024-03-06 17:41:21 +08:00
|
|
|
|
2024-07-10 14:22:18 +08:00
|
|
|
def verify_adc_registers(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}")
|
2024-03-06 17:41:21 +08:00
|
|
|
|
2024-07-10 14:22:18 +08:00
|
|
|
def read_frame():
|
|
|
|
return read_from_memory(ADC_FRAME_ADDR, 1)[0]
|
2024-03-06 17:41:21 +08:00
|
|
|
|
2024-07-10 14:22:18 +08:00
|
|
|
def perform_bitslip():
|
2024-03-06 17:41:21 +08:00
|
|
|
for i in range(4):
|
2024-07-10 14:22:18 +08:00
|
|
|
current_frame = read_frame()
|
2024-03-06 17:41:21 +08:00
|
|
|
if current_frame != 0x0C:
|
2024-07-10 14:22:18 +08:00
|
|
|
print(f"Performing bitslip (iteration: {i}). Current frame: 0x{current_frame:02x}")
|
2024-03-06 17:41:21 +08:00
|
|
|
write_to_memory(ADC_BITSLIP_ADDR, 1)
|
|
|
|
else:
|
2024-07-10 14:22:18 +08:00
|
|
|
print(f"No bitslip required; Current frame: 0x{current_frame:02x}")
|
|
|
|
return
|
2024-03-06 17:41:21 +08:00
|
|
|
|
2024-07-10 14:22:18 +08:00
|
|
|
def find_edge():
|
|
|
|
prev_frame = read_frame()
|
|
|
|
transition = False
|
|
|
|
for tap_delay in range(32):
|
2024-03-06 17:41:21 +08:00
|
|
|
write_to_memory(ADC_DELAY_ADDR, tap_delay)
|
2024-07-10 14:22:18 +08:00
|
|
|
current_frame = read_frame()
|
2024-03-06 17:41:21 +08:00
|
|
|
|
2024-07-10 14:22:18 +08:00
|
|
|
print(f"Tap delay: {tap_delay}, Current frame: 0x{current_frame:02x}")
|
2024-03-06 17:41:21 +08:00
|
|
|
|
2024-07-10 14:22:18 +08:00
|
|
|
if current_frame != prev_frame:
|
|
|
|
if not transition:
|
|
|
|
transition = True
|
|
|
|
else:
|
|
|
|
final_delay = (tap_delay // 2) + 2
|
|
|
|
print(f"Edge detected; setting iDelay to: {final_delay}")
|
|
|
|
write_to_memory(ADC_DELAY_ADDR, final_delay)
|
|
|
|
return
|
2024-03-06 17:41:21 +08:00
|
|
|
|
|
|
|
prev_frame = current_frame
|
|
|
|
|
2024-07-10 14:22:18 +08:00
|
|
|
# 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)
|
2024-03-06 17:41:21 +08:00
|
|
|
|
2024-07-10 14:22:18 +08:00
|
|
|
def read_adc_channel(high_addr, low_addr):
|
|
|
|
return (read_from_memory(high_addr, 1)[0] << 8) | read_from_memory(low_addr, 1)[0]
|
2024-03-06 17:41:21 +08:00
|
|
|
|
2024-07-10 14:22:18 +08:00
|
|
|
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)
|
2024-03-06 17:41:21 +08:00
|
|
|
print(f"Final ADC_CH0: 0x{adc_ch0:04x}")
|
|
|
|
print(f"Final ADC_CH1: 0x{adc_ch1:04x}")
|
|
|
|
|
2024-07-10 14:22:18 +08:00
|
|
|
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}")
|
|
|
|
return afe_ctrl
|
2024-03-06 17:41:21 +08:00
|
|
|
|
2024-07-10 14:22:18 +08:00
|
|
|
def configure_ltc2195():
|
2024-03-06 17:41:21 +08:00
|
|
|
spi = spidev.SpiDev()
|
|
|
|
try:
|
2024-07-10 14:22:18 +08:00
|
|
|
spi.open(MAIN_ADC_BUS, MAIN_ADC_DEVICE)
|
|
|
|
spi.max_speed_hz = 50000
|
|
|
|
spi.mode = 0b00 # CPOL = 0 CPHA = 0
|
2024-04-15 15:28:20 +08:00
|
|
|
spi.cshigh = False
|
2024-03-06 17:41:21 +08:00
|
|
|
|
2024-07-10 14:22:18 +08:00
|
|
|
test_pattern = 0x811F
|
|
|
|
main_adc_config(spi, test_pattern)
|
2024-03-06 17:41:21 +08:00
|
|
|
|
2024-07-10 14:22:18 +08:00
|
|
|
verify_adc_registers(spi, {
|
|
|
|
0x01: 0x20,
|
|
|
|
0x02: 0x15,
|
|
|
|
0x03: (test_pattern & 0xFF00) >> 8,
|
|
|
|
0x04: test_pattern & 0xFF
|
|
|
|
})
|
2024-03-06 17:41:21 +08:00
|
|
|
|
2024-07-10 14:22:18 +08:00
|
|
|
# Performing Word Align
|
|
|
|
perform_bitslip()
|
|
|
|
find_edge()
|
|
|
|
print_adc_channels()
|
2024-03-06 17:41:21 +08:00
|
|
|
|
2024-07-10 14:22:18 +08:00
|
|
|
main_adc_test_mode(spi, False)
|
|
|
|
verify_adc_registers(spi, {0x02: 0x11}) # Verify test mode is off
|
2024-03-06 17:41:21 +08:00
|
|
|
|
2024-07-10 14:22:18 +08:00
|
|
|
enable_adc_afe()
|
2024-03-06 17:41:21 +08:00
|
|
|
|
2024-07-10 14:22:18 +08:00
|
|
|
finally:
|
|
|
|
spi.close()
|
2024-03-06 17:41:21 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-07-10 14:22:18 +08:00
|
|
|
configure_ltc2195()
|