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.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
Linux Kernel 6.13 Offers Improvements for AMD/Apple Users
The latest Linux kernel is now available, and it includes plenty of improvements, especially for those who use AMD or Apple-based systems.
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.