|
1 | 1 | # Peripheral Addresses
|
2 | 2 |
|
3 |
| -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. |
| 3 | +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 functions in [bcm_host.c](https://github.com/raspberrypi/userland/blob/3fd8527eefd8790b4e8393458efc5f94eb21a615/host_applications/linux/libs/bcm_host/bcm_host.c) to help provide platform independence. |
| 4 | + |
| 5 | +**Note**: please use these functions rather than hardcoded values, as this will ensure future compatibility. |
4 | 6 |
|
5 | 7 | `unsigned bcm_host_get_peripheral_address()`
|
6 | 8 |
|
7 |
| -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. |
| 9 | +This returns the ARM-side physical address where peripherals are mapped. |
8 | 10 |
|
9 | 11 | `unsigned bcm_host_get_peripheral_size()`
|
10 | 12 |
|
11 |
| -This returns the size of the peripheral's space, which is 0x01000000 for all models. |
| 13 | +This returns the size of the peripheral's space. |
12 | 14 |
|
13 | 15 | `unsigned bcm_host_get_sdram_address()`
|
14 | 16 |
|
15 |
| -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). |
| 17 | +This returns the bus address of the SDRAM. |
16 | 18 |
|
17 |
| -## Building a C program using these functions |
| 19 | +Here are the current values as of this writing, in table form: |
18 | 20 |
|
19 |
| -The `include` file and library are installed by default on a Raspberry Pi OS system. |
| 21 | +| SoC | Peripheral Address | Peripheral Size | SDRAM Address | Source | |
| 22 | +| --- | --- | --- | --- | --- | |
| 23 | +| BCM2835 | 0x20000000 | 0x01000000 | 0x40000000 | [bcm2835.dtsi](https://github.com/raspberrypi/linux/blob/7f465f823c2ecbade5877b8bbcb2093a8060cb0e/arch/arm/boot/dts/bcm2835.dtsi#L21) | |
| 24 | +| BCM2836 | 0x3f000000 | 0x01000000 | 0xC0000000 | [bcm2836.dtsi](https://github.com/raspberrypi/linux/blob/7f465f823c2ecbade5877b8bbcb2093a8060cb0e/arch/arm/boot/dts/bcm2836.dtsi#L10) | |
| 25 | +| BCM2837 | 0x3f000000 | 0x01000000 | 0xC0000000 | [bcm2837.dtsi](https://github.com/raspberrypi/linux/blob/7f465f823c2ecbade5877b8bbcb2093a8060cb0e/arch/arm/boot/dts/bcm2837.dtsi#L9) | |
| 26 | +| BCM2711 | 0xfe000000 | 0x01800000 | 0xc0000000 | [bcm2711.dtsi](https://github.com/raspberrypi/linux/blob/7f465f823c2ecbade5877b8bbcb2093a8060cb0e/arch/arm/boot/dts/bcm2711.dtsi#L41) | |
20 | 27 |
|
21 |
| -Add the following line to your C program: |
| 28 | +## Building a C program using these functions |
| 29 | + |
| 30 | +The include file and library are installed by default on a Raspberry Pi OS system. Just add the following line to your C program: |
| 31 | +```C |
| 32 | +#include <bcm_host.h> |
22 | 33 | ```
|
| 34 | + |
| 35 | +Example: |
| 36 | +```C |
| 37 | +#include <stdio.h> |
23 | 38 | #include <bcm_host.h>
|
| 39 | + |
| 40 | +int main(void) { |
| 41 | + printf("bcm_host_get_peripheral_address -> 0x%08x\n", bcm_host_get_peripheral_address()); |
| 42 | + printf("bcm_host_get_peripheral_size -> 0x%08x\n", bcm_host_get_peripheral_size()); |
| 43 | + printf("bcm_host_get_sdram_address -> 0x%08x\n", bcm_host_get_sdram_address()); |
| 44 | + |
| 45 | + return 0; |
| 46 | +} |
24 | 47 | ```
|
| 48 | +
|
25 | 49 | Link with:
|
26 | 50 | ```
|
27 | 51 | -lbcm_host
|
28 | 52 | ```
|
29 | 53 | So a simple command line compile might be:
|
30 |
| -``` |
| 54 | +```shell |
31 | 55 | cc myfile.c -I/opt/vc/include -L/opt/vc/lib -lbcm_host -o myfile
|
32 | 56 | ```
|
0 commit comments