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
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.