DDS-based Rasp Pi function generator
GUI
If you want to design your own GUI or embed the generator in another system, the code in Listing 1 is all you need. The Python files that create a convenient GUI to control the instrument can be downloaded from the Linux Magazine FTP site [13], along with the code for iio.py
. The GUI routines include:
dds_gui.py
vlab.py
flab.py
dial.py
The dds_gui.py
program for the function generator uses the Tkinter GUI toolkit [14]. The code mostly sets up the GUI elements and wires the button events to the appropriate routines. At the end, the program sets the instrument into its default state, enters the main loop, and waits for user input.
The vlab.py
and flab.py
routines extend the capabilities of a Tkinter Label
and add editing facilities (for voltage and frequency, respectively), enabling a cursor to be moved from digit to digit and for that digit to be incremented and decremented.
The dial.py
routine implements a custom Tkinter widget (control) that simulates the dial or spin knob often found on these types of instrument. Buttons allow you to jog values up and down by a single digit and to move up and down by a decade. These controls work closely with the frequency and voltage labels.
The Linux Setup
Although the following procedure should work on any modern Linux distro, I used Arch Linux on my Rasp Pi. A fully bootable SD card image of the instrument's OS and all the source code are available on my GitHub page [15].
To access the DAC and DDS chips, some device drivers must be loaded. In turn, those drivers must be given information on the location and capabilities of the chips. These types of devices do not support auto-discovery, so in the world of embedded ARM processors (and increasingly elsewhere), a device tree is used to locate and load suitable drivers for devices. Listing 2 shows the device trees for the DAC and DDS chips.
Listing 2
Device Trees
# dac_tree.txt $ ls -1 /sys/bus/iio/devices/iio\:device0/out* /sys/bus/iio/devices/iio:device0/out_voltage0_raw /sys/bus/iio/devices/iio:device0/out_voltage0_scale ** # dds_tree.txt $ ls -1 /sys/bus/iio/devices/iio\:device1/out* /sys/bus/iio/devices/iio:device1/out_altvoltage0_frequency0 /sys/bus/iio/devices/iio:device1/out_altvoltage0_frequency1 /sys/bus/iio/devices/iio:device1/out_altvoltage0_frequency_scale /sys/bus/iio/devices/iio:device1/out_altvoltage0_frequencysymbol /sys/bus/iio/devices/iio:device1/out_altvoltage0_out0_wavetype /sys/bus/iio/devices/iio:device1/out_altvoltage0_out0_wavetype_available /sys/bus/iio/devices/iio:device1/out_altvoltage0_out_enable /sys/bus/iio/devices/iio:device1/out_altvoltage0_phase0 /sys/bus/iio/devices/iio:device1/out_altvoltage0_phase1 /sys/bus/iio/devices/iio:device1/out_altvoltage0_phase_scale /sys/bus/iio/devices/iio:device1/out_altvoltage0_phasesymbol
If you have used any Rasp Pi HATS before, you might be familiar with adding overlays in the config.txt
file. In the present case, one overlay file, dds_hat.dtbo
, is copied to the /boot/overlays
directory and referenced from /boot/config.txt
:
lcd_rotate=2 gpu_mem=64 initramfs initramfs-linux.img followkernel dtoverlay=dds_hat
The .dtbo
overlay is a binary file produced by compiling a .dts
file with the device tree compiler [16], available with most ARM-based distros. As stated earlier, the overlay tells the kernel where to find a specific device (e.g., its SPI address), something about its configuration (e.g., clock frequency), and which device driver to load. This information is passed to the device driver so that it can communicate with the device and set it up appropriately. The overlay provides a description to the kernel of the device and the environment in which it exists.
Once the drivers are loaded during boot-up, the appropriate entries should appear in the /sys
file tree, and you will see that the entries correspond to those used by the Python routines in Listing 1.
To avoid having to run the dds_hat
program as root, you can add some udev rules so that non-sudo users can access the IIO subsystem:
$ cat /etc/udev/rules.d/99-iio.rules ACTION=="add", PROGRAM="/bin/sh -c 'chown -R alarm:alarm /sys/bus/iio/devices/iio\:device0/*'" ACTION=="add", PROGRAM="/bin/sh -c 'chown -R alarm:alarm /sys/bus/iio/devices/iio\:device1/*'"
These rules are quite broad, and this approach is not recommended on a multiuser server; however, for an embedded device like this, it seems acceptable.
You should now be able to run the Python program and enjoy your function generator!
Conclusion
Although scope for improvement always exists, both for hardware and software, I hope the instrument described here and the details of how it was built, its function, and how it works internally prove to be of interest. This DDS HAT board could be put to a number of other uses with different software. For example, you could produce complex amplitude, phase, and frequency modulation schemes. Some examples can be found in the datasheets and associated application notes for the DDS chip that would allow integration into a communications system.
Infos
- AD9833 DDS specs: https://www.analog.com/media/en/technical-documentation/data-sheets/ad9833.pdf
- DDS tutorial: https://www.analog.com/media/en/training-seminars/tutorials/MT-085.pdf
- DAC chip: https://www.analog.com/media/en/technical-documentation/data-sheets/ad5426_5432_5443.pdf
- Linux IIO system: https://wiki.analog.com/software/linux/docs/iio/iio
- Elliptic filters: https://en.wikipedia.org/wiki/Elliptic_filter
- Filter design tool: https://rf-tools.com/lc-filter/
- TI power amplifier: http://www.ti.com/lit/ds/sbos372e/sbos372e.pdf
- Raspberry Pi universal power supply: https://www.raspberrypi.org/products/raspberry-pi-universal-power-supply/
- KiCAD: http://kicad-pcb.org/
- Raspberry Pi touchscreen case: https://thepihut.com/products/raspberry-pi-official-7-touchscreen-case?variant=15155531396
- Raspberry Pi 7-inch display: https://www.raspberrypi.org/products/raspberry-pi-touch-display/
- Decade: https://en.wikipedia.org/wiki/Decade_(log_scale)
- Code for this article: ftp://ftp.linux-magazine.com/pub/listings/linux-magazine.com/225/
- Tkinter for Python reference: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html
- Author's GitHub project page: https://github.com/andrewrussellmalcolm/dds_hat
- Device tree compiler reference: https://elinux.org/Device_Tree_Reference
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.