From 05ebabbf6b77f96aa104563ddf2cd84bfa50bc45 Mon Sep 17 00:00:00 2001 From: Dmitri Ranfft Date: Wed, 6 Dec 2017 13:04:06 +0100 Subject: [PATCH] AP_HAL_Linux: fixed RPi version detection on kernel 4.9 --- libraries/AP_HAL_Linux/Util_RPI.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/libraries/AP_HAL_Linux/Util_RPI.cpp b/libraries/AP_HAL_Linux/Util_RPI.cpp index 0aea8d8585..32d8fbd0c3 100644 --- a/libraries/AP_HAL_Linux/Util_RPI.cpp +++ b/libraries/AP_HAL_Linux/Util_RPI.cpp @@ -28,9 +28,34 @@ UtilRPI::UtilRPI() _check_rpi_version(); } +#define MAX_SIZE_LINE 50 int UtilRPI::_check_rpi_version() { int hw; + + char buffer[MAX_SIZE_LINE] = { 0 }; + FILE* f = fopen("/sys/firmware/devicetree/base/model", "r"); + if (f != nullptr && fgets(buffer, MAX_SIZE_LINE, f) != nullptr) { + int ret = sscanf(buffer+12, "%d", &_rpi_version); + fclose(f); + if (ret != EOF) { + + // Preserving old behavior. + if (_rpi_version > 2) { + _rpi_version = 2; + } + // RPi 1 doesn't have a number there, so sscanf() won't have read anything. + else if (_rpi_version == 0) { + _rpi_version = 1; + } + + printf("%s. (intern: %d)\n", buffer, _rpi_version); + + return _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) {