Add shell script to configure ADC via SPI

This commit is contained in:
Joris van Rantwijk 2024-10-05 00:35:20 +02:00
parent 9a9163b7f0
commit 75833de0a3
3 changed files with 160 additions and 8 deletions

View File

@ -0,0 +1,150 @@
#!/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 $?

View File

@ -24,15 +24,16 @@ case "$1" in
cal_save cal_save
;; ;;
*) *)
script="${0##*/}"
cat <<EOF cat <<EOF
Usage: $0 {show|save} Usage: $script {show|save}
Manage analog channel calibration. Manage analog channel calibration.
$0 show $script show
Display calibration. Display calibration.
$0 save $script save
Copy a changed calibration file to the SD card. Copy a changed calibration file to the SD card.
Before running this command, the changed calibration must be written to Before running this command, the changed calibration must be written to
${CONFIG_DIR}/calibration.conf.new. ${CONFIG_DIR}/calibration.conf.new.

View File

@ -274,19 +274,20 @@ case "$1" in
ipcfg_save ipcfg_save
;; ;;
*) *)
script="${0##*/}"
cat <<EOF cat <<EOF
Usage: $0 {init|show|config|save|restart} Usage: $script {init|show|config|save|restart}
Manage IP address configuration. Manage IP address configuration.
$0 init $script init
Initialize IP address from saved configuration. Initialize IP address from saved configuration.
This command is used during boot and should not be invoked manually. This command is used during boot and should not be invoked manually.
$0 show $script show
Display active and saved IP address configuration. Display active and saved IP address configuration.
$0 config {options} $script config {options}
Change active IP address configuration. Change active IP address configuration.
The new configuration is not written to the SD card. The new configuration is not written to the SD card.
@ -297,7 +298,7 @@ Manage IP address configuration.
--netmask n.n.n.n Specify netmask. --netmask n.n.n.n Specify netmask.
--gateway n.n.n.n Specify gateway address, or "" to disable gateway. --gateway n.n.n.n Specify gateway address, or "" to disable gateway.
$0 save {options} $script save {options}
Change the saved IP address configuration on the SD card. Change the saved IP address configuration on the SD card.
Options are the same as for command 'config'. Options are the same as for command 'config'.