Util_RPI.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <AP_HAL/AP_HAL.h>
  2. #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO || \
  3. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO2 || \
  4. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_EDGE || \
  5. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_ERLEBRAIN2 || \
  6. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BH || \
  7. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_DARK || \
  8. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_PXFMINI || \
  9. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIGATOR
  10. #include <errno.h>
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <sys/stat.h>
  15. #include <time.h>
  16. #include <unistd.h>
  17. #include "Util.h"
  18. #include "Util_RPI.h"
  19. extern const AP_HAL::HAL &hal;
  20. using namespace Linux;
  21. UtilRPI::UtilRPI()
  22. {
  23. _check_rpi_version();
  24. }
  25. int UtilRPI::_check_rpi_version()
  26. {
  27. const unsigned int MAX_SIZE_LINE = 50;
  28. char buffer[MAX_SIZE_LINE];
  29. int hw;
  30. FILE *f = fopen("/sys/firmware/devicetree/base/model", "r");
  31. if (f != nullptr && fgets(buffer, MAX_SIZE_LINE, f) != nullptr) {
  32. int ret = sscanf(buffer + 12, "%d", &_rpi_version);
  33. fclose(f);
  34. if (ret != EOF) {
  35. if (_rpi_version > 3) {
  36. _rpi_version = 4;
  37. } else if (_rpi_version > 2) {
  38. // Preserving old behavior.
  39. _rpi_version = 2;
  40. } else if (_rpi_version == 0) {
  41. // RPi 1 doesn't have a number there, so sscanf() won't have read anything.
  42. _rpi_version = 1;
  43. }
  44. printf("%s. (intern: %d)\n", buffer, _rpi_version);
  45. return _rpi_version;
  46. }
  47. }
  48. // Attempting old method if the version couldn't be read with the new one.
  49. hw = Util::from(hal.util)->get_hw_arm32();
  50. if (hw == UTIL_HARDWARE_RPI4) {
  51. printf("Raspberry Pi 4 with BCM2711!\n");
  52. _rpi_version = 4;
  53. } else if (hw == UTIL_HARDWARE_RPI2) {
  54. printf("Raspberry Pi 2/3 with BCM2709!\n");
  55. _rpi_version = 2;
  56. } else if (hw == UTIL_HARDWARE_RPI1) {
  57. printf("Raspberry Pi 1 with BCM2708!\n");
  58. _rpi_version = 1;
  59. } else {
  60. /* defaults to RPi version 2/3 */
  61. fprintf(stderr, "Could not detect RPi version, defaulting to 2/3\n");
  62. _rpi_version = 2;
  63. }
  64. return _rpi_version;
  65. }
  66. int UtilRPI::get_rpi_version() const
  67. {
  68. return _rpi_version;
  69. }
  70. #endif