If there is no kernel driver available, and a program needs to access a peripheral address directly with mmap, it needs to know where in the virtual memory map the peripheral bus segment has been placed. This varies according to which model of Raspberry Pi is being used, so there are three helper function available to provide platform independence. Note: please use these functions rather than hardcoded values, as this will ensure future compatibility.
This returns the ARM-side physical address where peripherals are mapped. This is 0x20000000 on the Pi Zero, Pi Zero W, and the first generation of the Raspberry Pi and Compute Module, and 0x3f000000 on the Pi 2, Pi 3 and Compute Module 3.
This returns the size of the peripheral's space, which is 0x01000000 for all models.
This returns the bus address of the SDRAM. This is 0x40000000 on the Pi Zero, Pi Zero W, and the first generation of the Raspberry Pi and Compute Module (GPU L2 cached), and 0xC0000000 on the Pi 2, Pi 3 and Compute Module 3 (uncached).
Building a C program using these functions
include file and library are installed by default on a Raspbian system.
Add the following line to your C program:
So a simple command line compile might be:
cc myfile.c -I/opt/vc/include -L/opt/vc/lib -lbcm_host -o myfile