1
0
Fork 0
nix-servo/fast-servo/pyfastservo/dac.py

190 lines
6.2 KiB
Python
Raw Normal View History

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 time
2024-03-06 17:41:21 +08:00
import spidev
from pyfastservo.common import (
2024-03-06 17:41:21 +08:00
CH0_HIGH_WORD_ADDR,
CH0_LOW_WORD_ADDR,
CH1_HIGH_WORD_ADDR,
CH1_LOW_WORD_ADDR,
CTRL_ADDR,
MAP_MASK,
PAGESIZE,
write_to_memory,
read_from_memory
2024-03-06 17:41:21 +08:00
)
# /dev/spidev2.0 <=> spidev<BUS>.<DEVICE>
MAIN_DAC_BUS = 2
MAIN_DAC_DEVICE = 0
DAC_VERSION = 0x0A
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 hard_reset(spi):
2024-08-28 12:34:31 +08:00
spi_write(spi, 0x00, 0x20) # Software reset
spi_write(spi, 0x00, 0x00) # Release software reset
spi_read(spi, 0x00) # Read reset address (necessary for reset to take effect)
def check_version(spi):
version = spi_read(spi, 0x1F)
print(f"DAC version: 0x{version:02X}")
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
2024-08-28 12:34:31 +08:00
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, LVDS interface, 4 LVDS lanes
def dac_self_calibration(spi):
spi_write(spi, 0x12, 0x00) # Reset calibration status
spi_write(spi, 0x0E, 0x08) # Enable calibration clock, default divide ratio
spi_write(spi, 0x0E, 0x38) # CALSELI = 1, CALSELQ = 1, CALCLK = 1
spi_write(spi, 0x12, 0x10) # Set CALEN bit
while True:
status = spi_read(spi, 0x0F)
if status & 0xC0 == 0xC0: # Both CALSTATI and CALSTATQ are 1
break
time.sleep(0.01)
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")
2024-03-06 17:41:21 +08:00
def manual_override(enable=True):
reg_contents = read_from_memory(CTRL_ADDR, 1)[0]
print(f"REG contents: 0b{reg_contents:03b}")
to_write = reg_contents | 0b1 if enable else reg_contents & 0b110
write_to_memory(CTRL_ADDR, to_write)
def power_down(channel, power_down=True):
assert channel in (0, 1)
bitmask = 1 << (channel + 1) & 0b111
reg_contents = read_from_memory(CTRL_ADDR, 1)[0]
value = (1 if power_down else 0) << (channel + 1)
reg_contents &= ~bitmask
to_write = reg_contents | value
write_to_memory(CTRL_ADDR, to_write)
reg_contents = read_from_memory(CTRL_ADDR, 1)[0]
print(f"REG contents: 0b{reg_contents:03b}")
2024-08-29 17:16:10 +08:00
# 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)
2024-03-06 17:41:21 +08:00
2024-08-29 17:29:19 +08:00
def write_ramp():
signal = [i for i in range(16384)]
for value in signal:
write_sample(0, value)
def configure_ad9117():
spi = spidev.SpiDev()
spi.open(MAIN_DAC_BUS, MAIN_DAC_DEVICE)
spi.max_speed_hz = 5000
spi.mode = 0b00 # CPOL = 0 CPHA = 0
spi.cshigh = False
try:
hard_reset(spi)
if not check_version(spi):
print("Unrecognized DAC version")
return False
configure_dac(spi)
2024-08-22 17:35:11 +08:00
# dac_self_calibration(spi)
2024-03-06 17:41:21 +08:00
2024-08-20 16:35:49 +08:00
# Enable DAC outputs
spi_write(spi, 0x01, spi_read(spi, 0x01) & ~((1 << 4) | (1 << 3)))
power_down(0, False)
power_down(1, False)
manual_override(True)
2024-03-06 17:41:21 +08:00
2024-08-29 17:29:19 +08:00
write_ramp()
# # 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
2024-08-29 17:16:10 +08:00
2024-08-29 17:29:19 +08:00
# print(f"Sample value 0x{sample_value:04X} written to both channels")
2024-08-29 17:16:10 +08:00
2024-08-29 17:29:19 +08:00
# # 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]
2024-08-29 17:16:10 +08:00
2024-08-29 17:29:19 +08:00
# # Reconstruct the 14-bit values
# ch0_value = (ch0_high << 8) | ch0_low
# ch1_value = (ch1_high << 8) | ch1_low
2024-08-29 17:16:10 +08:00
2024-08-29 17:29:19 +08:00
# print(f"Read back value for channel 0: 0x{ch0_value:04X}")
# print(f"Read back value for channel 1: 0x{ch1_value:04X}")
2024-08-29 17:16:10 +08:00
print("AD9117 configuration completed successfully")
return True
2024-03-06 17:41:21 +08:00
except Exception as e:
print(f"Error configuring AD9117: {e}")
return False
finally:
spi.close()
2024-03-06 17:41:21 +08:00
if __name__ == "__main__":
configure_ad9117()