2023-06-23 18:15:53 -04:00
|
|
|
# Copyright 2023 (C) Peter McGoron
|
|
|
|
# This file is a part of Upsilon, a free and open source software project.
|
|
|
|
# For license terms, refer to the files in `doc/copying` in the Upsilon
|
|
|
|
# source distribution.
|
|
|
|
#
|
|
|
|
# Upsilon Micropython Standard Library.
|
|
|
|
|
2023-06-23 14:51:35 -04:00
|
|
|
from mmio import *
|
|
|
|
|
2023-06-23 18:15:53 -04:00
|
|
|
# Write a 20 bit twos-complement value to a DAC.
|
|
|
|
def dac_write_volt(val, num):
|
|
|
|
"""
|
|
|
|
Write a 20 bit twos-complement value to a DAC.
|
|
|
|
|
|
|
|
:param val: Two's complement 20 bit integer. The number is bitmasked
|
|
|
|
to the appropriate length, so negative Python integers are also
|
|
|
|
accepted. This DOES NOT check if the integer actually fits in 20
|
|
|
|
bits.
|
|
|
|
|
|
|
|
:param num: DAC number.
|
|
|
|
:raises Exception:
|
|
|
|
"""
|
|
|
|
write_dac_send_buf(1 << 20 | (val & 0xFFFFF), num)
|
2023-06-23 14:51:35 -04:00
|
|
|
write_dac_arm(1, num)
|
|
|
|
write_dac_arm(0, num)
|
|
|
|
|
2023-06-23 18:15:53 -04:00
|
|
|
# Read a register from a DAC.
|
|
|
|
def dac_read_reg(val, num):
|
2023-06-23 14:51:35 -04:00
|
|
|
write_dac_send_buf(1 << 23 | val, num)
|
|
|
|
write_dac_arm(1, num)
|
|
|
|
write_dac_arm(0, num)
|
|
|
|
return read_dac_recv_buf(num)
|
|
|
|
|
2023-06-23 18:15:53 -04:00
|
|
|
# Initialize a DAC by setting it's output value to 0, and
|
|
|
|
# removing the output restriction from the settings register.
|
2023-06-23 14:51:35 -04:00
|
|
|
def dac_init(num):
|
|
|
|
write_dac_sel(0,num)
|
2023-06-23 18:15:53 -04:00
|
|
|
dac_write_volt(0, num)
|
|
|
|
write_dac_send_buf(1 << 21 | 1 << 1, num)
|
2023-06-23 14:51:35 -04:00
|
|
|
write_dac_arm(1, num)
|
|
|
|
write_dac_arm(0, num)
|
2023-06-23 18:15:53 -04:00
|
|
|
return dac_read_reg(1 << 21, num)
|
2023-06-23 14:51:35 -04:00
|
|
|
|
2023-06-23 18:15:53 -04:00
|
|
|
# Read a value from an ADC.
|
|
|
|
def adc_read(num):
|
2023-06-23 14:51:35 -04:00
|
|
|
write_adc_arm(1, num)
|
|
|
|
write_adc_arm(0, num)
|
2023-06-23 18:15:53 -04:00
|
|
|
return read_adc_recv_buf(num)
|