2020-01-20 20:07:20 +08:00
|
|
|
"""RTIO driver for Mirny (4 channel GHz PLLs)
|
|
|
|
"""
|
|
|
|
|
2019-06-17 01:17:42 +08:00
|
|
|
from artiq.language.core import kernel, delay
|
|
|
|
from artiq.language.units import us
|
|
|
|
|
|
|
|
from numpy import int32
|
|
|
|
|
|
|
|
from artiq.coredevice import spi2 as spi
|
|
|
|
|
|
|
|
|
2020-10-09 05:36:50 +08:00
|
|
|
SPI_CONFIG = (
|
|
|
|
0 * spi.SPI_OFFLINE
|
|
|
|
| 0 * spi.SPI_END
|
|
|
|
| 0 * spi.SPI_INPUT
|
|
|
|
| 1 * spi.SPI_CS_POLARITY
|
|
|
|
| 0 * spi.SPI_CLK_POLARITY
|
|
|
|
| 0 * spi.SPI_CLK_PHASE
|
|
|
|
| 0 * spi.SPI_LSB_FIRST
|
|
|
|
| 0 * spi.SPI_HALF_DUPLEX
|
|
|
|
)
|
2019-06-17 01:17:42 +08:00
|
|
|
|
|
|
|
# SPI clock write and read dividers
|
|
|
|
SPIT_WR = 4
|
|
|
|
SPIT_RD = 16
|
|
|
|
|
|
|
|
SPI_CS = 1
|
|
|
|
|
2020-01-20 20:07:20 +08:00
|
|
|
WE = 1 << 24
|
|
|
|
|
2021-01-30 01:46:47 +08:00
|
|
|
# supported CPLD code version
|
|
|
|
PROTO_REV_MATCH = 0x0
|
|
|
|
|
2020-01-20 20:07:20 +08:00
|
|
|
|
2019-06-17 01:17:42 +08:00
|
|
|
class Mirny:
|
2020-10-09 05:36:50 +08:00
|
|
|
"""
|
|
|
|
Mirny PLL-based RF generator.
|
2020-01-20 20:07:20 +08:00
|
|
|
|
|
|
|
:param spi_device: SPI bus device
|
2020-10-09 05:36:50 +08:00
|
|
|
:param refclk: Reference clock (SMA, MMCX or on-board 100 MHz oscillator)
|
|
|
|
frequency in Hz
|
|
|
|
:param clk_sel: Reference clock selection.
|
2021-06-22 17:57:28 +08:00
|
|
|
Valid options are: "XO" - onboard crystal oscillator;
|
|
|
|
"SMA" - front-panel SMA connector; "MMCX" - internal MMCX connector.
|
2021-02-02 23:23:47 +08:00
|
|
|
Passing an integer writes it as ``clk_sel`` in the CPLD's register 1.
|
|
|
|
The effect depends on the hardware revision.
|
2020-01-20 20:07:20 +08:00
|
|
|
:param core_device: Core device name (default: "core")
|
|
|
|
"""
|
2020-10-09 05:36:50 +08:00
|
|
|
|
2021-01-30 01:46:47 +08:00
|
|
|
kernel_invariants = {"bus", "core", "refclk", "clk_sel_hw_rev"}
|
2019-06-17 01:17:42 +08:00
|
|
|
|
2021-02-02 23:23:47 +08:00
|
|
|
def __init__(self, dmgr, spi_device, refclk=100e6, clk_sel="XO", core_device="core"):
|
2019-06-17 01:17:42 +08:00
|
|
|
self.core = dmgr.get(core_device)
|
|
|
|
self.bus = dmgr.get(spi_device)
|
|
|
|
|
2021-01-30 01:46:47 +08:00
|
|
|
# reference clock frequency
|
2020-10-09 05:36:50 +08:00
|
|
|
self.refclk = refclk
|
2021-02-02 23:23:47 +08:00
|
|
|
if not (10 <= self.refclk / 1e6 <= 600):
|
|
|
|
raise ValueError("Invalid refclk")
|
2020-10-09 05:36:50 +08:00
|
|
|
|
2021-01-30 01:46:47 +08:00
|
|
|
# reference clock selection
|
2021-02-02 23:23:47 +08:00
|
|
|
try:
|
2021-01-30 01:46:47 +08:00
|
|
|
self.clk_sel_hw_rev = {
|
2021-02-02 23:23:47 +08:00
|
|
|
# clk source: [reserved, reserved, v1.1, v1.0]
|
|
|
|
"xo": [-1, -1, 0, 0],
|
|
|
|
"mmcx": [-1, -1, 3, 2],
|
|
|
|
"sma": [-1, -1, 2, 3],
|
2021-01-30 01:46:47 +08:00
|
|
|
}[clk_sel.lower()]
|
2021-02-02 23:23:47 +08:00
|
|
|
except AttributeError: # not a string, fallback to int
|
|
|
|
if clk_sel & 0x3 != clk_sel:
|
|
|
|
raise ValueError("Invalid clk_sel") from None
|
|
|
|
self.clk_sel_hw_rev = [clk_sel] * 4
|
|
|
|
except KeyError:
|
|
|
|
raise ValueError("Invalid clk_sel") from None
|
|
|
|
|
2021-01-30 01:46:47 +08:00
|
|
|
self.clk_sel = -1
|
|
|
|
|
|
|
|
# board hardware revision
|
2021-02-02 23:23:47 +08:00
|
|
|
self.hw_rev = 0 # v1.0: 3, v1.1: 2
|
2020-10-09 05:36:50 +08:00
|
|
|
|
|
|
|
# TODO: support clk_div on v1.0 boards
|
|
|
|
|
2019-06-17 01:17:42 +08:00
|
|
|
@kernel
|
|
|
|
def read_reg(self, addr):
|
2020-01-20 20:07:20 +08:00
|
|
|
"""Read a register"""
|
2020-10-09 05:36:50 +08:00
|
|
|
self.bus.set_config_mu(
|
|
|
|
SPI_CONFIG | spi.SPI_INPUT | spi.SPI_END, 24, SPIT_RD, SPI_CS
|
|
|
|
)
|
2019-06-17 01:17:42 +08:00
|
|
|
self.bus.write((addr << 25))
|
2020-10-09 05:36:50 +08:00
|
|
|
return self.bus.read() & int32(0xFFFF)
|
2019-06-17 01:17:42 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def write_reg(self, addr, data):
|
2020-01-20 20:07:20 +08:00
|
|
|
"""Write a register"""
|
2019-06-17 01:17:42 +08:00
|
|
|
self.bus.set_config_mu(SPI_CONFIG | spi.SPI_END, 24, SPIT_WR, SPI_CS)
|
2020-10-09 05:36:50 +08:00
|
|
|
self.bus.write((addr << 25) | WE | ((data & 0xFFFF) << 8))
|
2019-06-17 01:17:42 +08:00
|
|
|
|
|
|
|
@kernel
|
2020-10-09 05:36:50 +08:00
|
|
|
def init(self, blind=False):
|
|
|
|
"""
|
|
|
|
Initialize and detect Mirny.
|
|
|
|
|
2021-02-02 23:23:47 +08:00
|
|
|
Select the clock source based the board's hardware revision.
|
|
|
|
Raise ValueError if the board's hardware revision is not supported.
|
|
|
|
|
|
|
|
:param blind: Verify presence and protocol compatibility. Raise ValueError on failure.
|
2020-10-09 05:36:50 +08:00
|
|
|
"""
|
2021-01-30 01:46:47 +08:00
|
|
|
reg0 = self.read_reg(0)
|
|
|
|
self.hw_rev = reg0 & 0x3
|
|
|
|
|
2020-10-09 05:36:50 +08:00
|
|
|
if not blind:
|
2021-01-30 01:46:47 +08:00
|
|
|
if (reg0 >> 2) & 0x3 != PROTO_REV_MATCH:
|
2020-10-09 05:36:50 +08:00
|
|
|
raise ValueError("Mirny PROTO_REV mismatch")
|
|
|
|
delay(100 * us) # slack
|
|
|
|
|
|
|
|
# select clock source
|
2021-02-02 23:23:47 +08:00
|
|
|
self.clk_sel = self.clk_sel_hw_rev[self.hw_rev]
|
|
|
|
|
|
|
|
if self.clk_sel < 0:
|
|
|
|
raise ValueError("Hardware revision not supported")
|
|
|
|
|
2020-10-09 05:36:50 +08:00
|
|
|
self.write_reg(1, (self.clk_sel << 4))
|
|
|
|
delay(1000 * us)
|
2019-06-17 01:17:42 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def set_att_mu(self, channel, att):
|
|
|
|
"""Set digital step attenuator in machine units.
|
|
|
|
|
|
|
|
:param att: Attenuation setting, 8 bit digital.
|
|
|
|
"""
|
|
|
|
self.bus.set_config_mu(SPI_CONFIG | spi.SPI_END, 16, SPIT_WR, SPI_CS)
|
|
|
|
self.bus.write(((channel | 8) << 25) | (att << 16))
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def write_ext(self, addr, length, data):
|
2020-01-20 20:07:20 +08:00
|
|
|
"""Perform SPI write to a prefixed address"""
|
2019-06-17 01:17:42 +08:00
|
|
|
self.bus.set_config_mu(SPI_CONFIG, 8, SPIT_WR, SPI_CS)
|
|
|
|
self.bus.write(addr << 25)
|
2020-10-09 05:36:50 +08:00
|
|
|
self.bus.set_config_mu(SPI_CONFIG | spi.SPI_END, length, SPIT_WR, SPI_CS)
|
2019-06-17 01:17:42 +08:00
|
|
|
if length < 32:
|
|
|
|
data <<= 32 - length
|
|
|
|
self.bus.write(data)
|