From b6f51233db607d13383e39c2def451cebbcfd2a8 Mon Sep 17 00:00:00 2001 From: Julien BERAUD Date: Tue, 22 Dec 2015 14:49:12 +0100 Subject: [PATCH] AP_HAL_Linux: Make RPI get_hw method public Valid also on Bebop --- libraries/AP_HAL_Linux/Util.cpp | 41 +++++++++++++++++++++++++ libraries/AP_HAL_Linux/Util.h | 17 +++++++++-- libraries/AP_HAL_Linux/Util_RPI.cpp | 46 ++++++++--------------------- 3 files changed, 68 insertions(+), 36 deletions(-) diff --git a/libraries/AP_HAL_Linux/Util.cpp b/libraries/AP_HAL_Linux/Util.cpp index 4fbe26fc8f..768e0df9e6 100644 --- a/libraries/AP_HAL_Linux/Util.cpp +++ b/libraries/AP_HAL_Linux/Util.cpp @@ -160,4 +160,45 @@ int Util::read_file(const char *path, const char *fmt, ...) return ret; } +const char *Linux::Util::_hw_names[UTIL_NUM_HARDWARES] = { + [UTIL_HARDWARE_RPI1] = "BCM2708", + [UTIL_HARDWARE_RPI2] = "BCM2709", + [UTIL_HARDWARE_BEBOP] = "Mykonos3 board", + [UTIL_HARDWARE_BEBOP2] = "Milos board", +}; + +#define MAX_SIZE_LINE 50 +int Util::get_hw_arm32() +{ + int ret = -ENOENT; + char buffer[MAX_SIZE_LINE]; + const char* hardware_description_entry = "Hardware"; + char* flag; + FILE* f; + + f = fopen("/proc/cpuinfo", "r"); + + if (f == NULL) { + ret = -errno; + goto end; + } + + while (fgets(buffer, MAX_SIZE_LINE, f) != NULL) { + flag = strstr(buffer, hardware_description_entry); + if (flag != NULL) { + for (uint8_t i = 0; i < UTIL_NUM_HARDWARES; i++) { + if (strstr(buffer, _hw_names[i]) != 0) { + ret = i; + goto close_end; + } + } + } + } + +close_end: + fclose(f); +end: + return ret; +} + #endif // CONFIG_HAL_BOARD == HAL_BOARD_LINUX diff --git a/libraries/AP_HAL_Linux/Util.h b/libraries/AP_HAL_Linux/Util.h index 344318dfaf..ae0f6a83c5 100644 --- a/libraries/AP_HAL_Linux/Util.h +++ b/libraries/AP_HAL_Linux/Util.h @@ -9,6 +9,14 @@ #include "ToneAlarmDriver.h" #include "Semaphores.h" +enum hw_type { + UTIL_HARDWARE_RPI1 = 0, + UTIL_HARDWARE_RPI2, + UTIL_HARDWARE_BEBOP, + UTIL_HARDWARE_BEBOP2, + UTIL_NUM_HARDWARES, +}; + class Linux::Util : public AP_HAL::Util { public: static Util *from(AP_HAL::Util *util) { @@ -25,7 +33,7 @@ public: bool toneAlarm_init(); void toneAlarm_set_tune(uint8_t tune); - + void _toneAlarm_timer_tick(); /* @@ -64,14 +72,17 @@ public: // create a new semaphore AP_HAL::Semaphore *new_semaphore(void) override { return new Linux::Semaphore; } - + + int get_hw_arm32(); + private: static Linux::ToneAlarm _toneAlarm; Linux::Heat *_heat; int saved_argc; char* const *saved_argv; const char* custom_log_directory = NULL; - const char* custom_terrain_directory = NULL; + const char* custom_terrain_directory = NULL; + static const char *_hw_names[UTIL_NUM_HARDWARES]; }; diff --git a/libraries/AP_HAL_Linux/Util_RPI.cpp b/libraries/AP_HAL_Linux/Util_RPI.cpp index 281614dd55..655f917173 100644 --- a/libraries/AP_HAL_Linux/Util_RPI.cpp +++ b/libraries/AP_HAL_Linux/Util_RPI.cpp @@ -14,6 +14,7 @@ #include #include "Util_RPI.h" +#include "Util.h" extern const AP_HAL::HAL& hal; @@ -24,43 +25,22 @@ UtilRPI::UtilRPI() _check_rpi_version(); } -#define MAX_SIZE_LINE 50 int UtilRPI::_check_rpi_version() { - char buffer[MAX_SIZE_LINE]; - const char* hardware_description_entry = "Hardware"; - const char* v1 = "BCM2708"; - const char* v2 = "BCM2709"; - char* flag; - FILE* fd; + int hw; + hw = Util::from(hal.util)->get_hw_arm32(); - fd = fopen("/proc/cpuinfo", "r"); - - while (fgets(buffer, MAX_SIZE_LINE, fd) != NULL) { - flag = strstr(buffer, hardware_description_entry); - if (flag != NULL) { - if (strstr(buffer, v2) != NULL) { - printf("Raspberry Pi 2 with BCM2709!\n"); - fclose(fd); - - _rpi_version = 2; - return _rpi_version; - } - else if (strstr(buffer, v1) != NULL) { - printf("Raspberry Pi 1 with BCM2708!\n"); - fclose(fd); - - _rpi_version = 1; - return _rpi_version; - } - } + if (hw == UTIL_HARDWARE_RPI2) { + printf("Raspberry Pi 2 with BCM2709!\n"); + _rpi_version = 2; + } else if (hw == UTIL_HARDWARE_RPI1) { + printf("Raspberry Pi 1 with BCM2708!\n"); + _rpi_version = 1; + } else { + /* defaults to 1 */ + fprintf(stderr, "Could not detect RPi version, defaulting to 1\n"); + _rpi_version = 1; } - - /* defaults to 1 */ - fprintf(stderr, "Could not detect RPi version, defaulting to 1\n"); - fclose(fd); - - _rpi_version = 1; return _rpi_version; }