redpitaya-puzzlefw/sw/buildroot_overlay/opt/puzzlefw/bin/puzzle-adccfg

151 lines
2.9 KiB
Plaintext
Raw Normal View History

#!/bin/sh
#
# Configure LTC2145 ADC via SPI.
#
# This only works on a 4-input Red Pitaya.
#
# Run spi-pipe with the correct parameters.
run_spi_pipe() {
spi-pipe -d "$1" -m 0 -s 1000000 -l 0 -B 8 -b 2 -n 1 >/dev/null
}
# Initialize both ADCs.
adc_init() {
force=
dcs=0
shift
while [ -n "$1" ]; do
case "$1" in
--force)
force=1
;;
--dcs)
dcs=1
;;
*)
echo "ERROR: Unknown option '$1'" >&2
exit 1
esac
shift
done
if [ -z "$force" ]; then
echo "ERROR: Not initializing ADC without --force option." >&2
exit 1
fi
for spidev in /dev/spidev0.0 /dev/spidev0.1 ; do
echo "Initializing ADC via $spidev"
# Reset register (address = 0x00)
#
# bit 7 = RESET
#
printf '\000\200' | run_spi_pipe $spidev
# Timing register (address = 0x02)
#
# bit 0 = DCS
# bits 2:1 = CLKPHASE
# bit 3 = CLKINV
#
printf '\002\20'"$dcs" | run_spi_pipe $spidev
# Output mode register (address = 0x03)
#
# bits 1:0 = OUTMODE (2 = double data rate CMOS output mode)
# bit 2 = OUTOFF
# bit 3 = TERMON
# bits 6:4 = ILVDS
#
printf '\003\002' | run_spi_pipe $spidev
done
}
# Write data format register.
adc_format() {
# Data format register (address = 0x04)
#
# bit 0 = TWOSCOMP
# bit 1 = RAND
# bit 2 = ABP
# bits 5:3 = OUTTEST
d1=0 # octal digit 1 = OUTTEST
d0=0 # octal digit 0 = RAND*2
shift
while [ -n "$1" ]; do
case "$1" in
--testzero)
d1=1
;;
--testone)
d1=3
;;
--testchecker)
d1=5
;;
--testalter)
d1=7
;;
--rand)
d0=2
;;
*)
echo "ERROR: Unknown option '$1'" >&2
exit 1
esac
shift
done
for spidev in /dev/spidev0.0 /dev/spidev0.1 ; do
printf '\004\0'"${d1}${d0}" | run_spi_pipe $spidev
done
}
case "$1" in
init)
adc_init "$@"
;;
format)
adc_format "$@"
;;
*)
script="${0##*/}"
cat <<EOF
Usage: $script {init|format}
$script init {options}
Reset ADCs and initialize timing and output mode.
This must NOT be done while the FPGA is active.
options:
--force Required option to override warning.
--dcs Enable clock duty cycle stabilizer.
$script format {options}
Configure data format.
By default, disables test patterns and disables output randomizer.
options:
--testzero Enable all-zero test pattern.
--testone Enable all-one test pattern.
--testchecker Enable checkerboard test pattern.
--testalter Enable alternating test pattern.
--rand Enable output randomizer mode.
EOF
exit 1
;;
esac
exit $?