Raspberry-Pi-DMA-Tutorial
Raspberry-Pi-DMA-Tutorial copied to clipboard
Problem with PERI_PHYS_BASE
Hi - this tutorial is awesome! Thankyou. Like many others, I have struggled to find tutorials and examples for using Pi DMA and I think your contribution is great.
I have a suggested improvement.... Your code will compile but it will not run correctly as-is on a Raspberry Pi 4. I suspect there will also be issues for other raspberry pis that do not have their peripheral base address at 0x3F000000.
I have resolved the problem as follows. This solution should be generic so that it works for all other varieties of PI regardless of where their peripheral base addresses are set. The solution requires a call to a library to determine the base address. I required some links to the header files to get it to work - I am unsure if these are pi-4 specific or are generally required. Try without the links first, and if you get compile and link errors because the header file and library cannot be found, add the links.
-
Install the following additional libraries/header files.
sudo apt-get install libraspberrypi-dev raspberrypi-kernel-headers
-
These are the additional links I required on my pi-4 bullseye system.
cd /opt/vc/include/ ; for i in * ; do ln -s /opt/vc/include/$i /usr/include/$i ; done
cd /opt/vc/lib/ ; for i in * ; do ln -s /opt/vc/lib/$i /usr/lib/$i ; done
-
add the following #include at the start of each of the three C dma demo files
#include <bcm_host.h>
-
change references to PERI_PHYS_BASE to bcm_host_get_peripheral_address()
-
linked with the bcm_host library. To make it simple, I changed the Makefile as follows:
CFLAGS=-O2 -W -Wall -std=c99 -D_XOPEN_SOURCE=500
LDFLAGS=-lbcm_host
all: dma-demo dma-unpaced dma-paced
dma-demo.o : dma-demo.c
-$(CC) -c dma-demo.c -o $@ $(CFLAGS)
dma-unpaced.o : dma-unpaced.c
-$(CC) -c dma-unpaced.c -o $@ $(CFLAGS)
dma-paced.o : dma-paced.c
-$(CC) -c dma-paced.c -o $@ $(CFLAGS)
mailbox.o : mailbox.c
-$(CC) -c mailbox.c -o $@ $(CFLAGS)
dma-demo: dma-demo.o mailbox.o
-$(CC) dma-demo.o mailbox.o -o $@ $(LDFLAGS)
-chmod 750 dma-demo
-sudo chown root.gpio dma-demo
-sudo chmod u+s dma-demo
dma-unpaced: dma-unpaced.o mailbox.o
-$(CC) dma-unpaced.o mailbox.o -o $@ $(LDFLAGS)
-chmod 750 dma-unpaced
-sudo chown root.gpio dma-unpaced
-sudo chmod u+s dma-unpaced
dma-paced: dma-paced.o mailbox.o
-$(CC) dma-paced.o mailbox.o -o $@ $(LDFLAGS)
-chmod 750 dma-paced
-sudo chown root.gpio dma-paced
-sudo chmod u+s dma-paced
clean:
rm -f ./*.o ./dma-unpaced ./dma-paced ./dma-demo
Thanks! It feels great to know that this tutorial has really helped you!
This tutorial was tested on a Pi 3B+. It will be great if the code can also run on Pi 4 :)
Unfortunately I do not have a Pi 4 to test your changes. Could you submit a PR for your changes?