AP_HAL_Linux: Make RPI get_hw method public

Valid also on Bebop
This commit is contained in:
Julien BERAUD 2015-12-22 14:49:12 +01:00 committed by Lucas De Marchi
parent cadef09542
commit b6f51233db
3 changed files with 68 additions and 36 deletions

View File

@ -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

View File

@ -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];
};

View File

@ -14,6 +14,7 @@
#include <time.h>
#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;
}