added sram saw

This commit is contained in:
Ayzen
2026-02-03 19:29:40 +03:00
parent fd1095c50a
commit 0d6a73d835
7 changed files with 99 additions and 39 deletions

View File

@ -16,6 +16,9 @@ AD9102_SAW_STEP_DEFAULT = 1
AD9102_PAT_PERIOD_DEFAULT = 0xFFFF
AD9102_PAT_PERIOD_BASE_DEFAULT = 0x02
AD9102_DAC_CLK_HZ = None # set to actual DAC clock if you want freq->SAW_STEP conversion
AD9102_FLAG_SRAM = 0x0004
AD9102_SRAM_SAMPLES_DEFAULT = 16
AD9102_SRAM_HOLD_DEFAULT = 1
class TaskType(IntEnum):
Manual = 0x00
@ -307,44 +310,77 @@ def calc_pat_period_for_duty(saw_step: int, duty: float, pat_period_base: int, t
pat_period = 0xFFFF
return pat_period
def calc_sram_samples_for_freq(freq_hz: float, dac_clk_hz: float, hold: int = None):
if hold is None or hold <= 0:
hold = AD9102_SRAM_HOLD_DEFAULT
if freq_hz is None or freq_hz <= 0 or dac_clk_hz is None or dac_clk_hz <= 0:
return AD9102_SRAM_SAMPLES_DEFAULT
samples = int(round(dac_clk_hz / (freq_hz * hold)))
if samples < 2:
samples = 2
if samples > 4096:
samples = 4096
return samples
def create_AD9833_ramp_command(saw_step: int = None,
pat_period: int = None,
pat_period_base: int = None,
enable: bool = True,
triangle: bool = True):
if saw_step is None:
saw_step = AD9102_SAW_STEP_DEFAULT
if pat_period is None:
pat_period = AD9102_PAT_PERIOD_DEFAULT
if pat_period_base is None:
pat_period_base = AD9102_PAT_PERIOD_BASE_DEFAULT
if saw_step < 1:
saw_step = 1
if saw_step > 63:
saw_step = 63
if pat_period < 0:
pat_period = 0
if pat_period > 0xFFFF:
pat_period = 0xFFFF
if pat_period_base < 0:
pat_period_base = 0
if pat_period_base > 0x0F:
pat_period_base = 0x0F
triangle: bool = True,
sram_mode: bool = False,
sram_samples: int = None,
sram_hold: int = None):
flags = 0
if enable:
flags |= 0x0001
if triangle:
flags |= 0x0002
if sram_mode:
flags |= AD9102_FLAG_SRAM
param0 = ((pat_period_base & 0x0F) << 8) | (saw_step & 0xFF)
crc_word = flags ^ param0 ^ pat_period
if sram_mode:
if sram_samples is None:
sram_samples = AD9102_SRAM_SAMPLES_DEFAULT
if sram_samples < 2:
sram_samples = 2
if sram_samples > 4096:
sram_samples = 4096
if sram_hold is None or sram_hold <= 0:
sram_hold = AD9102_SRAM_HOLD_DEFAULT
if sram_hold > 0x0F:
sram_hold = 0x0F
param0 = sram_samples & 0xFFFF
param1 = sram_hold & 0x000F
else:
if saw_step is None:
saw_step = AD9102_SAW_STEP_DEFAULT
if pat_period is None:
pat_period = AD9102_PAT_PERIOD_DEFAULT
if pat_period_base is None:
pat_period_base = AD9102_PAT_PERIOD_BASE_DEFAULT
if saw_step < 1:
saw_step = 1
if saw_step > 63:
saw_step = 63
if pat_period < 0:
pat_period = 0
if pat_period > 0xFFFF:
pat_period = 0xFFFF
if pat_period_base < 0:
pat_period_base = 0
if pat_period_base > 0x0F:
pat_period_base = 0x0F
param0 = ((pat_period_base & 0x0F) << 8) | (saw_step & 0xFF)
param1 = pat_period
crc_word = flags ^ param0 ^ param1
data = flipfour(AD9833_CMD_HEADER) # Word 0 (header)
data += flipfour(int_to_hex(flags))
data += flipfour(int_to_hex(param0))
data += flipfour(int_to_hex(pat_period))
data += flipfour(int_to_hex(param1))
data += flipfour(int_to_hex(crc_word))
return bytearray.fromhex(data)