From 70cb2822e5274966c6fa65c4356911eb7a55004e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Fri, 10 Jan 2020 10:53:38 -0300 Subject: [PATCH] AP_HAL_LINUX: Add support for Raspberry Pi 4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Raspberry Pi 4 uses a new BCM cpu, the BCM2711 with a peripheral base address of 0xFE000000 Signed-off-by: Patrick José Pereira --- libraries/AP_HAL_Linux/GPIO_RPI.cpp | 10 +++++++++- libraries/AP_HAL_Linux/Util.cpp | 1 + libraries/AP_HAL_Linux/Util.h | 1 + libraries/AP_HAL_Linux/Util_RPI.cpp | 10 +++++++--- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/libraries/AP_HAL_Linux/GPIO_RPI.cpp b/libraries/AP_HAL_Linux/GPIO_RPI.cpp index 5065d5f823..13f143b31b 100644 --- a/libraries/AP_HAL_Linux/GPIO_RPI.cpp +++ b/libraries/AP_HAL_Linux/GPIO_RPI.cpp @@ -22,6 +22,7 @@ // Raspberry Pi GPIO memory #define BCM2708_PERI_BASE 0x20000000 #define BCM2709_PERI_BASE 0x3F000000 +#define BCM2711_PERI_BASE 0xFE000000 #define GPIO_BASE(address) (address + 0x200000) // GPIO setup. Always use INP_GPIO(x) before OUT_GPIO(x) or SET_GPIO_ALT(x,y) @@ -44,7 +45,14 @@ GPIO_RPI::GPIO_RPI() void GPIO_RPI::init() { int rpi_version = UtilRPI::from(hal.util)->get_rpi_version(); - uint32_t gpio_address = rpi_version == 1 ? GPIO_BASE(BCM2708_PERI_BASE) : GPIO_BASE(BCM2709_PERI_BASE); + uint32_t gpio_address; + if(rpi_version == 1) { + gpio_address = GPIO_BASE(BCM2708_PERI_BASE); + } else if (rpi_version == 2) { + gpio_address = GPIO_BASE(BCM2709_PERI_BASE); + } else { + gpio_address = GPIO_BASE(BCM2711_PERI_BASE); + } int mem_fd = open("/dev/mem", O_RDWR|O_SYNC|O_CLOEXEC); if (mem_fd < 0) { diff --git a/libraries/AP_HAL_Linux/Util.cpp b/libraries/AP_HAL_Linux/Util.cpp index 4af8c5b4c0..2f8dc9d390 100644 --- a/libraries/AP_HAL_Linux/Util.cpp +++ b/libraries/AP_HAL_Linux/Util.cpp @@ -199,6 +199,7 @@ int Util::read_file(const char *path, const char *fmt, ...) const char *Linux::Util::_hw_names[UTIL_NUM_HARDWARES] = { [UTIL_HARDWARE_RPI1] = "BCM2708", [UTIL_HARDWARE_RPI2] = "BCM2709", + [UTIL_HARDWARE_RPI4] = "BCM2711", [UTIL_HARDWARE_BEBOP] = "Mykonos3 board", [UTIL_HARDWARE_BEBOP2] = "Milos board", [UTIL_HARDWARE_DISCO] = "Evinrude board", diff --git a/libraries/AP_HAL_Linux/Util.h b/libraries/AP_HAL_Linux/Util.h index 531292e889..2686fd4d3a 100644 --- a/libraries/AP_HAL_Linux/Util.h +++ b/libraries/AP_HAL_Linux/Util.h @@ -16,6 +16,7 @@ namespace Linux { enum hw_type { UTIL_HARDWARE_RPI1 = 0, UTIL_HARDWARE_RPI2, + UTIL_HARDWARE_RPI4, UTIL_HARDWARE_BEBOP, UTIL_HARDWARE_BEBOP2, UTIL_HARDWARE_DISCO, diff --git a/libraries/AP_HAL_Linux/Util_RPI.cpp b/libraries/AP_HAL_Linux/Util_RPI.cpp index d0142e99de..aac886397a 100644 --- a/libraries/AP_HAL_Linux/Util_RPI.cpp +++ b/libraries/AP_HAL_Linux/Util_RPI.cpp @@ -40,8 +40,9 @@ int UtilRPI::_check_rpi_version() int ret = sscanf(buffer + 12, "%d", &_rpi_version); fclose(f); if (ret != EOF) { - - if (_rpi_version > 2) { + if (_rpi_version > 3) { + _rpi_version = 4; + } else if (_rpi_version > 2) { // Preserving old behavior. _rpi_version = 2; } else if (_rpi_version == 0) { @@ -58,7 +59,10 @@ int UtilRPI::_check_rpi_version() // Attempting old method if the version couldn't be read with the new one. hw = Util::from(hal.util)->get_hw_arm32(); - if (hw == UTIL_HARDWARE_RPI2) { + if (hw == UTIL_HARDWARE_RPI4) { + printf("Raspberry Pi 4 with BCM2711!\n"); + _rpi_version = 4; + } else if (hw == UTIL_HARDWARE_RPI2) { printf("Raspberry Pi 2/3 with BCM2709!\n"); _rpi_version = 2; } else if (hw == UTIL_HARDWARE_RPI1) {